Skip to main content

View

Trait View 

Source
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 renderable that participates in GPUI’s reactive graph — the unifying model behind Render and RenderOnce.

When entity_id() returns Some, that id becomes the view’s identity: it gets a unique element-id space (so internal use_state / .id(..) never collide across siblings) and cx.notify() on that entity re-renders only this view’s subtree. None behaves like a stateless component.

You rarely implement View directly. Entity<T: Render> and any T: RenderOnce get a blanket impl below; implement it by hand only when a component needs both parent-supplied props and a backing entity for identity.

Required Methods§

Source

fn entity_id(&self) -> Option<EntityId>

This view’s identity, if it has one. A view typically holds the backing entity as a field and returns its EntityId here.

The id becomes this view’s ElementId, so two views keyed on the same entity must not be rendered at the same position in the element tree (e.g. as siblings under the same parent): their internal element state (use_state, scroll offsets, etc.) would silently collide. Nesting is fine — the id is scoped by the parent path.

Source

fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement

Render this view into an element tree, consuming self.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl View for AnyView

AnyView is the type-erased View: its render is a function pointer rather than a concrete type, but it participates in the reactive graph exactly like any other view via [ViewElement].

Source§

impl<T: Render> View for Entity<T>

An entity that renders itself (Render) is a View keyed on its own id.

Source§

impl<T: RenderOnce> View for T

A stateless component (RenderOnce) is a View with no identity.