1
//! A press on the title row of the front window and the motion that follows move it, the
2
//! release keeps it, and the keyboard mode takes no part of it.
3

            
4
use super::overlap::{FRONT_AT, left_click, left_drag, left_release, overlapped, place_window};
5
use super::report_chart::{call_for, chart_requests, echoed, show, with_table};
6
use super::window_keys::drawn_kitty_chart;
7
use crate::app::App;
8
use crate::event::{Intent, apply};
9
use crate::focus::{FocusTarget, current_focus};
10
use crate::test_support::draw_into;
11
use crate::window::WindowId;
12
use ratatui::crossterm::event::{KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
13
use rpc::chart::ChartBackend;
14
use std::time::{Duration, Instant};
15
use tvui::{FrameMode, Place};
16

            
17
29
pub(super) fn place_of(app: &App, id: WindowId) -> Place {
18
29
    let place = app.desktop.place_of(id, app.desktop_rect());
19
29
    place.expect("the window is open")
20
29
}
21

            
22
2
pub(super) fn front_id(app: &App) -> WindowId {
23
2
    app.front_window().expect("a front window")
24
2
}
25

            
26
7
fn title_cell(dx: u16) -> (u16, u16) {
27
7
    let x = u16::try_from(FRONT_AT.x).unwrap_or_default() + dx;
28
7
    (x, u16::try_from(FRONT_AT.y).unwrap_or_default())
29
7
}
30

            
31
#[test]
32
1
fn a_title_drag_moves_the_window_by_the_motion_and_the_release_keeps_it() {
33
1
    let (mut app, _, front) = overlapped();
34
1
    let (start, now) = (title_cell(10), Instant::now());
35
1
    left_click(&mut app, start, now);
36
1
    assert_eq!(app.frame_state(front).mode, FrameMode::Dragging);
37
1
    left_drag(&mut app, (start.0 + 4, start.1 + 3), now);
38
1
    assert_eq!(
39
1
        place_of(&app, front),
40
1
        Place {
41
1
            x: 24,
42
1
            y: 8,
43
1
            ..FRONT_AT
44
1
        }
45
    );
46
1
    left_drag(&mut app, (start.0 + 6, start.1 + 5), now);
47
1
    left_release(&mut app, (start.0 + 6, start.1 + 5), now);
48
1
    assert_eq!(
49
1
        place_of(&app, front),
50
1
        Place {
51
1
            x: 26,
52
1
            y: 10,
53
1
            ..FRONT_AT
54
1
        }
55
    );
56
1
    assert!(!app.dragging());
57
1
    assert_eq!(app.frame_state(front).mode, FrameMode::Active);
58
1
}
59

            
60
#[test]
61
1
fn a_press_and_release_on_the_title_without_motion_change_nothing() {
62
1
    let (mut app, _, front) = overlapped();
63
1
    let (at, now) = (title_cell(10), Instant::now());
64
1
    left_click(&mut app, at, now);
65
1
    left_release(&mut app, at, now);
66
1
    assert_eq!(place_of(&app, front), FRONT_AT);
67
1
    assert!(!app.dragging());
68
1
    assert!(app.take_outbound().is_empty());
69
1
}
70

            
71
#[test]
72
1
fn the_window_hangs_off_the_left_with_one_column_inside() {
73
1
    let (mut app, _, front) = overlapped();
74
1
    let corner = title_cell(FRONT_AT.width - 1);
75
1
    left_click(&mut app, corner, Instant::now());
76
1
    left_drag(&mut app, (0, corner.1), Instant::now());
77
1
    left_release(&mut app, (0, corner.1), Instant::now());
78
1
    let place = place_of(&app, front);
79
1
    assert_eq!(place.x, 1 - i32::from(FRONT_AT.width));
80
1
    assert_eq!(place.y, FRONT_AT.y);
81
1
}
82

            
83
#[test]
84
1
fn the_top_edge_stays_on_the_desktop() {
85
1
    let (mut app, _, front) = overlapped();
86
1
    let at = title_cell(10);
87
1
    left_click(&mut app, at, Instant::now());
88
1
    left_drag(&mut app, (at.0, 0), Instant::now());
89
1
    assert_eq!(place_of(&app, front).y, i32::from(app.desktop_rect().y));
90
1
}
91

            
92
#[test]
93
1
fn the_window_stays_where_the_grab_holds_it_under_the_pointer() {
94
1
    let (mut app, _, front) = overlapped();
95
1
    let at = title_cell(7);
96
1
    left_click(&mut app, at, Instant::now());
97
1
    left_drag(&mut app, (40, 9), Instant::now());
98
1
    assert_eq!(place_of(&app, front).x, 40 - 7);
99
1
    assert_eq!(place_of(&app, front).y, 9);
100
1
}
101

            
102
#[test]
103
1
fn a_drag_disarms_the_double_click_of_the_press_that_began_it() {
104
1
    let (mut app, _, front) = overlapped();
105
1
    let (at, now) = (title_cell(10), Instant::now());
106
1
    left_click(&mut app, at, now);
107
1
    left_drag(&mut app, (at.0 + 4, at.1), now);
108
1
    left_release(&mut app, (at.0 + 4, at.1), now);
109
1
    left_click(&mut app, at, now);
110
1
    assert!(!app.desktop.window(front).is_some_and(|w| w.zoomed));
111
1
}
112

            
113
#[test]
114
1
fn a_rear_title_press_raises_and_the_next_press_drags() {
115
1
    let (mut app, rear, _) = overlapped();
116
1
    let at = (8, 2);
117
1
    left_click(&mut app, at, Instant::now());
118
1
    assert_eq!(app.front_window(), Some(rear));
119
1
    assert!(!app.dragging());
120
1
    left_click(&mut app, at, Instant::now() + Duration::from_secs(2));
121
1
    left_drag(&mut app, (11, 4), Instant::now());
122
1
    assert_eq!(place_of(&app, rear).x, 5);
123
1
    assert_eq!(place_of(&app, rear).y, 4);
124
1
}
125

            
126
#[test]
127
1
fn motion_and_release_do_nothing_in_the_keyboard_mode() {
128
1
    let (mut app, _, front) = overlapped();
129
1
    app.begin_size_move();
130
1
    let before = place_of(&app, front);
131
1
    left_drag(&mut app, (40, 14), Instant::now());
132
1
    left_release(&mut app, (40, 14), Instant::now());
133
1
    assert_eq!(place_of(&app, front), before);
134
1
    assert_eq!(current_focus(&app), FocusTarget::Drag);
135
1
}
136

            
137
#[test]
138
1
fn a_motion_with_no_drag_begun_by_the_mouse_does_nothing() {
139
1
    let (mut app, _, front) = overlapped();
140
1
    left_drag(&mut app, (40, 14), Instant::now());
141
1
    assert_eq!(place_of(&app, front), FRONT_AT);
142
1
    assert!(!app.dragging());
143
1
}
144

            
145
#[test]
146
1
fn other_buttons_do_not_move_the_held_window() {
147
1
    let (mut app, _, front) = overlapped();
148
1
    let at = title_cell(10);
149
1
    left_click(&mut app, at, Instant::now());
150
1
    let right = MouseEvent {
151
1
        kind: MouseEventKind::Drag(MouseButton::Right),
152
1
        column: 40,
153
1
        row: 14,
154
1
        modifiers: KeyModifiers::NONE,
155
1
    };
156
1
    crate::mouse::handle_mouse(&mut app, right, Instant::now());
157
1
    assert_eq!(place_of(&app, front), FRONT_AT);
158
1
}
159

            
160
10
pub(super) fn mouse_at(
161
10
    app: &mut App,
162
10
    kind: MouseEventKind,
163
10
    (column, row): (u16, u16),
164
10
    (size, when): ((u16, u16), Instant),
165
10
) {
166
10
    let mouse = MouseEvent {
167
10
        kind,
168
10
        column,
169
10
        row,
170
10
        modifiers: KeyModifiers::NONE,
171
10
    };
172
10
    crate::mouse::handle_mouse(app, mouse, when);
173
10
    draw_into(app, size.0, size.1);
174
10
}
175

            
176
#[tokio::test]
177
1
async fn the_release_fetches_the_chart_again_and_a_press_alone_does_not() {
178
1
    let (mut app, sent) = with_table(60, 24, ChartBackend::Halfblocks);
179
1
    show(&mut app);
180
1
    let id = front_id(&app);
181
1
    place_window(
182
1
        &mut app,
183
1
        id,
184
1
        Place {
185
1
            x: 10,
186
1
            y: 5,
187
1
            width: 30,
188
1
            height: 10,
189
1
        },
190
1
    );
191
1
    draw_into(&mut app, 60, 24);
192
1
    let before = chart_requests(&app).len();
193
1
    let (at, left) = ((20, 5), MouseButton::Left);
194
1
    let (size, later) = ((60, 24), Instant::now() + Duration::from_secs(2));
195
1
    let first = (size, Instant::now());
196
1
    let second = (size, later);
197
1
    mouse_at(&mut app, MouseEventKind::Down(left), at, first);
198
1
    mouse_at(&mut app, MouseEventKind::Up(left), at, first);
199
1
    assert_eq!(chart_requests(&app).len(), before);
200
1
    mouse_at(&mut app, MouseEventKind::Down(left), at, second);
201
1
    mouse_at(&mut app, MouseEventKind::Drag(left), (22, 6), second);
202
1
    assert_eq!(chart_requests(&app).len(), before);
203
1
    mouse_at(&mut app, MouseEventKind::Up(left), (22, 6), second);
204
1
    assert_eq!(chart_requests(&app).len(), before + 1);
205
1
    assert!(echoed(&mut app, &sent).await.contains(&call_for(280, 160)));
206
1
}
207

            
208
#[tokio::test]
209
1
async fn the_kitty_image_goes_with_the_first_motion_not_with_the_press() {
210
1
    let (mut app, image) = drawn_kitty_chart();
211
1
    draw_into(&mut app, 40, 20);
212
1
    let (at, left) = ((10, 1), MouseButton::Left);
213
1
    let size = ((40, 20), Instant::now());
214
1
    mouse_at(&mut app, MouseEventKind::Down(left), at, size);
215
1
    assert!(app.take_outbound().is_empty());
216
1
    mouse_at(&mut app, MouseEventKind::Drag(left), (11, 1), size);
217
1
    assert_eq!(app.take_outbound(), vec![tvui::kitty_delete(image)]);
218
1
    apply(&mut app, Intent::DragCancel);
219
1
}