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}