1use crate::{
3 AnyElement, App, Bounds, Element, GlobalElementId, InspectorElementId, IntoElement, LayoutId,
4 Pixels, Window,
5};
6
7pub fn deferred(child: impl IntoElement) -> Deferred {
9 Deferred {
10 child: Some(child.into_any_element()),
11 priority: 0,
12 }
13}
14
15pub struct Deferred {
18 child: Option<AnyElement>,
19 priority: usize,
20}
21
22impl Deferred {
23 pub fn with_priority(mut self, priority: usize) -> Self {
27 self.priority = priority;
28 self
29 }
30}
31
32impl Element for Deferred {
33 type RequestLayoutState = ();
34 type PrepaintState = ();
35
36 fn id(&self) -> Option<crate::ElementId> {
37 None
38 }
39
40 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
41 None
42 }
43
44 fn request_layout(
45 &mut self,
46 _id: Option<&GlobalElementId>,
47 _inspector_id: Option<&InspectorElementId>,
48 window: &mut Window,
49 cx: &mut App,
50 ) -> (LayoutId, ()) {
51 let layout_id = self.child.as_mut().unwrap().request_layout(window, cx);
52 (layout_id, ())
53 }
54
55 fn prepaint(
56 &mut self,
57 _id: Option<&GlobalElementId>,
58 _inspector_id: Option<&InspectorElementId>,
59 _bounds: Bounds<Pixels>,
60 _request_layout: &mut Self::RequestLayoutState,
61 window: &mut Window,
62 _cx: &mut App,
63 ) {
64 let child = self.child.take().unwrap();
65 let element_offset = window.element_offset();
66 window.defer_draw(child, element_offset, self.priority, None)
67 }
68
69 fn paint(
70 &mut self,
71 _id: Option<&GlobalElementId>,
72 _inspector_id: Option<&InspectorElementId>,
73 _bounds: Bounds<Pixels>,
74 _request_layout: &mut Self::RequestLayoutState,
75 _prepaint: &mut Self::PrepaintState,
76 _window: &mut Window,
77 _cx: &mut App,
78 ) {
79 }
80}
81
82impl Deferred {
83 pub fn priority(mut self, priority: usize) -> Self {
86 self.priority = priority;
87 self
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use std::{cell::Cell, rc::Rc};
94
95 use crate::{
96 Context, Entity, StyleRefinement, TestAppContext, Window, anchored, deferred, div, point,
97 prelude::*, px, size,
98 };
99
100 struct SubscriptionLeaf {
101 value: usize,
102 output: Rc<Cell<usize>>,
103 }
104
105 struct SubscriptionPanel {
106 leaf: Entity<SubscriptionLeaf>,
107 deferred_child: bool,
108 renders: Rc<Cell<usize>>,
109 }
110
111 impl Render for SubscriptionPanel {
112 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
113 self.renders.set(self.renders.get() + 1);
114 let leaf = self.leaf.clone();
115 let child = crate::canvas(
116 |_, _, _| {},
117 move |_, _, _, cx| {
118 let leaf = leaf.read(cx);
119 leaf.output.set(leaf.value);
120 },
121 )
122 .size(px(50.));
123 if self.deferred_child {
124 deferred(child).into_any_element()
125 } else {
126 child.into_any_element()
127 }
128 }
129 }
130
131 struct SubscriptionRoot {
132 panel: Entity<SubscriptionPanel>,
133 deferred_panel: bool,
134 renders: Rc<Cell<usize>>,
135 }
136
137 impl Render for SubscriptionRoot {
138 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
139 self.renders.set(self.renders.get() + 1);
140 let panel = self
141 .panel
142 .clone()
143 .cached(StyleRefinement::default().size(px(100.)));
144 if self.deferred_panel {
145 deferred(panel).into_any_element()
146 } else {
147 panel.into_any_element()
148 }
149 }
150 }
151
152 #[gpui::test]
153 fn test_cached_reuse_keeps_entity_subscriptions(cx: &mut TestAppContext) {
154 for deferred_child in [true, false] {
155 for deferred_panel in [false, true] {
156 let output = Rc::new(Cell::new(0));
157 let renders = Rc::new(Cell::new(0));
158 let root_renders = Rc::new(Cell::new(0));
159 let leaf = cx.new(|_| SubscriptionLeaf {
160 value: 1,
161 output: output.clone(),
162 });
163 let panel = cx.new(|_| SubscriptionPanel {
164 leaf: leaf.clone(),
165 deferred_child,
166 renders: renders.clone(),
167 });
168 let window = cx.open_window(size(px(800.), px(600.)), |_, _| SubscriptionRoot {
169 panel,
170 deferred_panel,
171 renders: root_renders.clone(),
172 });
173 cx.run_until_parked();
174 assert_eq!(output.get(), 1);
175 let initial_renders = renders.get();
176 for _ in 0..2 {
177 window
178 .update(cx, |_, _, cx| cx.notify())
179 .expect("notify root");
180 cx.run_until_parked();
181 assert_eq!(
182 renders.get(),
183 initial_renders,
184 "panel should reuse its cache"
185 );
186 }
187
188 let renders_before_notify = root_renders.get();
189 leaf.update(cx, |leaf, cx| {
190 leaf.value = 2;
191 cx.notify();
192 });
193 cx.run_until_parked();
194 let context =
195 format!("deferred child {deferred_child}, deferred panel {deferred_panel}");
196 let fast_redrew_panel = renders.get() > initial_renders;
198 assert!(
199 root_renders.get() > renders_before_notify || fast_redrew_panel,
200 "leaf notification should redraw: {context}"
201 );
202 window
205 .update(cx, |_, window, _| window.refresh())
206 .expect("refresh canvas");
207 cx.run_until_parked();
208 assert_eq!(output.get(), 2, "{context}");
209 }
210 }
211 }
212
213 struct PanelView;
217
218 impl Render for PanelView {
219 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
220 div().key_context("Panel").size_full().child(
221 deferred(
222 anchored().position(point(px(10.), px(10.))).child(
223 div().key_context("Popover").w(px(200.)).h(px(200.)).child(
224 deferred(
225 anchored().position(point(px(30.), px(30.))).child(
226 div()
227 .key_context("NestedMenu")
228 .debug_selector(|| "NESTED_MENU".into())
229 .w(px(50.))
230 .h(px(50.)),
231 ),
232 )
233 .with_priority(2),
234 ),
235 ),
236 )
237 .with_priority(1),
238 )
239 }
240 }
241
242 struct RootView {
243 panel: Entity<PanelView>,
244 }
245
246 impl Render for RootView {
247 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
248 div().key_context("Root").size_full().child(
249 self.panel
250 .clone()
251 .cached(StyleRefinement::default().size_full()),
252 )
253 }
254 }
255
256 #[gpui::test]
264 fn test_nested_deferred_draws_with_reused_views(cx: &mut TestAppContext) {
265 let window = cx.open_window(size(px(800.), px(600.)), |_, cx| {
266 let panel = cx.new(|_| PanelView);
267 RootView { panel }
268 });
269 cx.run_until_parked();
270
271 let menu_bounds = window
272 .update(cx, |_, window, _| {
273 window
274 .rendered_frame
275 .debug_bounds
276 .get("NESTED_MENU")
277 .copied()
278 })
279 .unwrap()
280 .expect("NESTED_MENU debug bounds not found");
281 assert_eq!(menu_bounds.size, size(px(50.), px(50.)));
282
283 window.update(cx, |_, _, cx| cx.notify()).unwrap();
287 cx.run_until_parked();
288
289 window.update(cx, |_, _, cx| cx.notify()).unwrap();
292 cx.run_until_parked();
293
294 window
296 .update(cx, |root, _, cx| {
297 root.panel.update(cx, |_, cx| cx.notify());
298 })
299 .unwrap();
300 cx.run_until_parked();
301
302 window
303 .update(cx, |_, window, _| {
304 assert_eq!(window.rendered_frame.deferred_draws.len(), 2);
305 assert!(
306 window
307 .rendered_frame
308 .debug_bounds
309 .contains_key("NESTED_MENU")
310 );
311 })
312 .unwrap();
313 }
314}