1use crate::{BorrowWindow, Bounds, ElementId, LayoutId, Pixels, Point, ViewContext};
2use derive_more::{Deref, DerefMut};
3pub(crate) use smallvec::SmallVec;
4
5pub trait Element: 'static + Send + Sync {
6 type ViewState: 'static + Send + Sync;
7 type ElementState: 'static + Send + Sync;
8
9 fn element_id(&self) -> Option<ElementId>;
10
11 fn layout(
12 &mut self,
13 state: &mut Self::ViewState,
14 element_state: Option<Self::ElementState>,
15 cx: &mut ViewContext<Self::ViewState>,
16 ) -> (LayoutId, Self::ElementState);
17
18 fn paint(
19 &mut self,
20 bounds: Bounds<Pixels>,
21 state: &mut Self::ViewState,
22 element_state: &mut Self::ElementState,
23 cx: &mut ViewContext<Self::ViewState>,
24 );
25}
26
27pub trait StatefulElement: Element {
28 fn element_id(&self) -> ElementId {
29 Element::element_id(self).unwrap()
30 }
31}
32
33#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
34pub(crate) struct GlobalElementId(SmallVec<[ElementId; 8]>);
35
36pub trait ParentElement {
37 type State;
38
39 fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::State>; 2]>;
40
41 fn child(mut self, child: impl IntoAnyElement<Self::State>) -> Self
42 where
43 Self: Sized,
44 {
45 self.children_mut().push(child.into_any());
46 self
47 }
48
49 fn children(mut self, iter: impl IntoIterator<Item = impl IntoAnyElement<Self::State>>) -> Self
50 where
51 Self: Sized,
52 {
53 self.children_mut()
54 .extend(iter.into_iter().map(|item| item.into_any()));
55 self
56 }
57}
58
59trait ElementObject<S>: 'static + Send + Sync {
60 fn layout(&mut self, state: &mut S, cx: &mut ViewContext<S>) -> LayoutId;
61 fn paint(&mut self, state: &mut S, offset: Option<Point<Pixels>>, cx: &mut ViewContext<S>);
62}
63
64struct RenderedElement<E: Element> {
65 element: E,
66 phase: ElementRenderPhase<E::ElementState>,
67}
68
69#[derive(Default)]
70enum ElementRenderPhase<S> {
71 #[default]
72 Rendered,
73 LayoutRequested {
74 layout_id: LayoutId,
75 frame_state: Option<S>,
76 },
77 Painted {
78 bounds: Bounds<Pixels>,
79 frame_state: Option<S>,
80 },
81}
82
83/// Internal struct that wraps an element to store Layout and ElementState after the element is rendered.
84/// It's allocated as a trait object to erase the element type and wrapped in AnyElement<E::State> for
85/// improved usability.
86impl<E: Element> RenderedElement<E> {
87 fn new(element: E) -> Self {
88 RenderedElement {
89 element,
90 phase: ElementRenderPhase::Rendered,
91 }
92 }
93
94 fn paint_with_element_state(
95 &mut self,
96 bounds: Bounds<Pixels>,
97 view_state: &mut E::ViewState,
98 frame_state: &mut Option<E::ElementState>,
99 cx: &mut ViewContext<E::ViewState>,
100 ) {
101 if let Some(id) = self.element.element_id() {
102 cx.with_element_state(id, |element_state, cx| {
103 let mut element_state = element_state.unwrap();
104 self.element
105 .paint(bounds, view_state, &mut element_state, cx);
106 ((), element_state)
107 });
108 } else {
109 self.element
110 .paint(bounds, view_state, frame_state.as_mut().unwrap(), cx);
111 }
112 }
113}
114
115impl<E, S> ElementObject<E::ViewState> for RenderedElement<E>
116where
117 E: Element<ElementState = S>,
118 S: 'static + Send + Sync,
119{
120 fn layout(&mut self, state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) -> LayoutId {
121 let (layout_id, frame_state) = if let Some(id) = self.element.element_id() {
122 let layout_id = cx.with_element_state(id, |element_state, cx| {
123 self.element.layout(state, element_state, cx)
124 });
125 (layout_id, None)
126 } else {
127 let (layout_id, frame_state) = self.element.layout(state, None, cx);
128 (layout_id, Some(frame_state))
129 };
130
131 self.phase = ElementRenderPhase::LayoutRequested {
132 layout_id,
133 frame_state,
134 };
135
136 layout_id
137 }
138
139 fn paint(
140 &mut self,
141 view_state: &mut E::ViewState,
142 offset: Option<Point<Pixels>>,
143 cx: &mut ViewContext<E::ViewState>,
144 ) {
145 self.phase = match std::mem::take(&mut self.phase) {
146 ElementRenderPhase::Rendered => panic!("must call layout before paint"),
147
148 ElementRenderPhase::LayoutRequested {
149 layout_id,
150 mut frame_state,
151 } => {
152 let mut bounds = cx.layout_bounds(layout_id);
153 offset.map(|offset| bounds.origin += offset);
154 self.paint_with_element_state(bounds, view_state, &mut frame_state, cx);
155 ElementRenderPhase::Painted {
156 bounds,
157 frame_state,
158 }
159 }
160
161 ElementRenderPhase::Painted {
162 bounds,
163 mut frame_state,
164 } => {
165 self.paint_with_element_state(bounds, view_state, &mut frame_state, cx);
166 ElementRenderPhase::Painted {
167 bounds,
168 frame_state,
169 }
170 }
171 };
172 }
173}
174
175pub struct AnyElement<S>(Box<dyn ElementObject<S>>);
176
177impl<S: 'static + Send + Sync> AnyElement<S> {
178 pub fn layout(&mut self, state: &mut S, cx: &mut ViewContext<S>) -> LayoutId {
179 self.0.layout(state, cx)
180 }
181
182 pub fn paint(&mut self, state: &mut S, offset: Option<Point<Pixels>>, cx: &mut ViewContext<S>) {
183 self.0.paint(state, offset, cx)
184 }
185}
186
187pub trait IntoAnyElement<S> {
188 fn into_any(self) -> AnyElement<S>;
189}
190
191impl<E: Element> IntoAnyElement<E::ViewState> for E {
192 fn into_any(self) -> AnyElement<E::ViewState> {
193 AnyElement(Box::new(RenderedElement::new(self)))
194 }
195}
196
197impl<S> IntoAnyElement<S> for AnyElement<S> {
198 fn into_any(self) -> AnyElement<S> {
199 self
200 }
201}