Skip to main content

mgui_core/render_object/
rrect.rs

1use super::*;
2
3// =================================================================
4// 叶子节点
5// =================================================================
6
7/// 圆角矩形(纯叶子,无子节点)
8pub struct RRectRenderObject {
9    pub size: Size,
10    pub color: Color,
11    pub radius: f32,
12    pub element_id: Option<ElementId>,
13}
14
15impl RRectRenderObject {
16    pub fn new(size: Size, color: Color, radius: f32) -> Self {
17        Self { size, color, radius, element_id: None }
18    }
19
20    pub fn with_element_id(mut self, id: ElementId) -> Self {
21        self.element_id = Some(id);
22        self
23    }
24}
25
26impl RenderObject for RRectRenderObject {
27    fn layout(&mut self, constraints: BoxConstraints, _ctx: &mut LayoutContext) -> Size {
28        self.size = constraints.constrain(self.size);
29        self.size
30    }
31
32    fn paint(&self, offset: Point, painter: &mut PaintList) {
33        let rect = Rect::new(offset.x, offset.y, self.size.width, self.size.height);
34        painter.rounded_rect(rect, self.color, self.radius);
35    }
36
37    fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
38        let rect = Rect::new(offset.x, offset.y, self.size.width, self.size.height);
39        if rect.contains(position) {
40            self.element_id
41        } else {
42            None
43        }
44    }
45
46    fn set_element_id(&mut self, id: ElementId) {
47        self.element_id = Some(id);
48    }
49
50    /// 阶段 2-E:RO 树遍历用
51    fn element_id(&self) -> Option<ElementId> {
52        self.element_id
53    }
54
55    /// 阶段 4-D:RRect 的 size = widget 声明的 size(在 layout 后可能再被 constrain)
56    fn content_size(&self) -> Size {
57        self.size
58    }
59
60    /// 属性动画:从 `AnimatedRRectState` 就地读当前呈现值写 size / color / radius。
61    ///
62    /// 与 opacity 的 `apply_state` 不同:三者都是 `PaintCommand::Rect` 的字段/尺寸、
63    /// 被缓存在 `Layer.cached_paint_list` 里,**必须返回 Updated 让所在 layer 重新
64    /// paint** 才会生成新命令;不重画就会一直显示旧值。幂等:三字段都未变返回
65    /// `Unchanged`。
66    ///
67    /// **bounds 语义**:`size` 只写 RO 自身字段,**不调 `mark_layout_dirty`** ——
68    /// 一旦失效 layout 缓存,`layout_cached` 会用父约束 `constrain()` 二次 clamp
69    /// 掉呈现值,且会触发父层重排(二期布局动画范围)。呈现值优先:父的
70    /// `slot.cached_size`、兄弟偏移保持上次 layout 结果不动。paint/hit_test/
71    /// content_size/a11y bounds 都直接读 `self.size`,天然同源。
72    fn apply_state(&mut self, state: &mut dyn std::any::Any) -> StateApply {
73        let track_state = match state.downcast_ref::<crate::widget::animated::AnimatedRRectState>() {
74            Some(s) => s,
75            None => return StateApply::Unsupported,
76        };
77        let next_size = track_state.presented_size();
78        let next_color = track_state.presented_color();
79        let next_radius = track_state.presented_corner();
80        let size_changed = next_size != self.size;
81        let color_changed = next_color != self.color;
82        let radius_changed = (next_radius - self.radius).abs() >= f32::EPSILON;
83        if !size_changed && !color_changed && !radius_changed {
84            return StateApply::Unchanged;
85        }
86        self.size = next_size;
87        self.color = next_color;
88        self.radius = next_radius;
89        StateApply::Updated
90    }
91}