1use crate::{Layout, Painter, Pos2, Rect, Region, Vec2, grid, vec2};
2
3#[cfg(debug_assertions)]
4use crate::{Align2, Color32, Stroke};
5
6pub(crate) struct Placer {
7 grid: Option<grid::GridLayout>,
9 layout: Layout,
10 region: Region,
11}
12
13impl Placer {
14 pub(crate) fn new(max_rect: Rect, layout: Layout) -> Self {
15 let region = layout.region_from_max_rect(max_rect);
16 Self {
17 grid: None,
18 layout,
19 region,
20 }
21 }
22
23 #[inline(always)]
24 pub(crate) fn set_grid(&mut self, grid: grid::GridLayout) {
25 self.grid = Some(grid);
26 }
27
28 pub(crate) fn save_grid(&mut self) {
29 if let Some(grid) = &mut self.grid {
30 grid.save();
31 }
32 }
33
34 #[inline(always)]
35 pub(crate) fn grid(&self) -> Option<&grid::GridLayout> {
36 self.grid.as_ref()
37 }
38
39 #[inline(always)]
40 pub(crate) fn is_grid(&self) -> bool {
41 self.grid.is_some()
42 }
43
44 #[inline(always)]
45 pub(crate) fn layout(&self) -> &Layout {
46 &self.layout
47 }
48
49 #[inline(always)]
50 pub(crate) fn prefer_right_to_left(&self) -> bool {
51 self.layout.prefer_right_to_left()
52 }
53
54 #[inline(always)]
55 pub(crate) fn min_rect(&self) -> Rect {
56 self.region.min_rect
57 }
58
59 #[inline(always)]
60 pub(crate) fn max_rect(&self) -> Rect {
61 self.region.max_rect
62 }
63
64 #[inline(always)]
65 pub(crate) fn force_set_min_rect(&mut self, min_rect: Rect) {
66 self.region.min_rect = min_rect;
67 }
68
69 #[inline(always)]
70 pub(crate) fn cursor(&self) -> Rect {
71 self.region.cursor
72 }
73
74 #[inline(always)]
75 pub(crate) fn set_cursor(&mut self, cursor: Rect) {
76 self.region.cursor = cursor;
77 }
78}
79
80impl Placer {
81 pub(crate) fn align_size_within_rect(&self, size: Vec2, outer: Rect) -> Rect {
82 if let Some(grid) = &self.grid {
83 grid.align_size_within_rect(size, outer)
84 } else {
85 self.layout.align_size_within_rect(size, outer)
86 }
87 }
88
89 pub(crate) fn available_rect_before_wrap(&self) -> Rect {
90 if let Some(grid) = &self.grid {
91 grid.available_rect(&self.region)
92 } else {
93 self.layout.available_rect_before_wrap(&self.region)
94 }
95 }
96
97 pub(crate) fn available_size(&self) -> Vec2 {
100 if let Some(grid) = &self.grid {
101 grid.available_rect(&self.region).size()
102 } else {
103 self.layout.available_size(&self.region)
104 }
105 }
106
107 pub(crate) fn next_space(&self, child_size: Vec2, item_spacing: Vec2) -> Rect {
112 debug_assert!(
113 0.0 <= child_size.x && 0.0 <= child_size.y,
114 "Negative child size: {child_size:?}"
115 );
116 self.region.sanity_check();
117 if let Some(grid) = &self.grid {
118 grid.next_cell(self.region.cursor, child_size)
119 } else {
120 self.layout
121 .next_frame(&self.region, child_size, item_spacing)
122 }
123 }
124
125 pub(crate) fn next_widget_position(&self) -> Pos2 {
127 if let Some(grid) = &self.grid {
128 grid.next_cell(self.region.cursor, Vec2::ZERO).center()
129 } else {
130 self.layout.next_widget_position(&self.region)
131 }
132 }
133
134 pub(crate) fn justify_and_align(&self, rect: Rect, child_size: Vec2) -> Rect {
136 debug_assert!(!rect.any_nan(), "rect: {rect:?}");
137 debug_assert!(!child_size.any_nan(), "child_size is NaN: {child_size:?}");
138
139 if let Some(grid) = &self.grid {
140 grid.justify_and_align(rect, child_size)
141 } else {
142 self.layout.justify_and_align(rect, child_size)
143 }
144 }
145
146 pub(crate) fn advance_cursor(&mut self, amount: f32) {
151 debug_assert!(
152 self.grid.is_none(),
153 "You cannot advance the cursor when in a grid layout"
154 );
155 self.layout.advance_cursor(&mut self.region, amount);
156 }
157
158 pub(crate) fn advance_after_rects(
164 &mut self,
165 frame_rect: Rect,
166 widget_rect: Rect,
167 item_spacing: Vec2,
168 ) {
169 debug_assert!(!frame_rect.any_nan(), "frame_rect: {frame_rect:?}");
170 debug_assert!(
171 !widget_rect.any_nan(),
172 "widget_rect is NaN: {widget_rect:?}"
173 );
174 self.region.sanity_check();
175
176 if let Some(grid) = &mut self.grid {
177 grid.advance(&mut self.region.cursor, frame_rect, widget_rect);
178 } else {
179 self.layout.advance_after_rects(
180 &mut self.region.cursor,
181 frame_rect,
182 widget_rect,
183 item_spacing,
184 );
185 }
186
187 self.expand_to_include_rect(frame_rect); self.region.sanity_check();
190 }
191
192 pub(crate) fn end_row(&mut self, item_spacing: Vec2, painter: &Painter) {
195 if let Some(grid) = &mut self.grid {
196 grid.end_row(&mut self.region.cursor, painter);
197 } else {
198 self.layout.end_row(&mut self.region, item_spacing);
199 }
200 }
201
202 pub(crate) fn set_row_height(&mut self, height: f32) {
204 self.layout.set_row_height(&mut self.region, height);
205 }
206}
207
208impl Placer {
209 pub(crate) fn expand_to_include_rect(&mut self, rect: Rect) {
211 self.region.expand_to_include_rect(rect);
212 }
213
214 pub(crate) fn expand_to_include_x(&mut self, x: f32) {
216 self.region.expand_to_include_x(x);
217 }
218
219 pub(crate) fn expand_to_include_y(&mut self, y: f32) {
221 self.region.expand_to_include_y(y);
222 }
223
224 fn next_widget_space_ignore_wrap_justify(&self, size: Vec2) -> Rect {
225 self.layout
226 .next_widget_space_ignore_wrap_justify(&self.region, size)
227 }
228
229 pub(crate) fn set_max_width(&mut self, width: f32) {
232 let rect = self.next_widget_space_ignore_wrap_justify(vec2(width, 0.0));
233 let region = &mut self.region;
234 region.max_rect.min.x = rect.min.x;
235 region.max_rect.max.x = rect.max.x;
236 region.max_rect |= region.min_rect; region.cursor.min.x = region.max_rect.min.x;
239 region.cursor.max.x = region.max_rect.max.x;
240
241 region.sanity_check();
242 }
243
244 pub(crate) fn set_max_height(&mut self, height: f32) {
247 let rect = self.next_widget_space_ignore_wrap_justify(vec2(0.0, height));
248 let region = &mut self.region;
249 region.max_rect.min.y = rect.min.y;
250 region.max_rect.max.y = rect.max.y;
251 region.max_rect |= region.min_rect; region.cursor.min.y = region.max_rect.min.y;
254 region.cursor.max.y = region.max_rect.max.y;
255
256 region.sanity_check();
257 }
258
259 pub(crate) fn set_min_width(&mut self, width: f32) {
262 if width <= 0.0 {
263 return;
264 }
265 let rect = self.next_widget_space_ignore_wrap_justify(vec2(width, 0.0));
266 self.region.expand_to_include_x(rect.min.x);
267 self.region.expand_to_include_x(rect.max.x);
268 }
269
270 pub(crate) fn set_min_height(&mut self, height: f32) {
273 if height <= 0.0 {
274 return;
275 }
276 let rect = self.next_widget_space_ignore_wrap_justify(vec2(0.0, height));
277 self.region.expand_to_include_y(rect.min.y);
278 self.region.expand_to_include_y(rect.max.y);
279 }
280}
281
282impl Placer {
283 #[cfg(debug_assertions)]
284 pub(crate) fn debug_paint_cursor(&self, painter: &crate::Painter, text: impl ToString) {
285 let stroke = Stroke::new(1.0, Color32::DEBUG_COLOR);
286
287 if let Some(grid) = &self.grid {
288 let rect = grid.next_cell(self.cursor(), Vec2::splat(0.0));
289 painter.rect_stroke(rect, 1.0, stroke, epaint::StrokeKind::Inside);
290 let align = Align2::CENTER_CENTER;
291 painter.debug_text(align.pos_in_rect(&rect), align, stroke.color, text);
292 } else {
293 self.layout
294 .paint_text_at_cursor(painter, &self.region, stroke, text);
295 }
296 }
297}