Skip to main content

View

Trait View 

Source
pub trait View {
    // Required method
    fn id(&self) -> ViewId;

    // Provided methods
    fn view_style(&self) -> Option<Style> { ... }
    fn view_class(&self) -> Option<StyleClassRef> { ... }
    fn debug_name(&self) -> Cow<'static, str> { ... }
    fn update(&mut self, cx: &mut UpdateCx<'_>, state: Box<dyn Any>) { ... }
    fn style_pass(&mut self, cx: &mut StyleCx<'_>) { ... }
    fn event_capture(&mut self, _cx: &mut EventCx<'_>) -> EventPropagation { ... }
    fn event(&mut self, _cx: &mut EventCx<'_>) -> EventPropagation { ... }
    fn paint(&mut self, cx: &mut PaintCx<'_>) { ... }
    fn post_paint(&mut self, cx: &mut PaintCx<'_>) { ... }
}
Expand description

The View trait contains the methods for implementing updates, styling, layout, events, and painting.

The id method must be implemented. The other methods may be implemented as necessary to implement the functionality of the View.

§State Management in a Custom View

For all reactive state that your type contains, either in the form of signals or derived signals, you need to process the changes within an effect. The most common pattern is to get the data in an effect and pass it in to id.update_state() and then handle that data in the update method of the View trait.

For example a minimal slider might look like the following. First, we define the struct that contains the ViewId. Then, we use a function to construct the slider. As part of this function we create an effect that will be re-run every time the signals in the percent closure change. In the effect we send the change to the associated ViewId. This change can then be handled in the View::update method.


struct Slider {
    id: ViewId,
    percent: f32,
}
pub fn slider(percent: impl Fn() -> f32 + 'static) -> Slider {
    let id = ViewId::new();

    // If the following effect is not created, and `percent` is accessed directly,
    // `percent` will only be accessed a single time and will not be reactive.
    // Therefore the following `create_effect` is necessary for reactivity.
    create_effect(move |_| {
        let percent = percent();
        id.update_state(percent);
    });
    Slider { id, percent: 0.0 }
}
impl View for Slider {
    fn id(&self) -> ViewId {
        self.id
    }

    fn update(&mut self, cx: &mut floem::context::UpdateCx, state: Box<dyn std::any::Any>) {
        if let Ok(percent) = state.downcast::<f32>() {
            self.percent = *percent;
            self.id.request_layout();
        }
    }
}

Required Methods§

Source

fn id(&self) -> ViewId

Provided Methods§

Source

fn view_style(&self) -> Option<Style>

Source

fn view_class(&self) -> Option<StyleClassRef>

Source

fn debug_name(&self) -> Cow<'static, str>

Source

fn update(&mut self, cx: &mut UpdateCx<'_>, state: Box<dyn Any>)

Use this method to react to changes in view-related state. You will usually send state to this hook manually using the View’s Id handle

self.id.update_state(SomeState)

You are in charge of downcasting the state to the expected type.

If the update needs other passes to run you’re expected to call _cx.window_state_mut().request_changes.

Source

fn style_pass(&mut self, cx: &mut StyleCx<'_>)

Use this method to style the view’s children.

If the style changes needs other passes to run you’re expected to call cx.window_state.style_dirty.insert(view_id).

Source

fn event_capture(&mut self, _cx: &mut EventCx<'_>) -> EventPropagation

Handles an event during the capture phase of event propagation.

The capture phase runs from the root of the dispatch path down toward the target element, before the target itself receives the event. This method is invoked for each element encountered during that downward traversal.

In contrast, Self::event runs during the target and bubble phases, after the event reaches the target and begins propagating back up the ancestor chain.

Capture handlers are useful when an element must observe or influence an event before the target processes it. This can be necessary when:

  • the ancestor needs an opportunity to intercept or block the event before it reaches the target
  • the ancestor must coordinate state across a subtree before the target reacts
  • the event must be transformed or redirected before normal handling occurs

Returning EventPropagation::Stop prevents the event from propagating to additional elements further along the dispatch path. The current element’s handlers will still complete.

This differs from EventCx::stop_immediate_propagation, which halts dispatch immediately, preventing any remaining handlers on the current element from running.

Calling EventCx::prevent_default marks the event as having its default behavior suppressed. This flag is shared across all phases of dispatch, allowing capture handlers to prevent the default action before the event reaches the target.

§See also
  • Self::event — handles the same event during the target and bubble phases.
§Default behavior

The default implementation ignores the event and allows propagation to continue.

Source

fn event(&mut self, _cx: &mut EventCx<'_>) -> EventPropagation

Handles an event during the target and bubble phases of event propagation.

After the capture phase completes, the event is delivered to the target element and then propagates upward through its ancestors. This method is invoked for the target and for each ancestor encountered during that upward traversal.

In contrast, Self::event_capture runs earlier during the capture phase while the event travels downward toward the target.

Returning EventPropagation::Stop prevents the event from propagating to additional elements further along the dispatch path. The current element’s handlers will still complete.

This differs from EventCx::stop_immediate_propagation, which stops dispatch immediately and prevents any remaining handlers on the current element from executing.

Calling EventCx::prevent_default suppresses the event’s default behavior. This flag is shared across all phases of dispatch and can be set in either Self::event_capture or Self::event.

§Propagation behavior

Event dispatch proceeds in this order:

  1. Self::event_capture on ancestors from root → target
  2. Self::event on the target
  3. Self::event on ancestors from target → root

Propagation may be stopped at any point by returning EventPropagation::Stop or by calling EventCx::stop_immediate_propagation.

§Default behavior

The default implementation ignores the event and allows propagation to continue.

Source

fn paint(&mut self, cx: &mut PaintCx<'_>)

View-specific implementation. Called during paint traversal for this view. Children are painted automatically by Floem. Views should only paint their own content (backgrounds, borders, custom drawing).

Source

fn post_paint(&mut self, cx: &mut PaintCx<'_>)

Trait Implementations§

Source§

impl View for Box<dyn View>

Source§

fn id(&self) -> ViewId

Source§

fn view_style(&self) -> Option<Style>

Source§

fn view_class(&self) -> Option<StyleClassRef>

Source§

fn debug_name(&self) -> Cow<'static, str>

Source§

fn update(&mut self, cx: &mut UpdateCx<'_>, state: Box<dyn Any>)

Use this method to react to changes in view-related state. You will usually send state to this hook manually using the View’s Id handle Read more
Source§

fn style_pass(&mut self, cx: &mut StyleCx<'_>)

Use this method to style the view’s children. Read more
Source§

fn event_capture(&mut self, cx: &mut EventCx<'_>) -> EventPropagation

Handles an event during the capture phase of event propagation. Read more
Source§

fn event(&mut self, cx: &mut EventCx<'_>) -> EventPropagation

Handles an event during the target and bubble phases of event propagation. Read more
Source§

fn paint(&mut self, cx: &mut PaintCx<'_>)

View-specific implementation. Called during paint traversal for this view. Children are painted automatically by Floem. Views should only paint their own content (backgrounds, borders, custom drawing).
Source§

fn post_paint(&mut self, cx: &mut PaintCx<'_>)

Implementations on Foreign Types§

Source§

impl View for Box<dyn View>

Source§

fn id(&self) -> ViewId

Source§

fn view_style(&self) -> Option<Style>

Source§

fn view_class(&self) -> Option<StyleClassRef>

Source§

fn debug_name(&self) -> Cow<'static, str>

Source§

fn update(&mut self, cx: &mut UpdateCx<'_>, state: Box<dyn Any>)

Source§

fn style_pass(&mut self, cx: &mut StyleCx<'_>)

Source§

fn event_capture(&mut self, cx: &mut EventCx<'_>) -> EventPropagation

Source§

fn event(&mut self, cx: &mut EventCx<'_>) -> EventPropagation

Source§

fn paint(&mut self, cx: &mut PaintCx<'_>)

Source§

fn post_paint(&mut self, cx: &mut PaintCx<'_>)

Implementors§