View

Trait View 

Source
pub trait View:
    Sized
    + 'static
    + Debug
    + ViewMarker {
    type Args<'v>;
    type Response: 'static + Default;

    // Required method
    fn create(args: Self::Args<'_>) -> Self;

    // Provided methods
    fn update(&mut self, args: Self::Args<'_>, ui: &Ui<'_>) -> Self::Response { ... }
    fn flex(&self) -> Flex { ... }
    fn interests(&self) -> Interest { ... }
    fn primary_axis(&self) -> Axis { ... }
    fn event(&mut self, event: ViewEvent, ctx: EventCtx<'_>) -> Handled { ... }
    fn size(&self, intrinsic: IntrinsicSize<'_>, axis: Axis, extent: f32) -> f32 { ... }
    fn layout(&mut self, layout: Layout<'_>, space: Space) -> Size { ... }
    fn draw(&mut self, render: Render<'_, '_>) { ... }
    fn default_event(&mut self, event: ViewEvent, ctx: EventCtx<'_>) -> Handled { ... }
    fn default_layout(&mut self, layout: Layout<'_>, space: Space) -> Size { ... }
    fn default_draw(&mut self, render: Render<'_, '_>) { ... }
}
Expand description

View is the main trait for describing a view.

A view is basically a thing that exists in the Ui

Views must be:

  • Debug
  • Sized
  • 'static

If the sync feature is enabled, they must also be Send and Sync

Required Associated Types§

Source

type Args<'v>

Arguments for building a view.

This is typically a Builder associated with this View

Source

type Response: 'static + Default

Response from this view that is given to the user

This type must be:

  • 'static
  • Default

This type gets wrapped in a Response with the views’ current id.

If your view does not return any data to the user, you can simply just use ()

Required Methods§

Source

fn create(args: Self::Args<'_>) -> Self

Create your view with the args provided from the user

This method is required for all views.

Provided Methods§

Source

fn update(&mut self, args: Self::Args<'_>, ui: &Ui<'_>) -> Self::Response

Update your view with the args provided from the user

§Ui

This gives you access to the Ui which lets you construct other views in-place.

The views constructed this way will become your children.

§Default behavior

By default, this just calls View::create and returns a Default Response

§NOTE on updating your view state.

If you do not override this, no state will be persisted across frames.

Source

fn flex(&self) -> Flex

If your view can be flexible, this returns how flexible it is.

By default, views are flexible and don’t ask for any specific flex space

Source

fn interests(&self) -> Interest

If you want to receive events, you need to provide some Interests

This is a set of bit flags which lets the runtime filter and dispatch events efficiently.

By default, views aren’t interested in events

Source

fn primary_axis(&self) -> Axis

The primary axis of your view.

This is used as a hint for other views to adjust to your axis

By default, views are horizontal.

Source

fn event(&mut self, event: ViewEvent, ctx: EventCtx<'_>) -> Handled

When you provide specific Interest and an event is processed by the runtime, this method is called.

EventCtx allows you to get your children and interact with the input state.

You must return a Handled from this. If you intend to consume the event (e.g.) keep it from propagating, you should return Handled::Sink otherwise you should return Handled::Bubble to continue the event propagation

A default event handler is provided for you that dispatches the event to your children, if you have any.

If you want to handle an event and then continue the default behavior, you can use self.default_event(event, ctx)

Source

fn size(&self, intrinsic: IntrinsicSize<'_>, axis: Axis, extent: f32) -> f32

This is a hint can be used for determining how much space a view will take up on a specific axis with a specific extent (width/height).

This should return how much space on that axis this view would like to use

Note, this is only a hint and isn’t part of the layout algorithm. You can get this data for your children with Layout::intrinsic_size

By default, this’ll gather the maximumze size of your children for the provided parameters

Source

fn layout(&mut self, layout: Layout<'_>, space: Space) -> Size

Lay out your view.

This gives you a Space provided by your parent that you can use. You can use this to determine the Size your view

You can get your children from the Layout type.

A default layout handler is provided for you that constrains your children to the available space.

If you want to do some layout calculations and then continue the default behavior, you can use self.default_layout(layout, space)

§Common usages.

If you want to take up all the remainder space of your parent:

  • return space.max

If you’ve calculated the total size of your children, you can further constrain this size to fit into the space:

  • space.fit(size)
  • space.constrain_min(size)

with Space::fit it’ll clamp to the size and handle infinite sizes for you

with Space::constrain_min it’ll produce the smallest size of the compute size and the provided space

Source

fn draw(&mut self, render: Render<'_, '_>)

Draw your view

This gives you a Render context that is cropped to the size calcualted from layout

A default draw handler is provided for you that draws your children for you.

If you want to do some drawing and then continue the default behavior, you can use self.default_draw(render)

Source

fn default_event(&mut self, event: ViewEvent, ctx: EventCtx<'_>) -> Handled

The default event handling behavior to delegate to your children.

§NOTE

You should not override this, its provided out of convenience and has a simple, correct implementation.

Using the implementing here can be useful to determining how you should handle events w/ your children.

Source

fn default_layout(&mut self, layout: Layout<'_>, space: Space) -> Size

The default layout handling behavior to delegate to your children.

§NOTE

You should not override this, its provided out of convenience and has a simple, correct implementation.

Using the implementing here can be useful to determining how you should layout your children

Source

fn default_draw(&mut self, render: Render<'_, '_>)

The default draw handling behavior to delegate to your children.

§NOTE

You should not override this, its provided out of convenience and has a simple, correct implementation.

Using the implementing here can be useful to determining how you should draw your children

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 View for Aligned

Source§

impl View for Background

Source§

impl View for BorderView

Source§

impl View for Button

Source§

impl View for Constrain

Source§

impl View for Expander

Source§

impl View for Fill

Source§

impl View for FlexView

Source§

impl View for KeyArea

Source§

impl View for Label

Source§

impl View for List

Source§

impl View for MarginView

Source§

impl View for MouseArea

Source§

impl View for Offset

Source§

impl View for Progress

Source§

impl View for Separator

Source§

impl View for Toggle

Source§

impl View for Unconstrained