Concepts
Elements and rendering
Views, the Render trait, element trees and what to keep out of render.
A view is an Entity<T> where T: Render. render returns an element tree
laid out with flexbox, styled with Tailwind-like methods.
impl Render for Card {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.gap_2()
.p_4()
.border_1()
.child(self.title.clone())
.when(self.expanded, |this| this.child(self.body.clone()))
.when_some(self.footer.as_ref(), |this, footer| this.child(footer.clone()))
}
}SharedStringis the text type. It wraps&'static strorArc<str>and implementsIntoElement, so it can be passed to.childdirectly..when(condition, ...)and.when_some(option, ...)keep conditional children readable withoutifblocks that return different types.- Components that are built only to be turned into elements implement
RenderOnceand deriveIntoElement, takingselfby value and&mut Appinstead of aContext<Self>. - Long lists use
uniform_listorlistso only visible rows are rendered. See theuniform_listandlist_exampleexamples incrates/gpui/examples.
Rendering is immediate mode over retained state: each frame rebuilds the
element tree from entity state, and GPUI diffs layout and paints on the GPU.
Keep render cheap and free of side effects. In particular, never call
entity.update on another entity from inside render; the fork’s dylint
lints under tooling/lints flag this.