Trait View

Source
pub trait View:
    Any
    + AnyView
    + Send
    + Sync {
    // Required method
    fn draw(&self, printer: &Printer<'_, '_>);

    // Provided methods
    fn layout(&mut self, _: XY<usize>) { ... }
    fn needs_relayout(&self) -> bool { ... }
    fn required_size(&mut self, constraint: XY<usize>) -> XY<usize> { ... }
    fn on_event(&mut self, _: Event) -> EventResult { ... }
    fn call_on_any(
        &mut self,
        _: &Selector<'_>,
        _: &mut dyn FnMut(&mut (dyn View + 'static)),
    ) { ... }
    fn focus_view(
        &mut self,
        _: &Selector<'_>,
    ) -> Result<EventResult, ViewNotFound> { ... }
    fn take_focus(
        &mut self,
        source: Direction,
    ) -> Result<EventResult, CannotFocus> { ... }
    fn important_area(&self, view_size: XY<usize>) -> Rect { ... }
    fn type_name(&self) -> &'static str { ... }
}
Expand description

Main trait defining a view behaviour.

This is what you should implement to define a custom View.

Required Methods§

Source

fn draw(&self, printer: &Printer<'_, '_>)

Draws the view with the given printer (includes bounds) and focus.

This is the only required method to implement.

Provided Methods§

Source

fn layout(&mut self, _: XY<usize>)

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 negotiated. It is guaranteed to be the size available for the call to draw().

Source

fn needs_relayout(&self) -> bool

Should return true if the view content changed since the last call to layout().

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 or layout with stable parameters, the children may cache the result themselves and speed up the process anyway.
Source

fn required_size(&mut self, constraint: XY<usize>) -> XY<usize>

Returns the minimum size the view requires with the given restrictions.

This is the main way a view communicate its size to its parent.

Some views have a fixed size, and will ignore the constraint parameter entirely.

Some views are flexible, and may adapt fully or partially to the constraints.

Default implementation always return (1,1).

Source

fn on_event(&mut self, _: Event) -> EventResult

Called when an event is received (key press, mouse event, …).

You can return an EventResult:

  • EventResult::Ignored means the event was not processed and may be sent to another view.
  • EventResult::Consumed means the event was consumed and should not be sent to any other view. It may in addition include a callback to be run.

The default implementation just ignores any event.

Source

fn call_on_any( &mut self, _: &Selector<'_>, _: &mut dyn FnMut(&mut (dyn View + 'static)), )

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.

View groups should implement this to forward the call to each children.

Default implementation is a no-op.

Source

fn focus_view(&mut self, _: &Selector<'_>) -> Result<EventResult, ViewNotFound>

Moves the focus to the view identified by the given selector.

Returns Ok(_) if the view was found and selected. A callback may be included, it should be run on &mut Cursive.

Default implementation simply returns Err(ViewNotFound).

Source

fn take_focus(&mut self, source: Direction) -> Result<EventResult, CannotFocus>

Attempt to give this view the focus.

source indicates where the focus comes from. When the source is unclear (for example mouse events), Direction::none() can be used.

Returns Ok(_) if the focus was taken. Returns Err(_) if this view does not take focus (default implementation).

Source

fn important_area(&self, view_size: XY<usize>) -> 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.

Source

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

Returns the type of this view.

Useful when you have a &dyn View.

View implementation don’t usually have to override this.

Implementations§

Source§

impl dyn View

Source

pub fn downcast_ref<T>(&self) -> Option<&T>
where T: Any,

Attempts to downcast self to a concrete type.

Source

pub fn downcast_mut<T>(&mut self) -> Option<&mut T>
where T: Any,

Attempts to downcast self to a concrete type.

Source

pub fn downcast<T>(self: Box<dyn View>) -> Result<Box<T>, Box<dyn View>>
where T: Any,

Attempts to downcast Box<Self> to a concrete type.

Source

pub fn is<T>(&self) -> bool
where T: Any,

Checks if this view is of type T.

Trait Implementations§

Source§

impl IntoBoxedView for Box<dyn View>

Source§

fn into_boxed_view(self) -> Box<dyn View>

Returns a Box<View>.
Source§

impl Resolvable for Box<dyn View>

Source§

fn from_config( config: &Value, context: &Context, ) -> Result<Box<dyn View>, Error>

Build from a config (a JSON value). Read more
Source§

fn from_any(any: Box<dyn Any>) -> Option<Self>
where Self: Sized + Any,

Build from an Any variable. Read more

Implementors§

Source§

impl View for ObservedScreenView

Source§

impl View for Button

Source§

impl View for Checkbox

Source§

impl View for DebugView

Source§

impl View for Dialog

Source§

impl View for DummyView

Source§

impl View for EditView

Source§

impl View for FixedLayout

Source§

impl View for LinearLayout

Source§

impl View for ListView

Source§

impl View for MenuPopup

Source§

impl View for Menubar

Source§

impl View for ProgressBar

Source§

impl View for SliderView

Source§

impl View for StackView

Source§

impl View for TextArea

Source§

impl View for TextView

Source§

impl<T> View for Canvas<T>
where T: 'static + Send + Sync,

Source§

impl<T> View for RadioButton<T>
where T: 'static + Send + Sync,

Source§

impl<T> View for SelectView<T>
where T: 'static + Send + Sync,

Source§

impl<T> View for T
where T: ViewWrapper,

Source§

impl<V> View for ScrollView<V>
where V: View,