Expand description
§View and Widget Traits
Views are self-contained components that can be composed together to create complex UIs. Views are the main building blocks of Floem.
Views are structs that implement the View and [Widget] traits. Many of these structs will also contain a child field that also implements View. In this way, views can be composed together easily to create complex UIs. This is the most common way to build UIs in Floem. For more information on how to compose views check out the views module.
Creating a struct and manually implementing the View and [Widget] traits is typically only needed for building new widgets and for special cases. The rest of this module documentation is for help when manually implementing View and [Widget] on your own types.
§The View and Widget Traits
The View trait is the trait that Floem uses to build and display elements, and it builds on the [Widget] trait. The [Widget] trait contains the methods for implementing updates, styling, layout, events, and painting.
Eventually, the goal is for Floem to integrate the [Widget] trait with other rust UI libraries so that the widget layer can be shared among all compatible UI libraries.
§State management
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 with the [ViewData] that contains the [Id].
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 [Id]. This change can then be handled in the [Widget::update] method.
use floem::ViewId;
use floem::reactive::*;
struct Slider {
id: ViewId,
}
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,
}
}Re-exports§
pub use tuple::*;
Modules§
Structs§
- Lazy
View - A wrapper type for lazy view construction.
- Stack
- A stack of view attributes. Each entry is associated with a view decorator call.
- Stack
Offset - Stacking
Info - Information about a view’s stacking context. Used to determine paint order and event dispatch order.
- ViewId
- A small unique identifier for an instance of a View.
- View
State - View state stores internal state associated with a view which is owned and managed by Floem.
- Visibility
- Controls view visibility state.
Enums§
- Visibility
Phase - The current phase of visibility for enter/exit animations.
Traits§
- HasView
Id - A trait for types that have an associated
ViewId. - Into
View - Converts a value into a
View. - Into
View Iter - Trait for types that can be converted into an iterator of views.
- Parent
View - A trait for views that can accept children.
- View
- The View trait contains the methods for implementing updates, styling, layout, events, and painting.
Functions§
- default_
compute_ layout - Computes the layout of the view’s children, if any.
- process_
pending_ scope_ reparents - Process any views that had scope re-parenting deferred.
- recursively_
layout_ view - Default implementation of
View::layout()which can be used by view implementations that need the default behavior and also need to implement that method to do additional work.