mod.rs

 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        Box::new(self)
54    }
55}
56
57pub trait ParentElement<'a>: Extend<Box<dyn Element>> + Sized {
58    fn add_children(&mut self, children: impl IntoIterator<Item = Box<dyn Element>>) {
59        self.extend(children);
60    }
61
62    fn add_child(&mut self, child: Box<dyn Element>) {
63        self.add_childen(Some(child));
64    }
65
66    fn with_children(mut self, children: impl IntoIterator<Item = Box<dyn Element>>) -> Self {
67        self.add_children(children);
68        self
69    }
70
71    fn with_child(self, child: Box<dyn Element>) -> Self {
72        self.with_children(Some(child))
73    }
74}
75
76impl<'a, T> ParentElement<'a> for T where T: Extend<Box<dyn Element>> {}
77
78pub fn try_rect(origin: Option<Vector2F>, size: Option<Vector2F>) -> Option<RectF> {
79    origin.and_then(|origin| size.map(|size| RectF::new(origin, size)))
80}