1
//! TV's `moveGrow`: how a window moves and resizes, from the keyboard or the mouse, within TV's limits.
2

            
3
use ratatui::layout::Rect;
4

            
5
use super::place::{Place, cell, within};
6
use super::{Desktop, MIN_SIZE};
7

            
8
/// One step of the size/move mode, in cells.
9
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10
pub enum Change {
11
    /// Moves the origin by columns and rows.
12
    Move(i32, i32),
13
    /// Grows or shrinks the bottom-right corner by columns and rows.
14
    Grow(i32, i32),
15
    /// Puts the origin at the column and row, as a mouse drag of the title places it.
16
    MoveTo(i32, i32),
17
    /// Sets the width and height, as a mouse drag of the corner sizes it.
18
    SizeTo(i32, i32),
19
    /// Puts the left edge at the desktop's.
20
    Left,
21
    /// Puts the right edge at the desktop's.
22
    Right,
23
    /// Puts the top edge at the desktop's.
24
    Top,
25
    /// Puts the bottom edge at the desktop's.
26
    Bottom,
27
}
28

            
29
impl<Id: Copy + Eq> Desktop<Id> {
30
    /// Changes the place of `id` by `change` within `area`, as TV's `moveGrow` limits it: the
31
    /// size stays between 16 by 6 and the desktop's, the origin may hang off the left, right and
32
    /// bottom with one column and row inside, and the top edge never rises above the desktop. A
33
    /// zoomed window is changed from the desktop it fills and stops being zoomed. False when `id`
34
    /// is not open. A window without `grow` ignores `Grow` and `SizeTo` and keeps its stored size.
35
102
    pub fn move_grow(&mut self, id: Id, area: Rect, change: Change) -> bool {
36
102
        let Some(index) = self.position(id) else {
37
1
            return false;
38
        };
39
101
        let window = &mut self.windows[index];
40
101
        if !window.caps.grow && matches!(change, Change::Grow(..) | Change::SizeTo(..)) {
41
3
            return true;
42
98
        }
43
98
        let from = match window.zoomed {
44
8
            true => Place::from(area),
45
90
            false => window.place.limited(area),
46
        };
47
98
        window.set_place(changed(from, area, change, MIN_SIZE));
48
98
        window.zoomed = false;
49
98
        true
50
102
    }
51
}
52

            
53
/// `from` with `change` applied: the size within `min` (the area where that is smaller) and the
54
/// area's size, then the origin within the limits.
55
#[must_use]
56
539
pub fn changed(from: Place, area: Rect, change: Change, min: (u16, u16)) -> Place {
57
539
    let (mut x, mut y) = (from.x, from.y);
58
539
    let (mut width, mut height) = (i32::from(from.width), i32::from(from.height));
59
539
    let (left, top) = (i32::from(area.x), i32::from(area.y));
60
539
    let (right, bottom) = (i32::from(area.right()), i32::from(area.bottom()));
61
539
    match change {
62
365
        Change::Move(columns, rows) => (x, y) = (x + columns, y + rows),
63
63
        Change::Grow(columns, rows) => (width, height) = (width + columns, height + rows),
64
56
        Change::MoveTo(column, row) => (x, y) = (column, row),
65
36
        Change::SizeTo(columns, rows) => (width, height) = (columns, rows),
66
5
        Change::Left => x = left,
67
4
        Change::Top => y = top,
68
10
        Change::Right | Change::Bottom => {}
69
    }
70
539
    width = within(
71
539
        width,
72
539
        i32::from(min.0.min(area.width)),
73
539
        i32::from(area.width),
74
539
    );
75
539
    height = within(
76
539
        height,
77
539
        i32::from(min.1.min(area.height)),
78
539
        i32::from(area.height),
79
539
    );
80
539
    match change {
81
5
        Change::Right => x = right - width,
82
5
        Change::Bottom => y = bottom - height,
83
529
        _ => {}
84
    }
85
539
    Place {
86
539
        x,
87
539
        y,
88
539
        width: cell(width),
89
539
        height: cell(height),
90
539
    }
91
539
    .limited(area)
92
539
}
93

            
94
#[cfg(test)]
95
mod tests;