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            ctx.scene.push_layer();
53            child.paint(bounds.origin(), ctx);
54            ctx.scene.pop_layer();
55        }
56    }
57
58    fn dispatch_event(
59        &mut self,
60        event: &Event,
61        _: pathfinder_geometry::rect::RectF,
62        _: &mut Self::LayoutState,
63        _: &mut Self::PaintState,
64        ctx: &mut EventContext,
65    ) -> bool {
66        for child in self.children.iter_mut().rev() {
67            if child.dispatch_event(event, ctx) {
68                return true;
69            }
70        }
71        false
72    }
73}
74
75impl Extend<ElementBox> for Stack {
76    fn extend<T: IntoIterator<Item = ElementBox>>(&mut self, children: T) {
77        self.children.extend(children)
78    }
79}