stack.rs

 1use crate::{
 2    geometry::vector::Vector2F, AfterLayoutContext, AppContext, Element, Event, EventContext,
 3    LayoutContext, MutableAppContext, PaintContext, SizeConstraint,
 4};
 5
 6pub struct Stack {
 7    children: Vec<Box<dyn Element>>,
 8    size: Option<Vector2F>,
 9}
10
11impl Stack {
12    pub fn new() -> Self {
13        Stack {
14            children: Vec::new(),
15            size: None,
16        }
17    }
18}
19
20impl Element for Stack {
21    fn layout(
22        &mut self,
23        constraint: SizeConstraint,
24        ctx: &mut LayoutContext,
25        app: &AppContext,
26    ) -> Vector2F {
27        let mut size = constraint.min;
28        for child in &mut self.children {
29            size = size.max(child.layout(constraint, ctx, app));
30        }
31        self.size = Some(size);
32        size
33    }
34
35    fn after_layout(&mut self, ctx: &mut AfterLayoutContext, app: &mut MutableAppContext) {
36        for child in &mut self.children {
37            child.after_layout(ctx, app);
38        }
39    }
40
41    fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext, app: &AppContext) {
42        for child in &mut self.children {
43            child.paint(origin, ctx, app);
44        }
45    }
46
47    fn dispatch_event(&self, event: &Event, ctx: &mut EventContext, app: &AppContext) -> bool {
48        for child in self.children.iter().rev() {
49            if child.dispatch_event(event, ctx, app) {
50                return true;
51            }
52        }
53        false
54    }
55
56    fn size(&self) -> Option<Vector2F> {
57        self.size
58    }
59}
60
61impl Extend<Box<dyn Element>> for Stack {
62    fn extend<T: IntoIterator<Item = Box<dyn Element>>>(&mut self, children: T) {
63        self.children.extend(children)
64    }
65}