mgui_core/layout/
indexed_stack.rs1use crate::id::ElementId;
13use crate::paint::PaintList;
14use crate::render_object::{make_ro_shared, BoxConstraints, LayoutContext, RRectRenderObject, RenderObject, SharedRO};
15use crate::widget::{Widget, WidgetNode};
16use crate::{Color, Point, Size};
17
18pub struct IndexedStackRenderObject {
20 pub index: usize,
21 children: Vec<SharedRO>,
22 offsets: Vec<Point>,
23 sizes: Vec<Size>,
24 cached_size: Size,
25}
26
27impl IndexedStackRenderObject {
28 pub fn new(index: usize) -> Self {
29 Self { index, children: Vec::new(), offsets: Vec::new(), sizes: Vec::new(), cached_size: Size::ZERO }
30 }
31}
32
33impl RenderObject for IndexedStackRenderObject {
34 fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
35 let child_constraints = constraints.loosen();
36 let mut max_w = 0.0_f32;
37 let mut max_h = 0.0_f32;
38 for (i, child) in self.children.iter_mut().enumerate() {
39 let s = child.layout_cached(child_constraints, ctx);
40 if let Some(slot) = self.sizes.get_mut(i) {
41 *slot = s;
42 }
43 let off = self.offsets.get(i).copied().unwrap_or(Point::ZERO);
44 max_w = max_w.max(off.x + s.width);
45 max_h = max_h.max(off.y + s.height);
46 }
47 self.cached_size = Size::new(max_w, max_h);
48 constraints.constrain(self.cached_size)
49 }
50
51 fn content_size(&self) -> Size {
52 self.cached_size
53 }
54
55 fn paint(&self, offset: Point, painter: &mut PaintList) {
56 if let Some(child) = self.children.get(self.index) {
57 let off = self.offsets.get(self.index).copied().unwrap_or(Point::ZERO);
58 child.borrow().paint(Point::new(offset.x + off.x, offset.y + off.y), painter);
59 }
60 }
61
62 fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
63 let child = self.children.get(self.index)?;
64 let off = self.offsets.get(self.index).copied().unwrap_or(Point::ZERO);
65 child.borrow().hit_test(position, Point::new(offset.x + off.x, offset.y + off.y))
66 }
67
68 fn push_child(&mut self, offset: Point, child: SharedRO) {
69 self.children.push(child);
70 self.offsets.push(offset);
71 self.sizes.push(Size::ZERO);
72 }
73
74 fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
75 for (i, child) in self.children.iter_mut().enumerate() {
76 if let Some(new_ro) = stateful_ros.get_mut(i) {
77 *child = new_ro.clone();
78 }
79 if let Some(&cid) = child_ids.get(i) {
80 child.borrow_mut().set_element_id(cid);
81 }
82 }
83 }
84
85 fn child_slots(&self) -> Vec<(Point, SharedRO)> {
86 self.children.iter().zip(self.offsets.iter()).map(|(c, &off)| (off, c.clone())).collect()
87 }
88
89 fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
90 for (child, &off) in self.children.iter().zip(self.offsets.iter()) {
91 f(off, child);
92 }
93 }
94
95 fn children_count(&self) -> usize {
96 self.children.len()
97 }
98}
99
100#[derive(Debug)]
102pub struct IndexedStackWidget {
103 pub index: usize,
104 pub children: Vec<WidgetNode>,
105}
106
107impl IndexedStackWidget {
108 pub fn new(index: usize) -> Self {
109 Self { index, children: Vec::new() }
110 }
111
112 pub fn push(mut self, child: impl Widget) -> Self {
113 self.children.push(WidgetNode::new(child));
114 self
115 }
116
117 pub fn index(mut self, index: usize) -> Self {
118 self.index = index;
119 self
120 }
121}
122
123impl Widget for IndexedStackWidget {
124 fn as_any(&self) -> &dyn std::any::Any {
125 self
126 }
127
128 fn is_equivalent(&self, old: &dyn Widget) -> bool {
129 old.as_any()
130 .downcast_ref::<IndexedStackWidget>()
131 .is_some_and(|o| self.index == o.index && crate::widget::children_equivalent(&self.children, &o.children))
132 }
133
134 fn create_render_object(&self) -> Box<dyn RenderObject> {
135 let mut ro = IndexedStackRenderObject::new(self.index);
136 for child in &self.children {
137 ro.push_child(Point::ZERO, make_ro_shared(child.0.create_render_object()));
138 }
139 if self.children.is_empty() {
140 ro.push_child(
141 Point::ZERO,
142 make_ro_shared(Box::new(RRectRenderObject::new(Size::ZERO, Color::TRANSPARENT, 0.0))),
143 );
144 }
145 Box::new(ro)
146 }
147
148 fn children(&self) -> Vec<WidgetNode> {
149 self.children.clone()
150 }
151
152 fn visible_children(&self) -> Option<Vec<bool>> {
153 Some(self.children.iter().enumerate().map(|(i, _)| i == self.index).collect())
154 }
155}