Skip to main content

Widget

Trait Widget 

Source
pub trait Widget: Any {
Show 32 methods // Required methods fn core(&self) -> &WidgetCore; fn core_mut(&mut self) -> &mut WidgetCore; fn paint(&self, p: &mut dyn Painter); // Provided methods fn handle_event(&mut self, event: &Event, ctx: &mut EventCtx) -> Propagation { ... } fn feed_controllers( &mut self, event: &Event, ctx: &mut EventCtx, ) -> Propagation { ... } fn on_controller_event( &mut self, _event: &ControllerEvent, _ctx: &mut EventCtx, ) -> Propagation { ... } fn handles_capture(&self) -> bool { ... } fn event_mask(&self) -> EventMask { ... } fn get_id(&self) -> WidgetId { ... } fn get_parent(&self) -> WidgetId { ... } fn get_origin(&self) -> Point { ... } fn get_size(&self) -> Size { ... } fn get_color(&self) -> Color { ... } fn set_origin(&mut self, point: Point) { ... } fn set_size(&mut self, size: Size) { ... } fn set_color(&mut self, color: Color) { ... } fn is_hidden(&self) -> bool { ... } fn set_hidden(&mut self, state: bool) { ... } fn behavior(&self) -> Behavior { ... } fn set_behavior(&mut self, behavior: Behavior) { ... } fn state(&self) -> VisualState { ... } fn set_state(&mut self, state: VisualState) { ... } fn appearance(&self) -> &Appearance { ... } fn appearance_mut(&mut self) -> &mut Appearance { ... } fn set_style(&mut self, state: VisualState, style: StateStyle) { ... } fn style(&self) -> StateStyle { ... } fn is_enabled(&self) -> bool { ... } fn set_enabled(&mut self, enabled: bool) { ... } fn is_focusable(&self) -> bool { ... } fn accepts_text_input(&self) -> bool { ... } fn is_invalidated(&self) -> bool { ... } fn set_invalidated(&mut self, state: bool) { ... }
}
Expand description

A drawable, interactive object presented in a window.

Widgets display information (text, images, graphics) and provide controls the user can interact with. Every Widget stores a WidgetCore and implements three functions: core, core_mut, and paint. Interactive Widgets also implement handle_event. Every other function has a default implementation that works against the WidgetCore.

Widget requires Any so that the WidgetCache can return a Widget as its concrete type (WidgetCache::get_as).

Required Methods§

Source

fn core(&self) -> &WidgetCore

Returns the Widget’s shared state.

§Returns

A reference to the WidgetCore stored in the Widget.

Source

fn core_mut(&mut self) -> &mut WidgetCore

Returns the Widget’s shared state for modification.

§Returns

A mutable reference to the WidgetCore stored in the Widget.

Source

fn paint(&self, p: &mut dyn Painter)

Paints the Widget into its own texture.

The painter is already targeting the Widget’s surface, so drawing always starts at (0, 0) and is bounded by get_size. Compose the look of the Widget with the functions in the paint module (fill, border, text). The WidgetCache only calls this when the Widget is invalidated; between repaints, the texture is reused.

§Arguments
  • p - The painter, targeting this Widget. It is a Pushrod Painter, never an SDL2 type, so the Widget works with any backend.

Provided Methods§

Source

fn handle_event(&mut self, event: &Event, ctx: &mut EventCtx) -> Propagation

Handles an event sent to this Widget.

The WidgetCache sends mouse events to the Widget under the pointer (or the Widget that captured the mouse), and PointerEntered and PointerExited events when the pointer enters or leaves it. Events the Widget generates in response, such as Clicked, are queued with EventCtx::emit and delivered to the application after event processing completes. The default implementation feeds the event to the Widget’s controllers (see Widget::feed_controllers) and otherwise ignores it.

§Arguments
  • event - The event sent to this Widget.
  • ctx - The context used to queue events the Widget generates; EventCtx::phase says whether the event is being captured, handled as the target, or bubbled.
§Returns

Propagation::Stop to end delivery of an input event, or Propagation::Continue (the default) to let the next Widget in the capture, target, bubble path see it.

Source

fn feed_controllers(&mut self, event: &Event, ctx: &mut EventCtx) -> Propagation

Feeds an event to the Widget’s controllers and calls Widget::on_controller_event for each result, in order.

The default Widget::handle_event is this call. A Widget that overrides handle_event calls it to keep its controllers working.

§Arguments
  • event - The event sent to this Widget.
  • ctx - The context passed on to on_controller_event.
§Returns

Propagation::Stop if any on_controller_event call returned it, otherwise Propagation::Continue.

Source

fn on_controller_event( &mut self, _event: &ControllerEvent, _ctx: &mut EventCtx, ) -> Propagation

Receives a high-level result from one of the Widget’s controllers.

§Arguments
  • _event - What the controller reported, such as ControllerEvent::Clicked.
  • _ctx - The context used to queue events the Widget generates.
§Returns

Propagation::Stop to end delivery of the raw event that produced it; the default ignores the result and returns Propagation::Continue.

Source

fn handles_capture(&self) -> bool

Returns true if this Widget wants input events in the capture phase.

A capturing Widget sees pointer and key events before its descendants, and can return Propagation::Stop to keep them from reaching the target. Every Widget receives events in the bubble phase regardless.

§Returns

false by default.

Source

fn event_mask(&self) -> EventMask

Returns the classes of events this Widget wants to receive.

