stack.rs

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