1
//! Arranging the windows as a set: Tile, Cascade and Close all.
2

            
3
use super::App;
4
use crate::window::WindowId;
5
use ratatui::layout::Rect;
6
use tvui::Desktop;
7

            
8
impl App {
9
    /// Lays the windows out side by side over the desktop.
10
5
    pub(crate) fn tile_windows(&mut self) {
11
5
        self.arrange(Desktop::tile);
12
5
    }
13

            
14
    /// Stacks the windows from the desktop corner.
15
4
    pub(crate) fn cascade_windows(&mut self) {
16
4
        self.arrange(Desktop::cascade);
17
4
    }
18

            
19
    /// Closes every window, the rearmost first so that none is loaded just to be closed.
20
50
    pub(crate) fn close_all_windows(&mut self) {
21
50
        for id in self.window_ids_back_to_front() {
22
484
            self.close_window(id);
23
        }
24
50
    }
25

            
26
59
    fn window_ids_back_to_front(&self) -> Vec<WindowId> {
27
59
        let slots = self.desktop.iter_back_to_front();
28
574
        slots.map(|slot| slot.id).collect()
29
59
    }
30

            
31
    /// Moves every window with `layout`; the windows keep their place when it finds no room.
32
9
    fn arrange(&mut self, layout: fn(&mut Desktop<WindowId>, Rect) -> bool) {
33
9
        let ids = self.window_ids_back_to_front();
34
9
        let area = self.desktop_rect();
35
9
        if !layout(&mut self.desktop, area) {
36
2
            self.set_status("too many windows");
37
2
            return;
38
7
        }
39
7
        for id in ids {
40
70
            self.window_resized(id);
41
        }
42
7
        self.load_visible();
43
9
    }
44

            
45
    /// Drops the kitty image of window `id` and fetches its chart again: the pane changed size.
46
130
    pub(super) fn window_resized(&mut self, id: WindowId) {
47
130
        self.delete_image(id);
48
130
        self.refetch_report_chart(id);
49
130
    }
50
}