Skip to main content

mgui_core/widget/
primitives.rs

1use crate::render_object::{RRectRenderObject, RenderObject, StackRenderObject};
2use crate::{Color, Point, Size};
3
4use super::{Widget, WidgetNode};
5
6// =================================================================
7// 叶子 Widget
8// =================================================================
9
10/// 圆角矩形
11#[derive(Debug, Clone)]
12pub struct RRectWidget {
13    pub size: Size,
14    pub color: Color,
15    pub radius: f32,
16}
17
18impl RRectWidget {
19    pub fn new(size: Size, color: Color, radius: f32) -> Self {
20        Self { size, color, radius }
21    }
22}
23
24impl Widget for RRectWidget {
25    fn as_any(&self) -> &dyn std::any::Any {
26        self
27    }
28
29    fn create_render_object(&self) -> Box<dyn RenderObject> {
30        Box::new(RRectRenderObject::new(self.size, self.color, self.radius))
31    }
32
33    fn is_equivalent(&self, old: &dyn Widget) -> bool {
34        old.as_any()
35            .downcast_ref::<RRectWidget>()
36            .is_some_and(|o| self.size == o.size && self.color == o.color && self.radius == o.radius)
37    }
38}
39
40// =================================================================
41// 容器 Widget
42// =================================================================
43
44/// Stack(多子节点按各自 offset 叠加)
45#[derive(Debug, Default)]
46pub struct StackWidget {
47    pub children: Vec<(Point, WidgetNode)>,
48}
49
50impl StackWidget {
51    pub fn new() -> Self {
52        Self { children: Vec::new() }
53    }
54
55    pub fn push(mut self, offset: Point, child: impl Widget) -> Self {
56        self.children.push((offset, WidgetNode::new(child)));
57        self
58    }
59}
60
61impl Widget for StackWidget {
62    fn as_any(&self) -> &dyn std::any::Any {
63        self
64    }
65
66    fn create_render_object(&self) -> Box<dyn RenderObject> {
67        let mut stack = StackRenderObject::new();
68        for (offset, child) in &self.children {
69            stack = stack.push(*offset, child.0.create_render_object());
70        }
71        Box::new(stack)
72    }
73    fn children(&self) -> Vec<WidgetNode> {
74        self.children.iter().map(|(_, n)| n.clone()).collect()
75    }
76
77    /// 阶段 4-D:Stack 知道每个 child 在父内的 position
78    /// (WidgetNode 的 RO paint 时通过 StackRenderObject 的 push 传 offset)
79    fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> {
80        self.children.clone()
81    }
82
83    fn is_equivalent(&self, old: &dyn Widget) -> bool {
84        let Some(o) = old.as_any().downcast_ref::<StackWidget>() else {
85            return false;
86        };
87        self.children.len() == o.children.len()
88            && self.children.iter().zip(&o.children).all(|((pa, wa), (pb, wb))| pa == pb && wa.is_equivalent(wb))
89    }
90
91    fn semantics(&self) -> Option<crate::semantics::WidgetSemantics> {
92        Some(crate::semantics::WidgetSemantics::new(crate::semantics::SemanticsRole::Container).non_focusable())
93    }
94}