1
//! Where a new window opens: sized to its content and kept on the desktop.
2

            
3
use ratatui::layout::Rect;
4

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

            
8
impl<Id: Copy + Eq> Desktop<Id> {
9
    /// The rect of a new window of `size`, at most the desktop's, one cell down and right of the
10
    /// front window's drawn place and kept inside `area`; the corner of `area` with no window.
11
    #[must_use]
12
84
    pub fn fitted_rect(&self, (width, height): (u16, u16), area: Rect) -> Rect {
13
84
        let width = width.min(area.width);
14
84
        let height = height.min(area.height);
15
84
        let (x, y) = self
16
84
            .front()
17
77
            .and_then(|id| self.place_of(id, area))
18
84
            .map_or((i32::from(area.x), i32::from(area.y)), |front| {
19
77
                (front.x + 1, front.y + 1)
20
77
            });
21
84
        let x = within(
22
84
            x,
23
84
            i32::from(area.x),
24
84
            i32::from(area.right()) - i32::from(width),
25
84
        );
26
84
        let y = within(
27
84
            y,
28
84
            i32::from(area.y),
29
84
            i32::from(area.bottom()) - i32::from(height),
30
84
        );
31
84
        Rect::new(cell(x), cell(y), width, height)
32
84
    }
33

            
34
    /// Sizes `id` to `size`, at most `area`'s, and moves it back inside `area` when it would
35
    /// overhang; nothing when it is not open. A size equal to the stored one keeps the place.
36
16
    pub fn set_size(&mut self, id: Id, (width, height): (u16, u16), area: Rect) {
37
16
        let Some(window) = self.window_mut(id) else {
38
1
            return;
39
        };
40
15
        let width = width.min(area.width);
41
15
        let height = height.min(area.height);
42
15
        if (width, height) == (window.place.width, window.place.height) {
43
3
            return;
44
12
        }
45
12
        let right = i32::from(area.right()) - i32::from(width);
46
12
        let bottom = i32::from(area.bottom()) - i32::from(height);
47
12
        window.place = Place {
48
12
            x: within(window.place.x, i32::from(area.x), right),
49
12
            y: within(window.place.y, i32::from(area.y), bottom),
50
12
            width,
51
12
            height,
52
12
        };
53
16
    }
54
}