pub trait View: 'static + Sized {
// Required methods
fn entity_id(&self) -> Option<EntityId>;
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement;
}Expand description
A value that renders into elements and may carry an entity identity.
Every Entity of a Render type is a View, and so is every RenderOnce component.
Implement View directly when a component needs both props from its parent and an entity
identity. Otherwise, use Render for an entity-backed view or RenderOnce for a stateless
component.
When View::entity_id returns Some, that entity is the view’s identity. GPUI gives the
view its own space for element ids, so state inside it can’t collide with its siblings. A
Context::notify on that entity redraws only this view’s subtree. When it returns None,
the view is stateless. GPUI scopes it under its type name, and it has no entity to notify.
Required Methods§
Sourcefn entity_id(&self) -> Option<EntityId>
fn entity_id(&self) -> Option<EntityId>
Returns the entity that identifies this view, or None for a stateless view.
A view that holds its entity as a field returns the entity’s id here. The id becomes the
view’s ElementId. Two views backed by the same entity must not be siblings under one
parent, because their element state would collide. Nesting is fine, since the parent’s
path keeps the ids apart.
Sourcefn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement
Renders this view into elements. Takes self by value, so the view is used up.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
impl View for AnyView
AnyView is the type-erased View. Its render is a function pointer rather than a
concrete type, so it takes part in the reactive graph like any other view, with the same
identity as its entity.
impl<T: Render> View for Entity<T>
An entity that renders itself (Render) is a View keyed by its own id.
impl<T: RenderOnce> View for T
A stateless component (RenderOnce) is a View with no entity identity.