Skip to main content

WidgetCache

Struct WidgetCache 

Source
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

Source

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.

Source

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 - The Widget to 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).

Source

pub fn clear_focus(&mut self)

Removes the keyboard focus, so no Widget has it.

Source

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.

Source

pub fn focus_next(&mut self, backward: bool) -> bool

Moves focus to the next (or previous) group, wrapping at the ends.

§Arguments
  • backward - true for Shift-Tab.
§Returns

true if a Widget has focus afterward.

Source

pub fn focus_in_group(&mut self, forward: bool) -> bool

Moves focus within the focused Widget’s radio group, wrapping at the ends.

§Arguments
  • forward - true for the next member (Right, Down), false for the previous.
§Returns

true if focus moved; false if the focused Widget is not in a group of two or more.

Source§

impl WidgetCache

Source

pub fn new() -> Self

Creates an empty WidgetCache.

The first Widget added becomes the root Widget (ID 0).

§Returns

A new, empty WidgetCache.

Source

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.

Source

pub fn set_cache_policy(&mut self, policy: CachePolicy)

Sets the cache policy, as declared by the backend.

§Arguments
Source

pub fn add(&mut self, widget: Box<dyn Widget>) -> WidgetId

Adds a Widget as a child of the root Widget.

If the cache is empty, the Widget becomes the root Widget instead.

§Arguments
  • widget - The Widget to add.
§Returns

The ID assigned to the Widget.

Source

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 parent Widget.
  • widget - The Widget to add.
§Returns

The ID assigned to the Widget.

§Panics

Panics if the cache is not empty and parent does not exist.

Source

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 parent Widget.
  • widget - The Widget to add.
§Returns

The ID assigned to the Widget.

§Errors

Returns Error::UnknownWidget if the cache is not empty and parent does not exist.

Source

pub fn len(&self) -> usize

Returns the number of Widgets in the cache.

§Returns

The number of Widgets, including the root Widget.

Source

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.

Source

pub fn get(&self, id: WidgetId) -> Option<&dyn Widget>

Retrieves a Widget by ID.

§Arguments
  • id - The ID of the Widget.
§Returns

The Widget as a trait object, or None if the ID does not exist.

Source

pub fn get_mut(&mut self, id: WidgetId) -> Option<&mut dyn Widget>

Retrieves a Widget by ID for modification.

§Arguments
  • id - The ID of the Widget.
§Returns

The Widget as a mutable trait object, or None if the ID does not exist.

Source

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 the Widget.
§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());
Source

pub fn get_as_mut<T: Widget>(&mut self, id: WidgetId) -> Option<&mut T>

Retrieves a Widget by ID as its concrete type, for modification.

§Arguments
  • id - The ID of the Widget.
§Returns

The Widget as a mutable T, or None if the ID does not exist or the Widget is not a T.

Source

pub fn children_of(&self, id: WidgetId) -> &[WidgetId] ⓘ

Returns the children of a Widget.

§Arguments
  • id - The ID of the parent Widget.
§Returns

The IDs of the Widget’s direct children in drawing order (the last is drawn on top), or an empty slice if the Widget has no children or does not exist.

Source

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.

Source

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.

Source

pub fn absolute_origin(&self, id: WidgetId) -> Point

Returns the origin of a Widget in window coordinates.

§Arguments
  • id - The ID of the Widget.
§Returns

The Widget’s upper left-hand corner in window coordinates: its origin plus the origins of all of its ancestors. Returns (0, 0) if the ID does not exist.

Source

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.

Source

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 the Widget.
  • mask - The new mask.
§Returns

true if the Widget exists and its mask changed.

Source

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 the Widget.
  • kinds - The accepted kinds, or None to remove the filter.
Source

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.

Source

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.

Source

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 - The Widget to tick. Unknown IDs are ignored.
Source

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.

Source

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.

Source

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.

Source

pub fn needs_draw(&self) -> bool

Returns whether the next draw_loop has anything to do.

§Returns

true if a full redraw is pending or any Widget is invalidated.

Source

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.

  1. Capture: from the root down to the target’s parent, only Widgets whose handles_capture is true.
  2. Target: the Widget itself.
  3. 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 - The Widget the 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.

Source

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 - The Widget the event is for.
  • event - The event.
§Returns

The events the Widgets generated while handling it, in the order emitted.

Source

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).

Source

pub fn handle_event(&mut self, event: InputEvent) -> Vec<Event>

Routes a raw input event to the Widgets.

  • A mouse button press sends PointerPressed to the top-most Widget under the pointer, which captures the mouse until the button is released.
  • Mouse motion sends PointerExited to the Widget that was left and PointerEntered to the Widget that was entered (when they differ), then PointerMoved to the capturing Widget, or the Widget under the pointer.
  • A mouse button release sends PointerReleased to the capturing Widget, then ends the capture.
  • Presses, releases, and motion travel through the capture, target, and bubble phases (see WidgetCache::propagate_with); hover events go to one Widget.
  • A mouse button press first focuses the nearest focusable Widget at 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 in WidgetCache::focus_next, WidgetCache::focus_in_group, and WidgetCache::set_focus. Typed text goes to the focused Widget.
  • Other events are ignored.
  • Disabled Widgets receive none of these events, but still block the Widgets beneath them.
§Arguments
  • event - The input event from the backend.
§Returns

The Events the Widgets generated while handling the event, in the order emitted.

Source

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 the Widget textures.
§Returns

true if a frame was drawn and the backend should present it, false if nothing changed.

Trait Implementations§

Source§

impl Default for WidgetCache

Source§

fn default() -> Self

Creates an empty WidgetCache; the same as WidgetCache::new.

§Returns

A new, empty WidgetCache.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.