sinomo-ui-layout 0.6.1

Layout constraints and helpers for SUI.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
use sui_core::{Point, Rect, Size};

use crate::{Alignment, Constraints};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GridTrackMax {
    Auto,
    Points(f32),
    Fraction(f32),
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GridTrack {
    Fixed(f32),
    Auto,
    Fraction(f32),
    MinMax { min: f32, max: GridTrackMax },
}

impl GridTrack {
    pub const fn fixed(points: f32) -> Self {
        Self::Fixed(points)
    }

    pub const fn auto() -> Self {
        Self::Auto
    }

    pub const fn fraction(weight: f32) -> Self {
        Self::Fraction(weight)
    }

    pub const fn minmax(min: f32, max: GridTrackMax) -> Self {
        Self::MinMax { min, max }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GridPlacement {
    pub row: usize,
    pub column: usize,
    pub row_span: usize,
    pub column_span: usize,
}

impl GridPlacement {
    pub const fn new(row: usize, column: usize) -> Self {
        Self {
            row,
            column,
            row_span: 1,
            column_span: 1,
        }
    }

    pub const fn span(mut self, rows: usize, columns: usize) -> Self {
        self.row_span = if rows == 0 { 1 } else { rows };
        self.column_span = if columns == 0 { 1 } else { columns };
        self
    }
}

impl Default for GridPlacement {
    fn default() -> Self {
        Self::new(0, 0)
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GridItem {
    pub placement: GridPlacement,
    pub minimum_size: Size,
    pub natural_size: Size,
    pub horizontal_alignment: Alignment,
    pub vertical_alignment: Alignment,
}

impl GridItem {
    pub fn new(placement: GridPlacement, natural_size: Size) -> Self {
        let natural_size = sanitize_size(natural_size);
        Self {
            placement,
            minimum_size: natural_size,
            natural_size,
            horizontal_alignment: Alignment::Stretch,
            vertical_alignment: Alignment::Stretch,
        }
    }

    pub fn minimum_size(mut self, minimum_size: Size) -> Self {
        self.minimum_size = sanitize_size(minimum_size);
        self
    }

    pub const fn align(mut self, horizontal: Alignment, vertical: Alignment) -> Self {
        self.horizontal_alignment = horizontal;
        self.vertical_alignment = vertical;
        self
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct GridStyle {
    pub columns: Vec<GridTrack>,
    pub rows: Vec<GridTrack>,
    pub column_gap: f32,
    pub row_gap: f32,
}

impl GridStyle {
    pub fn new(columns: impl IntoIterator<Item = GridTrack>) -> Self {
        Self {
            columns: columns.into_iter().collect(),
            rows: Vec::new(),
            column_gap: 0.0,
            row_gap: 0.0,
        }
    }

    pub fn rows(mut self, rows: impl IntoIterator<Item = GridTrack>) -> Self {
        self.rows = rows.into_iter().collect();
        self
    }

    pub fn gap(mut self, gap: f32) -> Self {
        let gap = finite_non_negative(gap);
        self.column_gap = gap;
        self.row_gap = gap;
        self
    }

    pub fn column_gap(mut self, gap: f32) -> Self {
        self.column_gap = finite_non_negative(gap);
        self
    }

    pub fn row_gap(mut self, gap: f32) -> Self {
        self.row_gap = finite_non_negative(gap);
        self
    }
}

impl Default for GridStyle {
    fn default() -> Self {
        Self::new([GridTrack::Auto])
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GridItemLayout {
    pub cell: Rect,
    pub rect: Rect,
}

#[derive(Debug, Clone, PartialEq)]
pub struct GridLayout {
    pub size: Size,
    pub column_offsets: Vec<f32>,
    pub column_widths: Vec<f32>,
    pub row_offsets: Vec<f32>,
    pub row_heights: Vec<f32>,
    pub items: Vec<GridItemLayout>,
}

/// Lay out `items` on `style`'s tracks, adding auto tracks for items placed
/// past them, up to [`MAX_GRID_TRACKS`] rows and columns.
pub fn grid_layout(style: &GridStyle, items: &[GridItem], constraints: Constraints) -> GridLayout {
    let placements = items
        .iter()
        .map(|item| within_track_limit(item.placement))
        .collect::<Vec<_>>();
    let column_count = placements
        .iter()
        .map(|placement| placement.column + placement.column_span)
        .max()
        .unwrap_or(0)
        .max(style.columns.len())
        .max(1);
    let row_count = placements
        .iter()
        .map(|placement| placement.row + placement.row_span)
        .max()
        .unwrap_or(0)
        .max(style.rows.len())
        .max(1);

    let columns = padded_tracks(&style.columns, column_count);
    let rows = padded_tracks(&style.rows, row_count);
    let (column_widths, width) = resolve_tracks(
        &columns,
        style.column_gap,
        constraints.min.width,
        constraints.max.width,
        items
            .iter()
            .zip(&placements)
            .map(|(item, placement)| AxisItem {
                start: placement.column,
                span: placement.column_span,
                minimum: item.minimum_size.width,
                natural: item.natural_size.width,
            }),
    );
    let (row_heights, height) = resolve_tracks(
        &rows,
        style.row_gap,
        constraints.min.height,
        constraints.max.height,
        items
            .iter()
            .zip(&placements)
            .map(|(item, placement)| AxisItem {
                start: placement.row,
                span: placement.row_span,
                minimum: item.minimum_size.height,
                natural: item.natural_size.height,
            }),
    );
    let column_offsets = track_offsets(&column_widths, style.column_gap);
    let row_offsets = track_offsets(&row_heights, style.row_gap);
    let item_layouts = items
        .iter()
        .zip(&placements)
        .map(|(item, placement)| {
            let cell = span_rect(
                *placement,
                &column_offsets,
                &column_widths,
                &row_offsets,
                &row_heights,
                style.column_gap,
                style.row_gap,
            );
            let child_size = Size::new(
                aligned_extent(
                    item.horizontal_alignment,
                    cell.width(),
                    item.natural_size.width,
                ),
                aligned_extent(
                    item.vertical_alignment,
                    cell.height(),
                    item.natural_size.height,
                ),
            );
            let origin = Point::new(
                aligned_origin(
                    item.horizontal_alignment,
                    cell.x(),
                    cell.width(),
                    child_size.width,
                ),
                aligned_origin(
                    item.vertical_alignment,
                    cell.y(),
                    cell.height(),
                    child_size.height,
                ),
            );
            GridItemLayout {
                cell,
                rect: Rect::from_origin_size(origin, child_size),
            }
        })
        .collect();

    GridLayout {
        size: constraints.clamp(Size::new(width, height)),
        column_offsets,
        column_widths,
        row_offsets,
        row_heights,
        items: item_layouts,
    }
}

/// The most rows or columns [`grid_layout`] adds for placed items.
pub const MAX_GRID_TRACKS: usize = 1 << 16;

/// `placement` pulled back within [`MAX_GRID_TRACKS`], with spans of at least
/// one track, so a stray index can't allocate tracks without end or overflow.
fn within_track_limit(placement: GridPlacement) -> GridPlacement {
    let row = placement.row.min(MAX_GRID_TRACKS - 1);
    let column = placement.column.min(MAX_GRID_TRACKS - 1);
    GridPlacement {
        row,
        column,
        row_span: placement.row_span.clamp(1, MAX_GRID_TRACKS - row),
        column_span: placement.column_span.clamp(1, MAX_GRID_TRACKS - column),
    }
}

#[derive(Clone, Copy)]
struct AxisItem {
    start: usize,
    span: usize,
    minimum: f32,
    natural: f32,
}

fn padded_tracks(tracks: &[GridTrack], count: usize) -> Vec<GridTrack> {
    let mut result = tracks.to_vec();
    result.resize(count, GridTrack::Auto);
    result
}

fn resolve_tracks(
    tracks: &[GridTrack],
    gap: f32,
    minimum_container: f32,
    maximum_container: f32,
    items: impl Iterator<Item = AxisItem> + Clone,
) -> (Vec<f32>, f32) {
    let gap = finite_non_negative(gap);
    let gap_total = gap * tracks.len().saturating_sub(1) as f32;
    let mut minimums = tracks.iter().map(track_minimum).collect::<Vec<_>>();
    let mut sizes = minimums.clone();

    for item in items.clone().filter(|item| item.span == 1) {
        if let Some(track) = tracks.get(item.start) {
            let minimum = finite_non_negative(item.minimum);
            let natural = finite_non_negative(item.natural).max(minimum);
            if !matches!(track, GridTrack::Fixed(_)) {
                minimums[item.start] = minimums[item.start].max(minimum);
                sizes[item.start] = sizes[item.start].max(track_natural_target(*track, natural));
            }
        }
    }
    for item in items.filter(|item| item.span > 1) {
        distribute_span_shortfall(
            tracks,
            &mut minimums,
            item.start,
            item.span,
            finite_non_negative(item.minimum),
            gap,
        );
        distribute_span_shortfall(
            tracks,
            &mut sizes,
            item.start,
            item.span,
            finite_non_negative(item.natural).max(finite_non_negative(item.minimum)),
            gap,
        );
    }
    for (size, minimum) in sizes.iter_mut().zip(&minimums) {
        *size = size.max(*minimum);
    }
    for ((track, minimum), size) in tracks.iter().zip(&mut minimums).zip(&mut sizes) {
        let cap = track_maximum(*track);
        *minimum = minimum.min(cap);
        *size = size.min(cap).max(*minimum);
    }

    let target = if maximum_container.is_finite() {
        maximum_container.max(0.0)
    } else {
        finite_non_negative(minimum_container).max(sum_tracks(&sizes, gap_total))
    };
    let available_tracks = (target - gap_total).max(0.0);
    let current_tracks = sizes.iter().sum::<f32>();
    if current_tracks < available_tracks {
        distribute_fractional_space(tracks, &mut sizes, available_tracks - current_tracks);
    } else if current_tracks > available_tracks {
        shrink_to_fit(&mut sizes, &minimums, current_tracks - available_tracks);
    }

    let natural = sum_tracks(&sizes, gap_total);
    let resolved =
        natural
            .max(finite_non_negative(minimum_container))
            .min(if maximum_container.is_finite() {
                maximum_container.max(0.0)
            } else {
                f32::INFINITY
            });
    (sizes, resolved)
}

fn track_minimum(track: &GridTrack) -> f32 {
    match *track {
        GridTrack::Fixed(points) => finite_non_negative(points),
        GridTrack::MinMax { min, .. } => finite_non_negative(min),
        GridTrack::Auto | GridTrack::Fraction(_) => 0.0,
    }
}

fn track_natural_target(track: GridTrack, natural: f32) -> f32 {
    match track {
        GridTrack::Fixed(points) => finite_non_negative(points),
        GridTrack::Auto | GridTrack::Fraction(_) => natural,
        GridTrack::MinMax { min, max } => match max {
            GridTrackMax::Auto | GridTrackMax::Fraction(_) => natural.max(min),
            GridTrackMax::Points(max) => natural.clamp(min.max(0.0), max.max(min).max(0.0)),
        },
    }
}

fn track_maximum(track: GridTrack) -> f32 {
    match track {
        GridTrack::Fixed(points) => finite_non_negative(points),
        GridTrack::MinMax {
            min,
            max: GridTrackMax::Points(max),
        } => finite_non_negative(max).max(finite_non_negative(min)),
        GridTrack::Auto
        | GridTrack::Fraction(_)
        | GridTrack::MinMax {
            max: GridTrackMax::Auto | GridTrackMax::Fraction(_),
            ..
        } => f32::INFINITY,
    }
}

fn fractional_weight(track: GridTrack) -> f32 {
    match track {
        GridTrack::Fraction(weight) => finite_non_negative(weight),
        GridTrack::MinMax {
            max: GridTrackMax::Fraction(weight),
            ..
        } => finite_non_negative(weight),
        _ => 0.0,
    }
}

fn distribute_fractional_space(tracks: &[GridTrack], sizes: &mut [f32], extra: f32) {
    let weight = tracks.iter().copied().map(fractional_weight).sum::<f32>();
    if weight <= 0.0 || extra <= 0.0 {
        return;
    }
    for (track, size) in tracks.iter().copied().zip(sizes) {
        let track_weight = fractional_weight(track);
        if track_weight > 0.0 {
            *size += extra * track_weight / weight;
        }
    }
}

fn shrink_to_fit(sizes: &mut [f32], minimums: &[f32], mut overflow: f32) {
    while overflow > 0.001 {
        let shrinkable = sizes
            .iter()
            .zip(minimums)
            .filter(|(size, minimum)| **size > **minimum + 0.001)
            .count();
        if shrinkable == 0 {
            break;
        }
        let share = overflow / shrinkable as f32;
        let mut consumed = 0.0;
        for (size, minimum) in sizes.iter_mut().zip(minimums) {
            let amount = (*size - *minimum).max(0.0).min(share);
            *size -= amount;
            consumed += amount;
        }
        if consumed <= 0.001 {
            break;
        }
        overflow -= consumed;
    }
}

fn distribute_span_shortfall(
    tracks: &[GridTrack],
    sizes: &mut [f32],
    start: usize,
    span: usize,
    target: f32,
    gap: f32,
) {
    let end = (start + span).min(sizes.len());
    if start >= end {
        return;
    }
    let current = sizes[start..end].iter().sum::<f32>() + gap * (end - start - 1) as f32;
    let shortfall = (target - current).max(0.0);
    if shortfall <= 0.0 {
        return;
    }
    let eligible = (start..end)
        .filter(|index| !matches!(tracks[*index], GridTrack::Fixed(_)))
        .collect::<Vec<_>>();
    if eligible.is_empty() {
        return;
    }
    let share = shortfall / eligible.len() as f32;
    for index in eligible {
        sizes[index] += share;
    }
}

fn track_offsets(sizes: &[f32], gap: f32) -> Vec<f32> {
    let mut offset = 0.0;
    sizes
        .iter()
        .map(|size| {
            let current = offset;
            offset += *size + gap;
            current
        })
        .collect()
}

#[allow(clippy::too_many_arguments)]
fn span_rect(
    placement: GridPlacement,
    column_offsets: &[f32],
    column_widths: &[f32],
    row_offsets: &[f32],
    row_heights: &[f32],
    column_gap: f32,
    row_gap: f32,
) -> Rect {
    let column = placement.column.min(column_widths.len().saturating_sub(1));
    let row = placement.row.min(row_heights.len().saturating_sub(1));
    let column_end = (column + placement.column_span.max(1)).min(column_widths.len());
    let row_end = (row + placement.row_span.max(1)).min(row_heights.len());
    let width = column_widths[column..column_end].iter().sum::<f32>()
        + column_gap * column_end.saturating_sub(column + 1) as f32;
    let height = row_heights[row..row_end].iter().sum::<f32>()
        + row_gap * row_end.saturating_sub(row + 1) as f32;
    Rect::new(column_offsets[column], row_offsets[row], width, height)
}

fn aligned_extent(alignment: Alignment, available: f32, natural: f32) -> f32 {
    if alignment == Alignment::Stretch {
        available
    } else {
        finite_non_negative(natural).min(available)
    }
}

fn aligned_origin(alignment: Alignment, start: f32, available: f32, child_extent: f32) -> f32 {
    match alignment {
        Alignment::Start | Alignment::Stretch => start,
        Alignment::Center => start + (available - child_extent).max(0.0) * 0.5,
        Alignment::End => start + (available - child_extent).max(0.0),
    }
}

fn sum_tracks(sizes: &[f32], gap_total: f32) -> f32 {
    sizes.iter().sum::<f32>() + gap_total
}

fn finite_non_negative(value: f32) -> f32 {
    if value.is_finite() {
        value.max(0.0)
    } else {
        0.0
    }
}

fn sanitize_size(size: Size) -> Size {
    Size::new(
        finite_non_negative(size.width),
        finite_non_negative(size.height),
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn far_out_placements_stay_within_the_track_limit() {
        let style = GridStyle::new([GridTrack::Auto]);
        let items = [
            GridItem::new(GridPlacement::new(1_000_000_000, 3), Size::new(10.0, 10.0)),
            GridItem::new(
                GridPlacement::new(usize::MAX, usize::MAX).span(usize::MAX, usize::MAX),
                Size::new(10.0, 10.0),
            ),
        ];
        let layout = grid_layout(
            &style,
            &items,
            Constraints::new(Size::ZERO, Size::new(400.0, 400.0)),
        );
        assert!(layout.column_widths.len() <= MAX_GRID_TRACKS);
        assert!(layout.row_heights.len() <= MAX_GRID_TRACKS);
        assert_eq!(layout.items.len(), 2);
    }

    #[test]
    fn fraction_tracks_share_remaining_width_after_auto_content() {
        let style = GridStyle::new([
            GridTrack::Auto,
            GridTrack::Fraction(1.0),
            GridTrack::Fraction(2.0),
        ])
        .column_gap(10.0);
        let items = [GridItem::new(
            GridPlacement::new(0, 0),
            Size::new(80.0, 20.0),
        )];
        let layout = grid_layout(&style, &items, Constraints::tight(Size::new(380.0, 40.0)));

        assert_eq!(layout.column_widths[0], 80.0);
        assert!((layout.column_widths[1] - 93.333_336).abs() < 0.001);
        assert!((layout.column_widths[2] - 186.666_67).abs() < 0.001);
    }

    #[test]
    fn spanning_item_grows_auto_tracks_and_alignment_uses_cell() {
        let style = GridStyle::new([GridTrack::Auto, GridTrack::Auto]).gap(8.0);
        let item = GridItem::new(GridPlacement::new(0, 0).span(1, 2), Size::new(208.0, 24.0))
            .minimum_size(Size::new(120.0, 20.0))
            .align(Alignment::Center, Alignment::Center);
        let layout = grid_layout(
            &style,
            &[item],
            Constraints::new(Size::ZERO, Size::new(240.0, 80.0)),
        );

        assert_eq!(layout.column_widths, vec![100.0, 100.0]);
        assert_eq!(layout.items[0].cell.width(), 208.0);
        assert_eq!(layout.items[0].rect.width(), 208.0);
    }
}