1
//! Ctrl-F5: the front window moves and sizes from the keyboard until it is kept or put back.
2

            
3
use super::report_chart::{call_for, chart_requests, echoed, show, with_table};
4
use super::window_keys::{answer_chart, drawn_kitty_chart};
5
use crate::app::{App, MenuId};
6
use crate::event::{Intent, apply};
7
use crate::focus::{FocusTarget, current_focus};
8
use crate::form::Form;
9
use crate::keymap::{here, in_view_all};
10
use crate::modal::Modal;
11
use crate::test_support::{alt, ctrl, draw_frame, hint_pairs, key, press};
12
use crate::view::ViewId;
13
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
14
use ratatui::layout::Rect;
15
use rpc::chart::ChartBackend;
16
use tvui::{Change, FrameMode, Role, Window};
17

            
18
const START: Rect = Rect::new(10, 5, 30, 10);
19

            
20
11
fn chord(code: KeyCode, modifiers: KeyModifiers) -> KeyEvent {
21
11
    KeyEvent::new(code, modifiers)
22
11
}
23

            
24
7
fn shift(code: KeyCode) -> KeyEvent {
25
7
    chord(code, KeyModifiers::SHIFT)
26
7
}
27

            
28
4
fn ctrl_shift(code: KeyCode) -> KeyEvent {
29
4
    chord(code, KeyModifiers::CONTROL | KeyModifiers::SHIFT)
30
4
}
31

            
32
28
fn start_size_move(app: &mut App) {
33
28
    press(app, ctrl(KeyCode::F(5)));
34
28
}
35

            
36
17
fn front(app: &App) -> Window<crate::window::WindowId> {
37
17
    let id = app.front_window().expect("a front window");
38
17
    app.desktop.window(id).expect("it is open")
39
17
}
40

            
41
23
fn rect(app: &App) -> Rect {
42
23
    let id = app.front_window().expect("a front window");
43
23
    let place = app.desktop.restored_place(id, app.desktop_rect());
44
23
    let place = place.expect("it is open");
45
23
    crate::test_support::flat(place)
46
23
}
47

            
48
12
fn placed(app: &mut App) {
49
12
    let window = front(app);
50
12
    app.desktop.restore(Window {
51
12
        place: tvui::Place::from(START),
52
12
        zoomed: false,
53
12
        ..window
54
12
    });
55
12
}
56

            
57
11
fn placed_app() -> App {
58
11
    let mut app = crate::test_support::app();
59
11
    placed(&mut app);
60
11
    app
61
11
}
62

            
63
3
fn mode_keys() -> [(&'static str, Intent); 22] {
64
3
    let drag = Intent::Drag;
65
3
    [
66
3
        ("Left", drag(Change::Move(-1, 0))),
67
3
        ("Right", drag(Change::Move(1, 0))),
68
3
        ("Up", drag(Change::Move(0, -1))),
69
3
        ("Down", drag(Change::Move(0, 1))),
70
3
        ("S-Left", drag(Change::Grow(-1, 0))),
71
3
        ("S-Right", drag(Change::Grow(1, 0))),
72
3
        ("S-Up", drag(Change::Grow(0, -1))),
73
3
        ("S-Down", drag(Change::Grow(0, 1))),
74
3
        ("C-Left", drag(Change::Move(-8, 0))),
75
3
        ("C-Right", drag(Change::Move(8, 0))),
76
3
        ("C-Up", drag(Change::Move(0, -4))),
77
3
        ("C-Down", drag(Change::Move(0, 4))),
78
3
        ("C-S-Left", drag(Change::Grow(-8, 0))),
79
3
        ("C-S-Right", drag(Change::Grow(8, 0))),
80
3
        ("C-S-Up", drag(Change::Grow(0, -4))),
81
3
        ("C-S-Down", drag(Change::Grow(0, 4))),
82
3
        ("Home", drag(Change::Left)),
83
3
        ("End", drag(Change::Right)),
84
3
        ("PageUp", drag(Change::Top)),
85
3
        ("PageDown", drag(Change::Bottom)),
86
3
        ("Enter", Intent::DragDone),
87
3
        ("Esc", Intent::DragCancel),
88
3
    ]
89
3
}
90

            
91
#[test]
92
1
fn ctrl_f5_puts_the_front_window_in_the_dragging_state() {
93
1
    let mut app = placed_app();
94
1
    let id = app.front_window().expect("a front window");
95
1
    assert_eq!(app.frame_state(id).mode, FrameMode::Active);
96
1
    start_size_move(&mut app);
97
1
    assert_eq!(current_focus(&app), FocusTarget::Drag);
98
1
    assert_eq!(app.frame_state(id).mode, FrameMode::Dragging);
99
1
}
100

            
101
#[test]
102
1
fn the_arrows_move_by_one_and_shift_arrows_size_by_one() {
103
1
    let mut app = placed_app();
104
1
    start_size_move(&mut app);
105
1
    let steps = [
106
1
        (key(KeyCode::Right), Rect::new(11, 5, 30, 10)),
107
1
        (key(KeyCode::Down), Rect::new(11, 6, 30, 10)),
108
1
        (key(KeyCode::Left), Rect::new(10, 6, 30, 10)),
109
1
        (key(KeyCode::Up), Rect::new(10, 5, 30, 10)),
110
1
        (shift(KeyCode::Right), Rect::new(10, 5, 31, 10)),
111
1
        (shift(KeyCode::Down), Rect::new(10, 5, 31, 11)),
112
1
        (shift(KeyCode::Left), Rect::new(10, 5, 30, 11)),
113
1
        (shift(KeyCode::Up), Rect::new(10, 5, 30, 10)),
114
1
    ];
115
1
    for (event, expected) in steps {
116
8
        press(&mut app, event);
117
8
        assert_eq!(rect(&app), expected, "{event:?}");
118
    }
119
1
}
120

            
121
#[test]
122
1
fn ctrl_arrows_step_eight_columns_or_four_rows() {
123
1
    let mut app = placed_app();
124
1
    start_size_move(&mut app);
125
1
    let steps = [
126
1
        (ctrl(KeyCode::Right), Rect::new(18, 5, 30, 10)),
127
1
        (ctrl(KeyCode::Down), Rect::new(18, 9, 30, 10)),
128
1
        (ctrl(KeyCode::Left), Rect::new(10, 9, 30, 10)),
129
1
        (ctrl(KeyCode::Up), Rect::new(10, 5, 30, 10)),
130
1
        (ctrl_shift(KeyCode::Right), Rect::new(10, 5, 38, 10)),
131
1
        (ctrl_shift(KeyCode::Down), Rect::new(10, 5, 38, 14)),
132
1
        (ctrl_shift(KeyCode::Left), Rect::new(10, 5, 30, 14)),
133
1
        (ctrl_shift(KeyCode::Up), Rect::new(10, 5, 30, 10)),
134
1
    ];
135
1
    for (event, expected) in steps {
136
8
        press(&mut app, event);
137
8
        assert_eq!(rect(&app), expected, "{event:?}");
138
    }
139
1
}
140

            
141
#[test]
142
1
fn home_end_page_up_and_page_down_go_to_the_desktop_edges() {
143
1
    let mut app = placed_app();
144
1
    let area = app.desktop_rect();
145
1
    start_size_move(&mut app);
146
1
    press(&mut app, key(KeyCode::Home));
147
1
    assert_eq!(rect(&app).x, area.x);
148
1
    press(&mut app, key(KeyCode::End));
149
1
    assert_eq!(rect(&app).right(), area.right());
150
1
    press(&mut app, key(KeyCode::PageUp));
151
1
    assert_eq!(rect(&app).y, area.y);
152
1
    press(&mut app, key(KeyCode::PageDown));
153
1
    assert_eq!(rect(&app).bottom(), area.bottom());
154
1
}
155

            
156
#[test]
157
1
fn a_window_moved_off_the_right_edge_keeps_a_column_and_draws_it() {
158
1
    let mut app = placed_app();
159
1
    start_size_move(&mut app);
160
1
    for _ in 0..12 {
161
12
        press(&mut app, ctrl(KeyCode::Right));
162
    }
163
1
    let area = app.desktop_rect();
164
1
    assert_eq!(rect(&app).x, area.right() - 1);
165
1
    let (buffer, _) = draw_frame(&app, 80, 24);
166
1
    assert_ne!(buffer[(area.right() - 1, START.y)].symbol(), "\u{2591}");
167
1
}
168

            
169
#[test]
170
1
fn enter_keeps_the_new_rect_and_leaves_the_mode() {
171
1
    let mut app = placed_app();
172
1
    start_size_move(&mut app);
173
1
    press(&mut app, key(KeyCode::Right));
174
1
    press(&mut app, shift(KeyCode::Down));
175
1
    press(&mut app, key(KeyCode::Enter));
176
1
    assert_eq!(rect(&app), Rect::new(11, 5, 30, 11));
177
1
    assert_eq!(current_focus(&app), FocusTarget::ViewPane);
178
1
    let id = app.front_window().expect("a front window");
179
1
    assert_eq!(app.frame_state(id).mode, FrameMode::Active);
180
1
}
181

            
182
#[test]
183
1
fn a_zoomed_window_moves_from_the_full_desktop() {
184
1
    let mut app = crate::test_support::app();
185
1
    let area = app.desktop_rect();
186
1
    assert!(front(&app).zoomed);
187
1
    start_size_move(&mut app);
188
1
    press(&mut app, key(KeyCode::Right));
189
1
    assert!(!front(&app).zoomed);
190
1
    assert_eq!(
191
1
        rect(&app),
192
1
        Rect::new(area.x + 1, area.y, area.width, area.height)
193
    );
194
1
}
195

            
196
#[test]
197
1
fn escape_restores_the_rect_and_the_zoom() {
198
1
    let mut app = crate::test_support::app();
199
1
    let before = front(&app);
200
1
    assert!(before.zoomed);
201
1
    start_size_move(&mut app);
202
1
    press(&mut app, key(KeyCode::Right));
203
1
    press(&mut app, shift(KeyCode::Down));
204
1
    assert!(!front(&app).zoomed);
205
1
    press(&mut app, key(KeyCode::Esc));
206
1
    assert_eq!(front(&app), before);
207
1
    assert_eq!(current_focus(&app), FocusTarget::ViewPane);
208
1
}
209

            
210
#[test]
211
1
fn other_keys_do_nothing_in_the_mode_and_the_menu_stays_closed() {
212
1
    let mut app = placed_app();
213
1
    start_size_move(&mut app);
214
1
    let order: Vec<_> = app.desktop.iter_back_to_front().copied().collect();
215
1
    let ignored = [
216
1
        key(KeyCode::Char('q')),
217
1
        key(KeyCode::Char(':')),
218
1
        key(KeyCode::Char('?')),
219
1
        key(KeyCode::Tab),
220
1
        key(KeyCode::F(1)),
221
1
        key(KeyCode::F(5)),
222
1
        key(KeyCode::F(6)),
223
1
        key(KeyCode::F(10)),
224
1
        ctrl(KeyCode::F(4)),
225
1
        ctrl(KeyCode::F(5)),
226
1
        alt(KeyCode::Char('x')),
227
1
        alt(KeyCode::Char('m')),
228
1
        alt(KeyCode::Char('3')),
229
1
    ];
230
1
    for event in ignored {
231
13
        press(&mut app, event);
232
13
        assert_eq!(current_focus(&app), FocusTarget::Drag, "{event:?}");
233
13
        assert!(app.menu.is_none() && !app.should_quit, "{event:?}");
234
13
        assert!(app.overlays.is_empty() && !app.cmdline.active, "{event:?}");
235
13
        let now: Vec<_> = app.desktop.iter_back_to_front().copied().collect();
236
13
        assert_eq!(now, order, "{event:?}");
237
    }
238
1
}
239

            
240
#[test]
241
1
fn a_dialog_blocks_entering_the_mode() {
242
1
    let mut app = placed_app();
243
1
    app.open_dialog_on_front(Modal::Form(Form::commodity_create()));
244
1
    start_size_move(&mut app);
245
1
    assert!(!app.dragging());
246
1
    assert_eq!(current_focus(&app), FocusTarget::Overlay);
247
1
    apply(&mut app, Intent::SizeWindow);
248
1
    assert!(!app.dragging());
249
1
}
250

            
251
#[test]
252
1
fn the_first_entry_of_the_window_menu_starts_the_mode() {
253
1
    let mut app = placed_app();
254
1
    app.open_menu_of(MenuId::Window);
255
1
    press(&mut app, key(KeyCode::Enter));
256
1
    assert_eq!(current_focus(&app), FocusTarget::Drag);
257
1
    assert!(app.menu.is_none());
258
1
    app.end_drag(false);
259
1
    app.open_menu_of(MenuId::Window);
260
1
    press(&mut app, key(KeyCode::Char('s')));
261
1
    assert_eq!(current_focus(&app), FocusTarget::Drag);
262
1
}
263

            
264
#[test]
265
1
fn every_preset_binds_ctrl_f5_and_the_mode_keys() {
266
1
    let apps = [
267
1
        crate::test_support::app(),
268
1
        crate::test_support::emacs_app(),
269
1
        crate::test_support::vim_app(),
270
1
    ];
271
1
    for mut app in apps {
272
3
        let name = app.keys.indicator();
273
3
        assert_eq!(
274
3
            in_view_all(&app).bound("C-F5"),
275
3
            Some(Intent::SizeWindow),
276
            "{name}"
277
        );
278
3
        start_size_move(&mut app);
279
3
        assert_eq!(current_focus(&app), FocusTarget::Drag, "{name}");
280
3
        let keys = here(&app);
281
3
        for (chord, intent) in mode_keys() {
282
66
            assert_eq!(keys.bound(chord), Some(intent), "{name} {chord}");
283
        }
284
    }
285
1
}
286

            
287
#[test]
288
1
fn the_status_line_shows_the_mode_hints() {
289
1
    let mut app = placed_app();
290
1
    start_size_move(&mut app);
291
1
    let shown = hint_pairs(&app);
292
1
    let shown: Vec<(&str, &str)> = shown
293
1
        .iter()
294
4
        .map(|(key, label)| (key.as_str(), label.as_str()))
295
        .collect();
296
1
    let hints = [
297
1
        ("Left", "Move"),
298
1
        ("S-Left", "Size"),
299
1
        ("Enter", "OK"),
300
1
        ("Esc", "Cancel"),
301
1
    ];
302
1
    assert_eq!(shown, hints);
303
1
}
304

            
305
#[test]
306
1
fn the_dragging_frame_is_single_in_the_icon_colour_with_its_icons() {
307
1
    let mut app = placed_app();
308
1
    start_size_move(&mut app);
309
1
    let (buffer, _) = draw_frame(&app, 80, 24);
310
1
    let cell = &buffer[(START.x, START.y)];
311
1
    assert_eq!(cell.symbol(), "\u{250c}");
312
1
    assert_eq!(cell.style().fg, app.theme.style(Role::FrameIcon).fg);
313
1
    let row: String = (START.x..START.right())
314
30
        .map(|x| buffer[(x, START.y)].symbol().to_string())
315
        .collect();
316
1
    assert!(row.starts_with("\u{250c}\u{2500}[\u{25a0}]"), "{row}");
317
1
    let icon = buffer[(START.x + 3, START.y)].style();
318
1
    assert_eq!(icon.fg, app.theme.style(Role::FrameIcon).fg);
319
1
}
320

            
321
#[tokio::test]
322
1
async fn accepting_fetches_the_chart_again_at_the_new_size() {
323
1
    let (mut app, sent) = with_table(60, 24, ChartBackend::Halfblocks);
324
1
    show(&mut app);
325
1
    press(&mut app, key(KeyCode::F(5)));
326
1
    placed(&mut app);
327
1
    let before = chart_requests(&app).len();
328
1
    start_size_move(&mut app);
329
1
    press(&mut app, shift(KeyCode::Left));
330
1
    press(&mut app, key(KeyCode::Right));
331
1
    assert_eq!(chart_requests(&app).len(), before);
332
1
    press(&mut app, key(KeyCode::Enter));
333
1
    assert_eq!(chart_requests(&app).len(), before + 1);
334
    // The window is 29x10 after the shrink: 27x8 cells inside its border.
335
1
    assert!(echoed(&mut app, &sent).await.contains(&call_for(270, 160)));
336
1
}
337

            
338
#[tokio::test]
339
1
async fn cancelling_fetches_the_chart_again_at_the_old_size() {
340
1
    let (mut app, _) = with_table(60, 24, ChartBackend::Halfblocks);
341
1
    show(&mut app);
342
1
    let before = chart_requests(&app).len();
343
1
    start_size_move(&mut app);
344
1
    press(&mut app, key(KeyCode::Esc));
345
1
    assert_eq!(chart_requests(&app).len(), before + 1);
346
1
}
347

            
348
#[tokio::test]
349
1
async fn the_kitty_image_goes_when_the_mode_starts_and_returns_with_a_fresh_chart() {
350
1
    let (mut app, image) = drawn_kitty_chart();
351
1
    start_size_move(&mut app);
352
1
    assert_eq!(app.take_outbound(), vec![tvui::kitty_delete(image)]);
353
1
    press(&mut app, key(KeyCode::Enter));
354
1
    answer_chart(&mut app);
355
1
    assert!(
356
1
        super::report_chart::text(&super::report_chart::rendered(&app, 40, 20)).contains("a=T")
357
1
    );
358
1
}
359

            
360
#[test]
361
1
fn every_view_window_can_be_sized() {
362
1
    for kind in ViewId::ALL {
363
10
        let mut app = crate::test_support::app();
364
10
        app.switch_tab(kind);
365
10
        start_size_move(&mut app);
366
10
        assert_eq!(current_focus(&app), FocusTarget::Drag, "{kind:?}");
367
10
        press(&mut app, key(KeyCode::Enter));
368
10
        assert_ne!(current_focus(&app), FocusTarget::Drag, "{kind:?}");
369
    }
370
1
}