1
//! A dialog sits where it was moved to, hangs off the left, right and bottom at full size, and
2
//! shows only what the desktop does.
3

            
4
use crate::app::App;
5
use crate::event::{Intent, apply};
6
use crate::form::Form;
7
use crate::hits::{Hits, Slot, Target};
8
use crate::modal::{ConfirmAction, Modal};
9
use crate::overlay::Placement;
10
use ratatui::Terminal;
11
use ratatui::backend::TestBackend;
12
use ratatui::buffer::Buffer;
13
use ratatui::layout::{Position, Rect};
14
use tvui::Place;
15

            
16
const SIZE: (u16, u16) = (80, 24);
17

            
18
struct Drawn {
19
    buffer: Buffer,
20
    hits: Hits,
21
    cursor: Option<Position>,
22
}
23

            
24
33
fn drawn(app: &App) -> Drawn {
25
33
    let mut terminal = Terminal::new(TestBackend::new(SIZE.0, SIZE.1)).expect("test terminal");
26
33
    let mut hits = Hits::default();
27
33
    terminal
28
33
        .draw(|frame| crate::draw::draw(frame, app, &mut hits))
29
33
        .expect("the frame draws");
30
33
    let backend = terminal.backend();
31
    Drawn {
32
33
        buffer: backend.buffer().clone(),
33
33
        hits,
34
33
        cursor: backend.cursor_visible().then(|| backend.cursor_position()),
35
    }
36
33
}
37

            
38
5
fn confirm() -> App {
39
5
    let mut app = crate::test_support::app();
40
5
    app.overlays.push(Modal::Confirm {
41
5
        prompt: "Discard changes?".to_string(),
42
5
        action: ConfirmAction::DiscardForm,
43
5
        focus: None,
44
5
    });
45
5
    app
46
5
}
47

            
48
18
fn slot(app: &App) -> Slot {
49
18
    drawn(app).hits.dialog().expect("a dialog was drawn")
50
18
}
51

            
52
/// Puts the dialog on top with its origin at `x` and `y`.
53
8
fn put_at(app: &mut App, x: i32, y: i32) -> Place {
54
8
    let auto = slot(app).auto;
55
8
    let place = Place {
56
8
        x,
57
8
        y,
58
8
        ..Place::from(auto)
59
8
    };
60
8
    app.overlays.set_placement(Placement::of(place, auto));
61
8
    place
62
8
}
63

            
64
170
fn glyph(drawn: &Drawn, x: u16, y: u16) -> &str {
65
170
    drawn.buffer[(x, y)].symbol()
66
170
}
67

            
68
7
fn title(drawn: &Drawn) -> Rect {
69
7
    let found = drawn
70
7
        .hits
71
7
        .entries()
72
7
        .into_iter()
73
123
        .rfind(|(_, t)| *t == Target::Title);
74
7
    found.map(|(rect, _)| rect).expect("a title row was drawn")
75
7
}
76

            
77
/// What the dialog drew from its top row `y` down, not the window under it.
78
2
fn dialog_hits(drawn: &Drawn, y: u16) -> Vec<(Rect, Target)> {
79
2
    let entries = drawn.hits.entries();
80
13
    let from = entries.iter().rposition(|(_, t)| *t == Target::Block);
81
2
    let hits = entries.into_iter().skip(from.unwrap_or(0) + 1);
82
11
    hits.filter(|(rect, t)| rect.y >= y && !matches!(t, Target::Hint(_)))
83
        .collect()
84
2
}
85

            
86
#[test]
87
1
fn a_dialog_opens_centred_and_a_moved_one_keeps_its_offset() {
88
1
    let mut app = confirm();
89
1
    let auto = slot(&app).auto;
90
1
    let first = drawn(&app);
91
1
    assert_eq!(title(&first).x, auto.x);
92
1
    put_at(&mut app, i32::from(auto.x) + 3, i32::from(auto.y) + 1);
93
1
    let moved = drawn(&app);
94
1
    assert_eq!(title(&moved).x, auto.x + 3);
95
1
    assert_eq!(title(&moved).y, auto.y + 1);
96
1
    assert_eq!(glyph(&moved, auto.x + 3, auto.y + 1), "╔");
97
1
    assert_eq!(glyph(&moved, auto.x, auto.y), " ");
98
1
}
99

            
100
#[test]
101
1
fn a_dialog_off_the_right_edge_is_clipped_to_the_cells_the_screen_shows() {
102
1
    let mut app = confirm();
103
1
    let host = slot(&app).host;
104
1
    let at = i32::from(host.right()) - 4;
105
1
    put_at(&mut app, at, i32::from(host.y) + 2);
106
1
    let frame = drawn(&app);
107
1
    let (x, y) = (host.right() - 4, host.y + 2);
108
1
    assert_eq!(glyph(&frame, x, y), "╔");
109
1
    assert_eq!(glyph(&frame, x + 3, y), "■");
110
1
    assert_eq!(title(&frame), Rect::new(x, y, 4, 1));
111
1
    let dialog = dialog_hits(&frame, y);
112
1
    assert!(!dialog.is_empty());
113
1
    for (rect, _) in dialog {
114
2
        assert!(
115
2
            rect.right() <= SIZE.0 && rect.bottom() <= host.bottom(),
116
            "{rect:?}"
117
        );
118
    }
119
1
    assert!(
120
1
        frame
121
1
            .hits
122
1
            .entries()
123
1
            .iter()
124
15
            .all(|(_, t)| !matches!(t, Target::Button(_)))
125
    );
126
1
}
127

            
128
#[test]
129
1
fn a_dialog_off_the_left_edge_shows_its_right_cells() {
130
1
    let mut app = confirm();
131
1
    let slot = slot(&app);
132
1
    let width = i32::from(slot.auto.width);
133
1
    put_at(&mut app, 3 - width, i32::from(slot.host.y) + 2);
134
1
    let frame = drawn(&app);
135
1
    let y = slot.host.y + 2;
136
1
    assert_eq!(glyph(&frame, 2, y), "╗");
137
1
    assert_eq!(glyph(&frame, 0, y), "═");
138
1
    assert_eq!(title(&frame), Rect::new(0, y, 3, 1));
139
1
    assert!(
140
1
        dialog_hits(&frame, y)
141
1
            .iter()
142
1
            .all(|(_, t)| *t != Target::Close)
143
    );
144
1
}
145

            
146
#[test]
147
1
fn a_dialog_off_the_bottom_leaves_the_status_line_alone() {
148
1
    let mut app = confirm();
149
1
    let slot = slot(&app);
150
1
    let inside = drawn(&app);
151
1
    put_at(
152
1
        &mut app,
153
1
        i32::from(slot.auto.x),
154
1
        i32::from(slot.host.bottom()) - 2,
155
1
    );
156
1
    let frame = drawn(&app);
157
1
    let status = slot.host.bottom();
158
160
    let row = |drawn: &Drawn| -> String { (0..SIZE.0).map(|x| glyph(drawn, x, status)).collect() };
159
1
    assert_eq!(row(&frame), row(&inside));
160
1
    assert_eq!(glyph(&frame, slot.auto.x, status - 2), "╔");
161
1
    assert_eq!(glyph(&frame, slot.auto.x, status - 1), "║");
162
1
}
163

            
164
2
fn typed_form() -> App {
165
2
    let mut form = Form::config_set();
166
2
    form.focus = 1;
167
2
    let mut app = crate::test_support::app();
168
2
    app.overlays.push(Modal::Form(form));
169
2
    app
170
2
}
171

            
172
#[test]
173
1
fn the_form_cursor_follows_a_moved_dialog_and_hides_when_its_cell_is_clipped() {
174
1
    let mut app = typed_form();
175
1
    let Some(home) = drawn(&app).cursor else {
176
        panic!("a focused text field shows the cursor");
177
    };
178
1
    let slot = slot(&app);
179
1
    let (x, y) = (i32::from(slot.auto.x) + 2, i32::from(slot.auto.y) + 1);
180
1
    put_at(&mut app, x, y);
181
1
    assert_eq!(
182
1
        drawn(&app).cursor,
183
1
        Some(Position::new(home.x + 2, home.y + 1))
184
    );
185
1
    put_at(&mut app, i32::from(slot.host.right()) - 1, y);
186
1
    assert_eq!(drawn(&app).cursor, None);
187
1
    put_at(&mut app, 1 - i32::from(slot.auto.width), y);
188
1
    assert_eq!(drawn(&app).cursor, None);
189
1
}
190

            
191
#[test]
192
1
fn only_a_confirm_has_its_own_size_as_the_least() {
193
1
    let mut app = confirm();
194
1
    let confirm = slot(&app);
195
1
    assert_eq!(confirm.min, (confirm.auto.width, confirm.auto.height));
196
1
    apply(&mut app, Intent::CloseTopmost);
197
1
    crate::test_support::open_draft(&mut app);
198
1
    let help = slot(&app);
199
1
    assert_ne!(help.min, (help.auto.width, help.auto.height));
200
1
}
201

            
202
#[test]
203
1
fn a_moved_dialog_keeps_its_corner_when_its_content_grows() {
204
1
    let mut form = Form::transaction_create();
205
1
    form.focus = 3;
206
1
    let mut app = crate::test_support::app();
207
1
    app.overlays.push(Modal::Form(form));
208
1
    let auto = slot(&app).auto;
209
1
    put_at(&mut app, i32::from(auto.x) + 2, i32::from(auto.y) + 1);
210
1
    let before = title(&drawn(&app));
211
1
    if let Some(Modal::Form(form)) = app.overlays.top_mut()
212
        && let Some(crate::widgets::Widget::Splits(splits)) =
213
1
            form.fields.get_mut(3).map(|f| &mut f.widget)
214
    {
215
4
        (0..4).for_each(|_| splits.add_row());
216
    }
217
1
    assert_ne!(slot(&app).auto.height, auto.height);
218
1
    let after = title(&drawn(&app));
219
1
    assert_eq!((after.x, after.y), (before.x, before.y));
220
1
}
221

            
222
#[test]
223
1
fn a_dialog_in_size_move_mode_has_the_dragging_frame_and_keeps_the_cursor() {
224
1
    let mut app = typed_form();
225
1
    app.hits = drawn(&app).hits;
226
1
    let auto = slot(&app).auto;
227
1
    apply(&mut app, Intent::SizeWindow);
228
1
    let frame = drawn(&app);
229
1
    assert_eq!(glyph(&frame, auto.x, auto.y), "┌");
230
1
    assert!(frame.cursor.is_some());
231
1
    apply(&mut app, Intent::DragDone);
232
1
    assert_eq!(glyph(&drawn(&app), auto.x, auto.y), "╔");
233
1
}