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§
Sourcefn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
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).
Sourcefn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
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§
Sourcefn event(&mut self, _ctx: &mut EventCtx<'_>, _event: &InputEvent) -> EventResult
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.
Sourcefn semantics(&self, _ctx: &mut SemanticsCtx)
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.
Sourcefn type_name(&self) -> &'static str
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).
Sourcefn visit_children(&self, _visitor: &mut dyn FnMut(&ChildPod))
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§
Trait Implementations§
Source§impl Widget for Box<dyn Widget>
A boxed widget is itself a Widget, delegating every pass to its contents.
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
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
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
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)
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
scene. Read moreSource§fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
ctx and reporting whether it was consumed. Read moreSource§fn semantics(&self, ctx: &mut SemanticsCtx)
fn semantics(&self, ctx: &mut SemanticsCtx)
ctx. Read moreDyn 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 AlignWidget
impl Widget for AlignWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for Box<dyn Widget>
A boxed widget is itself a Widget, delegating every pass to its contents.
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
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).
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for ButtonWidget
impl Widget for ButtonWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for CheckboxWidget
impl Widget for CheckboxWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for FlexWidget
impl Widget for FlexWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for GestureDetectorWidget
impl Widget for GestureDetectorWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for HeroWidget
impl Widget for HeroWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for IconButtonWidget
impl Widget for IconButtonWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for ImageWidget
impl Widget for ImageWidget
Source§impl Widget for PaddingWidget
impl Widget for PaddingWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for PinchDetectorWidget
impl Widget for PinchDetectorWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for PlatformViewWidget
impl Widget for PlatformViewWidget
fn layout(&mut self, _ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn semantics(&self, ctx: &mut SemanticsCtx)
Source§impl Widget for SafeAreaWidget
impl Widget for SafeAreaWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for ScaffoldWidget
impl Widget for ScaffoldWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for ScrollWidget
impl Widget for ScrollWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for ShieldWidget
impl Widget for ShieldWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for SizedBoxWidget
impl Widget for SizedBoxWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for SliderWidget
impl Widget for SliderWidget
fn layout(&mut self, _ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
Source§impl Widget for StackWidget
impl Widget for StackWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
fn visit_children(&self, visitor: &mut dyn FnMut(&ChildPod))
Source§impl Widget for TextInputWidget
impl Widget for TextInputWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn event(&mut self, ctx: &mut EventCtx<'_>, event: &InputEvent) -> EventResult
fn semantics(&self, ctx: &mut SemanticsCtx)
Source§impl Widget for TextWidget
impl Widget for TextWidget
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, bc: &BoxConstraints) -> Size
fn paint(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut dyn PaintScene)
fn semantics(&self, ctx: &mut SemanticsCtx)
Source§fn semantics(&self, ctx: &mut SemanticsCtx)
fn semantics(&self, ctx: &mut SemanticsCtx)
R23 — semantics forwarding follows input routing, exactly.
A transparent container (like Stack): the navigator
contributes no node of its own and forwards
ChildPod::semantics_child for exactly the pages
input_routed_pages says an input event
could reach — today { pages.last() }. Every other page is omitted:
no node, no recursion. Offering a screen-reader user a control they
physically cannot activate is worse than not offering it, so the two
reaches are derived from one function rather than kept in sync by hand.
§This omits more than paint culling does
A page under a transparent overlay is PageVisibility::Visible —
still painted — yet it is omitted here, because R23 tracks input
routing, not painting, and route_top routes only
to the top page. The divergence is deliberate: it is what makes a modal
modal to assistive technology for free (the page beneath a dialog is
already inert to a finger). The topmost transparent page — the dialog
itself — is forwarded, since it is pages.last().
§Why omission and not an accesskit flag
Not hidden: it would need a synthesized per-page wrapper node to carry
the flag (churning node ids on every navigation for nodes that exist
only to say “ignore me”), it would publish the stale bounds of a
covered page that layout skipped, and whether every platform adapter
honours the flag is unverified — omission needs no such trust. Not
clips_children, which asserts overflow: hidden and would be simply
false here. Not modal, which is the right flag but belongs on the
dialog widget: the navigator does not know a page is a dialog and cannot
infer it from BackPolicy (most shipped modals push through
push_transparent_for_result and so take the default
BackPolicy::Pop).
§During a transition
No special case: pages.last() is the destination page for a push,
pop and replace alike, and its mid-flight bounds are the animated pod
origins paint is using — consistent with the screen and
self-correcting within one transition. Input is fully suppressed
mid-transition (event_at), so a screen reader
activating a node during those ≤340ms hits exactly the same suppression
a finger would.