Skip to main content

mgui_core/render_object/
border.rs

1use super::*;
2use crate::paint::PaintList;
3use crate::Rect;
4
5/// Border 样式(四边等值宽度)
6#[derive(Debug, Clone, Copy, PartialEq)]
7pub struct Border {
8    pub width: f32,
9    pub color: Color,
10    pub radius: f32,
11}
12
13impl Default for Border {
14    fn default() -> Self {
15        Self { width: 0.0, color: Color::TRANSPARENT, radius: 0.0 }
16    }
17}
18
19impl Border {
20    pub fn new(width: f32, color: Color) -> Self {
21        Self { width, color, radius: 0.0 }
22    }
23    pub fn with_radius(mut self, r: f32) -> Self {
24        self.radius = r;
25        self
26    }
27
28    /// 以「外圈实心 + 内圈挖空(用 inner_bg 填充)」方式绘制描边环。
29    ///
30    /// `PaintList` 无 stroke 原语,故用两个实心 RRect 嵌套模拟描边
31    /// (与 Button ghost 模式同原理)。`inner_bg` 通常传装饰的背景色,
32    /// 使得内圈与外圈颜色衔接形成环;无背景色时传 `TRANSPARENT`(退化为实心块)。
33    pub fn paint_ring(&self, rect: Rect, inner_bg: Color, painter: &mut PaintList) {
34        if self.width <= 0.0 {
35            return;
36        }
37        painter.rounded_rect(rect, self.color, self.radius);
38        let t = self.width;
39        let inner =
40            Rect::new(rect.x + t, rect.y + t, (rect.width - t * 2.0).max(0.0), (rect.height - t * 2.0).max(0.0));
41        painter.rounded_rect(inner, inner_bg, (self.radius - t).max(0.0));
42    }
43}
44
45/// Border 容器(画 RRect 边框,内缩 child)
46///
47/// 实现:
48/// - layout:child 约束四边各减 border.width(跟 Padding 同逻辑)
49/// - self_size = child_size + 2 * border.width
50/// - paint:先画 border 实心覆盖 self_size 区域,然后 child 在 border_width 内缩位置画
51///   → child 覆盖 border inner,视觉效果即边框(child 背景透明时 border 整体可见,
52///   child 自己有 bg 时会盖掉 border inner,这是正确行为)
53///
54/// 和 Button ghost 模式原理相同(PaintList 没有 stroke,用 fill 模拟描边)。
55pub struct BorderRenderObject {
56    pub border: Border,
57    child: Option<SharedRO>,
58    cached_size: Size,
59    child_cached_size: Size,
60}
61
62impl BorderRenderObject {
63    pub fn new(border: Border, child: impl RenderObject + 'static) -> Self {
64        Self {
65            border,
66            child: Some(make_ro_shared(Box::new(child))),
67            cached_size: Size::ZERO,
68            child_cached_size: Size::ZERO,
69        }
70    }
71
72    pub fn empty(border: Border) -> Self {
73        Self { border, child: None, cached_size: Size::ZERO, child_cached_size: Size::ZERO }
74    }
75}
76
77impl RenderObject for BorderRenderObject {
78    fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
79        let t = self.border.width;
80        let t2 = t * 2.0;
81        let inner = BoxConstraints {
82            min_width: 0.0_f32.max(constraints.min_width - t2),
83            max_width: 0.0_f32.max(constraints.max_width - t2),
84            min_height: 0.0_f32.max(constraints.min_height - t2),
85            max_height: 0.0_f32.max(constraints.max_height - t2),
86        };
87        self.child_cached_size =
88            if let Some(child) = &mut self.child { child.layout_cached(inner, ctx) } else { Size::ZERO };
89        self.cached_size = Size::new(self.child_cached_size.width + t2, self.child_cached_size.height + t2);
90        constraints.constrain(self.cached_size)
91    }
92
93    fn content_size(&self) -> Size {
94        self.cached_size
95    }
96
97    fn paint(&self, offset: Point, painter: &mut PaintList) {
98        // 1. border 实心
99        let rect = crate::Rect::new(offset.x, offset.y, self.cached_size.width, self.cached_size.height);
100        painter.rounded_rect(rect, self.border.color, self.border.radius);
101
102        // 2. child 在内缩位置画(覆盖 border inner)
103        if let Some(child) = &self.child {
104            let t = self.border.width;
105            let inner_r = (self.border.radius - t).max(0.0);
106            let _ = inner_r; // child 自己画半径不受影响(border 的 inner 被 child 完全盖住)
107            child.borrow().paint(Point::new(offset.x + t, offset.y + t), painter);
108        }
109    }
110
111    fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
112        if let Some(child) = &self.child {
113            let t = self.border.width;
114            return child.borrow().hit_test(position, Point::new(offset.x + t, offset.y + t));
115        }
116        None
117    }
118
119    fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
120        if let Some(child) = &mut self.child {
121            if let Some(&cid) = child_ids.first() {
122                if let Some(new_ro) = stateful_ros.first_mut() {
123                    *child = new_ro.clone();
124                }
125                child.borrow_mut().set_element_id(cid);
126            }
127        }
128    }
129
130    fn child_slots(&self) -> Vec<(Point, SharedRO)> {
131        let t = self.border.width;
132        self.child.as_ref().map(|c| vec![(Point::new(t, t), c.clone())]).unwrap_or_default()
133    }
134
135    fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
136        let t = self.border.width;
137        if let Some(c) = &self.child {
138            f(Point::new(t, t), c);
139        }
140    }
141
142    fn children_count(&self) -> usize {
143        if self.child.is_some() {
144            1
145        } else {
146            0
147        }
148    }
149}