1use crate::id::ElementId;
9use crate::paint::PaintList;
10use crate::render_object::{make_ro_shared, BoxConstraints, LayoutContext, RRectRenderObject, RenderObject, SharedRO};
11use crate::widget::{Widget, WidgetNode};
12use crate::{Color, Point, Size};
13
14use super::alignment::Alignment;
15
16pub struct OverflowBoxRenderObject {
18 pub min_width: f32,
19 pub max_width: f32,
20 pub min_height: f32,
21 pub max_height: f32,
22 pub alignment: Alignment,
23 child: Option<SharedRO>,
24 cached_size: Size,
25 child_cached_size: Size,
26 child_offset: Point,
27}
28
29impl OverflowBoxRenderObject {
30 pub fn new(
31 min_width: f32,
32 max_width: f32,
33 min_height: f32,
34 max_height: f32,
35 alignment: Alignment,
36 child: Option<Box<dyn RenderObject>>,
37 ) -> Self {
38 Self {
39 min_width,
40 max_width,
41 min_height,
42 max_height,
43 alignment,
44 child: child.map(make_ro_shared),
45 cached_size: Size::ZERO,
46 child_cached_size: Size::ZERO,
47 child_offset: Point::ZERO,
48 }
49 }
50}
51
52impl RenderObject for OverflowBoxRenderObject {
53 fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
54 let child_constraints = BoxConstraints {
56 min_width: self.min_width,
57 max_width: self.max_width,
58 min_height: self.min_height,
59 max_height: self.max_height,
60 };
61 self.child_cached_size =
62 if let Some(child) = &mut self.child { child.layout_cached(child_constraints, ctx) } else { Size::ZERO };
63 let biggest = Size::new(constraints.max_width, constraints.max_height);
65 self.cached_size = if biggest.width.is_finite() && biggest.height.is_finite() {
66 biggest
67 } else {
68 constraints.constrain(self.child_cached_size)
69 };
70 let delta_w = self.cached_size.width - self.child_cached_size.width;
71 let delta_h = self.cached_size.height - self.child_cached_size.height;
72 self.child_offset = Point::new(self.alignment.x * delta_w, self.alignment.y * delta_h);
73 self.cached_size
74 }
75
76 fn content_size(&self) -> Size {
77 self.cached_size
78 }
79
80 fn paint(&self, offset: Point, painter: &mut PaintList) {
81 if let Some(child) = &self.child {
82 child.borrow().paint(Point::new(offset.x + self.child_offset.x, offset.y + self.child_offset.y), painter);
83 }
84 }
85
86 fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
87 self.child.as_ref().and_then(|c| {
88 c.borrow().hit_test(position, Point::new(offset.x + self.child_offset.x, offset.y + self.child_offset.y))
89 })
90 }
91
92 fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
93 if let Some(child) = &mut self.child {
94 if let Some(&cid) = child_ids.first() {
95 if let Some(new_ro) = stateful_ros.first_mut() {
96 *child = new_ro.clone();
97 }
98 child.borrow_mut().set_element_id(cid);
99 }
100 }
101 }
102
103 fn child_slots(&self) -> Vec<(Point, SharedRO)> {
104 self.child.as_ref().map(|c| vec![(self.child_offset, c.clone())]).unwrap_or_default()
105 }
106
107 fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
108 if let Some(c) = &self.child {
109 f(self.child_offset, c);
110 }
111 }
112
113 fn children_count(&self) -> usize {
114 if self.child.is_some() {
115 1
116 } else {
117 0
118 }
119 }
120}
121
122#[derive(Debug)]
124pub struct OverflowBoxWidget {
125 pub min_width: f32,
126 pub max_width: f32,
127 pub min_height: f32,
128 pub max_height: f32,
129 pub alignment: Alignment,
130 pub child: Option<WidgetNode>,
131}
132
133impl OverflowBoxWidget {
134 pub fn new(child: impl Widget) -> Self {
136 Self {
137 min_width: 0.0,
138 max_width: f32::INFINITY,
139 min_height: 0.0,
140 max_height: f32::INFINITY,
141 alignment: Alignment::CENTER,
142 child: Some(WidgetNode::new(child)),
143 }
144 }
145
146 pub fn min_w(mut self, w: f32) -> Self {
147 self.min_width = w;
148 self
149 }
150 pub fn max_w(mut self, w: f32) -> Self {
151 self.max_width = w;
152 self
153 }
154 pub fn min_h(mut self, h: f32) -> Self {
155 self.min_height = h;
156 self
157 }
158 pub fn max_h(mut self, h: f32) -> Self {
159 self.max_height = h;
160 self
161 }
162 pub fn align(mut self, alignment: Alignment) -> Self {
163 self.alignment = alignment;
164 self
165 }
166}
167
168impl Widget for OverflowBoxWidget {
169 fn as_any(&self) -> &dyn std::any::Any {
170 self
171 }
172
173 fn is_equivalent(&self, old: &dyn Widget) -> bool {
174 old.as_any().downcast_ref::<OverflowBoxWidget>().is_some_and(|o| {
175 self.min_width == o.min_width
176 && self.max_width == o.max_width
177 && self.min_height == o.min_height
178 && self.max_height == o.max_height
179 && self.alignment == o.alignment
180 && crate::widget::optional_child_equivalent(&self.child, &o.child)
181 })
182 }
183
184 fn create_render_object(&self) -> Box<dyn RenderObject> {
185 let child_ro = match &self.child {
186 Some(c) => c.0.create_render_object(),
187 None => Box::new(RRectRenderObject::new(Size::ZERO, Color::TRANSPARENT, 0.0)),
188 };
189 Box::new(OverflowBoxRenderObject::new(
190 self.min_width,
191 self.max_width,
192 self.min_height,
193 self.max_height,
194 self.alignment,
195 Some(child_ro),
196 ))
197 }
198
199 fn children(&self) -> Vec<WidgetNode> {
200 match &self.child {
201 Some(c) => vec![c.clone()],
202 None => vec![],
203 }
204 }
205}