1
//! Opening windows: the startup set, the TV number and the cascade slot a new window gets.
2
use super::App;
3
use super::windows::{desktop_area, new_view};
4
use crate::draw::body_area;
5
use crate::view::ViewId;
6
use crate::window::{Placement, View, WindowId};
7
use ratatui::layout::Rect;
8
use tvui::Place;
9

            
10
/// The size of a window once unzoomed.
11
const WINDOW_SIZE: (u16, u16) = (60, 16);
12
/// The screen the cascade assumes until the terminal reports its size.
13
const FALLBACK_SCREEN: Rect = Rect::new(0, 0, 80, 24);
14
/// The columns and rows a window gives up at the least, so a small desktop still cascades.
15
const CASCADE_ROOM: u16 = 3;
16
/// TV numbers windows 1 to 9.
17
const MAX_NUMBER: u8 = 9;
18
impl App {
19
    /// Opens `view` at the next cascade slot with the lowest free number. It is zoomed when the
20
    /// front window is or when there is none.
21
20608
    pub(crate) fn open_new_window(&mut self, view: View) -> WindowId {
22
20608
        let placement = Placement {
23
20608
            rect: self.cascade_rect(),
24
20608
            zoomed: self.front_zoomed().unwrap_or(true),
25
20608
        };
26
20608
        let number = self.free_number();
27
20608
        self.open_window(view, placement, number)
28
20608
    }
29

            
30
    /// Opens a new window of `kind` on top and loads it.
31
115
    pub(crate) fn open_view(&mut self, kind: ViewId) {
32
115
        let id = self.open_new_window(new_view(kind));
33
115
        self.activate_window(id);
34
115
    }
35

            
36
20608
    fn front_zoomed(&self) -> Option<bool> {
37
20608
        let mut front = self.desktop.iter_back_to_front().rev();
38
20608
        front.find(|slot| slot.caps.zoom).map(|slot| slot.zoomed)
39
20608
    }
40

            
41
    /// The lowest of 1 to 9 no window has.
42
20608
    fn free_number(&self) -> Option<u8> {
43
416822
        (1..=MAX_NUMBER).find(|number| self.windows.iter().all(|win| win.number != Some(*number)))
44
20608
    }
45

            
46
    /// The next ordinal of a window of `kind`: one past the highest open one.
47
20687
    pub(super) fn next_ordinal(&self, kind: ViewId) -> u32 {
48
94554
        let same = self.windows.iter().filter(|win| win.view.kind() == kind);
49
20687
        same.map(|win| win.ordinal).max().map_or(1, |last| last + 1)
50
20687
    }
51

            
52
    /// The part of the terminal the windows sit on.
53
20997
    pub(super) fn desktop_rect(&self) -> Rect {
54
20997
        let fallback = body_area(FALLBACK_SCREEN);
55
20997
        self.terminal.env().map_or(fallback, desktop_area)
56
20997
    }
57

            
58
    /// The rect one step down and right of the front window's, wrapping back to the desktop
59
    /// corner when it would leave the desktop. A desktop too small for the usual size gets
60
    /// smaller windows, so that there is always a slot.
61
20608
    fn cascade_rect(&self) -> Rect {
62
20608
        let area = self.desktop_rect();
63
20608
        let width = WINDOW_SIZE.0.min(area.width.saturating_sub(CASCADE_ROOM));
64
20608
        let height = WINDOW_SIZE.1.min(area.height.saturating_sub(CASCADE_ROOM));
65
20608
        let front = self
66
20608
            .front_window()
67
18508
            .and_then(|id| self.desktop.restored_place(id, area));
68
20608
        let (x, y) = front.map_or((i32::from(area.x), i32::from(area.y)), |place| {
69
18508
            (place.x + 1, place.y + 1)
70
18508
        });
71
20608
        let next = Place {
72
20608
            x,
73
20608
            y,
74
20608
            width,
75
20608
            height,
76
20608
        };
77
20608
        match Place::from(next.visible_in(area)) == next {
78
18566
            true => next.visible_in(area),
79
2042
            false => Rect::new(area.x, area.y, width, height),
80
        }
81
20608
    }
82
}