kui-core 0.1.0-alpha.45

kui contract: flat per-frame tree, clay-style flex layout, text stack, events as data, quad display list
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Selecting inside a `cells` grid (ADR 0017, decision 4): a terminal's
//! screen selects in cells rather than in bytes, its ends are absolute
//! lines so a scroll does not move them, and a copy trims the trailing
//! blanks that make a screenful paste like a rectangle instead of text.

use kui_core::{
    Cell, CellGrid, Color, Core, InputEvent, Key, MouseButton, NodeSpec, Size, TextStyle, Vec2,
};

const COLS: usize = 10;
const ROWS: usize = 3;

/// Three rows of text, padded with blanks the way a terminal pads a
/// screen: "hello", "brave world" (clipped), "bye".
fn screen() -> Vec<Cell> {
    let lines = ["hello", "brave  ", "bye"];
    let mut cells = vec![Cell::new(' ', 0xffffffff, 0); ROWS * COLS];
    for (r, line) in lines.iter().enumerate() {
        for (c, ch) in line.chars().take(COLS).enumerate() {
            cells[r * COLS + c] = Cell::new(ch, 0xffffffff, 0);
        }
    }
    cells
}

/// One grid, `selectable`, whose row 0 is absolute line `origin`.
fn frame(core: &mut Core, origin: u64) -> Key {
    let cells = screen();
    let mut ui = core.frame(Size::new(400.0, 300.0), 1.0);
    ui.configure_root(NodeSpec::column().fill());
    let grid = CellGrid {
        rows: ROWS,
        cols: COLS,
        cells: &cells,
        style: TextStyle::new(14.0).family(kui_core::FontFamily::Mono),
        cursor: None,
        origin_line: origin,
    };
    let key = ui.child_key("term");
    ui.cells_keyed("term", &grid, NodeSpec::column().fill().selectable());
    ui.finish();
    key
}

/// The middle of cell (row, col), from the grid's own metrics.
fn cell_at(core: &mut Core, key: Key, row: usize, col: usize) -> Vec2 {
    let m = core.measure_text(
        "M",
        &TextStyle::new(14.0).family(kui_core::FontFamily::Mono),
        None,
    );
    let _ = key;
    Vec2::new(m.width * (col as f32 + 0.5), m.height * (row as f32 + 0.5))
}

fn drag(core: &mut Core, from: Vec2, to: Vec2) {
    drag_clicks(core, from, to, 1);
}

/// The same, with the click count the driver counted: 2 is a double click
/// held and dragged, 3 a triple.
fn drag_clicks(core: &mut Core, from: Vec2, to: Vec2, clicks: u8) {
    core.handle_input(InputEvent::CursorMoved(from));
    core.handle_input(InputEvent::MouseDown {
        button: MouseButton::Primary,
        clicks,
    });
    core.handle_input(InputEvent::CursorMoved(to));
    core.handle_input(InputEvent::MouseUp {
        button: MouseButton::Primary,
    });
}

#[test]
fn a_drag_across_a_grid_selects_lines_and_copies_them_trimmed() {
    let mut core = Core::new();
    let key = frame(&mut core, 0);
    let from = cell_at(&mut core, key, 0, 0);
    let to = cell_at(&mut core, key, 2, 3);
    drag(&mut core, from, to);
    let sel = core.cell_selection().expect("a cell selection");
    assert_eq!(sel.node, key);
    assert_eq!(sel.anchor.line, 0);
    assert_eq!(
        core.copy_selection().as_deref(),
        Some("hello\nbrave\nbye"),
        "trailing blanks are trimmed per line, and `brave  ` loses its two"
    );
}

#[test]
fn the_ends_are_absolute_lines_so_a_scroll_does_not_move_them() {
    let mut core = Core::new();
    let key = frame(&mut core, 500);
    let from = cell_at(&mut core, key, 1, 0);
    let to = cell_at(&mut core, key, 1, 5);
    drag(&mut core, from, to);
    let sel = core.cell_selection().expect("a selection");
    assert_eq!(
        (sel.anchor.line, sel.focus.line),
        (501, 501),
        "row 1 of a screen whose row 0 is line 500"
    );
    assert_eq!(core.copy_selection().as_deref(), Some("brave"));
}

#[test]
fn a_selection_paints_over_the_cells_it_covers() {
    let mut core = Core::new();
    let key = frame(&mut core, 0);
    let from = cell_at(&mut core, key, 0, 0);
    let to = cell_at(&mut core, key, 0, 5);
    drag(&mut core, from, to);
    frame(&mut core, 0);
    let tint = kui_core::select::TINT;
    let tints = core
        .output()
        .0
        .quads
        .iter()
        .filter(|q| q.color == tint)
        .count();
    assert_eq!(tints, 1, "one run of selected columns on one row");
}

