1
//! The `▼` mark opens a list at once; a click in a text puts the caret under it.
2

            
3
use super::click_tests::{click_on, now, presets, press};
4
use super::*;
5
use crate::form::Field;
6
use crate::test_support::{area_of, redraw};
7
use crate::widgets::grid::CellId;
8
use crate::widgets::splits::{COL_FROM, COL_VALUE};
9
use crate::widgets::{Editor, SelectOption, SelectWidget, SplitsWidget, Widget};
10
use ratatui::Terminal;
11
use ratatui::backend::TestBackend;
12

            
13
const SPLITS: usize = 3;
14

            
15
95
pub(super) fn form_mut(app: &mut App) -> &mut crate::form::Form {
16
95
    match app.overlays.top_mut() {
17
95
        Some(Modal::Form(form)) => form,
18
        _ => panic!("no form is open"),
19
    }
20
95
}
21

            
22
/// A transaction form with `field` ahead of the splits.
23
41
pub(super) fn with_field(mut app: App, field: Field) -> App {
24
41
    let mut form = crate::form::Form::transaction_create();
25
41
    form.fields.insert(0, field);
26
41
    app.overlays.push(Modal::Form(form));
27
41
    app
28
41
}
29

            
30
41
fn field(label: &'static str, widget: Widget) -> Field {
31
41
    Field {
32
41
        key: None,
33
41
        label,
34
41
        widget,
35
41
    }
36
41
}
37

            
38
22
pub(super) fn select_field() -> Field {
39
22
    let mut select = SelectWidget::new();
40
    let option = |i: usize| SelectOption {
41
66
        id: format!("id{i}"),
42
66
        label: format!("label{i}"),
43
66
    };
44
22
    select.set_options((0..3).map(option).collect());
45
22
    field("pick", Widget::Select(select))
46
22
}
47

            
48
13
fn text_field(text: &str) -> Field {
49
13
    field("note", Widget::Text(Editor::with_buffer(text)))
50
13
}
51

            
52
6
fn area_field(text: &str, cursor: (usize, usize)) -> Field {
53
6
    let mut editor = Editor::with_buffer(text).multiline(true);
54
6
    editor.set_cursor_position(cursor);
55
6
    field("memo", Widget::Text(editor))
56
6
}
57

            
58
23
fn editor_of(app: &mut App, index: usize) -> &Editor {
59
23
    match form_mut(app).fields.get(index).map(|field| &field.widget) {
60
23
        Some(Widget::Text(editor)) => editor,
61
        _ => panic!("field {index} is not a text"),
62
    }
63
23
}
64

            
65
19
fn click_at(app: &mut App, target: Target, dx: u16, dy: u16) {
66
19
    redraw(app);
67
19
    let area = area_of(app, target);
68
19
    let position = Position::new(area.x + dx, area.y + dy);
69
19
    press(
70
19
        app,
71
19
        position,
72
19
        MouseEventKind::Down(MouseButton::Left),
73
19
        now(),
74
19
    );
75
19
}
76

            
77
22
fn caret(field: usize) -> Target {
78
22
    Target::Caret { field, cell: None }
79
22
}
80

            
81
20
fn cell_id(col: usize) -> CellId {
82
20
    CellId {
83
20
        tag: false,
84
20
        row: 0,
85
20
        col,
86
20
    }
87
20
}
88

            
89
11
fn splits_of(app: &mut App) -> &mut SplitsWidget {
90
11
    match form_mut(app)
91
11
        .fields
92
11
        .get_mut(SPLITS + 1)
93
11
        .map(|f| &mut f.widget)
94
    {
95
11
        Some(Widget::Splits(splits)) => splits,
96
        _ => panic!("no splits"),
97
    }
98
11
}
99

            
100
#[test]
101
1
fn a_click_on_the_mark_of_an_unfocused_select_opens_it_at_once() {
102
1
    for app in presets() {
103
3
        let mut app = with_field(app, select_field());
104
3
        form_mut(&mut app).focus = 1;
105
3
        click_on(
106
3
            &mut app,
107
3
            Target::Mark {
108
3
                field: 0,
109
3
                cell: None,
110
3
            },
111
3
            now(),
112
3
        );
113
3
        let form = form_mut(&mut app);
114
3
        assert_eq!(form.focus, 0);
115
3
        assert!(form.focused_select().is_some_and(|select| select.open));
116
    }
117
1
}
118

            
119
#[test]
120
1
fn a_click_elsewhere_on_an_unfocused_select_only_focuses_it() {
121
1
    for app in presets() {
122
3
        let mut app = with_field(app, select_field());
123
3
        form_mut(&mut app).focus = 1;
124
3
        click_on(&mut app, Target::Field(0), now());
125
3
        let form = form_mut(&mut app);
126
3
        assert_eq!(form.focus, 0);
127
3
        assert!(form.focused_select().is_some_and(|select| !select.open));
128
    }
129
1
}
130

            
131
#[test]
132
1
fn a_click_on_the_mark_of_a_select_cell_opens_it_at_once() {
133
1
    for app in presets() {
134
3
        let mut app = with_field(app, select_field());
135
3
        let target = Target::Mark {
136
3
            field: SPLITS + 1,
137
3
            cell: Some(cell_id(COL_FROM)),
138
3
        };
139
3
        click_on(&mut app, target, now());
140
3
        let form = form_mut(&mut app);
141
3
        assert_eq!(form.focus, SPLITS + 1);
142
3
        assert!(form.focused_select().is_some_and(|select| select.open));
143
    }
144
1
}
145

            
146
#[test]
147
1
fn a_click_in_a_long_text_puts_the_caret_under_it_counting_the_scroll() {
148
1
    let text = "0123456789".repeat(12);
149
1
    let text = text.as_str();
150
1
    for app in presets() {
151
3
        let mut app = with_field(app, text_field(text));
152
3
        form_mut(&mut app).focus = 1;
153
3
        redraw(&mut app);
154
3
        let width = usize::from(area_of(&app, caret(0)).width);
155
3
        click_at(&mut app, caret(0), 3, 0);
156
3
        assert_eq!(form_mut(&mut app).focus, 0);
157
3
        let skipped = text.chars().count() + 1 - (width - 1);
158
3
        assert_eq!(editor_of(&mut app, 0).cursor(), skipped + 2);
159
    }
160
1
}
161

            
162
#[test]
163
1
fn a_click_past_the_end_of_a_short_text_lands_at_its_end() {
164
1
    let mut app = with_field(crate::test_support::app(), text_field("abc"));
165
1
    click_at(&mut app, caret(0), 15, 0);
166
1
    assert_eq!(editor_of(&mut app, 0).cursor(), 3);
167
1
}
168

            
169
7
fn amount_cell() -> Target {
170
7
    Target::Caret {
171
7
        field: SPLITS + 1,
172
7
        cell: Some(cell_id(COL_VALUE)),
173
7
    }
174
7
}
175

            
176
5
fn type_amount(app: &mut App, text: &str) {
177
5
    let splits = splits_of(app);
178
5
    splits.focus_cell_id(cell_id(COL_VALUE));
179
5
    if let Some(crate::widgets::FocusedSubWidget::Amount(amount)) = splits.focused_subwidget_mut() {
180
5
        amount.as_editor_mut().replace_buffer(text);
181
5
        amount.as_editor_mut().set_cursor(0);
182
    }
183
5
}
184

            
185
4
fn amount_cursor(app: &mut App) -> usize {
186
4
    match splits_of(app).focused_subwidget_mut() {
187
4
        Some(crate::widgets::FocusedSubWidget::Amount(amount)) => amount.editor().cursor(),
188
        _ => panic!("the amount cell is not focused"),
189
    }
190
4
}
191

            
192
#[test]
193
1
fn a_click_in_an_unfocused_amount_cell_focuses_it_and_places_the_caret() {
194
1
    for app in presets() {
195
3
        let mut app = with_field(app, text_field(""));
196
3
        type_amount(&mut app, "123456");
197
3
        click_at(&mut app, amount_cell(), 3, 0);
198
3
        assert!(form_mut(&mut app).cell_focused(SPLITS + 1, cell_id(COL_VALUE)));
199
3
        assert_eq!(amount_cursor(&mut app), 2);
200
    }
201
1
}
202

            
203
#[test]
204
1
fn a_click_in_a_focused_scrolled_amount_cell_counts_from_the_scroll() {
205
1
    let text = "1234567890".repeat(12);
206
1
    let text = text.as_str();
207
1
    let mut app = with_field(crate::test_support::app(), text_field(""));
208
1
    type_amount(&mut app, text);
209
1
    form_mut(&mut app).focus_cell(SPLITS + 1, cell_id(COL_VALUE));
210
1
    if let Some(crate::widgets::FocusedSubWidget::Amount(amount)) =
211
1
        splits_of(&mut app).focused_subwidget_mut()
212
    {
213
1
        amount.as_editor_mut().set_cursor(text.len());
214
    }
215
1
    redraw(&mut app);
216
1
    let width = usize::from(area_of(&app, amount_cell()).width);
217
1
    click_at(&mut app, amount_cell(), 1, 0);
218
1
    assert_eq!(amount_cursor(&mut app), text.len() + 1 - (width - 1));
219
1
}
220

            
221
#[test]
222
1
fn a_click_in_a_text_area_puts_the_caret_on_the_clicked_line_and_column() {
223
1
    for app in presets() {
224
3
        let mut app = with_field(app, area_field("first\nsecond line\nthird", (0, 0)));
225
3
        form_mut(&mut app).focus = 1;
226
3
        click_at(&mut app, caret(0), 4, 1);
227
3
        assert_eq!(form_mut(&mut app).focus, 0);
228
3
        assert_eq!(editor_of(&mut app, 0).cursor_position(), (1, 4));
229
    }
230
1
}
231

            
232
#[test]
233
1
fn a_click_past_a_line_or_below_the_text_clamps_into_it() {
234
1
    let mut app = with_field(crate::test_support::app(), area_field("ab\ncd", (0, 0)));
235
1
    click_at(&mut app, caret(0), 9, 0);
236
1
    assert_eq!(editor_of(&mut app, 0).cursor_position(), (0, 2));
237
1
    click_at(&mut app, caret(0), 1, 4);
238
1
    assert_eq!(editor_of(&mut app, 0).cursor_position(), (1, 1));
239
1
}
240

            
241
#[test]
242
1
fn a_click_in_a_scrolled_text_area_counts_from_the_viewport() {
243
1
    let text = "l0\nl1\nl2\nl3\nl4\nl5\nl6\nlast line";
244
1
    let mut app = with_field(crate::test_support::app(), area_field(text, (7, 2)));
245
1
    redraw(&mut app);
246
1
    assert_eq!(editor_of(&mut app, 0).viewport().top, 3);
247
1
    click_at(&mut app, caret(0), 1, 0);
248
1
    assert_eq!(editor_of(&mut app, 0).cursor_position(), (3, 1));
249
1
}
250

            
251
#[test]
252
1
fn a_click_in_a_text_area_lands_on_a_wide_character_by_either_cell() {
253
1
    let mut app = with_field(crate::test_support::app(), area_field("a\u{4e2d}b", (0, 0)));
254
1
    click_at(&mut app, caret(0), 2, 0);
255
1
    assert_eq!(editor_of(&mut app, 0).cursor_position(), (0, 1));
256
1
}
257

            
258
/// Draws `app` and returns where the terminal cursor is left.
259
8
fn screen_cursor(app: &mut App) -> Position {
260
8
    let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("test terminal");
261
8
    let mut hits = crate::hits::Hits::default();
262
8
    terminal
263
8
        .draw(|frame| crate::draw::draw(frame, app, &mut hits))
264
8
        .expect("the frame draws");
265
8
    app.hits = hits;
266
8
    terminal.get_cursor_position().expect("the cursor position")
267
8
}
268

            
269
#[test]
270
1
fn a_click_in_a_scrolled_text_keeps_the_view_and_the_caret_under_the_pointer() {
271
1
    let text = "0123456789".repeat(12);
272
1
    for app in presets() {
273
3
        let mut app = with_field(app, text_field(&text));
274
3
        form_mut(&mut app).focus = 0;
275
3
        screen_cursor(&mut app);
276
3
        let before = editor_of(&mut app, 0).viewport();
277
3
        click_at(&mut app, caret(0), 3, 0);
278
3
        let area = area_of(&app, caret(0));
279
3
        let at = screen_cursor(&mut app);
280
3
        assert_eq!(editor_of(&mut app, 0).viewport(), before);
281
3
        assert_eq!((at.x, at.y), (area.x + 3, area.y));
282
3
        assert_eq!(editor_of(&mut app, 0).cursor(), before.left + 2);
283
    }
284
1
}
285

            
286
#[test]
287
1
fn a_click_in_a_scrolled_amount_cell_keeps_the_caret_under_the_pointer() {
288
1
    let text = "1234567890".repeat(12);
289
1
    let mut app = with_field(crate::test_support::app(), text_field(""));
290
1
    type_amount(&mut app, &text);
291
1
    form_mut(&mut app).focus_cell(SPLITS + 1, cell_id(COL_VALUE));
292
1
    if let Some(crate::widgets::FocusedSubWidget::Amount(amount)) =
293
1
        splits_of(&mut app).focused_subwidget_mut()
294
    {
295
1
        amount.as_editor_mut().set_cursor(text.len());
296
    }
297
1
    screen_cursor(&mut app);
298
1
    click_at(&mut app, amount_cell(), 2, 0);
299
1
    let area = area_of(&app, amount_cell());
300
1
    let at = screen_cursor(&mut app);
301
1
    assert_eq!((at.x, at.y), (area.x + 2, area.y));
302
1
}
303

            
304
#[test]
305
1
fn a_click_in_a_text_of_a_dialog_off_the_left_edge_lands_on_the_char_clicked() {
306
1
    let mut app = with_field(crate::test_support::app(), text_field("abcdefghij"));
307
1
    redraw(&mut app);
308
1
    let text = area_of(&app, caret(0));
309
1
    press(
310
1
        &mut app,
311
1
        Position::new(text.x + 3, text.y),
312
1
        MouseEventKind::Down(MouseButton::Left),
313
1
        now(),
314
1
    );
315
1
    let clicked = editor_of(&mut app, 0).cursor();
316
1
    assert!(clicked > 0);
317
1
    let slot = app.hits.dialog().expect("a dialog was drawn");
318
1
    let x = i32::from(slot.auto.x) - i32::from(text.x) - 2;
319
1
    let place = tvui::Place {
320
1
        x,
321
1
        ..tvui::Place::from(slot.auto)
322
1
    };
323
1
    app.overlays
324
1
        .set_placement(crate::overlay::Placement::of(place, slot.auto));
325
1
    redraw(&mut app);
326
1
    let shown = area_of(&app, caret(0));
327
1
    assert_eq!(shown.x, 0);
328
1
    press(
329
1
        &mut app,
330
1
        Position::new(1, shown.y),
331
1
        MouseEventKind::Down(MouseButton::Left),
332
1
        now(),
333
1
    );
334
1
    assert_eq!(editor_of(&mut app, 0).cursor(), clicked);
335
1
}