Builder

Trait Builder 

Source
pub trait Builder<'v>: Sized {
    type View: View<Args<'v> = Self>;
}
Expand description

Builders are required to build and update views

This is a simple trait that ‘binds’ two types together – a cheap builder and a view.

When the UI creates or updates a view, this builder gets passed to it.

When the user wants to show a view, they can pass its builder to ui.show() or ui.show_children()

§Note on the 'v lifetime.

This lifetime is provided but you don’t have to use it. If you borrow any data from the application, it must conform to this ’v lifetime. Views cannot borrow data as they must be ’static

§Builders can also be views

// views must be `Debug`, builders don't have to be
#[derive(Debug)]
struct Foo;

impl<'v> Builder<'v> for Foo {
    type View = Self; // we cannot borrow anything for 'v because a View must be 'static
}

impl View for Foo {
    type Args<'v> = Self; // we can build ourselves
    type Response = ();

    fn create(args: Self::Args<'_>) -> Self {
        // args here is 'Self'
        // so we can just return it
        // but we can also do Foo{} or Self{}
        args
    }

    // this method has a default implementation which is the same as below
    // but we'll implement it for eludication
    fn update(&mut self, args: Self::Args<'_>, ui: &Ui) -> Self::Response {
        // we're passed 'Foo' (the View of the Builder, which happens to be .. us)
        // so we can just replace ourselves with the new one.
        //
        // if your builder has any parameters provided by the user.
        // you should update yourself here. atleast partially.
        //
        // by default, `update` just replaces calls *self = Self::create()
        *self = args;
    }
}

By convention, you should provide a function that creates your builder with some default parameters. then expose the constructed builder to the user.

The associated view does not have to be public, but the builder should be public

Required Associated Types§

Source

type View: View<Args<'v> = Self>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'v> Builder<'v> for Aligned

Source§

impl<'v> Builder<'v> for Background

Source§

impl<'v> Builder<'v> for BorderView

Source§

impl<'v> Builder<'v> for Button

Source§

impl<'v> Builder<'v> for Constrain

Source§

impl<'v> Builder<'v> for Expander

Source§

impl<'v> Builder<'v> for Fill

Source§

impl<'v> Builder<'v> for FlexView

Source§

impl<'v> Builder<'v> for KeyArea

Source§

impl<'v> Builder<'v> for Label

Source§

impl<'v> Builder<'v> for List

Source§

impl<'v> Builder<'v> for MarginView

Source§

impl<'v> Builder<'v> for MouseArea

Source§

impl<'v> Builder<'v> for Offset

Source§

impl<'v> Builder<'v> for Progress

Source§

impl<'v> Builder<'v> for Separator

Source§

impl<'v> Builder<'v> for Slider<'v>

Source§

type View = SliderView

Source§

impl<'v> Builder<'v> for TextInput<'v>

Source§

type View = InputView

Source§

impl<'v> Builder<'v> for Toggle

Source§

impl<'v> Builder<'v> for ToggleSwitch<'v>

Source§

type View = ToggleSwitchView

Source§

impl<'v> Builder<'v> for Unconstrained

Source§

impl<'v> Builder<'v> for Wrap

Source§

type View = WrapView