#[test]
fn a_grid_never_joins_the_text_scope_around_it() {
    let mut core = Core::new();
    let cells = screen();
    let mut ui = core.frame(Size::new(400.0, 300.0), 1.0);
    ui.configure_root(NodeSpec::column().fill());
    let outer = ui.with_keyed("card", NodeSpec::column().grow_width().selectable(), |ui| {
        ui.text("a label", TextStyle::new(14.0));
        let grid = CellGrid {
            rows: ROWS,
            cols: COLS,
            cells: &cells,
            style: TextStyle::new(14.0).family(kui_core::FontFamily::Mono),
            cursor: None,
            origin_line: 0,
        };
        ui.cells_keyed("term", &grid, NodeSpec::column().grow_width());
    });
    ui.finish();
    // Selecting the card takes its text and none of the screen: the grid
    // is not part of the paragraph around it.
    assert!(core.select_all_in(outer));
    assert_eq!(core.selection_text().as_deref(), Some("a label"));
}

#[test]
fn one_selection_per_window_holds_across_the_two_kinds() {
    let mut core = Core::new();
    let cells = screen();
    let mut ui = core.frame(Size::new(400.0, 300.0), 1.0);
    ui.configure_root(NodeSpec::column().fill());
    let card = ui.text_in_keyed(
        "card",
        NodeSpec::column().grow_width().selectable(),
        "a label",
        TextStyle::new(14.0),
    );
    let grid = CellGrid {
        rows: ROWS,
        cols: COLS,
        cells: &cells,
        style: TextStyle::new(14.0).family(kui_core::FontFamily::Mono),
        cursor: None,
        origin_line: 0,
    };
    let term = ui.child_key("term");
    ui.cells_keyed(
        "term",
        &grid,
        NodeSpec::column().grow_width().height(60.0).selectable(),
    );
    ui.finish();
    core.select_all_in(card);
    assert!(core.selection().is_some());
    core.set_cell_selection(kui_core::CellSelection::new(
        term,
        kui_core::CellEnd::new(0, 0),
        kui_core::CellEnd::new(0, 5),
    ));
    assert!(core.selection().is_none(), "the text selection went");
    assert_eq!(core.copy_selection().as_deref(), Some("hello"));
    let _ = Color::BLACK;
}

/// A right-click on a grid gets a menu that works on *cells*. Reading the
/// text selection for it left Copy dimmed over a terminal with half its
/// screen selected, and Select All did nothing at all.
#[test]
fn the_stock_menu_over_a_grid_copies_cells() {
    let mut core = Core::new();
    let key = frame(&mut core, 0);
    let from = cell_at(&mut core, key, 0, 0);
    let to = cell_at(&mut core, key, 0, 5);
    drag(&mut core, from, to);
    core.handle_input(InputEvent::CursorMoved(from));
    core.handle_input(InputEvent::MouseDown {
        button: kui_core::MouseButton::Secondary,
        clicks: 1,
    });
    let menu = core.menu().expect("a menu over the grid").clone();
    let at = menu
        .items
        .iter()
        .position(|i| i.role == kui_core::MenuRole::Copy)
        .expect("a Copy row");
    assert!(
        menu.items[at].enabled,
        "cells are selected, so Copy can act"
    );

    // And choosing it puts the cells on the clipboard — the row was lit
    // from the cell selection, so acting on it has to read the same one.
    core.activate_menu_item(at).expect("the row is enabled");
    let acts = core.take_menu_actions();
    assert!(
        matches!(
            acts.first(),
            Some(kui_core::MenuAction::SetClipboard { text, .. }) if text == "hello"
        ),
        "Copy over a grid copies the cells, not the (absent) text selection: {acts:?}"
    );

    // And Select All takes the whole screen it was given.
    core.close_menu();
    assert!(core.select_all_in(key));
    assert_eq!(core.copy_selection().as_deref(), Some("hello\nbrave\nbye"));
}

/// A selection scrolled entirely off the grid copies nothing — rather than
/// an empty string, which would let Cmd-C wipe the clipboard.
#[test]
fn a_grid_selection_scrolled_away_copies_nothing() {
    let mut core = Core::new();
    let key = frame(&mut core, 0);
    let from = cell_at(&mut core, key, 0, 0);
    let to = cell_at(&mut core, key, 0, 5);
    drag(&mut core, from, to);
    assert_eq!(core.copy_selection().as_deref(), Some("hello"));
    // The terminal scrolls: row 0 is now absolute line 900.
    frame(&mut core, 900);
    assert_eq!(
        core.copy_selection(),
        None,
        "nothing of the selection is on screen, so there is nothing to copy"
    );
}

