1
//! Tile, Cascade and Close all.
2

            
3
use super::report_chart::env;
4
use crate::app::App;
5
use crate::event::{Intent, acts, apply};
6
use crate::form::Form;
7
use crate::route::{Route, RouteCtx};
8
use crate::view::ViewId;
9
use crate::window::WindowId;
10
use ratatui::layout::Rect;
11
use rpc::chart::ChartBackend;
12

            
13
62
fn rect_of(app: &App, id: WindowId) -> Option<Rect> {
14
62
    let place = app.desktop.restored_place(id, app.desktop_rect())?;
15
62
    Some(place.visible_in(app.desktop_rect()))
16
62
}
17

            
18
6
fn rects(app: &App) -> Vec<Option<Rect>> {
19
6
    let slots = app.desktop.iter_back_to_front();
20
60
    slots.map(|slot| rect_of(app, slot.id)).collect()
21
6
}
22

            
23
3
fn all_unzoomed(app: &App) -> bool {
24
21
    app.desktop.iter_back_to_front().all(|slot| !slot.zoomed)
25
3
}
26

            
27
#[test]
28
1
fn tile_lays_ten_windows_out_from_the_rearmost_one() {
29
1
    let mut app = crate::test_support::app();
30
1
    app.toggle_zoom_of(ViewId::Transactions);
31
1
    let (front, rearmost) = (app.front_window(), app.desktop.rearmost());
32
1
    apply(&mut app, Intent::Tile);
33
1
    assert_eq!(
34
1
        rearmost.and_then(|id| rect_of(&app, id)),
35
1
        Some(Rect::new(0, 1, 26, 7))
36
    );
37
1
    assert_eq!(
38
1
        front.and_then(|id| rect_of(&app, id)),
39
1
        Some(Rect::new(53, 17, 27, 6))
40
    );
41
1
    assert!(all_unzoomed(&app));
42
1
    assert_eq!(app.front_window(), front);
43
1
}
44

            
45
#[test]
46
1
fn tile_on_a_bigger_terminal_uses_the_whole_desktop() {
47
1
    let mut app = crate::test_support::app();
48
1
    app.sync_terminal(env(120, 36, ChartBackend::Halfblocks));
49
1
    app.tile_windows();
50
1
    let area = app.desktop_rect();
51
10
    let sum: u32 = rects(&app).iter().flatten().map(|rect| rect.area()).sum();
52
1
    assert_eq!(sum, area.area());
53
1
}
54

            
55
#[test]
56
1
fn tile_with_no_room_says_so_and_moves_nothing() {
57
1
    let mut app = crate::test_support::app();
58
1
    app.sync_terminal(env(80, 4, ChartBackend::Halfblocks));
59
1
    app.toggle_zoom_of(ViewId::Tags);
60
1
    let before = rects(&app);
61
1
    app.tile_windows();
62
1
    assert_eq!(app.status, "too many windows");
63
1
    assert_eq!(rects(&app), before);
64
1
    assert!(!all_unzoomed(&app));
65
1
}
66

            
67
#[test]
68
1
fn cascade_steps_from_the_rearmost_window_and_keeps_the_corner() {
69
1
    let mut app = crate::test_support::app();
70
1
    app.toggle_zoom_of(ViewId::Reports);
71
1
    let front = app.front_window();
72
1
    apply(&mut app, Intent::Cascade);
73
1
    let area = app.desktop_rect();
74
1
    let found = rects(&app);
75
1
    for (step, rect) in (0u16..).zip(&found) {
76
10
        let expected = Rect::new(
77
10
            area.x + step,
78
10
            area.y + step,
79
10
            area.width - step,
80
10
            area.height - step,
81
10
        );
82
10
        assert_eq!(*rect, Some(expected), "window {step}");
83
    }
84
1
    assert!(all_unzoomed(&app));
85
1
    assert_eq!(app.front_window(), front);
86
1
}
87

            
88
#[test]
89
1
fn cascade_with_no_room_says_so_and_moves_nothing() {
90
1
    let mut app = crate::test_support::app();
91
1
    app.sync_terminal(env(80, 14, ChartBackend::Halfblocks));
92
1
    let before = rects(&app);
93
1
    app.cascade_windows();
94
1
    assert_eq!(app.status, "too many windows");
95
1
    assert_eq!(rects(&app), before);
96
1
}
97

            
98
#[test]
99
1
fn close_all_leaves_the_desktop_empty() {
100
1
    let mut app = crate::test_support::app();
101
1
    apply(&mut app, Intent::CloseAll);
102
1
    assert!(app.windows.is_empty());
103
1
    assert_eq!(app.front_window(), None);
104
1
    assert!(!acts(&app, Intent::CloseAll));
105
1
}
106

            
107
#[test]
108
1
fn close_all_settles_the_form_of_a_window() {
109
1
    let mut app = crate::test_support::app();
110
1
    let accounts = app.window_of(ViewId::Accounts);
111
1
    app.pending_routes.insert(
112
1
        5,
113
1
        Route {
114
1
            target: Some(accounts),
115
1
            ctx: RouteCtx::None,
116
1
        },
117
1
    );
118
1
    app.lock_pending_form(5, Form::config_set());
119
1
    app.close_all_windows();
120
1
    assert!(app.pending_form.is_none());
121
1
    assert!(app.status.contains("window closed"), "{}", app.status);
122
1
}
123

            
124
#[test]
125
1
fn the_arrange_commands_need_a_window() {
126
1
    let app = crate::test_support::empty_app();
127
1
    for intent in [Intent::Tile, Intent::Cascade, Intent::CloseAll] {
128
3
        assert!(!acts(&app, intent), "{intent:?}");
129
    }
130
1
}
131

            
132
#[test]
133
1
fn close_all_waits_while_a_dialog_is_open() {
134
1
    let mut app = crate::test_support::app();
135
1
    app.overlays
136
1
        .push(crate::modal::Modal::Form(Form::config_set()));
137
1
    assert!(!acts(&app, Intent::CloseAll));
138
1
    apply(&mut app, Intent::CloseAll);
139
1
    assert_eq!(app.windows.len(), ViewId::ALL.len());
140
1
}