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§
Sourcefn core(&self) -> &WidgetCore
fn core(&self) -> &WidgetCore
Sourcefn core_mut(&mut self) -> &mut WidgetCore
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.
Sourcefn paint(&self, p: &mut dyn Painter)
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 thisWidget. It is a PushrodPainter, never anSDL2type, so theWidgetworks with any backend.
Provided Methods§
Sourcefn handle_event(&mut self, event: &Event, ctx: &mut EventCtx) -> Propagation
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 thisWidget.ctx- The context used to queue events theWidgetgenerates;EventCtx::phasesays 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.
Sourcefn feed_controllers(&mut self, event: &Event, ctx: &mut EventCtx) -> Propagation
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 thisWidget.ctx- The context passed on toon_controller_event.
§Returns
Propagation::Stop if any on_controller_event call returned it, otherwise
Propagation::Continue.
Sourcefn on_controller_event(
&mut self,
_event: &ControllerEvent,
_ctx: &mut EventCtx,
) -> Propagation
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 asControllerEvent::Clicked._ctx- The context used to queue events theWidgetgenerates.
§Returns
Propagation::Stop to end delivery of the raw event that produced it; the default
ignores the result and returns Propagation::Continue.
Sourcefn handles_capture(&self) -> bool
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.
Sourcefn event_mask(&self) -> EventMask
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.
Sourcefn get_id(&self) -> WidgetId
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.
Sourcefn get_parent(&self) -> WidgetId
fn get_parent(&self) -> WidgetId
Sourcefn get_origin(&self) -> Point
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.
Sourcefn set_origin(&mut self, point: Point)
fn set_origin(&mut self, point: Point)
Moves the Widget, and invalidates it.
§Arguments
point- The new upper left-hand corner, relative to the parentWidget.
Sourcefn set_size(&mut self, size: Size)
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.
Sourcefn set_color(&mut self, color: Color)
fn set_color(&mut self, color: Color)
Sets the Widget’s main (background) color, and invalidates it.
§Arguments
color- The new background color.
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.
Hides or shows the Widget and all of its children, and invalidates it.
§Arguments
state-trueto hide theWidget,falseto show it.
Sourcefn set_behavior(&mut self, behavior: Behavior)
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.
Sourcefn state(&self) -> VisualState
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.
Sourcefn set_state(&mut self, state: VisualState)
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.
Sourcefn appearance(&self) -> &Appearance
fn appearance(&self) -> &Appearance
Returns the Widget’s per-state appearance.
§Returns
The Appearance holding one StateStyle per visual state.
Sourcefn appearance_mut(&mut self) -> &mut Appearance
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.
Sourcefn set_style(&mut self, state: VisualState, style: StateStyle)
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 asVisualState::HOVERED; an empty set styles the normal state.style- The overrides for that state.
Sourcefn style(&self) -> StateStyle
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.
Sourcefn is_enabled(&self) -> bool
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.
Sourcefn set_enabled(&mut self, enabled: bool)
fn set_enabled(&mut self, enabled: bool)
Enables or disables the Widget, and invalidates it if that changed it.
§Arguments
enabled-trueto enable theWidget,falseto disable it.
Sourcefn is_focusable(&self) -> bool
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.
Sourcefn accepts_text_input(&self) -> bool
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.
Sourcefn is_invalidated(&self) -> bool
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.
Sourcefn set_invalidated(&mut self, state: bool)
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-trueto repaint theWidgeton the next frame.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".