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