pub struct WidgetCache { /* private fields */ }Expand description
The tree of Widgets in a window.
Widgets are stored in a flat list, addressed by ID, with each Widget’s children kept in
drawing order. The cache also tracks the Widget under the mouse pointer and the Widget
that captured the mouse with a button press, routes raw input events to Widgets, and draws
them.
§Examples
use pushrod::cache::WidgetCache;
use pushrod::geometry::size;
use pushrod::root_widget::RootWidget;
let mut cache = WidgetCache::new();
let root = cache.add(Box::new(RootWidget::new(size(320, 200))));
assert_eq!(root, 0);
assert_eq!(cache.len(), 1);Implementations§
Source§impl WidgetCache
impl WidgetCache
Sourcepub fn focused(&self) -> Option<WidgetId>
pub fn focused(&self) -> Option<WidgetId>
Returns the Widget that has the keyboard focus.
§Returns
The ID of the focused Widget, or None if nothing has focus.
Sourcepub fn set_focus(&mut self, id: WidgetId) -> bool
pub fn set_focus(&mut self, id: WidgetId) -> bool
Gives the keyboard focus to a Widget.
The Widget that had focus receives Event::FocusLost, then the new one receives
Event::FocusGained (both are also queued for the application), the two Widgets’
VisualState::FOCUSED flags are updated, and EngineEvent::FocusChanged is queued.
Nothing happens if the Widget already has focus.
§Arguments
id- TheWidgetto focus.
§Returns
true if the Widget has focus afterward; false if it does not exist or cannot take
focus (not Behavior::FOCUSABLE, disabled, or hidden, itself or through a parent).
Sourcepub fn clear_focus(&mut self)
pub fn clear_focus(&mut self)
Removes the keyboard focus, so no Widget has it.
Sourcepub fn text_input_wanted(&self) -> bool
pub fn text_input_wanted(&self) -> bool
Returns whether the engine should ask the OS for text input.
§Returns
true while the focused Widget accepts typed text.
Sourcepub fn focus_next(&mut self, backward: bool) -> bool
pub fn focus_next(&mut self, backward: bool) -> bool
Sourcepub fn focus_in_group(&mut self, forward: bool) -> bool
pub fn focus_in_group(&mut self, forward: bool) -> bool
Source§impl WidgetCache
impl WidgetCache
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty WidgetCache.
The first Widget added becomes the root Widget (ID 0).
§Returns
A new, empty WidgetCache.
Sourcepub fn cache_policy(&self) -> CachePolicy
pub fn cache_policy(&self) -> CachePolicy
Returns the cache policy of the backend the cache paints with.
Defaults to CachePolicy::TextureCache. draw_loop implements TextureCache only;
CachePolicy::DirectDraw is declared for backends that paint straight to the screen
with clipping, and is not rendered by draw_loop.
§Returns
The CachePolicy.
Sourcepub fn set_cache_policy(&mut self, policy: CachePolicy)
pub fn set_cache_policy(&mut self, policy: CachePolicy)
Sourcepub fn add_child(
&mut self,
parent: WidgetId,
widget: Box<dyn Widget>,
) -> WidgetId
pub fn add_child( &mut self, parent: WidgetId, widget: Box<dyn Widget>, ) -> WidgetId
Adds a Widget as a child of another Widget.
The Widget’s origin is relative to its parent’s origin, and it is drawn after (on top of)
the parent’s existing children. If the cache is empty, the Widget becomes the root Widget
and parent is ignored.
§Arguments
parent- The ID of the parentWidget.widget- TheWidgetto add.
§Returns
The ID assigned to the Widget.
§Panics
Panics if the cache is not empty and parent does not exist.
Sourcepub fn try_add_child(
&mut self,
parent: WidgetId,
widget: Box<dyn Widget>,
) -> Result<WidgetId>
pub fn try_add_child( &mut self, parent: WidgetId, widget: Box<dyn Widget>, ) -> Result<WidgetId>
Adds a Widget as the last child of parent, reporting an unknown parent as an error
instead of panicking. Behaves like add_child otherwise.
§Arguments
parent- The ID of the parentWidget.widget- TheWidgetto add.
§Returns
The ID assigned to the Widget.
§Errors
Returns Error::UnknownWidget if the cache is not empty and parent does not exist.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of Widgets in the cache.
§Returns
The number of Widgets, including the root Widget.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether the cache contains no Widgets.
§Returns
true if no Widget (not even a root) has been added.
Sourcepub fn get_as<T: Widget>(&self, id: WidgetId) -> Option<&T>
pub fn get_as<T: Widget>(&self, id: WidgetId) -> Option<&T>
Retrieves a Widget by ID as its concrete type.
§Arguments
id- The ID of theWidget.
§Returns
The Widget as a T, or None if the ID does not exist or the Widget is not a T.
§Examples
use pushrod::cache::WidgetCache;
use pushrod::geometry::size;
use pushrod::root_widget::RootWidget;
let mut cache = WidgetCache::new();
cache.add(Box::new(RootWidget::new(size(320, 200))));
assert!(cache.get_as::<RootWidget>(0).is_some());Sourcepub fn children_of(&self, id: WidgetId) -> &[WidgetId] ⓘ
pub fn children_of(&self, id: WidgetId) -> &[WidgetId] ⓘ
Sourcepub fn get_current_widget(&self) -> WidgetId
pub fn get_current_widget(&self) -> WidgetId
Returns the Widget currently under the mouse pointer.
§Returns
The ID of the top-most visible Widget under the pointer, as of the last mouse event.
Sourcepub fn invalidate_all(&mut self)
pub fn invalidate_all(&mut self)
Forces the whole window to be redrawn on the next frame.
Every visible Widget’s texture is copied to the screen again (textures are not repainted
unless the Widget itself is invalidated). Used after the window is exposed or resized.
Sourcepub fn absolute_origin(&self, id: WidgetId) -> Point
pub fn absolute_origin(&self, id: WidgetId) -> Point
Sourcepub fn widget_at(&self, x: i32, y: i32) -> WidgetId
pub fn widget_at(&self, x: i32, y: i32) -> WidgetId
Finds the top-most visible Widget at a point in the window.
Hidden Widgets and their children are skipped.
§Arguments
x- The horizontal window coordinate, in pixels.y- The vertical window coordinate, in pixels.
§Returns
The ID of the top-most visible Widget containing the point, or 0 (the root Widget) if no
other Widget contains it.
Sourcepub fn set_event_mask(&mut self, id: WidgetId, mask: EventMask) -> bool
pub fn set_event_mask(&mut self, id: WidgetId, mask: EventMask) -> bool
Sets a Widget’s event mask and queues EngineEvent::EventMaskChanged when it changed.
Queued events are returned by WidgetCache::take_pending_events; the Engine delivers
them to the callbacks.
§Arguments
id- The ID of theWidget.mask- The new mask.
§Returns
true if the Widget exists and its mask changed.
Sourcepub fn set_engine_filter(
&mut self,
id: WidgetId,
kinds: Option<Vec<EngineEventKind>>,
)
pub fn set_engine_filter( &mut self, id: WidgetId, kinds: Option<Vec<EngineEventKind>>, )
Limits the engine events a Widget receives to the listed kinds.
The Widget still needs EventMask::ENGINE in its mask. Without a filter it receives
every kind.
§Arguments
id- The ID of theWidget.kinds- The accepted kinds, orNoneto remove the filter.
Sourcepub fn broadcast_engine_event(&mut self, event: &EngineEvent) -> Vec<Event>
pub fn broadcast_engine_event(&mut self, event: &EngineEvent) -> Vec<Event>
Broadcasts a completed engine event to the Widgets that want it.
A Widget receives it when it is enabled, the run loop’s mask and its own mask include
EventMask::ENGINE, and its kind filter (if any) accepts the event. Anything the
Widgets generate in response is returned and is never broadcast again, so engine events
cannot loop.
§Arguments
event- The engine event.
§Returns
The events the Widgets generated while handling it.
Sourcepub fn broadcast(&mut self, event: &Event) -> Vec<Event>
pub fn broadcast(&mut self, event: &Event) -> Vec<Event>
Broadcasts an event to every Widget that wants it, in drawing order.
A Widget receives the event when it is visible (neither it nor an ancestor is hidden),
enabled, and both the run loop’s mask and its own mask include the event’s class. Events
that are never filtered (see EventKind::mask_class) reach every visible, enabled
Widget. Delivery order is deterministic: parents before children, siblings in the order
they were added. Events the Widgets generate in response are returned, never
broadcast again.
Keyboard events are not broadcast; they belong to the focused Widget. Queues
EngineEvent::EventBroadcast describing the delivery.
§Arguments
event- The event to broadcast.
§Returns
The events the Widgets generated while handling it, in the order emitted.
Sourcepub fn request_animation(&mut self, id: WidgetId)
pub fn request_animation(&mut self, id: WidgetId)
Asks for an Event::Tick for a Widget on the next frame, from outside event handling,
for example to start an animation from a callback. Inside handle_event, use
EventCtx::request_animation.
§Arguments
id- TheWidgetto tick. Unknown IDs are ignored.
Sourcepub fn animation_requested(&self) -> bool
pub fn animation_requested(&self) -> bool
Returns whether any Widget has asked for a tick on the next frame.
§Returns
true while an animation request is waiting.
Sourcepub fn tick(&mut self, elapsed: Duration) -> Vec<Event>
pub fn tick(&mut self, elapsed: Duration) -> Vec<Event>
Delivers Event::Tick to the Widgets that requested animation since the last tick,
and only to those. The requests are consumed: a Widget that keeps animating requests
again while handling the tick. A Widget also needs EventMask::TIMER in its mask (and
the run loop’s), and must be enabled and visible. Queues EngineEvent::AnimationTicked
when at least one Widget received the tick.
§Arguments
elapsed- The time since the previous frame, carried by the tick.
§Returns
The events the Widgets generated while handling the tick, in order.
Sourcepub fn take_pending_events(&mut self) -> Vec<Event>
pub fn take_pending_events(&mut self) -> Vec<Event>
Removes and returns the engine events queued by the cache itself, in order.
§Returns
The queued events; empty if there are none.
Sourcepub fn needs_draw(&self) -> bool
pub fn needs_draw(&self) -> bool
Sourcepub fn propagate_with(
&mut self,
target: WidgetId,
event: &Event,
ctx: &mut EventCtx,
) -> Propagation
pub fn propagate_with( &mut self, target: WidgetId, event: &Event, ctx: &mut EventCtx, ) -> Propagation
Delivers an input event to target through the capture, target, and bubble phases.
- Capture: from the root down to the target’s parent, only
Widgets whosehandles_captureistrue. - Target: the
Widgetitself. - Bubble: from the target’s parent up to the root.
Propagation::Stop from any Widget ends delivery. Disabled and hidden Widgets, and
those whose mask excludes the event, are skipped. Events that do not propagate (see
EventKind::propagates) go to the target only.
§Arguments
target- TheWidgetthe event is for: under the pointer, capturing the mouse, or focused.event- The event.ctx- The context collecting generated events.
§Returns
Propagation::Stop if a Widget stopped delivery, otherwise Propagation::Continue.
Sourcepub fn propagate(&mut self, target: WidgetId, event: &Event) -> Vec<Event>
pub fn propagate(&mut self, target: WidgetId, event: &Event) -> Vec<Event>
Delivers an input event, such as a key press for the focused Widget, through the
capture, target, and bubble phases. See WidgetCache::propagate_with.
§Arguments
target- TheWidgetthe event is for.event- The event.
§Returns
The events the Widgets generated while handling it, in the order emitted.
Sourcepub fn deliver_custom(&mut self, custom: &CustomEvent) -> Vec<Event>
pub fn deliver_custom(&mut self, custom: &CustomEvent) -> Vec<Event>
Delivers a custom event to the Widgets its target names.
A Widget receives it when EventMask::CUSTOM is in both the run loop’s mask and its
own mask. Custom events are not input, so disabled Widgets still receive them; a
Broadcast reaches only visible Widgets. Delivery is in
drawing order, parents before children. The core takes no other action: no focus, mouse
capture, selection, or redraw changes.
§Arguments
custom- The custom event.
§Returns
The events the Widgets generated while handling it, in the order emitted (including any
custom events they posted, which the caller queues).
Sourcepub fn handle_event(&mut self, event: InputEvent) -> Vec<Event>
pub fn handle_event(&mut self, event: InputEvent) -> Vec<Event>
Routes a raw input event to the Widgets.
- A mouse button press sends
PointerPressedto the top-mostWidgetunder the pointer, which captures the mouse until the button is released. - Mouse motion sends
PointerExitedto theWidgetthat was left andPointerEnteredto theWidgetthat was entered (when they differ), thenPointerMovedto the capturingWidget, or theWidgetunder the pointer. - A mouse button release sends
PointerReleasedto the capturingWidget, then ends the capture. - Presses, releases, and motion travel through the capture, target, and bubble phases (see
WidgetCache::propagate_with); hover events go to oneWidget. - A mouse button press first focuses the nearest focusable
Widgetat or above the one pressed. - Key presses and releases go to the focused
Widget(or the root with no focus), then Tab, the arrow keys, Return, and Escape act as described inWidgetCache::focus_next,WidgetCache::focus_in_group, andWidgetCache::set_focus. Typed text goes to the focusedWidget. - Other events are ignored.
- Disabled
Widgets receive none of these events, but still block theWidgets beneath them.
§Arguments
event- The input event from the backend.
§Returns
The Events the Widgets generated while handling the event, in the order emitted.
Sourcepub fn draw_loop(&mut self, backend: &mut dyn Backend) -> bool
pub fn draw_loop(&mut self, backend: &mut dyn Backend) -> bool
Repaints changed Widgets and redraws the window if anything changed.
Every invalidated, visible Widget is repainted into its texture. If any Widget changed (or
a full redraw was requested), the window is cleared and every visible Widget’s texture is
copied to the screen in drawing order. Copying cached textures is cheap; repainting only
happens for Widgets that changed.
§Arguments
backend- The backend that owns the window and theWidgettextures.
§Returns
true if a frame was drawn and the backend should present it, false if nothing
changed.