Skip to main content

mgui_core/layout/
row.rs

1//! 简单水平堆叠容器(非 Flex)。
2//!
3//! 按子节点自然尺寸从左到右摆放,**不消费** `ParentData::Flex`。
4//! `ExpandedWidget` / `FlexibleWidget` 只有放进 `mgui_layout::PanelWidget`
5//! (Flex 模式)才生效;需要 grow/basis/自定义交叉轴对齐时应使用 PanelWidget。
6//!
7//! 交叉轴(垂直)**居中**,理由见 [`RowRenderObject`] 的文档:一行混合高度的控件
8//! (按钮 32 + 文本 20)顶对齐会排出锯齿,而"居中的行"是这条容器最常见的用法。
9
10use crate::render_object::{RenderObject, RowRenderObject};
11use crate::widget::{Widget, WidgetNode};
12
13/// 简单水平堆叠容器(非 Flex)
14#[derive(Debug, Default)]
15pub struct RowWidget {
16    pub spacing: f32,
17    pub children: Vec<WidgetNode>,
18}
19
20impl RowWidget {
21    pub fn new(spacing: f32) -> Self {
22        Self { spacing, children: Vec::new() }
23    }
24
25    pub fn push(mut self, child: impl Widget) -> Self {
26        self.children.push(WidgetNode::new(child));
27        self
28    }
29
30    /// 追加已擦除类型的子节点(动态组装的 widget 树,例如按状态增减的按钮行)
31    pub fn push_node(mut self, child: WidgetNode) -> Self {
32        self.children.push(child);
33        self
34    }
35}
36
37impl Widget for RowWidget {
38    fn as_any(&self) -> &dyn std::any::Any {
39        self
40    }
41
42    fn create_render_object(&self) -> Box<dyn RenderObject> {
43        let mut row = RowRenderObject::new(self.spacing);
44        for child in &self.children {
45            debug_assert!(
46                child.0.parent_data().is_none(),
47                "RowWidget 不消费 ParentData::Flex;弹性布局请改用 mgui_layout::PanelWidget::row"
48            );
49            row = row.push(child.0.create_render_object());
50        }
51        Box::new(row)
52    }
53
54    fn children(&self) -> Vec<WidgetNode> {
55        self.children.clone()
56    }
57
58    fn is_equivalent(&self, old: &dyn Widget) -> bool {
59        old.as_any().downcast_ref::<RowWidget>().is_some_and(|o| {
60            self.spacing == o.spacing && crate::widget::children_equivalent(&self.children, &o.children)
61        })
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use crate::render_object::{BoxConstraints, LayoutContext};
69    use crate::{Color, RRectWidget, Size};
70
71    #[test]
72    fn row_places_children_left_to_right_with_spacing() {
73        let mut ro = RowWidget::new(6.0)
74            .push(RRectWidget::new(Size::new(100.0, 20.0), Color::WHITE, 0.0))
75            .push(RRectWidget::new(Size::new(80.0, 10.0), Color::WHITE, 0.0))
76            .create_render_object();
77        let size = ro.layout(BoxConstraints::UNBOUNDED, &mut LayoutContext);
78        // 主轴累加 + 间距;交叉轴取最高那个(不是求和)
79        assert_eq!(size, Size::new(186.0, 20.0));
80    }
81
82    #[test]
83    fn row_centers_shorter_children_on_the_cross_axis() {
84        // 这条测的是"paint / hit_test / child_slots 用同一个交叉轴偏移"的那个共享来源:
85        // 偏移只在 layout 之后才有意义,所以先排一遍再读子节点偏移。
86        use crate::render_object::RenderObject;
87        let mut ro = RowWidget::new(0.0)
88            .push(RRectWidget::new(Size::new(10.0, 30.0), Color::WHITE, 0.0))
89            .push(RRectWidget::new(Size::new(10.0, 10.0), Color::WHITE, 0.0))
90            .create_render_object();
91        ro.layout(BoxConstraints::UNBOUNDED, &mut LayoutContext);
92        let slots = ro.child_slots();
93        assert_eq!(slots[0].0.y, 0.0, "最高的子节点定义了行高,它自己贴顶");
94        assert_eq!(slots[1].0.y, 10.0, "矮的子节点在 30 高的行里居中 → (30-10)/2");
95    }
96}