1
//! A press on the title row of a dialog and the motion that follows move it, the release keeps
2
//! it, and the limits are those of a window.
3

            
4
use super::overlap::{left_click, left_drag, left_release};
5
use crate::app::App;
6
use crate::focus::{FocusTarget, current_focus};
7
use crate::form::Form;
8
use crate::hits::Target;
9
use crate::modal::{ConfirmAction, Modal};
10
use crate::overlay::Placement;
11
use crate::test_support::{key, press, redraw};
12
use ratatui::crossterm::event::KeyCode;
13
use ratatui::layout::Rect;
14
use std::time::Instant;
15
use tvui::Place;
16

            
17
5
fn form_app() -> App {
18
5
    let mut app = crate::test_support::app();
19
5
    app.overlays.push(Modal::Form(Form::config_set()));
20
5
    redraw(&mut app);
21
5
    app
22
5
}
23

            
24
12
fn place_of(app: &App) -> Place {
25
12
    let slot = app.hits.dialog().expect("a dialog was drawn");
26
12
    app.overlays
27
12
        .placement()
28
12
        .place(slot.auto, slot.min, slot.host)
29
12
}
30

            
31
/// The cell `dx` columns into the title row of the dialog on top.
32
5
fn title_cell(app: &App, dx: u16) -> (u16, u16) {
33
5
    let found = app
34
5
        .hits
35
5
        .entries()
36
5
        .into_iter()
37
56
        .rfind(|(_, target)| *target == Target::Title);
38
5
    let (rect, _): (Rect, _) = found.expect("a title row was drawn");
39
5
    (rect.x + dx, rect.y)
40
5
}
41

            
42
7
fn at(cell: (u16, u16), dx: i32, dy: i32) -> (u16, u16) {
43
14
    let moved = |value: u16, by: i32| u16::try_from(i32::from(value) + by).unwrap_or_default();
44
7
    (moved(cell.0, dx), moved(cell.1, dy))
45
7
}
46

            
47
#[test]
48
1
fn a_title_drag_moves_the_dialog_by_the_motion_and_the_release_keeps_it() {
49
1
    let mut app = form_app();
50
1
    let (start, grab, now) = (place_of(&app), title_cell(&app, 10), Instant::now());
51
1
    left_click(&mut app, grab, now);
52
1
    assert_eq!(current_focus(&app), FocusTarget::Drag);
53
1
    left_drag(&mut app, at(grab, 4, 3), now);
54
1
    let moved = place_of(&app);
55
1
    assert_eq!((moved.x, moved.y), (start.x + 4, start.y + 3));
56
1
    left_drag(&mut app, at(grab, 6, 5), now);
57
1
    left_release(&mut app, at(grab, 6, 5), now);
58
1
    let kept = place_of(&app);
59
1
    assert_eq!((kept.x, kept.y), (start.x + 6, start.y + 5));
60
1
    assert_eq!((kept.width, kept.height), (start.width, start.height));
61
1
    assert!(!app.dragging());
62
1
}
63

            
64
#[test]
65
1
fn a_press_and_release_without_motion_change_nothing() {
66
1
    let mut app = form_app();
67
1
    let (grab, now) = (title_cell(&app, 10), Instant::now());
68
1
    left_click(&mut app, grab, now);
69
1
    left_release(&mut app, grab, now);
70
1
    assert_eq!(app.overlays.placement(), Placement::default());
71
1
    assert!(!app.dragging());
72
1
}
73

            
74
#[test]
75
1
fn a_drag_stops_at_the_limits_of_a_window() {
76
1
    let mut app = form_app();
77
1
    let host = app.hits.dialog().expect("drawn").host;
78
1
    let (start, grab, now) = (place_of(&app), title_cell(&app, 10), Instant::now());
79
1
    left_click(&mut app, grab, now);
80
1
    left_drag(&mut app, (0, 0), now);
81
1
    let high = place_of(&app);
82
1
    assert_eq!(high.y, i32::from(host.y));
83
1
    assert_eq!(high.x, -10);
84
1
    left_drag(&mut app, (79, 23), now);
85
1
    let low = place_of(&app);
86
1
    assert_eq!(low.y, i32::from(host.bottom()) - 1);
87
1
    assert_eq!(low.x, 69);
88
1
    left_release(&mut app, (79, 23), now);
89
1
    assert_eq!((low.width, low.height), (start.width, start.height));
90
1
}
91

            
92
#[test]
93
1
fn a_lost_release_ends_on_esc_or_the_next_press() {
94
1
    let mut app = form_app();
95
1
    let (start, grab, now) = (place_of(&app), title_cell(&app, 10), Instant::now());
96
1
    left_click(&mut app, grab, now);
97
1
    left_drag(&mut app, at(grab, 5, 2), now);
98
1
    press(&mut app, key(KeyCode::Esc));
99
1
    assert_eq!(place_of(&app), start);
100
1
    assert!(!app.dragging());
101

            
102
1
    left_click(&mut app, grab, now);
103
1
    left_drag(&mut app, at(grab, 5, 2), now);
104
1
    let moved = place_of(&app);
105
1
    left_click(&mut app, (0, 0), now);
106
1
    assert!(!app.dragging());
107
1
    assert_eq!(place_of(&app), moved);
108
1
}
109

            
110
#[test]
111
1
fn a_confirm_is_dragged_by_its_title_too() {
112
1
    let mut app = crate::test_support::app();
113
1
    app.overlays.push(Modal::Confirm {
114
1
        prompt: "Discard changes?".to_string(),
115
1
        action: ConfirmAction::DiscardForm,
116
1
        focus: None,
117
1
    });
118
1
    redraw(&mut app);
119
1
    let (start, grab, now) = (place_of(&app), title_cell(&app, 10), Instant::now());
120
1
    left_click(&mut app, grab, now);
121
1
    left_drag(&mut app, at(grab, 3, 2), now);
122
1
    left_release(&mut app, at(grab, 3, 2), now);
123
1
    let moved = place_of(&app);
124
1
    assert_eq!((moved.x, moved.y), (start.x + 3, start.y + 2));
125
1
}
126

            
127
#[test]
128
1
fn a_press_on_the_close_icon_does_not_drag() {
129
1
    let mut app = form_app();
130
1
    let icon = app
131
1
        .hits
132
1
        .entries()
133
1
        .into_iter()
134
11
        .rfind(|(_, target)| *target == Target::Close)
135
1
        .map(|(rect, _)| (rect.x + 1, rect.y))
136
1
        .expect("a close icon was drawn");
137
1
    left_click(&mut app, icon, Instant::now());
138
1
    assert!(!app.dragging());
139
1
}