pub trait Widget: Any + TypeName {
// Required method
fn paint(&self, cx: &PaintCx<'_>, out: &mut Vec<PaintOp>);
// Provided methods
fn measure(&mut self, _input: MeasureInput) -> Option<Size> { ... }
fn ime_area(&self) -> Option<Rect> { ... }
fn semantics(&self) -> Option<Semantics> { ... }
}Expand description
A piece of user interface owned by one tree node.
A widget holds its own state and is the only place that state lives. The
tree owns the widget and the Style that positions and paints it.
Implementing this trait requires the type to be 'static, because the tree
downcasts to the concrete type when a binding updates a widget property.
The TypeName bound is met by every type without any code, and supplies
the name of the widget’s type that diagnostics print.
Required Methods§
Sourcefn paint(&self, cx: &PaintCx<'_>, out: &mut Vec<PaintOp>)
fn paint(&self, cx: &PaintCx<'_>, out: &mut Vec<PaintOp>)
Appends this widget’s paint operations, in paint coordinates.
Paint coordinates are logical window coordinates with the scroll offsets of the enclosing scroll views left out, so outside a scrolled subtree they are the window’s own. A scroll then moves the operations without asking the widget to paint again.
cx carries the node’s rectangle after layout, in paint coordinates,
and its resolved style. Paint this node only: its children paint
themselves.
Provided Methods§
Sourcefn measure(&mut self, _input: MeasureInput) -> Option<Size>
fn measure(&mut self, _input: MeasureInput) -> Option<Size>
Returns the size of this widget’s content, or None to let the layout
engine size the node from its style.
input.known_width and input.known_height hold the sizes the engine
treats as fixed for this call, and either can be absent even when the
node’s style fixes a size. input.available_width and
input.available_height are the space left on each axis inside the
node’s padding and border. An axis can also ask for an intrinsic size
instead of offering space: AvailableSpace::MinContent asks for the
smallest size the content fits in, such as the widest word of a text,
and AvailableSpace::MaxContent asks for the size the content takes
without wrapping. The returned size is the content size, so the engine
adds padding and border around it.
The engine may call this several times in one layout pass.
The default returns None, which suits a widget that only paints. A
widget that returns a size must return the same size for the same
inputs, because the layout engine caches it until the node is marked
for layout again.
Sourcefn ime_area(&self) -> Option<Rect>
fn ime_area(&self) -> Option<Rect>
Returns the rectangle the input method should anchor to while this
widget holds the keyboard focus, in the paint coordinates of
Widget::paint.
The app reports the rectangle in window coordinates: it moves the rectangle by minus the scroll offsets that move the widget at the time it asks.
The app asks only the focused widget and reports None when nothing
is focused or the widget has no area to report. The default reports
None, which suits a widget that takes no text.
Sourcefn semantics(&self) -> Option<Semantics>
fn semantics(&self) -> Option<Semantics>
Returns the accessibility description that places this widget in the
projection, or None to leave it out.
The default reports None, which suits a widget that paints for
sighted users alone. A widget that returns a description is projected
with the role it reports, and an element can still override the role
with Element::role.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".