/// A grid's padding moves the cells, so a hit test has to move with them:
/// layout reserves the padding and the glyphs start inside it, and reading
/// row 0 off the node's own corner picked the cell above and left of the
/// one under the pointer.
#[test]
fn a_padded_grid_counts_its_rows_from_inside_the_padding() {
    const PAD: f32 = 12.0;
    let mut core = Core::new();
    let cells = screen();
    let mut ui = core.frame(Size::new(400.0, 300.0), 1.0);
    ui.configure_root(NodeSpec::column().fill());
    let grid = CellGrid {
        rows: ROWS,
        cols: COLS,
        cells: &cells,
        style: TextStyle::new(14.0).family(kui_core::FontFamily::Mono),
        cursor: None,
        origin_line: 0,
    };
    ui.cells_keyed(
        "term",
        &grid,
        NodeSpec::column().grow_width().pad(PAD).selectable(),
    );
    ui.finish();
    // The middle of row 1, column 1 — measured from the padded corner.
    let m = core.measure_text(
        "M",
        &TextStyle::new(14.0).family(kui_core::FontFamily::Mono),
        None,
    );
    let at = Vec2::new(PAD + m.width * 1.5, PAD + m.height * 1.5);
    drag(&mut core, at, at);
    let sel = core.cell_selection().expect("a selection");
    assert_eq!(
        (sel.anchor.line, sel.anchor.col),
        (1, 1),
        "the cell under the pointer, not the one the padding hides"
    );
}

/// A double click on a terminal takes the word under it, not the one cell
/// under it — the gesture every other selectable thing in the library has.
#[test]
fn a_double_click_takes_the_word_under_the_cell() {
    let mut core = Core::new();
    let key = frame(&mut core, 0);
    // Row 1 is "brave  ": the click lands mid-word.
    let at = cell_at(&mut core, key, 1, 3);
    drag_clicks(&mut core, at, at, 2);
    let sel = core.cell_selection().expect("a selection");
    assert_eq!(
        (sel.anchor.col, sel.focus.col),
        (0, 5),
        "the whole word, not the cell"
    );
    assert_eq!(core.copy_selection().as_deref(), Some("brave"));
}

/// A double click on the blanks a terminal pads its lines with takes the
/// blanks — one class of cell at a time, the way a double click in text
/// takes the run of spaces it lands in.
#[test]
fn a_double_click_on_the_padding_takes_the_padding() {
    let mut core = Core::new();
    let key = frame(&mut core, 0);
    let at = cell_at(&mut core, key, 1, 7);
    drag_clicks(&mut core, at, at, 2);
    let sel = core.cell_selection().expect("a selection");
    assert_eq!((sel.anchor.col, sel.focus.col), (5, COLS));
}

/// A triple click takes the whole row, edge to edge; the copy is what
/// trims the blanks off it.
#[test]
fn a_triple_click_takes_the_whole_row() {
    let mut core = Core::new();
    let key = frame(&mut core, 500);
    let at = cell_at(&mut core, key, 1, 3);
    drag_clicks(&mut core, at, at, 3);
    let sel = core.cell_selection().expect("a selection");
    assert_eq!((sel.anchor.line, sel.anchor.col), (501, 0));
    assert_eq!((sel.focus.line, sel.focus.col), (501, COLS));
    assert_eq!(core.copy_selection().as_deref(), Some("brave"));
}

/// Holding the second click and dragging selects by words: both ends
/// round outwards, so the word the drag started in stays whole even when
/// the pointer turns back past it.
#[test]
fn a_held_double_click_drags_by_words() {
    let mut core = Core::new();
    let key = frame(&mut core, 0);
    // Start in "brave" on row 1, drag up into "hello" on row 0.
    let from = cell_at(&mut core, key, 1, 3);
    let back = cell_at(&mut core, key, 0, 2);
    drag_clicks(&mut core, from, back, 2);
    let sel = core.cell_selection().expect("a selection");
    let (a, b) = sel.ordered();
    assert_eq!(
        ((a.line, a.col), (b.line, b.col)),
        ((0, 0), (1, 5)),
        "from the start of `hello` to the far edge of `brave`"
    );
    assert_eq!(core.copy_selection().as_deref(), Some("hello\nbrave"));
}

