mgui_core/render_object/
stack.rs1#[cfg(feature = "frame-log")]
2use log;
3
4use super::*;
5
6pub struct StackRenderObject {
10 children: Vec<ChildSlot>,
11 offsets: Vec<Point>,
12 cached_size: Size,
13}
14
15impl StackRenderObject {
16 pub fn new() -> Self {
17 Self { children: Vec::new(), offsets: Vec::new(), cached_size: Size::ZERO }
18 }
19
20 pub fn push(mut self, offset: Point, child: impl RenderObject + 'static) -> Self {
21 self.children.push(ChildSlot { child: make_ro_shared(Box::new(child)), cached_size: Size::ZERO });
22 self.offsets.push(offset);
23 self
24 }
25}
26
27impl Default for StackRenderObject {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl RenderObject for StackRenderObject {
34 fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
35 let child_constraints = constraints.loosen();
36 for slot in &mut self.children {
37 slot.cached_size = slot.child.layout_cached(child_constraints, ctx);
38 }
39 let mut max_w = 0.0_f32;
40 let mut max_h = 0.0_f32;
41 for (slot, offset) in self.children.iter().zip(self.offsets.iter()) {
42 max_w = max_w.max(offset.x + slot.cached_size.width);
43 max_h = max_h.max(offset.y + slot.cached_size.height);
44 }
45 self.cached_size = Size::new(max_w, max_h);
46 #[cfg(feature = "frame-log")]
47 log::trace!("[FRAME-STACK] layout children={} size={:?}", self.children.len(), self.cached_size);
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 for (slot, off) in self.children.iter().zip(self.offsets.iter()) {
57 slot.child.borrow().paint(Point::new(offset.x + off.x, offset.y + off.y), painter);
58 }
59 }
60
61 fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
62 for (slot, off) in self.children.iter().rev().zip(self.offsets.iter().rev()) {
64 if let Some(id) = slot.child.borrow().hit_test(position, Point::new(offset.x + off.x, offset.y + off.y)) {
65 return Some(id);
66 }
67 }
68 None
69 }
70
71 fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
72 for (i, (slot, &cid)) in self.children.iter_mut().zip(child_ids).enumerate() {
73 if let Some(new_ro) = stateful_ros.get_mut(i) {
76 slot.child = new_ro.clone();
77 }
78 slot.child.borrow_mut().set_element_id(cid);
79 }
80 }
81
82 fn push_child(&mut self, offset: Point, child: SharedRO) {
84 self.children.push(ChildSlot { child, cached_size: Size::ZERO });
85 self.offsets.push(offset);
86 }
87
88 fn child_slots(&self) -> Vec<(Point, SharedRO)> {
91 self.children.iter().zip(self.offsets.iter()).map(|(s, &off)| (off, s.child.clone())).collect()
92 }
93
94 fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
95 for (slot, &off) in self.children.iter().zip(self.offsets.iter()) {
96 f(off, &slot.child);
97 }
98 }
99
100 fn children_count(&self) -> usize {
101 self.children.len()
102 }
103}