1
//! TV's Tile and Cascade layouts (`tdesktop.cpp`): the rects of `count` windows over an area.
2

            
3
use ratatui::layout::Rect;
4

            
5
use super::MIN_SIZE;
6

            
7
/// The near-square `(columns, rows)` grid of `count` cells, rows first as TV's default does.
8
45
fn grid(count: u32) -> (u32, u32) {
9
45
    let mut side = count.isqrt();
10
45
    if !count.is_multiple_of(side) && count.is_multiple_of(side + 1) {
11
        side += 1;
12
45
    }
13
45
    side = side.max(count / side);
14
45
    (count / side, side)
15
45
}
16

            
17
/// The cell edge at `pos` of `num` along `len` cells from `lo`.
18
768
fn divide(lo: u16, len: u16, num: u32, pos: u32) -> u16 {
19
768
    let offset = u32::from(len) * pos / num;
20
768
    lo.saturating_add(u16::try_from(offset).unwrap_or(u16::MAX))
21
768
}
22

            
23
/// Where cell `pos` of a `cols` by `rows` grid lies in `area`; the last `count % cols` columns
24
/// have one row more.
25
192
fn cell(pos: u32, grid: (u32, u32), left_over: u32, area: Rect) -> Rect {
26
192
    let (cols, rows) = grid;
27
192
    let full = (cols - left_over) * rows;
28
192
    let (col, row, column_rows) = match pos < full {
29
127
        true => (pos / rows, pos % rows, rows),
30
65
        false => (
31
65
            (pos - full) / (rows + 1) + cols - left_over,
32
65
            (pos - full) % (rows + 1),
33
65
            rows + 1,
34
65
        ),
35
    };
36
192
    let left = divide(area.x, area.width, cols, col);
37
192
    let right = divide(area.x, area.width, cols, col + 1);
38
192
    let top = divide(area.y, area.height, column_rows, row);
39
192
    let bottom = divide(area.y, area.height, column_rows, row + 1);
40
192
    let width = (right - left).max(MIN_SIZE.0.min(area.width));
41
192
    let height = (bottom - top).max(MIN_SIZE.1.min(area.height));
42
192
    Rect::new(left, top, width, height)
43
192
}
44

            
45
/// The rects of `count` tiled windows, indexed by position: TV counts the cells up from the
46
/// rearmost window, so the front one takes the last. `None` when
47
/// a cell would have no room at all, TV's "too many windows".
48
36
pub(super) fn tile(count: usize, area: Rect) -> Option<Vec<Rect>> {
49
36
    let Ok(count) = u32::try_from(count) else {
50
        return None;
51
    };
52
36
    if count == 0 {
53
1
        return Some(Vec::new());
54
35
    }
55
35
    let grid = grid(count);
56
35
    if u32::from(area.width) < grid.0 || u32::from(area.height) < grid.1 {
57
6
        return None;
58
29
    }
59
29
    let left_over = count % grid.0;
60
    Some(
61
29
        (0..count)
62
192
            .map(|pos| cell(pos, grid, left_over, area))
63
            .collect(),
64
    )
65
36
}
66

            
67
/// The rects of `count` cascaded windows, back to front: each is one cell further down and right
68
/// than the one behind it, all reaching the area's bottom-right corner. `None` when the minimum
69
/// does not fit beside `count` steps, as in TV.
70
21
pub(super) fn cascade(count: usize, area: Rect) -> Option<Vec<Rect>> {
71
21
    let total = u16::try_from(count).ok()?;
72
21
    if MIN_SIZE.0 > area.width.saturating_sub(total)
73
19
        || MIN_SIZE.1 > area.height.saturating_sub(total)
74
    {
75
6
        return None;
76
15
    }
77
    Some(
78
15
        (0..count)
79
112
            .filter_map(|step| u16::try_from(step).ok())
80
112
            .map(|step| {
81
112
                Rect::new(
82
112
                    area.x.saturating_add(step),
83
112
                    area.y.saturating_add(step),
84
112
                    area.width - step,
85
112
                    area.height - step,
86
112
                )
87
112
            })
88
            .collect(),
89
    )
90
21
}
91

            
92
#[cfg(test)]
93
mod tests;