cranpose-ui 0.13.0

Composable widgets, text, layouts and input for Cranpose apps
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
621
622
623
624
//! The measure policy of `Grid`, after Compose's `GridMeasurePolicy`:
//! children get their cells, the tracks get their sizes, and children
//! measure in their cells and align there.

use cranpose_ui_layout::{
    Constraints, MeasurePolicy, MeasureResult, MeasureScope, Placement, round_to_px,
};
use smallvec::SmallVec;

use crate::{
    layout::core::{Alignment, Measurable},
    widgets::grid::{GridConfig, GridFlow, GridItem, GridTrackSize, MAX_GRID_INDEX},
};

const MAX_INDEX: usize = MAX_GRID_INDEX as usize;

/// Lays out the children of a `Grid` by its config.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct GridMeasurePolicy {
    pub(crate) config: GridConfig,
    pub(crate) layout_direction: crate::LayoutDirection,
    /// Device pixels per dp, which track sizes and gaps round to.
    pub(crate) density: f32,
}

/// A child's cell, from 0, and how it aligns there.
#[derive(Clone, Copy, Debug)]
struct Cell {
    child: usize,
    row: usize,
    column: usize,
    row_span: usize,
    column_span: usize,
    alignment: Alignment,
}

impl Cell {
    fn start(&self, axis: TrackAxis) -> usize {
        match axis {
            TrackAxis::Columns => self.column,
            TrackAxis::Rows => self.row,
        }
    }

    fn span(&self, axis: TrackAxis) -> usize {
        match axis {
            TrackAxis::Columns => self.column_span,
            TrackAxis::Rows => self.row_span,
        }
    }

    fn overlaps(&self, row: usize, column: usize, row_span: usize, column_span: usize) -> bool {
        row < self.row + self.row_span
            && self.row < row + row_span
            && column < self.column + self.column_span
            && self.column < column + column_span
    }
}

type Cells = SmallVec<[Cell; 16]>;
type Sizes = SmallVec<[f32; 8]>;

#[derive(Clone, Copy, PartialEq, Eq)]
enum TrackAxis {
    Columns,
    Rows,
}

/// The sizes of one axis' tracks and where each starts.
struct Tracks {
    sizes: Sizes,
    offsets: Sizes,
}

impl Tracks {
    fn new(sizes: Sizes, gap: f32) -> Self {
        let mut start = 0.0;
        let offsets = sizes
            .iter()
            .map(|size| {
                let offset = start;
                start += size + gap;
                offset
            })
            .collect();
        Self { sizes, offsets }
    }

    /// The length of all tracks with the gaps between them.
    fn total(&self) -> f32 {
        self.spanned(0, self.sizes.len())
    }

    /// The length of `span` tracks from `start`, with the gaps between them.
    fn spanned(&self, start: usize, span: usize) -> f32 {
        let end = (start + span).min(self.sizes.len());
        if start >= end {
            return 0.0;
        }
        self.offsets[end - 1] + self.sizes[end - 1] - self.offsets[start]
    }

    /// Where track `index` starts.
    fn offset(&self, index: usize) -> f32 {
        self.offsets.get(index).copied().unwrap_or_default()
    }
}

/// The grid's cells and the track counts they need, explicit and added.
struct Placed {
    cells: Cells,
    columns: usize,
    rows: usize,
}

impl GridMeasurePolicy {
    fn spec(&self, axis: TrackAxis, index: usize) -> GridTrackSize {
        let specs = match axis {
            TrackAxis::Columns => &self.config.columns,
            TrackAxis::Rows => &self.config.rows,
        };
        specs.get(index).copied().unwrap_or(GridTrackSize::Auto)
    }

