Trait vizia::view::View

source ·
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: &mut Canvas<OpenGl>) { ... }
    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§

source

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");
        })
    }
}
source

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;
}
source

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);
                }
            }

            _=> {}
        });
    }
}
source

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

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()));
    }
}
source

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

Object Safety§

This trait is not object safe.

Implementors§

source§

impl View for ArcTrack

source§

impl View for Avatar

source§

impl View for Badge

source§

impl View for Button

source§

impl View for ButtonGroup

source§

impl View for Checkbox

source§

impl View for Chip

source§

impl View for ComboPopup

source§

impl View for Datepicker

source§

impl View for Dialog

source§

impl View for Divider

source§

impl View for Dropdown

source§

impl View for Element

source§

impl View for FormControl

source§

impl View for FormGroup

source§

impl View for HStack

source§

impl View for Icon

source§

impl View for IconButton

source§

impl View for Image

source§

impl View for Label

source§

impl View for List

source§

impl View for MenuBar

source§

impl View for MenuButton

source§

impl View for MenuDivider

source§

impl View for NamedSlider

source§

impl View for Notification

source§

impl View for PickList

source§

impl View for Popup

source§

impl View for ProgressBar

source§

impl View for RadioButton

source§

impl View for Rating

source§

impl View for ScrollView

source§

impl View for Spinbox

source§

impl View for Submenu

source§

impl View for Switch

source§

impl View for TabView

source§

impl View for TickKnob

source§

impl View for Ticks

source§

impl View for ToggleButton

source§

impl View for Tooltip

source§

impl View for VStack

source§

impl View for VirtualList

source§

impl View for XYPad

source§

impl View for ZStack

source§

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

source§

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

source§

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

source§

impl<L> View for MenuPopup<L>
where L: Lens, <L as Lens>::Target: Into<bool>,

source§

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

source§

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