pub trait Render: 'static + Sized {
// Required method
fn render(
&mut self,
window: &mut Window,
cx: &mut Context<'_, Self>,
) -> impl IntoElement;
}Expand description
A view: an entity that GPUI draws in a window.
Implement Render for the state of a view. GPUI can then draw an Entity of that type as an
element, and Context::notify redraws it. Use a view when a part of the interface has state
that must survive from frame to frame. For stateless pieces, use RenderOnce.
GPUI calls render each time it draws the window and reaches this view. The view keeps its
state in self, not in the elements it returns. Those elements are consumed by the frame and
dropped, and the next call builds new ones. So render should only read state and build
elements from it. GPUI skips the call while nothing the view read has changed; see
retained view rendering.
§Example
use std::{cell::Cell, rc::Rc};
use gpui::{Context, IntoElement, Render, TestApp, Window, div, prelude::*};
struct Greeting {
name: String,
renders: Rc<Cell<usize>>,
}
impl Render for Greeting {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
self.renders.set(self.renders.get() + 1);
div().child(format!("Hello, {}", self.name))
}
}
let renders = Rc::new(Cell::new(0));
let mut app = TestApp::new();
let mut window = app.open_window({
let renders = renders.clone();
move |_window, _cx| Greeting {
name: "Ada".into(),
renders,
}
});
renders.set(0);
// Notify the view so the next draw renders it again instead of reusing its output.
window.update(|_, _, cx| cx.notify());
window.draw();
assert_eq!(renders.get(), 1);Required Methods§
Sourcefn render(
&mut self,
window: &mut Window,
cx: &mut Context<'_, Self>,
) -> impl IntoElement
fn render( &mut self, window: &mut Window, cx: &mut Context<'_, Self>, ) -> impl IntoElement
Builds the view’s element tree from its current state.
window is the window being drawn, and cx gives access to this view through
Context<Self>. Return the elements the view shows. Don’t change state in this
method, because GPUI may call it many times, or skip it, for one change.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".