vizia::view

Trait View

pub trait View: Sized + 'static {
    // Provided methods
    fn build<F>(self, cx: &mut Context, content: F) -> Handle<'_, Self>
       where F: FnOnce(&mut Context) { ... }
    fn element(&self) -> Option<&'static str> { ... }
    fn event(&mut self, cx: &mut EventContext<'_>, event: &mut Event) { ... }
    fn draw(&self, cx: &mut DrawContext<'_>, canvas: &Canvas) { ... }
    fn accessibility(&self, cx: &mut AccessContext<'_>, node: &mut AccessNode) { ... }
}
Expand description

A view is any object which can be displayed on the screen.

§Creating a Custom View

To create a custom view, first define a struct with any view-specific state.

pub struct CustomView {
    count: i32,
}

Next, implement the constructor for the custom view. Typically, the constructor will take &mut Context as the first argument and return a Handle to the view.

pub struct CustomView {
    count: i32,
}

impl CustomView {
    pub fn new(cx: &mut Context, count: i32) -> Handle<Self> {
        Self {
            count,
        }.build(cx, |cx|{
            // If we want the view to contain other views we can build those here.
        })
    }
}

The build method above is provided by the View trait, which we must implement for any custom view.

pub struct CustomView {
    count: i32,
}

impl CustomView {
    pub fn new(cx: &mut Context, count: i32) -> Handle<Self> {
        Self {
            count,
        }.build(cx, |cx|{
            // If we want the view to contain other views we can build those here.
        })
    }
}

impl View for CustomView {

}

The View trait contains methods, which can be optionally overridden, for assigning an element name, handling events, and performing custom drawing.

Provided Methods§

fn build<F>(self, cx: &mut Context, content: F) -> Handle<'_, Self>
where F: FnOnce(&mut Context),

Builds the view into the tree and returns a handle which can be used to apply style and layout modifiers to the view.

Typically this method is called within the constructor of a view, for example:

pub struct CustomView{}

impl CustomView {
    pub fn new(cx: &mut Context) -> Handle<Self> {
        Self{}.build(cx, |_|{})
    }
}

The content closure allows for a view to be built from other views. For example, a custom view could encapsulate a pair of labels:

pub struct CustomView{}

impl CustomView {
    pub fn new(cx: &mut Context) -> Handle<Self> {
        Self{}.build(cx, |cx|{
            Label::new(cx, "Hello");
            Label::new(cx, "World");
        })
    }
}

fn element(&self) -> Option<&'static str>

Specifies a name for the view type which can be used as an element selector in css.

§Example
pub struct CustomView{}

impl CustomView {
    pub fn new(cx: &mut Context) -> Handle<Self> {
        Self{}.build(cx, |_|{})
    }
}

impl View for CustomView {
    fn element(&self) -> Option<&'static str> {
        Some("custom_view")
    }
}

Then in css:

custom_view {
    background-color: red;
}

fn event(&mut self, cx: &mut EventContext<'_>, event: &mut Event)

Handles any events received by the view.

§Example
pub struct CustomView{}

impl CustomView {
    pub fn new(cx: &mut Context) -> Handle<Self> {
        Self{}.build(cx, |_|{})
    }
}

impl View for CustomView {
    fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
        event.map(|window_event, meta| match window_event{
            WindowEvent::MouseDown(_) => {
                if meta.target == cx.current() {
                    // Emit a `WindowClose` event when this view is clicked on.
                    cx.emit(WindowEvent::WindowClose);
                }
            }

            _=> {}
        });
    }
}

fn draw(&self, cx: &mut DrawContext<'_>, canvas: &Canvas)

Provides custom drawing for the view.

Usually the look of a view is determined by the style and layout properties of the view. However, the draw method of the View trait can be used to provide completely custom drawing for the view. The properties of the view can be accessed through the provided DrawContext and the provided Canvas can be used to draw custom paths.

§Example
pub struct CustomView{}

impl CustomView {
    pub fn new(cx: &mut Context) -> Handle<Self> {
        Self{}.build(cx, |_|{})
    }
}

impl View for CustomView {
    fn draw(&self, cx: &mut DrawContext, canvas: &mut Canvas) {
        // Get the bounding box of the current view.
        let bounds = cx.bounds();

        // Create a new `Path` from the `vg` module.
        let mut path = vg::Path::new();
        // Add a rectangle to the path with the dimensions of the view bounds.
        path.rect(bounds.x, bounds.y, bounds.w, bounds.h);
        // Fill the path onto the canvas with a red color.
        canvas.fill_path(&mut path, &vg::Paint::color(Color::red().into()));
    }
}

fn accessibility(&self, cx: &mut AccessContext<'_>, node: &mut AccessNode)

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl View for Window

source§

fn element(&self) -> Option<&'static str>

source§

fn event(&mut self, cx: &mut EventContext<'_>, event: &mut Event)

Implementors§

§

impl View for ArcTrack

§

impl View for Avatar

§

impl View for Badge

§

impl View for Button

§

impl View for ButtonGroup

§

impl View for Checkbox

§

impl View for Chip

§

impl View for Datepicker

§

impl View for Dialog

§

impl View for Divider

§

impl View for Dropdown

§

impl View for Element

§

impl View for HStack

§

impl View for Image

§

impl View for Label

§

impl View for List

§

impl View for Markdown

§

impl View for MenuBar

§

impl View for MenuButton

§

impl View for NamedSlider

§

impl View for PickList

§

impl View for Popup

§

impl View for ProgressBar

§

impl View for RadioButton

§

impl View for Rating

§

impl View for ScrollList

§

impl View for ScrollView

§

impl View for Spinbox

§

impl View for Submenu

§

impl View for Svg

§

impl View for Switch

§

impl View for TabView

§

impl View for TextSpan

§

impl View for TickKnob

§

impl View for Ticks

§

impl View for ToggleButton

§

impl View for Tooltip

§

impl View for VStack

§

impl View for VirtualList

§

impl View for XYPad

§

impl View for ZStack

§

impl<L1> View for Scrollbar<L1>
where L1: 'static + Lens<Target = f32>,

§

impl<L1, L2, T> View for ComboBox<L1, L2, T>
where L1: Lens<Target = Vec<T>>, T: 'static + Data + ToString, L2: Lens<Target = usize>,

§

impl<L> View for Knob<L>
where L: Lens<Target = f32>,

§

impl<L> View for Slider<L>
where L: Lens<Target = f32>,

§

impl<L> View for Textbox<L>
where L: Lens, <L as Lens>::Target: Data + ToStringLocalized + FromStr,