mgui_core/render_object/
column.rs1use super::*;
2
3pub struct ColumnRenderObject {
5 children: Vec<ChildSlot>,
6 pub spacing: f32,
7 cached_size: Size,
8}
9
10impl ColumnRenderObject {
11 pub fn new(spacing: f32) -> Self {
12 Self { children: Vec::new(), spacing, cached_size: Size::ZERO }
13 }
14
15 pub fn push(mut self, child: impl RenderObject + 'static) -> Self {
16 self.children.push(ChildSlot { child: make_ro_shared(Box::new(child)), cached_size: Size::ZERO });
17 self
18 }
19}
20
21impl RenderObject for ColumnRenderObject {
22 fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
23 let child_constraints = BoxConstraints {
24 min_width: 0.0,
25 max_width: constraints.max_width,
26 min_height: 0.0,
27 max_height: constraints.max_height,
28 };
29 let mut total_height: f32 = 0.0;
30 let mut max_width: f32 = 0.0;
31 for (i, slot) in self.children.iter_mut().enumerate() {
32 slot.cached_size = slot.child.layout_cached(child_constraints, ctx);
33 if i > 0 {
34 total_height += self.spacing;
35 }
36 total_height += slot.cached_size.height;
37 max_width = max_width.max(slot.cached_size.width);
38 }
39 self.cached_size = Size::new(max_width, total_height);
40 constraints.constrain(self.cached_size)
41 }
42
43 fn content_size(&self) -> Size {
44 self.cached_size
45 }
46
47 fn paint(&self, offset: Point, painter: &mut PaintList) {
48 let mut y = offset.y;
49 for slot in &self.children {
50 slot.child.borrow().paint(Point::new(offset.x, y), painter);
51 y += slot.cached_size.height + self.spacing;
52 }
53 }
54
55 fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
56 let mut ys: Vec<f32> = Vec::with_capacity(self.children.len());
58 let mut y = offset.y;
59 for slot in &self.children {
60 ys.push(y);
61 y += slot.cached_size.height + self.spacing;
62 }
63 for (slot, child_y) in self.children.iter().zip(ys.iter()).rev() {
65 if let Some(id) = slot.child.borrow().hit_test(position, Point::new(offset.x, *child_y)) {
66 return Some(id);
67 }
68 }
69 None
70 }
71
72 fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
73 for (i, (slot, &cid)) in self.children.iter_mut().zip(child_ids).enumerate() {
74 if let Some(new_ro) = stateful_ros.get_mut(i) {
75 slot.child = new_ro.clone();
76 }
77 slot.child.borrow_mut().set_element_id(cid);
78 }
79 }
80
81 fn push_child(&mut self, _offset: Point, child: SharedRO) {
83 self.children.push(ChildSlot { child, cached_size: Size::ZERO });
85 }
86
87 fn child_slots(&self) -> Vec<(Point, SharedRO)> {
89 let mut y = 0.0;
90 self.children
91 .iter()
92 .map(|s| {
93 let off = Point::new(0.0, y);
94 y += s.cached_size.height + self.spacing;
95 (off, s.child.clone())
96 })
97 .collect()
98 }
99
100 fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
101 let mut y = 0.0;
102 for (i, slot) in self.children.iter().enumerate() {
103 if i > 0 {
104 y += self.spacing;
105 }
106 f(Point::new(0.0, y), &slot.child);
107 y += slot.cached_size.height;
108 }
109 }
110
111 fn children_count(&self) -> usize {
112 self.children.len()
113 }
114}