1
//! Ctrl-F5 on a dialog: the dialog on top moves and sizes from the keyboard with the limits of a
2
//! window, Esc puts it back and Enter keeps it.
3

            
4
use crate::app::App;
5
use crate::event::{Intent, apply};
6
use crate::focus::{FocusTarget, current_focus};
7
use crate::form::Form;
8
use crate::modal::{ConfirmAction, Modal};
9
use crate::overlay::Placement;
10
use crate::test_support::{ctrl, key, press, redraw};
11
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
12
use tvui::{Change, Place};
13

            
14
6
fn form_app() -> App {
15
6
    let mut app = crate::test_support::app();
16
6
    app.overlays.push(Modal::Form(Form::config_set()));
17
6
    redraw(&mut app);
18
6
    app
19
6
}
20

            
21
1
fn confirm_app() -> App {
22
1
    let mut app = crate::test_support::app();
23
1
    app.overlays.push(Modal::Confirm {
24
1
        prompt: "Discard changes?".to_string(),
25
1
        action: ConfirmAction::DiscardForm,
26
1
        focus: None,
27
1
    });
28
1
    redraw(&mut app);
29
1
    app
30
1
}
31

            
32
18
fn place_of(app: &App) -> Place {
33
18
    let slot = app.hits.dialog().expect("a dialog was drawn");
34
18
    app.overlays
35
18
        .placement()
36
18
        .place(slot.auto, slot.min, slot.host)
37
18
}
38

            
39
7
fn begin(app: &mut App) {
40
7
    press(app, ctrl(KeyCode::F(5)));
41
7
    assert_eq!(current_focus(app), FocusTarget::Drag);
42
7
}
43

            
44
9
fn change(app: &mut App, change: Change) {
45
9
    apply(app, Intent::Drag(change));
46
9
    redraw(app);
47
9
}
48

            
49
3
fn shift(code: KeyCode) -> KeyEvent {
50
3
    KeyEvent::new(code, KeyModifiers::SHIFT)
51
3
}
52

            
53
#[test]
54
1
fn ctrl_f5_moves_the_form_with_the_arrows_and_enter_keeps_it() {
55
1
    let mut app = form_app();
56
1
    let start = place_of(&app);
57
1
    begin(&mut app);
58
1
    press(&mut app, key(KeyCode::Right));
59
1
    press(&mut app, key(KeyCode::Down));
60
1
    press(&mut app, key(KeyCode::Down));
61
1
    redraw(&mut app);
62
1
    let moved = place_of(&app);
63
1
    assert_eq!((moved.x, moved.y), (start.x + 1, start.y + 2));
64
1
    press(&mut app, key(KeyCode::Enter));
65
1
    assert_eq!(current_focus(&app), FocusTarget::Overlay);
66
1
    redraw(&mut app);
67
1
    assert_eq!(place_of(&app), moved);
68
1
}
69

            
70
#[test]
71
1
fn esc_puts_the_dialog_back() {
72
1
    let mut app = form_app();
73
1
    let start = place_of(&app);
74
1
    begin(&mut app);
75
1
    press(&mut app, key(KeyCode::Left));
76
1
    press(&mut app, shift(KeyCode::Right));
77
1
    redraw(&mut app);
78
1
    assert_ne!(place_of(&app), start);
79
1
    press(&mut app, key(KeyCode::Esc));
80
1
    redraw(&mut app);
81
1
    assert_eq!(place_of(&app), start);
82
1
    assert_eq!(app.overlays.placement(), Placement::default());
83
1
    assert_eq!(current_focus(&app), FocusTarget::Overlay);
84
1
}
85

            
86
#[test]
87
1
fn the_keys_of_the_mode_do_not_reach_the_form() {
88
1
    let mut app = form_app();
89
1
    let before = format!("{:?}", app.overlays.top());
90
1
    begin(&mut app);
91
1
    press(&mut app, key(KeyCode::Char('x')));
92
1
    press(&mut app, key(KeyCode::Esc));
93
1
    assert_eq!(format!("{:?}", app.overlays.top()), before);
94
1
}
95

            
96
#[test]
97
1
fn the_limits_are_one_column_inside_left_and_right_and_the_top_inside() {
98
1
    let mut app = form_app();
99
1
    let host = app.hits.dialog().expect("drawn").host;
100
1
    begin(&mut app);
101
1
    let width = i32::from(place_of(&app).width);
102
1
    change(&mut app, Change::Move(-500, 0));
103
1
    assert_eq!(place_of(&app).x, 1 - width);
104
1
    change(&mut app, Change::Move(1000, 0));
105
1
    assert_eq!(place_of(&app).x, i32::from(host.right()) - 1);
106
1
    change(&mut app, Change::Move(0, -500));
107
1
    assert_eq!(place_of(&app).y, i32::from(host.y));
108
1
    change(&mut app, Change::Move(0, 500));
109
1
    assert_eq!(place_of(&app).y, i32::from(host.bottom()) - 1);
110
1
}
111

            
112
#[test]
113
1
fn a_form_is_sized_between_its_least_and_the_desktop() {
114
1
    let mut app = form_app();
115
1
    let (slot, start) = (app.hits.dialog().expect("drawn"), place_of(&app));
116
1
    begin(&mut app);
117
1
    press(&mut app, shift(KeyCode::Right));
118
1
    press(&mut app, shift(KeyCode::Down));
119
1
    redraw(&mut app);
120
1
    let grown = place_of(&app);
121
1
    assert_eq!(
122
1
        (grown.width, grown.height),
123
1
        (start.width + 1, start.height + 1)
124
    );
125
1
    change(&mut app, Change::Grow(-500, -500));
126
1
    let least = place_of(&app);
127
1
    assert_eq!((least.width, least.height), slot.min);
128
1
    change(&mut app, Change::Grow(500, 500));
129
1
    let most = place_of(&app);
130
1
    assert_eq!(
131
1
        (most.width, most.height),
132
1
        (slot.host.width, slot.host.height)
133
    );
134
1
}
135

            
136
#[test]
137
1
fn a_confirm_moves_but_is_not_sized() {
138
1
    let mut app = confirm_app();
139
1
    let start = place_of(&app);
140
1
    begin(&mut app);
141
1
    change(&mut app, Change::Grow(10, 3));
142
1
    change(&mut app, Change::SizeTo(70, 20));
143
1
    assert_eq!(place_of(&app), start);
144
1
    change(&mut app, Change::Move(3, 1));
145
1
    let moved = place_of(&app);
146
1
    assert_eq!((moved.x, moved.y), (start.x + 3, start.y + 1));
147
1
    assert_eq!((moved.width, moved.height), (start.width, start.height));
148
1
}
149

            
150
#[test]
151
1
fn an_overlay_opened_during_the_mode_ends_it() {
152
1
    let mut app = form_app();
153
1
    begin(&mut app);
154
1
    app.overlays.push(Modal::Form(Form::config_set()));
155
1
    assert!(!app.dragging());
156
1
    assert_eq!(current_focus(&app), FocusTarget::Overlay);
157
1
}
158

            
159
#[test]
160
1
fn ctrl_f5_needs_a_drawn_dialog() {
161
1
    let mut app = crate::test_support::app();
162
1
    app.overlays.push(Modal::Form(Form::config_set()));
163
1
    press(&mut app, ctrl(KeyCode::F(5)));
164
1
    assert_eq!(current_focus(&app), FocusTarget::Overlay);
165
1
}