pub trait Layout {
// Required methods
fn rect(&self) -> Rect;
fn size_rules(&mut self, cx: &mut SizeCx<'_>, axis: AxisInfo) -> SizeRules;
fn set_rect(&mut self, cx: &mut SizeCx<'_>, rect: Rect, hints: AlignHints);
fn draw(&self, draw: DrawCx<'_>);
}Expand description
Positioning and drawing routines for Widgets
Layout is used to implement Widget sizing and drawing operations
(“layout”).
See Widget documentation and the #widget macro.
Layout may not be implemented independently.
§Implementation
The #widget macro will, when its layout property is specified,
generate an implementation of this trait (if omitted from the surrounding
#[impl_self]) or provide default implementations of its methods (if an
explicit impl of Layout is found but some methods are missing).
§Call order
Widgets must be configured before any
Layout methods are called. This is not applicable to non-widgets.
§Sizing
Sizing involves calling the following in order:
Layout::size_rulesfor the horizontal axisLayout::size_rulesfor the vertical axisLayout::set_rect
This order is required initially. Resizing may start at any of the above steps but must then proceed in-order for all remaining steps. If the scale factor is changed, then resizing must start from step 1.
Typically parent widgets call these methods from their own implementations
of Layout::size_rules and Layout::set_rect. When calling these
methods at other times, be sure to respect the call order.
Other Layout methods may only be called once sizing is complete.
Required Methods§
Sourcefn rect(&self) -> Rect
fn rect(&self) -> Rect
Get the widget’s region
Coordinates are relative to the parent’s coordinate space.
This is often the Rect passed to Layout::set_rect, though it is
not required to be; for example some widgets align themselves within
their allocated rect when it is too large; widgets may also exceed
their allocated rect if it is smaller than their minimum size.
This method is usually implemented by the #[widget] macro as
WidgetCoreRect::rect or from macro-defined layout.
Sourcefn size_rules(&mut self, cx: &mut SizeCx<'_>, axis: AxisInfo) -> SizeRules
fn size_rules(&mut self, cx: &mut SizeCx<'_>, axis: AxisInfo) -> SizeRules
Calculate size requirements for an axis
This method is used both to initialize a widget at a given scale factor and to assess size requirements.
§Calling
This method is called during sizing (see above).
§Implementation
For a description of the widget size model, see SizeRules.
For row/column/grid layouts, a crate::layout::RulesSolver engine
may be useful.
§Default implementation
The #[layout] macro may be used to
provide a default implementation.
Sourcefn set_rect(&mut self, cx: &mut SizeCx<'_>, rect: Rect, hints: AlignHints)
fn set_rect(&mut self, cx: &mut SizeCx<'_>, rect: Rect, hints: AlignHints)
Set size and position
§Calling
This method is called to finalize sizing (see above).
§Implementation
The size of the assigned rect is normally at least the minimum size
requested by Self::size_rules, but this is not guaranteed. In case
this minimum is not met, it is permissible for the widget to draw
outside of its assigned rect and to not function as normal.
The assigned rect may be larger than the widget’s size requirements,
regardless of the Stretch policy used: containers divide up space
based on children’s SizeRules but do not attempt to align content
when excess space is available. Instead, content is responsible for
aligning itself using the provided hints and/or local information.
This method must ensure that the widget’s new Rect is available
through Layout::rect. The easiest way to achieve this is usually
to call WidgetCoreRect::set_rect and allow the #[widget] macro to
provide an implementation of Layout::rect.
§Default implementation
The #[layout] macro may be used to
provide a default implementation.
Sourcefn draw(&self, draw: DrawCx<'_>)
fn draw(&self, draw: DrawCx<'_>)
Draw a widget and its children
§Calling
This method is invoked each frame to draw visible widgets. It should draw itself and recurse into all visible children.
§Implementation
§Default implementation
The #[layout] macro may be used to
provide a default implementation.
§Method modification
The #[widget] macro injects a call to DrawCx::set_id into this
method where possible, allowing correct detection of disabled and
highlight states.
This method modification should never cause issues (besides the implied limitation that widgets cannot easily detect a parent’s state while being drawn).
Implementations on Foreign Types§
Implementors§
impl Layout for Mark
impl Layout for TitleBar
impl Layout for TitleBarButtons
impl<A, W: Widget<Data = ()>> Layout for MapAny<A, W>
impl<Data: AppData> Layout for Window<Data>
impl<M: Clone + Debug + 'static> Layout for MarkButton<M>
impl<T: FormattableText + 'static> Layout for Label<T>
impl<T: FormattableText> Layout for Text<T>
Implement Layout, using default alignment where alignment is not provided