    /// Gives every child a cell: named areas and explicit cells first, then
    /// the next free cell along the flow. Compose's `resolveGridItemIndices`.
    fn place_cells(&self, measurables: &[Box<dyn Measurable>]) -> Placed {
        let explicit_columns = self.config.columns.len();
        let explicit_rows = self.config.rows.len();
        let flow = self.config.flow;
        let mut cells = Cells::new();
        let (mut rows, mut columns) = (explicit_rows, explicit_columns);
        let (mut cursor_row, mut cursor_column) = (0, 0);

        for (child, measurable) in measurables.iter().enumerate() {
            let item = measurable
                .parent_data()
                .layout_data::<GridItem>()
                .copied()
                .unwrap_or_default();
            let (row, column, mut row_span, mut column_span) = self.requested(&item);
            if row == 0 && explicit_rows > 0 && flow == GridFlow::Column {
                row_span = row_span.min(explicit_rows);
            }
            if column == 0 && explicit_columns > 0 && flow == GridFlow::Row {
                column_span = column_span.min(explicit_columns);
            }
            let requested_row = zero_based(row, explicit_rows);
            let requested_column = zero_based(column, explicit_columns);
            let occupied = |row: usize, column: usize| {
                row + row_span > MAX_INDEX
                    || column + column_span > MAX_INDEX
                    || cells
                        .iter()
                        .any(|cell| cell.overlaps(row, column, row_span, column_span))
            };
            let (row, column) = match (requested_row, requested_column) {
                (Some(row), Some(column)) => (row, column),
                (Some(row), None) => {
                    let from = if flow == GridFlow::Row && row == cursor_row {
                        cursor_column
                    } else {
                        0
                    };
                    (row, first_free(from, |column| occupied(row, column)))
                }
                (None, Some(column)) => {
                    let from = if flow == GridFlow::Column && column == cursor_column {
                        cursor_row
                    } else {
                        0
                    };
                    (first_free(from, |row| occupied(row, column)), column)
                }
                (None, None) => {
                    let wrap_at = match flow {
                        GridFlow::Row => (explicit_columns, column_span),
                        GridFlow::Column => (explicit_rows, row_span),
                    };
                    let (line, along) = match flow {
                        GridFlow::Row => (cursor_row, cursor_column),
                        GridFlow::Column => (cursor_column, cursor_row),
                    };
                    let (line, along) =
                        next_free_slot(line, along, wrap_at, |line, along| match flow {
                            GridFlow::Row => occupied(line, along),
                            GridFlow::Column => occupied(along, line),
                        });
                    let (row, column) = match flow {
                        GridFlow::Row => (line, along),
                        GridFlow::Column => (along, line),
                    };
                    (cursor_row, cursor_column) = match flow {
                        GridFlow::Row => (row, column + column_span),
                        GridFlow::Column => (row + row_span, column),
                    };
                    (row, column)
                }
            };
            rows = rows.max(row + row_span);
            columns = columns.max(column + column_span);
            cells.push(Cell {
                child,
                row,
                column,
                row_span,
                column_span,
                alignment: item.alignment,
            });
        }
        // Row by row from the top start, which is also the order children
        // measure, place and draw in; a stable sort keeps children of one
        // cell in composition order.
        cells.sort_by_key(|cell| (cell.row, cell.column));
        Placed {
            cells,
            columns,
            rows,
        }
    }

    /// The row, column and spans a child asks for, from its area when it
    /// names a known one.
    fn requested(&self, item: &GridItem) -> (i32, i32, usize, usize) {
        let (row, column, row_span, column_span) = match item.area {
            Some(name) => self
                .config
                .areas
                .iter()
                .find(|area| area.name == name)
                .map_or((0, 0, 1, 1), |area| {
                    (area.row, area.column, area.row_span, area.column_span)
                }),
            None => (item.row, item.column, item.row_span, item.column_span),
        };
        (row, column, row_span as usize, column_span as usize)
    }

