1use ratatui::layout::Rect;
4
5use super::place::{Place, cell, within};
6use super::{Desktop, MIN_SIZE};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum Change {
11 Move(i32, i32),
13 Grow(i32, i32),
15 MoveTo(i32, i32),
17 SizeTo(i32, i32),
19 Left,
21 Right,
23 Top,
25 Bottom,
27}
28
29impl<Id: Copy + Eq> Desktop<Id> {
30 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#[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;