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