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