    /// The sizes of one axis' tracks with `available` room on that axis.
    /// For rows, `columns` holds the column sizes the children's heights
    /// depend on. Compose's `calculateColumnWidths`/`calculateRowHeights`.
    fn size_tracks(
        &self,
        axis: TrackAxis,
        placed: &Placed,
        measurables: &[Box<dyn Measurable>],
        columns: Option<&Tracks>,
        available: f32,
    ) -> Tracks {
        let density = self.density;
        let count = match axis {
            TrackAxis::Columns => placed.columns,
            TrackAxis::Rows => placed.rows,
        };
        let gap = round_to_px(
            match axis {
                TrackAxis::Columns => self.config.column_gap,
                TrackAxis::Rows => self.config.row_gap,
            },
            density,
        );
        let room = if available.is_finite() {
            (available - gap * count.saturating_sub(1) as f32).max(0.0)
        } else {
            f32::INFINITY
        };
        let intrinsic = |cell: &Cell, min: bool| {
            let measurable = measurables[cell.child].as_ref();
            match (axis, columns) {
                (TrackAxis::Rows, Some(columns)) => {
                    let width = columns.spanned(cell.column, cell.column_span);
                    if min {
                        measurable.min_intrinsic_height(width)
                    } else {
                        measurable.max_intrinsic_height(width)
                    }
                }
                _ if min => measurable.min_intrinsic_width(f32::INFINITY),
                _ => measurable.max_intrinsic_width(f32::INFINITY),
            }
        };

        // The largest smallest and largest unbounded content size of the
        // children in one track each, for the tracks that size to content.
        let mut content_min: Sizes = SmallVec::from_elem(0.0, count);
        let mut content_max: Sizes = SmallVec::from_elem(0.0, count);
        for cell in placed.cells.iter().filter(|cell| cell.span(axis) == 1) {
            let index = cell.start(axis);
            let (min, max) = content_queries(self.spec(axis, index), room.is_finite());
            if min {
                content_min[index] = content_min[index].max(intrinsic(cell, true));
            }
            if max {
                content_max[index] = content_max[index].max(intrinsic(cell, false));
            }
        }

        // Pass 1: base sizes.
        let mut sizes: Sizes = SmallVec::with_capacity(count);
        let mut total_flex = 0.0_f32;
        for index in 0..count {
            let size = match self.spec(axis, index) {
                GridTrackSize::Fixed(size) => round_to_px(size.max(0.0), density),
                GridTrackSize::Percentage(fraction) if room.is_finite() => {
                    round_to_px(fraction.max(0.0) * room, density)
                }
                GridTrackSize::Flex(weight) => {
                    total_flex += flex_weight(weight.0);
                    content_min[index]
                }
                GridTrackSize::MinMax(min, weight) => {
                    total_flex += flex_weight(weight.0);
                    round_to_px(min.max(0.0), density)
                }
                GridTrackSize::MinContent => content_min[index],
                GridTrackSize::Auto if room.is_finite() => content_min[index],
                GridTrackSize::Percentage(_) | GridTrackSize::MaxContent | GridTrackSize::Auto => {
                    content_max[index]
                }
            };
            sizes.push(size);
        }

        // Pass 1.5: tracks under a spanning child grow to fit it.
        for cell in placed.cells.iter().filter(|cell| cell.span(axis) > 1) {
            let start = cell.start(axis);
            let end = (start + cell.span(axis)).min(count);
            let current = sizes[start..end].iter().sum::<f32>()
                + gap * cell.span(axis).saturating_sub(1) as f32;
            let required = intrinsic(cell, false);
            let growable = (start..end)
                .filter(|&index| grows_for_spans(self.spec(axis, index)))
                .count();
            if growable == 0 {
                continue;
            }
            // Whole device pixels, rounded up so the child is not narrower
            // than it asked, split as evenly as they go.
            let deficit = ((required - current) * density - 1e-3).ceil();
            if deficit <= 0.0 {
                continue;
            }
            let share = (deficit / growable as f32).floor();
            let mut remainder = deficit - share * growable as f32;
            for index in (start..end).filter(|&index| grows_for_spans(self.spec(axis, index))) {
                let extra = if remainder > 0.0 { 1.0 } else { 0.0 };
                remainder -= extra;
                sizes[index] += (share + extra) / density;
            }
        }

        if room.is_finite() {
            // Pass 1.8: auto tracks grow toward their content's unbounded size.
            grow_auto_tracks(
                &mut sizes,
                (0..count).filter(|&index| self.spec(axis, index) == GridTrackSize::Auto),
                &content_max,
                room,
                density,
            );
            // Pass 2: flexible tracks share what is left.
            let left = (room - sizes.iter().sum::<f32>()).max(0.0);
            if total_flex > 0.0 && left > 0.0 {
                let mut accumulated = 0.0_f32;
                let mut handed = 0.0_f32;
                for (index, size) in sizes.iter_mut().enumerate() {
                    let weight = match self.spec(axis, index) {
                        GridTrackSize::Flex(weight) | GridTrackSize::MinMax(_, weight) => {
                            flex_weight(weight.0)
                        }
                        _ => 0.0,
                    };
                    if weight <= 0.0 {
                        continue;
                    }
                    accumulated += weight;
                    // The last share ends exactly at the room's end.
                    let target = if accumulated >= total_flex {
                        left
                    } else {
                        round_to_px(accumulated / total_flex * left, density)
                    };
                    let share = (target - handed).max(0.0);
                    *size += share;
                    handed += share;
                }
            }
        }
        Tracks::new(sizes, gap)
    }

