Skip to main content

mgui_core/layout/
spacer.rs

1//! SpacerWidget — Flex 内部推开剩余空间的零尺寸叶子
2
3use crate::widget::Widget;
4
5use super::sized_box::SizedBoxRenderObject;
6
7/// Spacer(零尺寸叶子,配合 FlexItem::grow 推开剩余空间)
8///
9/// 本身没有任何尺寸,也不绘制任何东西。语义完全由 Flex 容器的
10/// `FlexItem` 参数决定 —— 通常是 `.spacer(grow)` 把它和
11/// `FlexItem::grow` 配对使用。
12///
13/// ```ignore
14/// FlexWidget::row()
15///     .push(title)
16///     .spacer(1.0)       // ← 把剩余空间都吃掉
17///     .push(button)
18/// ```
19#[derive(Debug, Default)]
20pub struct SpacerWidget;
21
22impl SpacerWidget {
23    pub fn new() -> Self {
24        Self
25    }
26}
27
28impl Widget for SpacerWidget {
29    fn as_any(&self) -> &dyn std::any::Any {
30        self
31    }
32
33    fn is_equivalent(&self, old: &dyn Widget) -> bool {
34        old.as_any().downcast_ref::<SpacerWidget>().is_some()
35    }
36
37    fn create_render_object(&self) -> Box<dyn crate::render_object::RenderObject> {
38        // 复用 SizedBox:显式 0×0 + 无子节点 → layout 恒返回 ZERO
39        Box::new(SizedBoxRenderObject::new(Some(0.0), Some(0.0), None))
40    }
41
42    fn semantics(&self) -> Option<crate::semantics::WidgetSemantics> {
43        // Spacer 是纯布局件,不出现在无障碍树里
44        None
45    }
46}