1
//! Left clicks on windows: a rear window is raised, the frame icons and the title row act on
2
//! the front one, and a modal blocks them all.
3

            
4
use super::overlap::{
5
    FRONT_AT, REAR_AT, left_click, left_release, overlapped, place_window, wheel_down,
6
};
7
use super::report_chart::{call_for, echoed, show, with_table};
8
use crate::app::App;
9
use crate::hits::Target;
10
use crate::modal::{ConfirmAction, Modal};
11
use crate::tabs::list::ListTab;
12
use crate::test_support::{area_of, redraw};
13
use crate::view::ViewId;
14
use crate::window::WindowId;
15
use ratatui::layout::Rect;
16
use rpc::chart::ChartBackend;
17
use std::time::{Duration, Instant};
18
use tvui::Place;
19

            
20
9
fn middle(rect: Rect) -> (u16, u16) {
21
9
    (rect.x + rect.width / 2, rect.y)
22
9
}
23

            
24
9
fn zoomed(app: &App, kind: ViewId) -> bool {
25
9
    let id = app.window_id(kind);
26
9
    id.and_then(|id| app.desktop.window(id))
27
9
        .is_some_and(|window| window.zoomed)
28
9
}
29

            
30
/// Two accounts lists, the second one in front and overlapping the first.
31
2
fn two_lists() -> (App, WindowId, WindowId) {
32
2
    let (mut app, ..) = overlapped();
33
2
    app.close_all_windows();
34
2
    let mut ids = Vec::new();
35
2
    for _ in 0..2 {
36
4
        app.open_view(ViewId::Accounts);
37
12
        let row = |i: usize| format!(r#"(:account :id "u{i}" :name "a{i}" :parent "")"#);
38
4
        let rows: Vec<String> = (0..3).map(row).collect();
39
4
        app.accounts_mut()
40
4
            .on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
41
4
        ids.extend(app.front_window());
42
    }
43
2
    let [rear, front] = ids[..] else {
44
        panic!("two windows");
45
    };
46
2
    place_window(&mut app, rear, REAR_AT);
47
2
    place_window(&mut app, front, FRONT_AT);
48
2
    redraw(&mut app);
49
2
    (app, rear, front)
50
2
}
51

            
52
5
fn selected(app: &App, id: WindowId) -> Option<String> {
53
5
    let list = app.win::<ListTab>(id)?;
54
5
    list.selected_id().map(str::to_string)
55
5
}
56

            
57
2
fn rear_row(app: &App, row: usize) -> (u16, u16) {
58
2
    let entries = app.hits.entries();
59
8
    let found = entries.iter().find(|(_, t)| *t == Target::Row(row));
60
2
    found.map_or((0, 0), |(rect, _)| (rect.x, rect.y))
61
2
}
62

            
63
#[test]
64
1
fn a_click_on_a_rear_row_raises_the_window_without_selecting_it() {
65
1
    let (mut app, rear, front) = two_lists();
66
1
    let before = selected(&app, rear);
67
1
    let at = rear_row(&app, 2);
68
1
    left_click(&mut app, at, Instant::now());
69
1
    assert_eq!(app.front_window(), Some(rear));
70
1
    assert_eq!(selected(&app, rear), before);
71
1
    assert_eq!(selected(&app, front), Some("u0".to_string()));
72
1
}
73

            
74
#[test]
75
1
fn the_wheel_over_a_rear_window_scrolls_it_and_raises_nothing() {
76
1
    let (mut app, rear, front) = two_lists();
77
1
    let at = rear_row(&app, 0);
78
1
    wheel_down(&mut app, at, Instant::now());
79
1
    assert_eq!(selected(&app, rear), Some("u1".to_string()));
80
1
    assert_eq!(selected(&app, front), Some("u0".to_string()));
81
1
    assert_eq!(app.front_window(), Some(front));
82
1
}
83

            
84
#[test]
85
1
fn a_double_click_on_a_rear_title_raises_the_window_and_zooms_it() {
86
1
    let (mut app, rear, _) = overlapped();
87
1
    let at = (
88
1
        u16::try_from(REAR_AT.x).unwrap_or_default() + 6,
89
1
        u16::try_from(REAR_AT.y).unwrap_or_default(),
90
1
    );
91
1
    let start = Instant::now();
92
1
    left_click(&mut app, at, start);
93
1
    assert_eq!(app.front_window(), Some(rear));
94
1
    assert!(!zoomed(&app, ViewId::Console));
95
1
    left_click(&mut app, at, start + Duration::from_millis(100));
96
1
    assert!(zoomed(&app, ViewId::Console));
97
1
}
98

            
99
#[test]
100
1
fn a_click_on_the_front_window_acts_in_it_where_it_covers_the_rear_one() {
101
1
    let (mut app, _, front) = overlapped();
102
1
    let row = area_of(&app, Target::Row(1));
103
1
    left_click(&mut app, (row.x, row.y), Instant::now());
104
1
    assert_eq!(app.front_window(), Some(front));
105
1
    assert_eq!(app.selected_id(ViewId::Accounts).as_deref(), Some("u1"));
106
1
}
107

            
108
#[test]
109
1
fn a_click_on_the_front_frame_over_the_rear_body_stays_in_front() {
110
1
    let (mut app, _, front) = overlapped();
111
1
    left_click(&mut app, (20, 8), Instant::now());
112
1
    assert_eq!(app.front_window(), Some(front));
113
1
}
114

            
115
#[test]
116
1
fn a_click_on_the_desktop_between_windows_goes_to_no_window() {
117
1
    let (mut app, ..) = overlapped();
118
1
    let before = (app.front_window(), app.windows.len());
119
1
    left_click(&mut app, (70, 20), Instant::now());
120
1
    assert_eq!((app.front_window(), app.windows.len()), before);
121
1
}
122

            
123
#[test]
124
1
fn the_close_icon_closes_the_front_window() {
125
1
    let (mut app, rear, _) = overlapped();
126
1
    let icon = middle(area_of(&app, Target::Close));
127
1
    left_click(&mut app, icon, Instant::now());
128
1
    assert!(app.window_id(ViewId::Accounts).is_some());
129
1
    left_release(&mut app, icon, Instant::now());
130
1
    assert_eq!(app.window_id(ViewId::Accounts), None);
131
1
    assert_eq!(app.front_window(), Some(rear));
132
1
}
133

            
134
#[test]
135
1
fn a_release_off_the_close_icon_keeps_the_window() {
136
1
    let (mut app, ..) = overlapped();
137
1
    let icon = middle(area_of(&app, Target::Close));
138
1
    left_click(&mut app, icon, Instant::now());
139
1
    left_release(&mut app, (icon.0 + 10, icon.1 + 3), Instant::now());
140
1
    left_release(&mut app, icon, Instant::now());
141
1
    assert!(app.window_id(ViewId::Accounts).is_some());
142
1
}
143

            
144
#[test]
145
1
fn the_zoom_icon_zooms_the_front_window_and_restores_it() {
146
1
    let (mut app, ..) = overlapped();
147
1
    for want in [true, false] {
148
2
        let icon = area_of(&app, Target::Zoom);
149
2
        left_click(&mut app, middle(icon), Instant::now());
150
2
        assert_eq!(zoomed(&app, ViewId::Accounts), want);
151
    }
152
1
}
153

            
154
#[tokio::test]
155
1
async fn the_zoom_icon_refetches_the_chart_at_the_new_size() {
156
1
    let (mut app, sent) = with_table(80, 24, ChartBackend::Halfblocks);
157
1
    show(&mut app);
158
1
    redraw(&mut app);
159
1
    let icon = area_of(&app, Target::Zoom);
160
1
    left_click(&mut app, middle(icon), Instant::now());
161
1
    let echoed = echoed(&mut app, &sent).await;
162
1
    assert!(echoed.contains(&call_for(580, 280)), "{echoed}");
163
1
}
164

            
165
2
fn title_x() -> u16 {
166
2
    u16::try_from(FRONT_AT.x).unwrap_or_default() + 10
167
2
}
168

            
169
2
fn title_y() -> u16 {
170
2
    u16::try_from(FRONT_AT.y).unwrap_or_default()
171
2
}
172

            
173
#[test]
174
1
fn two_quick_clicks_on_the_title_row_zoom_and_one_does_not() {
175
1
    let (mut app, ..) = overlapped();
176
1
    let at = (title_x(), title_y());
177
1
    let start = Instant::now();
178
1
    left_click(&mut app, at, start);
179
1
    assert!(!zoomed(&app, ViewId::Accounts));
180
1
    left_click(&mut app, at, start + Duration::from_millis(100));
181
1
    assert!(zoomed(&app, ViewId::Accounts));
182
1
}
183

            
184
#[test]
185
1
fn two_slow_clicks_on_the_title_row_do_not_zoom() {
186
1
    let (mut app, ..) = overlapped();
187
1
    let at = (title_x(), title_y());
188
1
    let start = Instant::now();
189
1
    left_click(&mut app, at, start);
190
1
    left_click(&mut app, at, start + Duration::from_secs(2));
191
1
    assert!(!zoomed(&app, ViewId::Accounts));
192
1
}
193

            
194
#[test]
195
1
fn a_modal_blocks_the_icons() {
196
1
    let (mut app, ..) = overlapped();
197
2
    let icons = [Target::Close, Target::Zoom].map(|target| middle(area_of(&app, target)));
198
1
    app.overlays.push(Modal::Confirm {
199
1
        prompt: "Sure?".to_string(),
200
1
        action: ConfirmAction::DiscardForm,
201
1
        focus: None,
202
1
    });
203
1
    redraw(&mut app);
204
1
    for icon in icons {
205
2
        left_click(&mut app, icon, Instant::now());
206
    }
207
1
    assert!(app.window_id(ViewId::Accounts).is_some());
208
1
    assert!(!zoomed(&app, ViewId::Accounts));
209
1
}
210

            
211
2
fn hung_front(x: i32) -> App {
212
2
    let (mut app, _, front) = overlapped();
213
2
    place_window(&mut app, front, Place { x, ..FRONT_AT });
214
2
    redraw(&mut app);
215
2
    app
216
2
}
217

            
218
#[test]
219
1
fn a_window_off_the_right_edge_has_only_its_visible_icons() {
220
1
    let mut app = hung_front(60);
221
1
    let hits = &app.hits;
222
18
    assert!(hits.entries().iter().all(|(_, t)| *t != Target::Zoom));
223
1
    let icon = middle(area_of(&app, Target::Close));
224
1
    left_click(&mut app, icon, Instant::now());
225
1
    left_release(&mut app, icon, Instant::now());
226
1
    assert_eq!(app.window_id(ViewId::Accounts), None);
227
1
}
228

            
229
#[test]
230
1
fn a_window_off_the_left_edge_has_only_its_visible_icons() {
231
1
    let mut app = hung_front(-30);
232
19
    assert!(app.hits.entries().iter().all(|(_, t)| *t != Target::Close));
233
1
    let icon = area_of(&app, Target::Zoom);
234
1
    left_click(&mut app, middle(icon), Instant::now());
235
1
    assert!(zoomed(&app, ViewId::Accounts));
236
1
}