    /// The column and row sizes under `constraints`.
    fn size_grid(
        &self,
        placed: &Placed,
        measurables: &[Box<dyn Measurable>],
        constraints: Constraints,
    ) -> (Tracks, Tracks) {
        let columns = self.size_tracks(
            TrackAxis::Columns,
            placed,
            measurables,
            None,
            constraints.max_width,
        );
        let rows = self.size_tracks(
            TrackAxis::Rows,
            placed,
            measurables,
            Some(&columns),
            constraints.max_height,
        );
        (columns, rows)
    }

    /// The grid's width with no bound on it, or its height when `width`
    /// bounds it: Compose measures a grid's intrinsic sizes this way, the
    /// smallest and the largest alike.
    fn intrinsic_width(&self, measurables: &[Box<dyn Measurable>]) -> f32 {
        let placed = self.place_cells(measurables);
        self.size_tracks(
            TrackAxis::Columns,
            &placed,
            measurables,
            None,
            f32::INFINITY,
        )
        .total()
    }

    fn intrinsic_height(&self, measurables: &[Box<dyn Measurable>], width: f32) -> f32 {
        let placed = self.place_cells(measurables);
        let constraints = Constraints {
            min_width: 0.0,
            max_width: width,
            min_height: 0.0,
            max_height: f32::INFINITY,
        };
        let (_, rows) = self.size_grid(&placed, measurables, constraints);
        rows.total()
    }
}

/// Whether a track of `spec` needs its children's smallest content size,
/// and their unbounded one.
fn content_queries(spec: GridTrackSize, bounded: bool) -> (bool, bool) {
    match spec {
        GridTrackSize::Flex(_) | GridTrackSize::MinContent => (true, false),
        GridTrackSize::Auto if bounded => (true, true),
        GridTrackSize::Auto | GridTrackSize::MaxContent => (false, true),
        GridTrackSize::Percentage(_) => (false, !bounded),
        GridTrackSize::Fixed(_) | GridTrackSize::MinMax(..) => (false, false),
    }
}

/// Whether a track grows for children that span it; fixed and percentage
/// tracks keep their size.
fn grows_for_spans(spec: GridTrackSize) -> bool {
    !matches!(spec, GridTrackSize::Fixed(_) | GridTrackSize::Percentage(_))
}

fn flex_weight(weight: f32) -> f32 {
    if weight.is_finite() {
        weight.max(0.0)
    } else {
        0.0
    }
}

/// The first index from `from` that `occupied` leaves free; 0 when none
/// below the largest index is.
fn first_free(from: usize, occupied: impl Fn(usize) -> bool) -> usize {
    (from..MAX_INDEX)
        .find(|&index| !occupied(index))
        .unwrap_or(0)
}

/// The first free slot from `line` and `along` for a child that fills
/// lines one after another: along a line, wrapping to the next line when
/// the child no longer fits in `wrap_at.0` explicit tracks of its span
/// `wrap_at.1`. A child wider than all of them stays at a line's start
/// and adds tracks. `(0, 0)` when no slot is free.
fn next_free_slot(
    mut line: usize,
    mut along: usize,
    (explicit, span): (usize, usize),
    occupied: impl Fn(usize, usize) -> bool,
) -> (usize, usize) {
    while line < MAX_INDEX && along < MAX_INDEX {
        if explicit > 0 && along + span > explicit && along > 0 {
            along = 0;
            line += 1;
            continue;
        }
        if !occupied(line, along) {
            return (line, along);
        }
        along += 1;
        if along > MAX_INDEX {
            along = 0;
            line += 1;
        }
    }
    (0, 0)
}

