1mod align;
2mod constrained_box;
3mod container;
4mod empty;
5mod event_handler;
6mod flex;
7mod label;
8mod line_box;
9mod stack;
10mod svg;
11mod uniform_list;
12
13pub use crate::presenter::ChildView;
14pub use align::*;
15pub use constrained_box::*;
16pub use container::*;
17pub use empty::*;
18pub use event_handler::*;
19pub use flex::*;
20pub use label::*;
21pub use line_box::*;
22pub use stack::*;
23pub use svg::*;
24pub use uniform_list::*;
25
26use crate::{
27 AfterLayoutContext, AppContext, Event, EventContext, LayoutContext, MutableAppContext,
28 PaintContext, SizeConstraint,
29};
30use pathfinder_geometry::{rect::RectF, vector::Vector2F};
31use std::any::Any;
32
33pub trait Element {
34 fn layout(
35 &mut self,
36 constraint: SizeConstraint,
37 ctx: &mut LayoutContext,
38 app: &AppContext,
39 ) -> Vector2F;
40
41 fn after_layout(&mut self, _: &mut AfterLayoutContext, _: &mut MutableAppContext) {}
42
43 fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext, app: &AppContext);
44
45 fn size(&self) -> Option<Vector2F>;
46
47 fn parent_data(&self) -> Option<&dyn Any> {
48 None
49 }
50
51 fn dispatch_event(&self, event: &Event, ctx: &mut EventContext, app: &AppContext) -> bool;
52
53 fn boxed(self) -> Box<dyn Element>
54 where
55 Self: 'static + Sized,
56 {
57 Box::new(self)
58 }
59}
60
61pub trait ParentElement<'a>: Extend<Box<dyn Element>> + Sized {
62 fn add_children(&mut self, children: impl IntoIterator<Item = Box<dyn Element>>) {
63 self.extend(children);
64 }
65
66 fn add_child(&mut self, child: Box<dyn Element>) {
67 self.add_children(Some(child));
68 }
69
70 fn with_children(mut self, children: impl IntoIterator<Item = Box<dyn Element>>) -> Self {
71 self.add_children(children);
72 self
73 }
74
75 fn with_child(self, child: Box<dyn Element>) -> Self {
76 self.with_children(Some(child))
77 }
78}
79
80impl<'a, T> ParentElement<'a> for T where T: Extend<Box<dyn Element>> {}
81
82pub fn try_rect(origin: Option<Vector2F>, size: Option<Vector2F>) -> Option<RectF> {
83 origin.and_then(|origin| size.map(|size| RectF::new(origin, size)))
84}