mgui_core/render_object/
border.rs1use super::*;
2use crate::paint::PaintList;
3use crate::Rect;
4
5#[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 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
45pub 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 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 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.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}