Skip to main content

mgui_core/render_object/
row.rs

1use super::*;
2
3/// 水平堆叠容器([`crate::layout::RowWidget`] 的 RO;Column 的横向对偶)。
4///
5/// **主轴(x)从左到右累加,交叉轴(y)居中** —— 这一条是刻意与 Column 不对称的:
6/// Column 的交叉轴是 x,它把子节点左对齐,因为正文/列表行的宽度差异不需要居中;
7/// 而一行控件里"按钮 + 文本 + 徽标"的自然高度天生不同(按钮 32、文本 20),
8/// 顶对齐会排出一条锯齿。真需要顶对齐的场景用 `PanelWidget::row`(Flex,有
9/// `cross_align`)或 `StackWidget`(绝对定位),这条只是最常用的那一个默认。
10///
11/// 与 Column 一样**不消费** `ParentData::Flex`:要 grow/basis 就用 mgui-layout 的 Flex。
12pub 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    /// 某个子节点在交叉轴上的偏移:在本行高度内垂直居中。
29    #[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        // 与 paint 同一套累加,避免"画在这里、命中在那儿"
72        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        // 反向遍历:后画的在上,优先命中(与 Column/Stack/Flex 一致)
79        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        // 与 Column 同理:累加位置在 layout 时由 cached_size 决定,push 时的 offset 不参与
99        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}