pub trait View: Any + AnyView {
fn draw(&self, printer: &Printer<'_, '_>);
fn layout(&mut self, : Vec2) { ... }
fn needs_relayout(&self) -> bool { ... }
fn required_size(&mut self, constraint: Vec2) -> Vec2 { ... }
fn on_event(&mut self, : Event) -> EventResult { ... }
fn call_on_any<'a>(&mut self, _: &Selector<'_>, _: AnyCb<'a>) { ... }
fn focus_view(&mut self, : &Selector<'_>) -> Result<(), ()> { ... }
fn take_focus(&mut self, source: Direction) -> bool { ... }
fn important_area(&self, view_size: Vec2) -> Rect { ... }
}
Expand description
Main trait defining a view behaviour.
This is what you should implement to define a custom View.
Required Methods
Provided Methods
sourcefn layout(&mut self, : Vec2)
fn layout(&mut self, : Vec2)
Called once the size for this view has been decided.
It can be used to pre-compute the configuration of child views.
View groups should propagate the information to their children.
At this point, the given size is final and cannot be negociated.
It is guaranteed to be the size available for the call to draw()
.
sourcefn needs_relayout(&self) -> bool
fn needs_relayout(&self) -> bool
Returns true
if the view content changed since last layout phase.
This is mostly an optimisation for views where the layout phase is expensive.
- Views can ignore it and always return true (default implementation). They will always be assumed to have changed.
- View Groups can ignore it and always re-layout their children.
- If they call
required_size
orlayout
with stable parameters, the children may cache the result themselves and speed up the process anyway.
- If they call
sourcefn required_size(&mut self, constraint: Vec2) -> Vec2
fn required_size(&mut self, constraint: Vec2) -> Vec2
Returns the minimum size the view requires with the given restrictions.
This is the main way a view communicate its size to its parent.
If the view is flexible (it has multiple size options), it can try
to return one that fits the given constraint
.
It’s also fine to ignore it and return a fixed value.
Default implementation always return (1,1)
.
sourcefn on_event(&mut self, : Event) -> EventResult
fn on_event(&mut self, : Event) -> EventResult
Called when an event is received (key press, mouse event, …).
You can return an EventResult
, with an optional callback to be run.
Default implementation just ignores it.
sourcefn call_on_any<'a>(&mut self, _: &Selector<'_>, _: AnyCb<'a>)
fn call_on_any<'a>(&mut self, _: &Selector<'_>, _: AnyCb<'a>)
Runs a closure on the view identified by the given selector.
See Finder::call_on
for a nicer interface, implemented for all
views.
If the selector doesn’t find a match, the closure will not be run.
Default implementation is a no-op.
sourcefn focus_view(&mut self, : &Selector<'_>) -> Result<(), ()>
fn focus_view(&mut self, : &Selector<'_>) -> Result<(), ()>
Moves the focus to the view identified by the given selector.
Returns Ok(())
if the view was found and selected.
Default implementation simply returns Err(())
.
sourcefn take_focus(&mut self, source: Direction) -> bool
fn take_focus(&mut self, source: Direction) -> bool
This view is offered focus. Will it take it?
source
indicates where the focus comes from.
When the source is unclear, Front
is usually used.
Default implementation always return false
.
sourcefn important_area(&self, view_size: Vec2) -> Rect
fn important_area(&self, view_size: Vec2) -> Rect
What part of the view is important and should be visible?
When only part of this view can be visible, this helps determine which part.
It is given the view size (same size given to layout
).
Default implementation return the entire view.
Trait Implementations
sourceimpl IntoBoxedView for Box<dyn View>
impl IntoBoxedView for Box<dyn View>
sourcefn as_boxed_view(self) -> Box<dyn View>
fn as_boxed_view(self) -> Box<dyn View>
Box<View>
.