/// And forwards, where the anchor keeps its *near* edge and the live end
/// takes the far one.
#[test]
fn a_held_double_click_dragged_forwards_keeps_the_first_word_whole() {
    let mut core = Core::new();
    let key = frame(&mut core, 0);
    let from = cell_at(&mut core, key, 0, 3);
    let to = cell_at(&mut core, key, 1, 1);
    drag_clicks(&mut core, from, to, 2);
    let sel = core.cell_selection().expect("a selection");
    let (a, b) = sel.ordered();
    assert_eq!(((a.line, a.col), (b.line, b.col)), ((0, 0), (1, 5)));
}

/// A held triple click drags by whole rows.
#[test]
fn a_held_triple_click_drags_by_rows() {
    let mut core = Core::new();
    let key = frame(&mut core, 0);
    let from = cell_at(&mut core, key, 0, 3);
    let to = cell_at(&mut core, key, 2, 1);
    drag_clicks(&mut core, from, to, 3);
    let sel = core.cell_selection().expect("a selection");
    let (a, b) = sel.ordered();
    assert_eq!(((a.line, a.col), (b.line, b.col)), ((0, 0), (2, COLS)));
    assert_eq!(core.copy_selection().as_deref(), Some("hello\nbrave\nbye"));
}

/// A wide glyph and the blank the app leaves after it are one character,
/// not a character and a space: the `WIDE` flag is on the glyph, so the
/// spacer is the cell *after* a flagged one.
#[test]
fn a_wide_glyph_copies_as_itself_and_selects_with_its_spacer() {
    use kui_core::cells::flags;
    let mut core = Core::new();
    let mut cells = vec![Cell::new(' ', 0xffffffff, 0); ROWS * COLS];
    // "漢字x" — each wide glyph followed by the app's blank.
    let wide = |ch: char| Cell::new(ch, 0xffffffff, 0).with(flags::WIDE);
    cells[0] = wide('漢');
    cells[2] = wide('字');
    cells[4] = Cell::new('x', 0xffffffff, 0);
    let mut ui = core.frame(Size::new(400.0, 300.0), 1.0);
    ui.configure_root(NodeSpec::column().fill());
    let key = ui.child_key("term");
    ui.cells_keyed(
        "term",
        &CellGrid {
            rows: ROWS,
            cols: COLS,
            cells: &cells,
            style: TextStyle::new(14.0).family(kui_core::FontFamily::Mono),
            cursor: None,
            origin_line: 0,
        },
        NodeSpec::column().fill().selectable(),
    );
    ui.finish();
    assert!(core.select_all_in(key));
    assert_eq!(
        core.copy_selection()
            .as_deref()
            .map(|t| t.lines().next().unwrap().to_string()),
        Some("漢字x".to_string()),
        "the glyphs themselves, and no blank between them"
    );
    // And a double click on the second glyph takes it with its spacer.
    let at = cell_at(&mut core, key, 0, 2);
    drag_clicks(&mut core, at, at, 2);
    let sel = core.cell_selection().expect("a selection");
    assert_eq!(
        (sel.anchor.col, sel.focus.col),
        (0, 5),
        "`漢字x` is one word"
    );
}

/// A host reading the selection from inside its own view is asking while
/// the next frame's tree is half-built — the grid it means is last
/// frame's. Text has always answered there (the places swap); a grid was
/// looked up in the tree and so answered nothing, which is what a Lua or
/// Node view calling `selection_text()` saw.
#[test]
fn a_view_can_read_the_grid_selection_while_it_builds_the_next_frame() {
    let mut core = Core::new();
    let key = frame(&mut core, 0);
    let at = cell_at(&mut core, key, 1, 3);
    drag_clicks(&mut core, at, at, 2);
    assert_eq!(core.copy_selection().as_deref(), Some("brave"));

    // Mid-frame, before the grid has been declared again.
    let cells = screen();
    let mut ui = core.frame(Size::new(400.0, 300.0), 1.0);
    ui.configure_root(NodeSpec::column().fill());
    assert_eq!(
        ui.selection_text().as_deref(),
        Some("brave"),
        "last frame's grid answers for it"
    );
    let grid = CellGrid {
        rows: ROWS,
        cols: COLS,
        cells: &cells,
        style: TextStyle::new(14.0).family(kui_core::FontFamily::Mono),
        cursor: None,
        origin_line: 0,
    };
    ui.cells_keyed("term", &grid, NodeSpec::column().fill().selectable());
    ui.finish();
    assert_eq!(core.copy_selection().as_deref(), Some("brave"));
}