Lines
100 %
Functions
83.33 %
Branches
//! TV's `moveGrow`: how a window moves and resizes, from the keyboard or the mouse, within TV's limits.
use ratatui::layout::Rect;
use super::place::{Place, cell, within};
use super::{Desktop, MIN_SIZE};
/// One step of the size/move mode, in cells.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Change {
/// Moves the origin by columns and rows.
Move(i32, i32),
/// Grows or shrinks the bottom-right corner by columns and rows.
Grow(i32, i32),
/// Puts the origin at the column and row, as a mouse drag of the title places it.
MoveTo(i32, i32),
/// Sets the width and height, as a mouse drag of the corner sizes it.
SizeTo(i32, i32),
/// Puts the left edge at the desktop's.
Left,
/// Puts the right edge at the desktop's.
Right,
/// Puts the top edge at the desktop's.
Top,
/// Puts the bottom edge at the desktop's.
Bottom,
}
impl<Id: Copy + Eq> Desktop<Id> {
/// Changes the place of `id` by `change` within `area`, as TV's `moveGrow` limits it: the
/// size stays between 16 by 6 and the desktop's, the origin may hang off the left, right and
/// bottom with one column and row inside, and the top edge never rises above the desktop. A
/// zoomed window is changed from the desktop it fills and stops being zoomed. False when `id`
/// is not open. A window without `grow` ignores `Grow` and `SizeTo` and keeps its stored size.
pub fn move_grow(&mut self, id: Id, area: Rect, change: Change) -> bool {
let Some(index) = self.position(id) else {
return false;
};
let window = &mut self.windows[index];
if !window.caps.grow && matches!(change, Change::Grow(..) | Change::SizeTo(..)) {
return true;
let from = match window.zoomed {
true => Place::from(area),
false => window.place.limited(area),
window.set_place(changed(from, area, change, MIN_SIZE));
window.zoomed = false;
true
/// `from` with `change` applied: the size within `min` (the area where that is smaller) and the
/// area's size, then the origin within the limits.
#[must_use]
pub fn changed(from: Place, area: Rect, change: Change, min: (u16, u16)) -> Place {
let (mut x, mut y) = (from.x, from.y);
let (mut width, mut height) = (i32::from(from.width), i32::from(from.height));
let (left, top) = (i32::from(area.x), i32::from(area.y));
let (right, bottom) = (i32::from(area.right()), i32::from(area.bottom()));
match change {
Change::Move(columns, rows) => (x, y) = (x + columns, y + rows),
Change::Grow(columns, rows) => (width, height) = (width + columns, height + rows),
Change::MoveTo(column, row) => (x, y) = (column, row),
Change::SizeTo(columns, rows) => (width, height) = (columns, rows),
Change::Left => x = left,
Change::Top => y = top,
Change::Right | Change::Bottom => {}
width = within(
width,
i32::from(min.0.min(area.width)),
i32::from(area.width),
);
height = within(
height,
i32::from(min.1.min(area.height)),
i32::from(area.height),
Change::Right => x = right - width,
Change::Bottom => y = bottom - height,
_ => {}
Place {
x,
y,
width: cell(width),
height: cell(height),
.limited(area)
#[cfg(test)]
mod tests;