/// Grows `auto` tracks from their smallest content size toward `content_max`
/// with the room their tracks leave, in proportion to how far each can
/// grow. Compose's `expandAutoTracks`.
fn grow_auto_tracks(
    sizes: &mut Sizes,
    auto: impl Iterator<Item = usize> + Clone,
    content_max: &Sizes,
    room: f32,
    density: f32,
) {
    let left = room - sizes.iter().sum::<f32>();
    if left <= 0.0 {
        return;
    }
    let potential = |index: usize| (content_max[index] - sizes[index]).max(0.0);
    let total: f32 = auto.clone().map(potential).sum();
    if total <= 0.0 {
        return;
    }
    let grow: SmallVec<[(usize, f32); 8]> = auto
        .map(|index| {
            let potential = potential(index);
            let extra = if left >= total {
                potential
            } else {
                round_to_px(potential / total * left, density)
            };
            (index, extra)
        })
        .collect();
    for (index, extra) in grow {
        sizes[index] += extra;
    }
}

/// A 1-based track index, negative from the last explicit track, as a
/// 0-based one. `None` when unspecified, or before the first track.
fn zero_based(index: i32, explicit: usize) -> Option<usize> {
    match index {
        0 => None,
        index if index > 0 => Some(index as usize - 1),
        index => explicit.checked_sub(index.unsigned_abs() as usize),
    }
}

impl MeasurePolicy for GridMeasurePolicy {
    fn measure(
        &self,
        scope: &dyn MeasureScope,
        measurables: &[Box<dyn Measurable>],
        constraints: Constraints,
    ) -> MeasureResult {
        let mut placements = Vec::new();
        let measurement = self.measure_into(scope, measurables, constraints, &mut placements);
        MeasureResult::new(measurement, placements)
    }

    fn measure_into(
        &self,
        _scope: &dyn MeasureScope,
        measurables: &[Box<dyn Measurable>],
        constraints: Constraints,
        placements: &mut Vec<Placement>,
    ) -> cranpose_ui_layout::Measurement {
        placements.clear();
        let density = self.density;
        let placed = self.place_cells(measurables);
        let (columns, rows) = self.size_grid(&placed, measurables, constraints);
        let (width, height) = constraints.constrain(columns.total(), rows.total());

        placements.reserve(placed.cells.len());
        for cell in &placed.cells {
            let cell_width = columns.spanned(cell.column, cell.column_span);
            let cell_height = rows.spanned(cell.row, cell.row_span);
            let placeable = measurables[cell.child].measure(Constraints {
                min_width: 0.0,
                max_width: cell_width,
                min_height: 0.0,
                max_height: cell_height,
            });
            let inside_x = cell
                .alignment
                .horizontal
                .align(cell_width, placeable.width(), density);
            let inside_x = self
                .layout_direction
                .place_x(inside_x, cell_width, placeable.width());
            let inside_y = cell
                .alignment
                .vertical
                .align(cell_height, placeable.height(), density);
            let cell_x =
                self.layout_direction
                    .place_x(columns.offset(cell.column), width, cell_width);
            let x = cell_x + inside_x;
            let y = rows.offset(cell.row) + inside_y;
            placeable.place(x, y);
            placements.push(Placement::new(placeable.node_id(), x, y, 0));
        }
        crate::modifier::Size { width, height }.into()
    }

    fn min_intrinsic_width(&self, measurables: &[Box<dyn Measurable>], _height: f32) -> f32 {
        self.intrinsic_width(measurables)
    }

    fn max_intrinsic_width(&self, measurables: &[Box<dyn Measurable>], _height: f32) -> f32 {
        self.intrinsic_width(measurables)
    }

    fn min_intrinsic_height(&self, measurables: &[Box<dyn Measurable>], width: f32) -> f32 {
        self.intrinsic_height(measurables, width)
    }

    fn max_intrinsic_height(&self, measurables: &[Box<dyn Measurable>], width: f32) -> f32 {
        self.intrinsic_height(measurables, width)
    }
}