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:
DebugSized'static
If the sync feature is enabled, they must also be Send and Sync
Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn update(&mut self, args: Self::Args<'_>, ui: &Ui<'_>) -> Self::Response
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.
Sourcefn flex(&self) -> Flex
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
Sourcefn interests(&self) -> Interest
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
Sourcefn primary_axis(&self) -> Axis
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.
Sourcefn event(&mut self, event: ViewEvent, ctx: EventCtx<'_>) -> Handled
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)
Sourcefn size(&self, intrinsic: IntrinsicSize<'_>, axis: Axis, extent: f32) -> f32
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
Sourcefn layout(&mut self, layout: Layout<'_>, space: Space) -> Size
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
Sourcefn draw(&mut self, render: Render<'_, '_>)
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)
Sourcefn default_event(&mut self, event: ViewEvent, ctx: EventCtx<'_>) -> Handled
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.
Sourcefn default_layout(&mut self, layout: Layout<'_>, space: Space) -> Size
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
Sourcefn default_draw(&mut self, render: Render<'_, '_>)
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.