Skip to main content

tvui/desktop/
drag.rs

1//! TV's `moveGrow`: how a window moves and resizes, from the keyboard or the mouse, within TV's limits.
2
3use ratatui::layout::Rect;
4
5use super::place::{Place, cell, within};
6use super::{Desktop, MIN_SIZE};
7
8/// One step of the size/move mode, in cells.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub 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
29impl<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    pub fn move_grow(&mut self, id: Id, area: Rect, change: Change) -> bool {
36        let Some(index) = self.position(id) else {
37            return false;
38        };
39        let window = &mut self.windows[index];
40        if !window.caps.grow && matches!(change, Change::Grow(..) | Change::SizeTo(..)) {
41            return true;
42        }
43        let from = match window.zoomed {
44            true => Place::from(area),
45            false => window.place.limited(area),
46        };
47        window.set_place(changed(from, area, change, MIN_SIZE));
48        window.zoomed = false;
49        true
50    }
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]
56pub fn changed(from: Place, area: Rect, change: Change, min: (u16, u16)) -> Place {
57    let (mut x, mut y) = (from.x, from.y);
58    let (mut width, mut height) = (i32::from(from.width), i32::from(from.height));
59    let (left, top) = (i32::from(area.x), i32::from(area.y));
60    let (right, bottom) = (i32::from(area.right()), i32::from(area.bottom()));
61    match change {
62        Change::Move(columns, rows) => (x, y) = (x + columns, y + rows),
63        Change::Grow(columns, rows) => (width, height) = (width + columns, height + rows),
64        Change::MoveTo(column, row) => (x, y) = (column, row),
65        Change::SizeTo(columns, rows) => (width, height) = (columns, rows),
66        Change::Left => x = left,
67        Change::Top => y = top,
68        Change::Right | Change::Bottom => {}
69    }
70    width = within(
71        width,
72        i32::from(min.0.min(area.width)),
73        i32::from(area.width),
74    );
75    height = within(
76        height,
77        i32::from(min.1.min(area.height)),
78        i32::from(area.height),
79    );
80    match change {
81        Change::Right => x = right - width,
82        Change::Bottom => y = bottom - height,
83        _ => {}
84    }
85    Place {
86        x,
87        y,
88        width: cell(width),
89        height: cell(height),
90    }
91    .limited(area)
92}
93
94#[cfg(test)]
95mod tests;