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