1
//! A press on the `─┘` corner of the front window and the motion after it size it; a lost
2
//! release ends the drag on Esc or the next press, and a closed window ends it.
3

            
4
use super::overlap::{FRONT_AT, REAR_AT, left_click, left_drag, left_release, overlapped};
5
use super::report_chart::{call_for, chart_requests, echoed, show, with_table};
6
use super::title_drag::{front_id, mouse_at, place_of};
7
use crate::app::App;
8
use crate::hits::Target;
9
use crate::test_support::{area_of, draw_into, key, press};
10
use crate::window::WindowId;
11
use ratatui::crossterm::event::{KeyCode, MouseButton, MouseEventKind};
12
use rpc::chart::ChartBackend;
13
use std::time::{Duration, Instant};
14
use tvui::{FrameMode, Place};
15

            
16
11
fn corner_of(place: Place) -> (u16, u16) {
17
11
    let x = u16::try_from(place.x + i32::from(place.width) - 1).unwrap_or_default();
18
11
    let y = u16::try_from(place.y + i32::from(place.height) - 1).unwrap_or_default();
19
11
    (x, y)
20
11
}
21

            
22
7
fn size_of(app: &App, id: WindowId) -> (u16, u16) {
23
7
    let place = place_of(app, id);
24
7
    (place.width, place.height)
25
7
}
26

            
27
#[test]
28
1
fn the_front_window_has_a_grow_target_on_its_corner_and_a_rear_one_has_none() {
29
1
    let (app, ..) = overlapped();
30
1
    let grow = area_of(&app, Target::Grow);
31
1
    let corner = corner_of(FRONT_AT);
32
1
    assert!(grow.contains(ratatui::layout::Position::new(corner.0, corner.1)));
33
1
    assert_eq!((grow.width, grow.height), (2, 1));
34
1
    let grows = app
35
1
        .hits
36
1
        .entries()
37
1
        .iter()
38
20
        .filter(|(_, target)| *target == Target::Grow)
39
        .count();
40
1
    assert_eq!(grows, 1);
41
1
}
42

            
43
#[test]
44
1
fn a_corner_drag_sizes_the_window_to_the_pointer() {
45
1
    let (mut app, _, front) = overlapped();
46
1
    let (corner, now) = (corner_of(FRONT_AT), Instant::now());
47
1
    left_click(&mut app, corner, now);
48
1
    assert_eq!(app.frame_state(front).mode, FrameMode::Dragging);
49
1
    left_drag(&mut app, (corner.0 + 5, corner.1 + 2), now);
50
1
    assert_eq!(size_of(&app, front), (45, 14));
51
1
    left_drag(&mut app, (40, 12), now);
52
1
    left_release(&mut app, (40, 12), now);
53
1
    assert_eq!(size_of(&app, front), (21, 8));
54
1
    let place = place_of(&app, front);
55
1
    assert_eq!((place.x, place.y), (FRONT_AT.x, FRONT_AT.y));
56
1
    assert!(!app.dragging());
57
1
}
58

            
59
#[test]
60
1
fn a_corner_drag_stops_at_the_minimum_size() {
61
1
    let (mut app, _, front) = overlapped();
62
1
    left_click(&mut app, corner_of(FRONT_AT), Instant::now());
63
1
    left_drag(&mut app, (21, 6), Instant::now());
64
1
    assert_eq!(size_of(&app, front), (16, 6));
65
1
}
66

            
67
#[test]
68
1
fn a_corner_drag_stops_at_the_desktop_size() {
69
1
    let (mut app, _, front) = overlapped();
70
1
    let hung = Place {
71
1
        x: -10,
72
1
        y: 2,
73
1
        ..FRONT_AT
74
1
    };
75
1
    super::overlap::place_window(&mut app, front, hung);
76
1
    draw_into(&mut app, 80, 24);
77
1
    left_click(&mut app, corner_of(hung), Instant::now());
78
1
    left_drag(&mut app, (79, 23), Instant::now());
79
1
    let area = app.desktop_rect();
80
1
    assert_eq!(size_of(&app, front), (area.width, area.height));
81
1
}
82

            
83
#[test]
84
1
fn a_press_on_the_corner_of_a_rear_window_raises_it_and_does_not_size_it() {
85
1
    let (mut app, rear, _) = overlapped();
86
1
    let clear = Place {
87
1
        width: 16,
88
1
        height: 6,
89
1
        ..REAR_AT
90
1
    };
91
1
    super::overlap::place_window(&mut app, rear, clear);
92
1
    draw_into(&mut app, 80, 24);
93
1
    left_click(&mut app, corner_of(clear), Instant::now());
94
1
    assert_eq!(app.front_window(), Some(rear));
95
1
    assert!(!app.dragging());
96
1
}
97

            
98
#[tokio::test]
99
1
async fn the_release_of_a_corner_drag_fetches_the_chart_at_the_new_size() {
100
1
    let (mut app, sent) = with_table(60, 24, ChartBackend::Halfblocks);
101
1
    show(&mut app);
102
1
    let id = front_id(&app);
103
1
    let start = Place {
104
1
        x: 10,
105
1
        y: 5,
106
1
        width: 30,
107
1
        height: 10,
108
1
    };
109
1
    super::overlap::place_window(&mut app, id, start);
110
1
    draw_into(&mut app, 60, 24);
111
1
    let before = chart_requests(&app).len();
112
1
    let (left, now) = (MouseButton::Left, Instant::now());
113
1
    let at = corner_of(start);
114
1
    mouse_at(&mut app, MouseEventKind::Down(left), at, ((60, 24), now));
115
1
    mouse_at(
116
1
        &mut app,
117
1
        MouseEventKind::Drag(left),
118
1
        (41, 15),
119
1
        ((60, 24), now),
120
1
    );
121
1
    assert_eq!(chart_requests(&app).len(), before);
122
1
    mouse_at(
123
1
        &mut app,
124
1
        MouseEventKind::Up(left),
125
1
        (41, 15),
126
1
        ((60, 24), now),
127
1
    );
128
1
    assert_eq!(chart_requests(&app).len(), before + 1);
129
1
    assert!(echoed(&mut app, &sent).await.contains(&call_for(300, 180)));
130
1
}
131

            
132
#[test]
133
1
fn a_lost_release_ends_on_esc_with_the_window_put_back() {
134
1
    let (mut app, _, front) = overlapped();
135
1
    left_click(&mut app, corner_of(FRONT_AT), Instant::now());
136
1
    left_drag(&mut app, (50, 14), Instant::now());
137
1
    assert_ne!(place_of(&app, front), FRONT_AT);
138
1
    press(&mut app, key(KeyCode::Esc));
139
1
    assert_eq!(place_of(&app, front), FRONT_AT);
140
1
    assert!(!app.dragging());
141
1
    left_drag(&mut app, (60, 20), Instant::now());
142
1
    assert_eq!(place_of(&app, front), FRONT_AT);
143
1
}
144

            
145
#[test]
146
1
fn a_lost_release_ends_on_the_next_press_which_keeps_the_window_and_acts() {
147
1
    let (mut app, _, front) = overlapped();
148
1
    let now = Instant::now();
149
1
    left_click(&mut app, corner_of(FRONT_AT), now);
150
1
    left_drag(&mut app, (50, 14), now);
151
1
    let sized = place_of(&app, front);
152
1
    assert_ne!(sized, FRONT_AT);
153
1
    let title = (u16::try_from(sized.x).unwrap_or_default() + 10, 5);
154
1
    left_click(&mut app, title, now + Duration::from_secs(2));
155
1
    assert_eq!(place_of(&app, front), sized);
156
1
    assert!(app.pointer_dragging());
157
1
    left_drag(&mut app, (title.0 + 1, 7), now);
158
1
    assert_eq!(place_of(&app, front).x, sized.x + 1);
159
1
}
160

            
161
#[test]
162
1
fn closing_the_window_mid_drag_ends_the_drag() {
163
1
    let (mut app, rear, front) = overlapped();
164
1
    left_click(&mut app, corner_of(FRONT_AT), Instant::now());
165
1
    left_drag(&mut app, (50, 14), Instant::now());
166
1
    app.close_window(front);
167
1
    assert!(!app.dragging());
168
1
    let before = place_of(&app, rear);
169
1
    left_drag(&mut app, (70, 20), Instant::now());
170
1
    left_release(&mut app, (70, 20), Instant::now());
171
1
    assert_eq!(place_of(&app, rear), before);
172
1
}
173

            
174
#[test]
175
1
fn a_press_on_the_dash_of_the_corner_keeps_the_width_on_a_vertical_drag() {
176
1
    let (mut app, _, front) = overlapped();
177
1
    let (x, y) = corner_of(FRONT_AT);
178
1
    left_click(&mut app, (x - 1, y), Instant::now());
179
1
    left_drag(&mut app, (x - 1, y + 2), Instant::now());
180
1
    assert_eq!(size_of(&app, front), (FRONT_AT.width, FRONT_AT.height + 2));
181
1
}
182

            
183
#[test]
184
1
fn the_one_visible_corner_cell_of_a_window_hung_off_the_left_sizes_it() {
185
1
    let (mut app, _, front) = overlapped();
186
1
    let hung = Place {
187
1
        x: 1 - i32::from(FRONT_AT.width),
188
1
        ..FRONT_AT
189
1
    };
190
1
    super::overlap::place_window(&mut app, front, hung);
191
1
    draw_into(&mut app, 80, 24);
192
1
    let y = corner_of(hung).1;
193
1
    left_click(&mut app, (0, y), Instant::now());
194
1
    assert!(app.pointer_dragging());
195
1
    left_drag(&mut app, (0, y + 2), Instant::now());
196
1
    assert_eq!(size_of(&app, front), (hung.width, hung.height + 2));
197
1
    left_drag(&mut app, (9, y + 2), Instant::now());
198
1
    assert_eq!(size_of(&app, front), (hung.width + 9, hung.height + 2));
199
1
}