The WidgetCache skips the Widget for every event whose class is not in this mask, so a Widget that handles no events costs nothing. An event is delivered only when it is also in the run loop’s mask.

§Returns

The mask; EventMask::NONE by default. A Widget that handles events calls core_mut().set_event_mask(..) at creation, or an application calls WidgetCache::set_event_mask.

Source

fn get_id(&self) -> WidgetId

Returns the Widget’s ID.

§Returns

The ID assigned by the WidgetCache when the Widget was added, or 0 if it has not been added yet.

Source

fn get_parent(&self) -> WidgetId

Returns the ID of the Widget’s parent.

§Returns

The parent’s ID. The root Widget is its own parent.

Source

fn get_origin(&self) -> Point

Returns the Widget’s origin.

§Returns

The upper left-hand corner of the Widget, relative to its parent. Use WidgetCache::absolute_origin for window coordinates.

Source

fn get_size(&self) -> Size

Returns the Widget’s size.

§Returns

The width and height of the Widget in pixels.

Source

fn get_color(&self) -> Color

Returns the Widget’s main (background) color.

§Returns

The background color.

Source

fn set_origin(&mut self, point: Point)

Moves the Widget, and invalidates it.

§Arguments
  • point - The new upper left-hand corner, relative to the parent Widget.
Source

fn set_size(&mut self, size: Size)

Resizes the Widget, and invalidates it. Its texture is resized on the next repaint.

§Arguments
  • size - The new width and height in pixels.
Source

fn set_color(&mut self, color: Color)

Sets the Widget’s main (background) color, and invalidates it.

§Arguments
  • color - The new background color.
Source

fn is_hidden(&self) -> bool

Returns whether the Widget is hidden.

Hidden Widgets and all of their children are not drawn and do not receive mouse events.

§Returns

true if the Widget is hidden, false if it is shown.

Source

fn set_hidden(&mut self, state: bool)

Hides or shows the Widget and all of its children, and invalidates it.

§Arguments
  • state - true to hide the Widget, false to show it.
Source

fn behavior(&self) -> Behavior

Returns what the Widget can do.

§Returns

The Widget’s Behavior flags; none are set on a new Widget.

Source

fn set_behavior(&mut self, behavior: Behavior)

Replaces the Widget’s behavior flags, and invalidates it.

Setting or clearing Behavior::HIDDEN hides or shows the Widget, the same as set_hidden.

§Arguments
  • behavior - The new flags.
Source

fn state(&self) -> VisualState

Returns how the Widget currently looks.

§Returns

The Widget’s VisualState flags; none are set (the normal state) on a new Widget.

Source

fn set_state(&mut self, state: VisualState)

Replaces the Widget’s visual state flags, and invalidates it only if the change can alter how it looks.

A change to PRESSED, CHECKED, INDETERMINATE, DISABLED or FOCUSED always repaints, since built-in looks and the state overlay depend on them. A change to HOVERED or DRAG_OVER repaints only if the effective style (see Widget::style) changed.

§Arguments
  • state - The new flags.
Source

fn appearance(&self) -> &Appearance

Returns the Widget’s per-state appearance.

§Returns

The Appearance holding one StateStyle per visual state.

Source

fn appearance_mut(&mut self) -> &mut Appearance

Returns the Widget’s per-state appearance for modification, and invalidates the Widget.

Use Widget::set_style to change one state and repaint only when it matters.

§Returns

The Appearance, to be edited in place.

Source

fn set_style(&mut self, state: VisualState, style: StateStyle)

Sets the style for one visual state, and invalidates the Widget only if the effective style for its current state changed.

§Arguments
  • state - The visual state flag to style, such as VisualState::HOVERED; an empty set styles the normal state.
  • style - The overrides for that state.
Source

fn style(&self) -> StateStyle

Returns the effective style for the Widget’s current state.

The style for the highest-priority active state wins, unset fields fall back to normal, and fields still None mean “use the built-in default”. paint implementations read this.

§Returns

The resolved StateStyle.

Source

fn is_enabled(&self) -> bool

Returns whether the Widget is enabled.

Disabled Widgets (VisualState::DISABLED) receive no input and cannot take focus.

§Returns

true if the Widget is enabled, false if it is disabled.

Source

fn set_enabled(&mut self, enabled: bool)

Enables or disables the Widget, and invalidates it if that changed it.

§Arguments
  • enabled - true to enable the Widget, false to disable it.
Source

fn is_focusable(&self) -> bool

Returns whether the Widget can take keyboard focus.

§Returns

true if the Widget is Behavior::FOCUSABLE, enabled, and not hidden.

Source

fn accepts_text_input(&self) -> bool

Returns whether the Widget is a text editor.

While a Widget that returns true has the keyboard focus, the Engine asks the operating system for text input, so typed characters arrive as Event::TextEntered. Override it in text fields and editors; the default is false.

§Returns

true if the Widget accepts typed text.

Source

fn is_invalidated(&self) -> bool

Returns whether the Widget needs to be repainted.

§Returns

true if the Widget will be repainted into its texture on the next frame.

Source

fn set_invalidated(&mut self, state: bool)

Sets whether the Widget needs to be repainted.

Setters call this automatically. Call it after changing state that affects how the Widget looks. An invalidated Widget is repainted into its texture on the next frame, and the screen is refreshed.

§Arguments
  • state - true to repaint the Widget on the next frame.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§