Skip to main content

Widget

Trait Widget 

Source
pub trait Widget: Any {
    // Required methods
    fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size;
    fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene);

    // Provided methods
    fn event(
        &mut self,
        _ctx: &mut EventCtx<'_>,
        _event: &InputEvent,
    ) -> EventResult { ... }
    fn semantics(&self, _ctx: &mut SemanticsCtx) { ... }
    fn type_name(&self) -> &'static str { ... }
    fn visit_children(&self, _visitor: &mut dyn FnMut(&ChildPod)) { ... }
}
Expand description

A retained UI element living in the widget tree.

Widget: Any so the render root can downcast a boxed widget back to the concrete element type its originating view produced (needed during rebuild). Implementing types get [Widget::downcast_mut] for free — no boilerplate method to write.

Required Methods§

Source

fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size

Choose a size within bc and return it. The chosen size must satisfy bc (callers may additionally clamp via BoxConstraints::constrain).

Source

fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)

Emit draw commands for this widget into scene.

Paint may advance a widget’s own animation state (e.g. a scroll fling integrated from a monotonic clock) as a v1 seam. A widget that does so must call PaintCtx::request_frame while the animation is still running so the shell re-invokes paint absent any external event — otherwise the desktop ControlFlow::Wait loop idles and the animation stalls. It must stop signalling once the animation reaches rest.

Provided Methods§

Source

fn event(&mut self, _ctx: &mut EventCtx<'_>, _event: &InputEvent) -> EventResult

Handle an input event, optionally mutating application state through ctx and reporting whether it was consumed.

Defaulted to EventResult::Ignored so non-interactive widgets are unaffected. Interactive widgets (Button/Checkbox/Slider) override this; containers forward to their ChildPod children via ChildPod::event_child.

Source

fn semantics(&self, _ctx: &mut SemanticsCtx)

Contribute this widget’s accessibility node(s) into ctx.

Defaulted to a no-op so non-semantic widgets (and every widget written before this seam) are unaffected — exactly like Widget::event. A leaf widget overrides it to call SemanticsCtx::push_node with its role and state; a container overrides it to forward to each child via ChildPod::semantics_child (a transparent container contributes no node of its own, only recursion). The pass runs after layout, so SemanticsCtx::origin/SemanticsCtx::size carry valid absolute geometry.

Source

fn type_name(&self) -> &'static str

This widget’s concrete type name, for read-only tooling.

Defaulted to core::any::type_name of the implementing type, so every widget reports its own name through the vtable — including one reached as a dyn Widget, where the concrete type is otherwise unrecoverable. Asking the live widget beats recording a name when it was built: a rebuild that swaps a child’s concrete type cannot leave a stale name behind, not even through the doubly-erased pods no reconciler can observe (docs/LIMITATIONS.md’s focus-double-erasure-swap-blind).

Diagnostic only: type_name’s output is not a stable contract across compiler versions, so never parse or match on it. Overriding it is sanctioned only for a transparent wrapper reporting what it wraps (the Box<dyn Widget> blanket impl is the one in-crate case).

Source

fn visit_children(&self, _visitor: &mut dyn FnMut(&ChildPod))

Visit this widget’s owned ChildPods, in declaration (paint) order — the read-only seam that makes the retained hierarchy enumerable.

Containers own their children as ChildPod fields rather than as arena nodes (see ChildPod), so nothing outside a container could walk into its subtree; this is that walk, and WidgetTree::inspect is its one in-crate consumer.

Defaulted to visiting nothing, exactly like Widget::event and Widget::semantics: a leaf widget needs no impl and pays nothing, and a container that never overrides it simply reads as a leaf to tooling. It runs behind &self and must not mutate anything a pass depends on — no build/layout/paint/event behavior may be routed through it.

No cycles by construction. A ChildPod owns its widget (Box<dyn Widget>); ownership is a tree, so a descent through visit_children terminates without any runtime cycle check. A widget that handed the visitor a pod it does not own would break that, which is why the visitor takes &ChildPod — there is no way to publish a shared one.

Implementations§

Source§

impl dyn Widget

Source

pub fn downcast_mut<W: Widget>(&mut self) -> Option<&mut W>

Attempt to downcast this trait object to a concrete widget type.

Uses trait upcasting (Widget: Any, stable since Rust 1.86; the workspace MSRV is 1.88) — no unsafe.

Trait Implementations§

Source§

impl Widget for Box<dyn Widget>

A boxed widget is itself a Widget, delegating every pass to its contents.

This blanket impl is what makes type-erased children work: a crate::view::AnyView’s element is a Box<dyn Widget>, and View::Element must implement Widget — so the box has to be a widget too. It also lets a Box<dyn Widget> be stored inside a ChildPod like any concrete widget. Box<dyn Widget> is 'static (hence Any), so it satisfies the Widget: Any bound and can be recovered by [downcast_mut](dyn Widget::downcast_mut) during rebuild.

Source§

fn type_name(&self) -> &'static str

Report the boxed widget’s name, not Box<dyn Widget> — the box is a storage detail of type erasure, never a tree element in its own right, and it nests (a doubly-boxed pod resolves through both layers).

Source§

fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size

Choose a size within bc and return it. The chosen size must satisfy bc (callers may additionally clamp via BoxConstraints::constrain).
Source§

fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)

Emit draw commands for this widget into scene. Read more
Source§

fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult

Handle an input event, optionally mutating application state through ctx and reporting whether it was consumed. Read more
Source§

fn semantics(&self, ctx: &mut SemanticsCtx)

Contribute this widget’s accessibility node(s) into ctx. Read more
Source§

fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))

Visit this widget’s owned ChildPods, in declaration (paint) order — the read-only seam that makes the retained hierarchy enumerable. Read more

Dyn Compatibility§

This trait is dyn compatible.

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

Implementations on Foreign Types§

Source§

impl Widget for Box<dyn Widget>

A boxed widget is itself a Widget, delegating every pass to its contents.

This blanket impl is what makes type-erased children work: a crate::view::AnyView’s element is a Box<dyn Widget>, and View::Element must implement Widget — so the box has to be a widget too. It also lets a Box<dyn Widget> be stored inside a ChildPod like any concrete widget. Box<dyn Widget> is 'static (hence Any), so it satisfies the Widget: Any bound and can be recovered by [downcast_mut](dyn Widget::downcast_mut) during rebuild.

Source§

fn type_name(&self) -> &'static str

Report the boxed widget’s name, not Box<dyn Widget> — the box is a storage detail of type erasure, never a tree element in its own right, and it nests (a doubly-boxed pod resolves through both layers).

Source§

fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size

Source§

fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)

Source§

fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult

Source§

fn semantics(&self, ctx: &mut SemanticsCtx)

Source§

fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))

Implementors§