1use crate::{
2 AnyElement, App, Bounds, Element, GlobalElementId, IntoElement, LayoutId, Pixels, Window,
3};
4
5/// Builds a `Deferred` element, which delays the layout and paint of its child.
6pub fn deferred(child: impl IntoElement) -> Deferred {
7 Deferred {
8 child: Some(child.into_any_element()),
9 priority: 0,
10 }
11}
12
13/// An element which delays the painting of its child until after all of
14/// its ancestors, while keeping its layout as part of the current element tree.
15pub struct Deferred {
16 child: Option<AnyElement>,
17 priority: usize,
18}
19
20impl Deferred {
21 /// Sets the `priority` value of the `deferred` element, which
22 /// determines the drawing order relative to other deferred elements,
23 /// with higher values being drawn on top.
24 pub fn with_priority(mut self, priority: usize) -> Self {
25 self.priority = priority;
26 self
27 }
28}
29
30impl Element for Deferred {
31 type RequestLayoutState = ();
32 type PrepaintState = ();
33
34 fn id(&self) -> Option<crate::ElementId> {
35 None
36 }
37
38 fn request_layout(
39 &mut self,
40 _id: Option<&GlobalElementId>,
41 window: &mut Window,
42 cx: &mut App,
43 ) -> (LayoutId, ()) {
44 let layout_id = self.child.as_mut().unwrap().request_layout(window, cx);
45 (layout_id, ())
46 }
47
48 fn prepaint(
49 &mut self,
50 _id: Option<&GlobalElementId>,
51 _bounds: Bounds<Pixels>,
52 _request_layout: &mut Self::RequestLayoutState,
53 window: &mut Window,
54 _cx: &mut App,
55 ) {
56 let child = self.child.take().unwrap();
57 let element_offset = window.element_offset();
58 window.defer_draw(child, element_offset, self.priority)
59 }
60
61 fn paint(
62 &mut self,
63 _id: Option<&GlobalElementId>,
64 _bounds: Bounds<Pixels>,
65 _request_layout: &mut Self::RequestLayoutState,
66 _prepaint: &mut Self::PrepaintState,
67 _window: &mut Window,
68 _cx: &mut App,
69 ) {
70 }
71}
72
73impl IntoElement for Deferred {
74 type Element = Self;
75
76 fn into_element(self) -> Self::Element {
77 self
78 }
79}
80
81impl Deferred {
82 /// Sets a priority for the element. A higher priority conceptually means painting the element
83 /// on top of deferred draws with a lower priority (i.e. closer to the viewer).
84 pub fn priority(mut self, priority: usize) -> Self {
85 self.priority = priority;
86 self
87 }
88}