1use blit::{Axis, Context, Fit, LayoutCx, Scalar};
2
3use super::{Align, Justify, Sizing, justify_offset, percent_share};
4
5blit::builder! {
6 #[derive(Clone, Copy, Debug, PartialEq)]
12 pub struct Layout<T: Scalar> {
13 new(axis: Axis),
14 gap: T = T::ZERO,
15 align: Align = Align::Start,
16 justify: Justify = Justify::Start,
17 }
18}
19
20blit::builder! {
21 #[derive(Clone, Copy, Debug, PartialEq)]
26 pub struct Item<T: Scalar> {
27 new(),
28 #[into]
29 width: Sizing<T> = Sizing::fit(),
30 #[into]
31 height: Sizing<T> = Sizing::fit(),
32 weight: f32 = 1.0,
33 }
34}
35
36impl<T: Scalar> Item<T> {
37 pub fn grow(mut self) -> Self {
38 self.width = Sizing::grow();
39 self.height = Sizing::grow();
40 self
41 }
42
43 pub fn sizing(&self, axis: Axis) -> Sizing<T> {
44 match axis {
45 Axis::Horizontal => self.width,
46 Axis::Vertical => self.height,
47 }
48 }
49}
50
51pub fn layout<T: Scalar>(axis: Axis) -> Layout<T> {
52 Layout::new(axis)
53}
54
55pub fn row<T: Scalar>() -> Layout<T> {
56 layout(Axis::Horizontal)
57}
58
59pub fn column<T: Scalar>() -> Layout<T> {
60 layout(Axis::Vertical)
61}
62
63pub fn item<T: Scalar>() -> Item<T> {
64 Item::new()
65}
66
67impl<C: Context<Scalar = T>, T: Scalar> blit::Layout<C> for Layout<T> {
68 type Item = Item<T>;
69
70 fn fit(&self, cx: &mut LayoutCx<'_, C, Self::Item>, axis: Axis) -> Fit<T> {
71 let mut extent = Fit::ZERO;
72 let mut count = 0usize;
73 for child in cx.children() {
74 let item = cx.item(child);
75 let sizing = self.resolve(cx, child, item, axis);
76 let child_fit = match sizing {
77 Sizing::Fixed(v) => Fit::exact(v.max(T::ZERO)),
78 Sizing::Percent(_) => Fit::ZERO,
79 Sizing::Fit { .. } | Sizing::Grow { .. } => {
80 let fit = cx.fit(child, axis);
81 Fit::new(sizing.clamp(fit.min), sizing.clamp(fit.natural))
82 }
83 };
84 if axis == self.axis {
85 extent += child_fit;
86 } else {
87 extent = extent.max(child_fit);
88 }
89 count += 1;
90 }
91 if axis == self.axis && count > 1 {
92 extent += Fit::exact(self.gap.max(T::ZERO).repeat(count - 1));
93 }
94 extent
95 }
96
97 fn distribute(&self, cx: &mut LayoutCx<'_, C, Self::Item>, axis: Axis, extent: T) {
98 if axis != self.axis {
99 for child in cx.children() {
100 let item = cx.item(child);
101 let sizing = self.resolve(cx, child, item, axis);
102 let assigned = match (sizing, self.align) {
103 (Sizing::Fixed(v), _) => v.max(T::ZERO),
104 (Sizing::Percent(p), _) => percent_share(extent, p, &mut 0.0),
105 (Sizing::Grow { .. }, _) | (Sizing::Fit { .. }, Align::Stretch) => {
106 sizing.clamp(extent).max(sizing.clamp(cx.fit(child, axis).min))
107 }
108 (Sizing::Fit { .. }, _) => {
109 let fit = cx.fit(child, axis);
110 sizing.clamp(fit.natural).min(extent).max(sizing.clamp(fit.min))
111 }
112 };
113 let child_cross = cx.set_extent(child, assigned);
114 let offset = match self.align {
115 Align::Start | Align::Stretch => T::ZERO,
116 Align::Center => T::ZERO.lerp((extent - child_cross).max(T::ZERO), 0.5),
117 Align::End => (extent - child_cross).max(T::ZERO),
118 };
119 cx.set_offset(child, offset);
120 }
121 return;
122 }
123
124 let gap = self.gap.max(T::ZERO);
125 let mut count = 0usize;
126 let mut grows = 0usize;
127 let mut fixed_used = T::ZERO;
128 let mut percent_sum = 0.0f32;
129 let mut fit_min = T::ZERO;
130 let mut fit_natural = T::ZERO;
131 let mut grow_min = T::ZERO;
132 let mut weights = 0.0f32;
133 let mut max_min_ratio = 0.0f32;
134 let mut min_max_ratio = f32::INFINITY;
135 let mut any_grow_locked = false;
136 for child in cx.children() {
137 count += 1;
138 let item = cx.item(child);
139 let sizing = self.resolve(cx, child, item, self.axis);
140 match sizing {
141 Sizing::Fixed(v) => fixed_used += v.max(T::ZERO),
142 Sizing::Percent(p) => {
143 assert!((0.0..=1.0).contains(&p));
144 percent_sum += p;
145 }
146 Sizing::Fit { .. } => {
147 let fit = cx.fit(child, self.axis);
148 fit_min += sizing.clamp(fit.min);
149 fit_natural += sizing.clamp(fit.natural);
150 }
151 Sizing::Grow { min, max } => {
152 assert!(
153 item.weight.is_finite() && item.weight > 0.0,
154 "flex weight must be finite and positive"
155 );
156 let item_min = min.max(T::ZERO);
157 assert!(item_min.is_finite(), "flex minimum must be finite");
158 let min = sizing.clamp(cx.fit(child, self.axis).min);
159 let max = max.max(item_min);
160 grows += 1;
161 grow_min += min;
162 if max > min {
163 weights += item.weight;
164 max_min_ratio = max_min_ratio.max(min.to_f32() / item.weight);
165 if max.is_finite() {
166 min_max_ratio = min_max_ratio.min(max.to_f32() / item.weight);
167 }
168 } else {
169 any_grow_locked = true;
170 }
171 }
172 }
173 }
174 if count == 0 {
175 return;
176 }
177
178 let gaps = gap.repeat(count - 1);
179 let pool = if extent.is_finite() {
180 (extent - gaps).max(T::ZERO)
181 } else {
182 T::UNBOUNDED
183 };
184 let percent_used = if pool.is_finite() && percent_sum > 0.0 {
185 T::from_f32(pool.to_f32() * percent_sum)
186 } else {
187 T::ZERO
188 };
189 let fit_budget = if pool.is_finite() {
190 (pool - fixed_used - percent_used - grow_min).max(T::ZERO)
191 } else {
192 T::UNBOUNDED
193 };
194
195 let shrink_cap = if fit_natural <= fit_budget {
197 None
198 } else if fit_budget <= fit_min {
199 Some(0.0f32)
200 } else {
201 let mut events = cx.scratch().alloc(count * 2);
202 for child in cx.children() {
203 let sizing = self.resolve(cx, child, cx.item(child), self.axis);
204 if let Sizing::Fit { .. } = sizing {
205 let fit = cx.fit(child, self.axis);
206 let (min, natural) = (sizing.clamp(fit.min), sizing.clamp(fit.natural));
207 if natural > min {
208 events.push((-natural.to_f32(), 1.0));
209 events.push((-min.to_f32(), -1.0));
210 }
211 }
212 }
213 Some(-water_level(&mut events, (fit_natural - fit_budget).to_f32()))
214 };
215
216 let mut non_grow_used = T::ZERO;
217 let place_non_grow = (grows == 0 || !extent.is_finite()) && self.justify == Justify::Start;
218 if grows < count || !extent.is_finite() {
219 let mut percentages = 0.0;
220 let mut shrink_rem = 0.0;
221 let mut cursor = T::ZERO;
222 for child in cx.children() {
223 let item = cx.item(child);
224 let sizing = self.resolve(cx, child, item, self.axis);
225 let assigned = match sizing {
226 Sizing::Grow { .. } if extent.is_finite() => continue,
227 Sizing::Grow { .. } => sizing.clamp(cx.fit(child, self.axis).natural),
228 Sizing::Fixed(v) => v.max(T::ZERO),
229 Sizing::Percent(p) => percent_share(pool, p, &mut percentages),
230 Sizing::Fit { .. } => {
231 let fit = cx.fit(child, self.axis);
232 let min = sizing.clamp(fit.min);
233 let natural = sizing.clamp(fit.natural);
234 match shrink_cap {
235 None => natural,
236 Some(cap) if natural.to_f32() <= cap => natural,
237 Some(cap) if min.to_f32() >= cap => min,
238 Some(cap) => T::allocate(&mut shrink_rem, cap).clamp(min, natural),
239 }
240 }
241 };
242 let actual = cx.set_extent(child, assigned);
243 non_grow_used += actual;
244 if place_non_grow {
245 cx.set_offset(child, cursor);
246 cursor += actual + gap;
247 }
248 }
249 if place_non_grow {
250 return;
251 }
252 }
253
254 if grows != 0 && extent.is_finite() {
255 let grow_budget = (pool - non_grow_used).max(T::ZERO);
256 let unit = if grow_budget <= grow_min || weights <= 0.0 {
257 0.0
258 } else {
259 let unconstrained = grow_budget.to_f32() / weights;
260 if !any_grow_locked && max_min_ratio <= unconstrained && unconstrained <= min_max_ratio {
261 unconstrained
262 } else {
263 let mut events = cx.scratch().alloc(grows * 2);
264 for child in cx.children() {
265 let item = cx.item(child);
266 let sizing = self.resolve(cx, child, item, self.axis);
267 if let Sizing::Grow { max, .. } = sizing {
268 let min = sizing.clamp(cx.fit(child, self.axis).min);
269 let max = max.max(min);
270 if max > min {
271 events.push((min.to_f32() / item.weight, item.weight));
272 if max.is_finite() {
273 events.push((max.to_f32() / item.weight, -item.weight));
274 }
275 }
276 }
277 }
278 water_level(&mut events, (grow_budget - grow_min).to_f32())
279 }
280 };
281
282 let mut allocation = 0.0;
283 let place_inline = grows == count && self.justify == Justify::Start;
284 let mut cursor = T::ZERO;
285 for child in cx.children() {
286 let item = cx.item(child);
287 let sizing = self.resolve(cx, child, item, self.axis);
288 let Sizing::Grow { max, .. } = sizing else {
289 continue;
290 };
291 let min = sizing.clamp(cx.fit(child, self.axis).min);
292 let max = max.max(min);
293 let target = unit * item.weight;
294 let main = if min >= max || target <= min.to_f32() {
295 min
296 } else if max.is_finite() && target >= max.to_f32() {
297 max
298 } else {
299 T::allocate(&mut allocation, target).clamp(min, max)
300 };
301 let actual = cx.set_extent(child, main);
302 if place_inline {
303 cx.set_offset(child, cursor);
304 cursor += actual + gap;
305 }
306 }
307 if place_inline {
308 return;
309 }
310 }
311
312 let (offset, mut spacing, extra_gap) = if self.justify == Justify::Start {
313 (T::ZERO, 0.0, 0.0)
314 } else {
315 let mut used = T::ZERO;
316 for child in cx.children() {
317 used += self.axis.extent(cx.size(child));
318 }
319 justify_offset(self.justify, (extent - used - gaps).max(T::ZERO), count)
320 };
321 let mut cursor = offset;
322 for child in cx.children() {
323 cx.set_offset(child, cursor + T::from_f32(spacing));
324 cursor += self.axis.extent(cx.size(child)) + gap;
325 spacing += extra_gap;
326 }
327 }
328}
329
330impl<T: Scalar> Layout<T> {
331 #[inline(always)]
332 fn resolve<C: Context<Scalar = T>>(
333 &self,
334 cx: &LayoutCx<'_, C, Item<T>>,
335 child: blit::NodeId,
336 item: &Item<T>,
337 axis: Axis,
338 ) -> Sizing<T> {
339 cx.fixed_extent(child, axis).map_or(item.sizing(axis), Sizing::Fixed)
340 }
341}
342
343fn water_level(events: &mut [(f32, f32)], mut budget: f32) -> f32 {
344 events.sort_unstable_by(|a, b| a.0.total_cmp(&b.0));
345 let mut active = 0.0f32;
346 let mut prev = events[0].0;
347 for &(coord, slope) in events.iter() {
348 let span = coord - prev;
349 if active > 0.0 && span > 0.0 {
350 let area = span * active;
351 if area >= budget {
352 return prev + budget / active;
353 }
354 budget -= area;
355 }
356 prev = coord;
357 active = (active + slope).max(0.0);
358 }
359 if active > 0.0 { prev + budget / active } else { prev }
360}