mgui_core/render_object/
padding.rs1use super::*;
2
3#[derive(Debug, Clone, Copy, PartialEq)]
5pub struct EdgeInsets {
6 pub left: f32,
7 pub top: f32,
8 pub right: f32,
9 pub bottom: f32,
10}
11
12impl EdgeInsets {
13 pub fn all(v: f32) -> Self {
15 Self { left: v, top: v, right: v, bottom: v }
16 }
17 pub fn symmetric(horizontal: f32, vertical: f32) -> Self {
18 Self { left: horizontal, top: vertical, right: horizontal, bottom: vertical }
19 }
20 pub fn only(left: f32, top: f32, right: f32, bottom: f32) -> Self {
21 Self { left, top, right, bottom }
22 }
23
24 pub fn horizontal(&self) -> f32 {
25 self.left + self.right
26 }
27 pub fn vertical(&self) -> f32 {
28 self.top + self.bottom
29 }
30}
31
32pub struct PaddingRenderObject {
34 pub padding: EdgeInsets,
35 child: Option<SharedRO>,
36 cached_size: Size,
37 child_cached_size: Size,
38}
39
40impl PaddingRenderObject {
41 pub fn new(padding: f32, child: impl RenderObject + 'static) -> Self {
43 Self::new_insets(EdgeInsets::all(padding), child)
44 }
45
46 pub fn new_insets(padding: EdgeInsets, child: impl RenderObject + 'static) -> Self {
47 Self {
48 padding,
49 child: Some(make_ro_shared(Box::new(child))),
50 cached_size: Size::ZERO,
51 child_cached_size: Size::ZERO,
52 }
53 }
54
55 pub fn empty(padding: EdgeInsets) -> Self {
57 Self { padding, child: None, cached_size: Size::ZERO, child_cached_size: Size::ZERO }
58 }
59}
60
61impl RenderObject for PaddingRenderObject {
62 fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
63 let p = &self.padding;
64 let inner = BoxConstraints {
65 min_width: 0.0_f32.max(constraints.min_width - p.horizontal()),
66 max_width: 0.0_f32.max(constraints.max_width - p.horizontal()),
67 min_height: 0.0_f32.max(constraints.min_height - p.vertical()),
68 max_height: 0.0_f32.max(constraints.max_height - p.vertical()),
69 };
70 self.child_cached_size =
71 if let Some(child) = &mut self.child { child.layout_cached(inner, ctx) } else { Size::ZERO };
72 self.cached_size =
73 Size::new(self.child_cached_size.width + p.horizontal(), self.child_cached_size.height + p.vertical());
74 constraints.constrain(self.cached_size)
75 }
76
77 fn paint(&self, offset: Point, painter: &mut PaintList) {
78 if let Some(child) = &self.child {
79 let p = &self.padding;
80 child.borrow().paint(Point::new(offset.x + p.left, offset.y + p.top), painter);
81 }
82 }
83
84 fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
85 if let Some(child) = &self.child {
86 let p = &self.padding;
87 return child.borrow().hit_test(position, Point::new(offset.x + p.left, offset.y + p.top));
88 }
89 None
90 }
91
92 fn content_size(&self) -> Size {
93 self.cached_size
94 }
95
96 fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
97 if let Some(child) = &mut self.child {
98 if let Some(&cid) = child_ids.first() {
99 if let Some(new_ro) = stateful_ros.first_mut() {
100 *child = new_ro.clone();
101 }
102 child.borrow_mut().set_element_id(cid);
103 }
104 }
105 }
106
107 fn push_child(&mut self, _offset: Point, _child: SharedRO) {
108 }
110
111 fn child_slots(&self) -> Vec<(Point, SharedRO)> {
112 self.child
113 .as_ref()
114 .map(|c| vec![(Point::new(self.padding.left, self.padding.top), c.clone())])
115 .unwrap_or_default()
116 }
117
118 fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
119 if let Some(c) = &self.child {
120 f(Point::new(self.padding.left, self.padding.top), c);
121 }
122 }
123
124 fn children_count(&self) -> usize {
125 if self.child.is_some() {
126 1
127 } else {
128 0
129 }
130 }
131}