1mod align;
2mod canvas;
3mod constrained_box;
4mod container;
5mod empty;
6mod event_handler;
7mod flex;
8mod label;
9mod line_box;
10mod list;
11mod mouse_event_handler;
12mod overlay;
13mod stack;
14mod svg;
15mod text;
16mod uniform_list;
17
18pub use crate::presenter::ChildView;
19pub use align::*;
20pub use canvas::*;
21pub use constrained_box::*;
22pub use container::*;
23pub use empty::*;
24pub use event_handler::*;
25pub use flex::*;
26pub use label::*;
27pub use line_box::*;
28pub use list::*;
29pub use mouse_event_handler::*;
30pub use overlay::*;
31pub use stack::*;
32pub use svg::*;
33pub use text::*;
34pub use uniform_list::*;
35
36use crate::{
37 geometry::{rect::RectF, vector::Vector2F},
38 json, DebugContext, Event, EventContext, LayoutContext, PaintContext, SizeConstraint,
39};
40use core::panic;
41use json::ToJson;
42use std::{
43 any::Any,
44 borrow::Cow,
45 cell::RefCell,
46 mem,
47 ops::{Deref, DerefMut},
48 rc::Rc,
49};
50
51trait AnyElement {
52 fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F;
53 fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext);
54 fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool;
55 fn debug(&self, cx: &DebugContext) -> serde_json::Value;
56
57 fn size(&self) -> Vector2F;
58 fn metadata(&self) -> Option<&dyn Any>;
59}
60
61pub trait Element {
62 type LayoutState;
63 type PaintState;
64
65 fn layout(
66 &mut self,
67 constraint: SizeConstraint,
68 cx: &mut LayoutContext,
69 ) -> (Vector2F, Self::LayoutState);
70
71 fn paint(
72 &mut self,
73 bounds: RectF,
74 layout: &mut Self::LayoutState,
75 cx: &mut PaintContext,
76 ) -> Self::PaintState;
77
78 fn dispatch_event(
79 &mut self,
80 event: &Event,
81 bounds: RectF,
82 layout: &mut Self::LayoutState,
83 paint: &mut Self::PaintState,
84 cx: &mut EventContext,
85 ) -> bool;
86
87 fn metadata(&self) -> Option<&dyn Any> {
88 None
89 }
90
91 fn debug(
92 &self,
93 bounds: RectF,
94 layout: &Self::LayoutState,
95 paint: &Self::PaintState,
96 cx: &DebugContext,
97 ) -> serde_json::Value;
98
99 fn boxed(self) -> ElementBox
100 where
101 Self: 'static + Sized,
102 {
103 ElementBox(ElementRc {
104 name: None,
105 element: Rc::new(RefCell::new(Lifecycle::Init { element: self })),
106 })
107 }
108
109 fn named(self, name: impl Into<Cow<'static, str>>) -> ElementBox
110 where
111 Self: 'static + Sized,
112 {
113 ElementBox(ElementRc {
114 name: Some(name.into()),
115 element: Rc::new(RefCell::new(Lifecycle::Init { element: self })),
116 })
117 }
118}
119
120pub enum Lifecycle<T: Element> {
121 Empty,
122 Init {
123 element: T,
124 },
125 PostLayout {
126 element: T,
127 constraint: SizeConstraint,
128 size: Vector2F,
129 layout: T::LayoutState,
130 },
131 PostPaint {
132 element: T,
133 constraint: SizeConstraint,
134 bounds: RectF,
135 layout: T::LayoutState,
136 paint: T::PaintState,
137 },
138}
139pub struct ElementBox(ElementRc);
140
141#[derive(Clone)]
142pub struct ElementRc {
143 name: Option<Cow<'static, str>>,
144 element: Rc<RefCell<dyn AnyElement>>,
145}
146
147impl<T: Element> AnyElement for Lifecycle<T> {
148 fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F {
149 let result;
150 *self = match mem::take(self) {
151 Lifecycle::Empty => unreachable!(),
152 Lifecycle::Init { mut element }
153 | Lifecycle::PostLayout { mut element, .. }
154 | Lifecycle::PostPaint { mut element, .. } => {
155 let (size, layout) = element.layout(constraint, cx);
156 debug_assert!(size.x().is_finite());
157 debug_assert!(size.y().is_finite());
158
159 result = size;
160 Lifecycle::PostLayout {
161 element,
162 constraint,
163 size,
164 layout,
165 }
166 }
167 };
168 result
169 }
170
171 fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext) {
172 *self = match mem::take(self) {
173 Lifecycle::PostLayout {
174 mut element,
175 constraint,
176 size,
177 mut layout,
178 } => {
179 let bounds = RectF::new(origin, size);
180 let paint = element.paint(bounds, &mut layout, cx);
181 Lifecycle::PostPaint {
182 element,
183 constraint,
184 bounds,
185 layout,
186 paint,
187 }
188 }
189 Lifecycle::PostPaint {
190 mut element,
191 constraint,
192 bounds,
193 mut layout,
194 ..
195 } => {
196 let bounds = RectF::new(origin, bounds.size());
197 let paint = element.paint(bounds, &mut layout, cx);
198 Lifecycle::PostPaint {
199 element,
200 constraint,
201 bounds,
202 layout,
203 paint,
204 }
205 }
206 _ => panic!("invalid element lifecycle state"),
207 }
208 }
209
210 fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool {
211 if let Lifecycle::PostPaint {
212 element,
213 bounds,
214 layout,
215 paint,
216 ..
217 } = self
218 {
219 element.dispatch_event(event, *bounds, layout, paint, cx)
220 } else {
221 panic!("invalid element lifecycle state");
222 }
223 }
224
225 fn size(&self) -> Vector2F {
226 match self {
227 Lifecycle::Empty | Lifecycle::Init { .. } => panic!("invalid element lifecycle state"),
228 Lifecycle::PostLayout { size, .. } => *size,
229 Lifecycle::PostPaint { bounds, .. } => bounds.size(),
230 }
231 }
232
233 fn metadata(&self) -> Option<&dyn Any> {
234 match self {
235 Lifecycle::Empty => unreachable!(),
236 Lifecycle::Init { element }
237 | Lifecycle::PostLayout { element, .. }
238 | Lifecycle::PostPaint { element, .. } => element.metadata(),
239 }
240 }
241
242 fn debug(&self, cx: &DebugContext) -> serde_json::Value {
243 match self {
244 Lifecycle::PostPaint {
245 element,
246 constraint,
247 bounds,
248 layout,
249 paint,
250 } => {
251 let mut value = element.debug(*bounds, layout, paint, cx);
252 if let json::Value::Object(map) = &mut value {
253 let mut new_map: crate::json::Map<String, serde_json::Value> =
254 Default::default();
255 if let Some(typ) = map.remove("type") {
256 new_map.insert("type".into(), typ);
257 }
258 new_map.insert("constraint".into(), constraint.to_json());
259 new_map.append(map);
260 json::Value::Object(new_map)
261 } else {
262 value
263 }
264 }
265 _ => panic!("invalid element lifecycle state"),
266 }
267 }
268}
269
270impl<T: Element> Default for Lifecycle<T> {
271 fn default() -> Self {
272 Self::Empty
273 }
274}
275
276impl ElementBox {
277 pub fn metadata<T: 'static>(&self) -> Option<&T> {
278 let element = unsafe { &*self.0.element.as_ptr() };
279 element.metadata().and_then(|m| m.downcast_ref())
280 }
281}
282
283impl Into<ElementRc> for ElementBox {
284 fn into(self) -> ElementRc {
285 self.0
286 }
287}
288
289impl Deref for ElementBox {
290 type Target = ElementRc;
291
292 fn deref(&self) -> &Self::Target {
293 &self.0
294 }
295}
296
297impl DerefMut for ElementBox {
298 fn deref_mut(&mut self) -> &mut Self::Target {
299 &mut self.0
300 }
301}
302
303impl ElementRc {
304 pub fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F {
305 self.element.borrow_mut().layout(constraint, cx)
306 }
307
308 pub fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext) {
309 self.element.borrow_mut().paint(origin, cx);
310 }
311
312 pub fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool {
313 self.element.borrow_mut().dispatch_event(event, cx)
314 }
315
316 pub fn size(&self) -> Vector2F {
317 self.element.borrow().size()
318 }
319
320 pub fn debug(&self, cx: &DebugContext) -> json::Value {
321 let mut value = self.element.borrow().debug(cx);
322
323 if let Some(name) = &self.name {
324 if let json::Value::Object(map) = &mut value {
325 let mut new_map: crate::json::Map<String, serde_json::Value> = Default::default();
326 new_map.insert("name".into(), json::Value::String(name.to_string()));
327 new_map.append(map);
328 return json::Value::Object(new_map);
329 }
330 }
331
332 value
333 }
334
335 pub fn with_metadata<T, F, R>(&self, f: F) -> R
336 where
337 T: 'static,
338 F: FnOnce(Option<&T>) -> R,
339 {
340 let element = self.element.borrow();
341 f(element.metadata().and_then(|m| m.downcast_ref()))
342 }
343}
344
345pub trait ParentElement<'a>: Extend<ElementBox> + Sized {
346 fn add_children(&mut self, children: impl IntoIterator<Item = ElementBox>) {
347 self.extend(children);
348 }
349
350 fn add_child(&mut self, child: ElementBox) {
351 self.add_children(Some(child));
352 }
353
354 fn with_children(mut self, children: impl IntoIterator<Item = ElementBox>) -> Self {
355 self.add_children(children);
356 self
357 }
358
359 fn with_child(self, child: ElementBox) -> Self {
360 self.with_children(Some(child))
361 }
362}
363
364impl<'a, T> ParentElement<'a> for T where T: Extend<ElementBox> {}