crcbl-ui 0.3.0

Crucible immediate-mode GUI
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
//! The text input: the engaged rule, the pointer, the clipboard's requests
//! and answers, the parts it draws and where they sit, and the caret's blink
//! and scrolling.

use std::time::Duration;

use super::*;
use crate::console::CARET_BLINK;
use crate::edit::{ClipboardOp, Edit, Motion};
#[cfg(feature = "parsed-font")]
use crate::font::Font;
#[cfg(feature = "parsed-font")]
use crate::font::layout::TextLayout;
use crate::style::PseudoClasses;
use crate::tree::{
    ClipboardAnswer, ClipboardReply, ClipboardRequest, Content, DOUBLE_CLICK_TIME, Engagement,
    Response, TextInputOptions,
};
use crate::widget::NATURAL_FONT_SIZE;

/// Every frame's length unless a test says otherwise.
const FRAME: Duration = Duration::from_millis(16);

/// A frame's text input of `edits`.
fn typed(edits: impl IntoIterator<Item = Edit>) -> TextInput {
    TextInput {
        dt: FRAME,
        edits: edits.into_iter().collect(),
        clipboard: Vec::new(),
    }
}

/// A frame's text input with no edits.
fn quiet() -> TextInput {
    typed([])
}

fn insert(text: &str) -> Edit {
    Edit::Insert(text.to_owned())
}

const fn step(motion: Motion, select: bool) -> Edit {
    Edit::Move { motion, select }
}

/// One frame of a column holding `#name`, a plain text input, then `#pass`, a
/// masked one with a placeholder.
struct Page {
    name: Response,
    pass: Response,
}

fn page(
    ui: &mut Ui,
    pointer: PointerInput,
    nav: NavInput,
    text: TextInput,
    values: &mut [String; 2],
) -> Page {
    frame_with_text(ui, pointer, nav, text, |ui| {
        let [name, pass] = values;
        Page {
            name: ui.text_input("#name", name),
            pass: ui.text_input_with(
                "#pass",
                pass,
                TextInputOptions {
                    placeholder: "password",
                    masked: true,
                    ..TextInputOptions::default()
                },
            ),
        }
    })
}

/// The child of `key` whose selector names `class`, as this frame built it.
fn part(ui: &Ui, key: NodeKey, class: &str) -> Option<NodeKey> {
    let parent = ui.nodes.iter().position(|node| node.key == key)?;
    ui.nodes
        .iter()
        .filter(|node| node.parent == Some(parent))
        .find(|node| ui.selectors[node.selector.0..node.selector.1].contains(class))
        .map(|node| node.key)
}

/// The text a span drew this frame.
fn shown(ui: &Ui, span: NodeKey) -> &str {
    let node = ui
        .nodes
        .iter()
        .find(|node| node.key == span)
        .expect("built");
    match node.content {
        Content::Text { start, end } => &ui.text[start..end],
        _ => panic!("not a text span"),
    }
}

/// How wide `text` draws in `span`'s bitmap font, measured by the atlas and
/// nothing of the widget's.
fn bitmap_width(ui: &Ui, span: NodeKey, text: &str) -> f32 {
    let scale = style_of(ui, span).font_size / NATURAL_FONT_SIZE;
    FontAtlas::built_in().text_width(text, scale)
}

/// Clicks `pos`: a press frame and a release frame.
fn click(ui: &mut Ui, pos: Vec2, values: &mut [String; 2]) -> Page {
    page(ui, press(pos), NavInput::default(), quiet(), values);
    page(ui, release(pos), NavInput::default(), quiet(), values)
}

/// A fresh page with `name` in `#name`, laid out once and `#name` engaged by
/// accept.
fn engaged_on(name: &str) -> (Ui, [String; 2]) {
    let mut ui = Ui::new();
    let mut values = [name.to_owned(), String::new()];
    page(&mut ui, idle(), NavInput::default(), quiet(), &mut values);
    page(&mut ui, idle(), NavInput::NAVIGATION, quiet(), &mut values);
    let engaged = page(&mut ui, idle(), NavInput::ACCEPT, quiet(), &mut values);
    assert_eq!(engaged.name.engagement, Engagement::Began);
    (ui, values)
}

/// **The LOCKED rule on a text input**: focus moves past it, and nothing
/// typed reaches it while it is only focused; accept engages it without
/// taking the text the accepting press committed; while engaged it takes the
/// edits in order, and a navigation step moves neither focus nor the caret
/// nor the selection accept made; back cancels to the text it engaged with;
/// accept commits.
#[test]
fn a_text_input_engages_takes_edits_cancels_to_its_snapshot_and_commits() {
    let mut ui = Ui::new();
    let mut values = [String::from("hi"), String::new()];
    page(&mut ui, idle(), NavInput::default(), quiet(), &mut values);
    let landed = page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([insert("x")]),
        &mut values,
    );
    assert!(landed.name.focused);
    let passed = page(&mut ui, idle(), DOWN, typed([insert("y")]), &mut values);
    assert!(
        passed.pass.focused,
        "down did not move focus past the input"
    );
    page(&mut ui, idle(), UP, quiet(), &mut values);
    assert_eq!(values, ["hi", ""], "a focused input took typing");

    let began = page(
        &mut ui,
        idle(),
        NavInput::ACCEPT,
        typed([insert(" ")]),
        &mut values,
    );
    assert_eq!(began.name.engagement, Engagement::Began);
    assert_eq!(values[0], "hi", "the accepting press's own text was typed");
    assert!(
        ui.text_editing(),
        "an engaged input is not reported as editing"
    );

    for nav in [RIGHT, LEFT, DOWN, NavInput::NEXT] {
        let held = page(&mut ui, idle(), nav, quiet(), &mut values);
        assert!(
            held.name.focused,
            "{nav:?} moved focus off the engaged input"
        );
        assert_eq!(
            ui.text_caret(held.name.key),
            Some((2, 0)),
            "{nav:?} moved the caret"
        );
    }
    let edited = page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([
            step(Motion::End, false),
            insert("!"),
            step(Motion::Left, false),
            insert("?"),
        ]),
        &mut values,
    );
    assert_eq!(values[0], "hi?!", "the edits were not taken in order");
    assert!(edited.name.changed);

    let cancelled = page(&mut ui, idle(), NavInput::BACK, quiet(), &mut values);
    assert_eq!(cancelled.name.engagement, Engagement::Cancelled);
    assert_eq!(values[0], "hi", "back did not restore the snapshot");
    assert!(cancelled.name.changed, "the restore was not reported");
    assert!(!ui.text_editing());

    page(&mut ui, idle(), NavInput::ACCEPT, quiet(), &mut values);
    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([step(Motion::End, false), insert("s")]),
        &mut values,
    );
    let committed = page(&mut ui, idle(), NavInput::ACCEPT, quiet(), &mut values);
    assert_eq!(committed.name.engagement, Engagement::Committed);
    page(&mut ui, idle(), NavInput::BACK, quiet(), &mut values);
    assert!(
        ui.back_requested(),
        "back after the commit was not the caller's"
    );
    assert_eq!(values[0], "his", "commit lost the edit, or back undid it");
}

/// **The pointer places the caret on the nearest boundary, a drag selects,
/// and a double-click selects the word under it** — and a drag that ends on
/// an input that was not engaged leaves it engaged, where a slider's drag
/// does not.
#[test]
fn a_click_places_the_caret_a_drag_selects_and_a_double_click_selects_a_word() {
    let mut ui = Ui::new();
    let mut values = [String::from("hello world"), String::new()];
    let first = page(&mut ui, idle(), NavInput::default(), quiet(), &mut values);
    let key = first.name.key;
    let span = part(&ui, key, ".text-input-text").expect("the text is a part");
    let (origin, bottom) = rect(&ui, span);
    let y = (origin.y + bottom.y) * 0.5;
    let advance = bitmap_width(&ui, span, "M");
    let at = |stops: f32| Vec2::new(origin.x + stops * advance, y);

    let clicked = click(&mut ui, at(3.4), &mut values);
    assert_eq!(clicked.name.engagement, Engagement::Began);
    assert_eq!(
        ui.text_caret(key),
        Some((3, 3)),
        "3.4 glyphs in is not stop 3"
    );
    // Two presses further apart in time than a double-click are two clicks.
    let slow = TextInput {
        dt: DOUBLE_CLICK_TIME,
        ..quiet()
    };
    let slow_click = |ui: &mut Ui, pos, values: &mut [String; 2]| {
        page(ui, press(pos), NavInput::default(), slow.clone(), values);
        page(ui, release(pos), NavInput::default(), slow.clone(), values);
    };
    slow_click(&mut ui, at(3.6), &mut values);
    assert_eq!(
        ui.text_caret(key),
        Some((4, 4)),
        "3.6 glyphs in is not stop 4"
    );

    // Far enough from the last press that it is no double-click.
    page(
        &mut ui,
        press(at(1.2)),
        NavInput::default(),
        quiet(),
        &mut values,
    );
    page(
        &mut ui,
        press(at(7.6)),
        NavInput::default(),
        quiet(),
        &mut values,
    );
    page(
        &mut ui,
        release(at(7.6)),
        NavInput::default(),
        quiet(),
        &mut values,
    );
    assert_eq!(
        ui.text_caret(key),
        Some((8, 1)),
        "the drag did not select 1..8"
    );
    assert_eq!(ui.engaged(), Some(key));

    click(&mut ui, at(8.5), &mut values);
    click(&mut ui, at(8.5), &mut values);
    assert_eq!(
        ui.text_caret(key),
        Some((11, 6)),
        "a double-click on `world` did not select it"
    );

    slow_click(&mut ui, at(2.2), &mut values);
    slow_click(&mut ui, at(2.2), &mut values);
    assert_eq!(
        ui.text_caret(key),
        Some((2, 2)),
        "slow clicks selected a word"
    );
    assert_eq!(values[0], "hello world", "the pointer changed the text");

    let mut fresh = Ui::new();
    let mut values = [String::from("hello world"), String::new()];
    page(
        &mut fresh,
        idle(),
        NavInput::default(),
        quiet(),
        &mut values,
    );
    page(
        &mut fresh,
        press(at(1.0)),
        NavInput::default(),
        quiet(),
        &mut values,
    );
    page(
        &mut fresh,
        press(at(5.0)),
        NavInput::default(),
        quiet(),
        &mut values,
    );
    page(
        &mut fresh,
        release(at(5.0)),
        NavInput::default(),
        quiet(),
        &mut values,
    );
    assert_eq!(
        fresh.engaged(),
        Some(key),
        "a drag-select did not end engaged"
    );
    assert_eq!(fresh.text_caret(key), Some((5, 1)));
}

/// **An engagement no pointer began selects the whole text, and the first key
/// typed replaces it**: accept on a focused input, and [`Ui::engage`], whose
/// first frame is engaged rather than begun and takes that frame's typing.
#[test]
fn an_engagement_no_pointer_began_selects_the_whole_text() {
    let (mut ui, mut values) = engaged_on("hello");
    let key = page(&mut ui, idle(), NavInput::NAVIGATION, quiet(), &mut values)
        .name
        .key;
    assert_eq!(
        ui.text_caret(key),
        Some((5, 0)),
        "accept did not select the text"
    );
    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([insert("x")]),
        &mut values,
    );
    assert_eq!(values[0], "x", "the key typed did not replace the text");

    let mut ui = Ui::new();
    let mut values = [String::from("hello"), String::new()];
    let laid = page(&mut ui, idle(), NavInput::default(), quiet(), &mut values);
    ui.engage(laid.name.key);
    page(
        &mut ui,
        idle(),
        NavInput::default(),
        typed([insert("y")]),
        &mut values,
    );
    assert_eq!(
        values[0], "y",
        "a key typed in the frame engage handed over did not replace the text"
    );
}

/// **A click engages with the caret where it lands, and a double-click selects
/// the word under it** — not the whole text, on an input a keyboard engagement
/// selected the whole of before.
#[test]
fn a_click_engages_with_the_caret_where_it_lands_and_a_double_click_takes_the_word() {
    let (mut ui, mut values) = engaged_on("hello world");
    let committed = page(&mut ui, idle(), NavInput::ACCEPT, quiet(), &mut values);
    assert_eq!(committed.name.engagement, Engagement::Committed);
    let key = committed.name.key;
    let span = part(&ui, key, ".text-input-text").expect("the text is a part");
    let (origin, bottom) = rect(&ui, span);
    let advance = bitmap_width(&ui, span, "M");
    let at = Vec2::new(origin.x + 3.4 * advance, (origin.y + bottom.y) * 0.5);

    let clicked = click(&mut ui, at, &mut values);
    assert_eq!(clicked.name.engagement, Engagement::Began);
    assert_eq!(
        ui.text_caret(key),
        Some((3, 3)),
        "the click selected rather than placing the caret"
    );
    click(&mut ui, at, &mut values);
    assert_eq!(
        ui.text_caret(key),
        Some((5, 0)),
        "a double-click on `hello` did not select the word"
    );
}

/// **An input built with `keep_caret` keeps its caret through an engagement**,
/// by accept and by [`Ui::engage`] alike, so the next key typed joins the text.
#[test]
fn an_input_that_keeps_its_caret_selects_nothing_when_it_engages() {
    let mut ui = Ui::new();
    let mut value = String::from("hello");
    let options = TextInputOptions {
        keep_caret: true,
        ..TextInputOptions::default()
    };
    let prompt = |ui: &mut Ui, nav, text, value: &mut String| {
        frame_with_text(ui, idle(), nav, text, |ui| {
            ui.text_input_with("#prompt", value, options)
        })
    };
    let laid = prompt(&mut ui, NavInput::default(), quiet(), &mut value);
    ui.engage(laid.key);
    prompt(
        &mut ui,
        NavInput::default(),
        typed([insert("!")]),
        &mut value,
    );
    assert_eq!(value, "hello!", "engage selected the text");

    prompt(&mut ui, NavInput::ACCEPT, quiet(), &mut value);
    let began = prompt(&mut ui, NavInput::ACCEPT, quiet(), &mut value);
    assert_eq!(began.engagement, Engagement::Began);
    assert_eq!(
        ui.text_caret(began.key),
        Some((6, 6)),
        "accept selected the text"
    );
}

/// **The selection highlight spans exactly the selected glyphs, and the caret
/// sits after the last typed glyph**, both measured against the atlas's own
/// advances — in the bitmap font, and in a parsed font against where
/// [`TextLayout`] draws each glyph, kerning included.
#[cfg(feature = "parsed-font")]
#[test]
fn the_selection_and_the_caret_sit_on_the_glyphs_they_name() {
    let (mut ui, mut values) = engaged_on("abcdef");
    let edits = [
        step(Motion::Home, false),
        step(Motion::Right, false),
        step(Motion::Right, true),
        step(Motion::Right, true),
    ];
    let selected = page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed(edits),
        &mut values,
    );
    let key = selected.name.key;
    let span = part(&ui, key, ".text-input-text").expect("text");
    let highlight = part(&ui, key, ".text-input-selection").expect("a selection is drawn");
    let (text_min, text_max) = rect(&ui, span);
    let (min, max) = rect(&ui, highlight);
    assert_eq!(
        min.x,
        text_min.x + bitmap_width(&ui, span, "a"),
        "left edge"
    );
    assert_eq!(
        max.x,
        text_min.x + bitmap_width(&ui, span, "abc"),
        "right edge"
    );
    assert_eq!(
        (min.y, max.y),
        (text_min.y, text_max.y),
        "not the line's height"
    );

    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([step(Motion::End, false), insert("g")]),
        &mut values,
    );
    assert!(
        part(&ui, key, ".text-input-selection").is_none(),
        "typing left a selection"
    );
    let caret = part(&ui, key, ".text-input-caret").expect("an engaged input has a caret");
    let (caret_min, caret_max) = rect(&ui, caret);
    let (text_min, text_max) = rect(&ui, span);
    assert_eq!(caret_min.x, text_min.x + bitmap_width(&ui, span, "abcdefg"));
    assert_eq!(
        caret_max.x - caret_min.x,
        1.0,
        "not default.css's caret width"
    );
    assert_eq!((caret_min.y, caret_max.y), (text_min.y, text_max.y));

    // A parsed font: the boundaries are glyph positions with kerning.
    let mut ui = Ui::new();
    ui.add_stylesheet(
        "sans.css",
        "text-input { font-family: sans-serif; font-size: 20px; }",
    );
    let mut values = [String::from("AVAWATAY"), String::new()];
    page(&mut ui, idle(), NavInput::default(), quiet(), &mut values);
    page(&mut ui, idle(), NavInput::NAVIGATION, quiet(), &mut values);
    page(&mut ui, idle(), NavInput::ACCEPT, quiet(), &mut values);
    let edits = [
        step(Motion::Home, false),
        step(Motion::Right, false),
        step(Motion::Right, false),
        step(Motion::Right, true),
        step(Motion::Right, true),
        step(Motion::Right, true),
    ];
    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed(edits),
        &mut values,
    );
    let span = part(&ui, key, ".text-input-text").expect("text");
    let style = style_of(&ui, span);
    let font = Font::sans();
    let layout = TextLayout::new(
        font,
        &values[0],
        style.font_size,
        style.text_line_height(font),
        None,
    );
    let glyph_x = |index: usize| layout.glyphs()[index].offset.x;
    let advance = font.advance(font.glyph_id('A')) * font.metrics().scale(style.font_size);
    assert!(
        (glyph_x(1) - advance).abs() > 0.5,
        "`AV` is not kerned, so the claim cannot tell kerning from advances"
    );
    let (text_min, _) = rect(&ui, span);
    let (min, max) = rect(
        &ui,
        part(&ui, key, ".text-input-selection").expect("selected"),
    );
    assert_eq!(
        min.x,
        (text_min.x + glyph_x(2)).round(),
        "left edge in the parsed font"
    );
    assert_eq!(
        max.x,
        (text_min.x + glyph_x(5)).round(),
        "right edge in the parsed font"
    );
}

/// **A line longer than the input scrolls to keep its caret in view**: the
/// caret inside the content box and the end of the line against it; Home
/// scrolls back to the start; and an input no longer engaged shows its start.
#[test]
fn a_long_line_scrolls_to_keep_its_caret_in_view() {
    let (mut ui, mut values) = engaged_on("");
    let line = "abcdefghijklmnopqrstuvwxyz0123456789";
    let typed_line = page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([insert(line)]),
        &mut values,
    );
    let key = typed_line.name.key;
    let span = part(&ui, key, ".text-input-text").expect("text");
    let caret = part(&ui, key, ".text-input-caret").expect("caret");
    let view = ui.store.by_key(key).expect("stored").content_box();
    let (text_min, _) = rect(&ui, span);
    let (caret_min, caret_max) = rect(&ui, caret);
    assert!(
        bitmap_width(&ui, span, line) > view.1.x - view.0.x,
        "the line fits, so nothing scrolls"
    );
    assert!(ui.scroll_offset_of(key).x > 0.0, "the input did not scroll");
    assert_eq!(
        caret_max.x, view.1.x,
        "the caret is not against the right edge"
    );
    assert_eq!(
        caret_min.x,
        text_min.x + bitmap_width(&ui, span, line),
        "the caret is not after the last glyph"
    );

    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([step(Motion::Home, false)]),
        &mut values,
    );
    let (caret_min, _) = rect(&ui, part(&ui, key, ".text-input-caret").expect("caret"));
    assert_eq!(ui.scroll_offset_of(key).x, 0.0, "Home did not scroll back");
    assert_eq!(
        caret_min.x, view.0.x,
        "the caret is not at the content's left"
    );

    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([step(Motion::End, false)]),
        &mut values,
    );
    assert!(
        ui.scroll_offset_of(key).x > 0.0,
        "End did not scroll to the end"
    );
    page(&mut ui, idle(), NavInput::ACCEPT, quiet(), &mut values);
    assert_eq!(
        ui.scroll_offset_of(key).x,
        0.0,
        "a committed input still shows the end of its line"
    );
}

/// **Copy and cut offer the selection, paste asks for a read, and a later
/// answer replaces the selection** — naming the input that asked, so an
/// answer to another input, or to one no longer engaged, changes nothing. A
/// refusal sets `:refused`, which draws `default.css`'s red border until the
/// next edit, and a masked input offers nothing.
#[test]
fn the_clipboard_is_requests_out_and_answers_back() {
    let (mut ui, mut values) = engaged_on("copy me");
    let copied = page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([Edit::SelectAll, Edit::Copy]),
        &mut values,
    );
    let key = copied.name.key;
    assert_eq!(
        ui.take_clipboard_requests(),
        [ClipboardRequest {
            from: key,
            op: ClipboardOp::Offer("copy me".to_owned()),
        }]
    );
    assert!(
        ui.take_clipboard_requests().is_empty(),
        "taking did not drain"
    );

    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([
            step(Motion::Home, false),
            step(Motion::WordRight, true),
            Edit::Cut,
        ]),
        &mut values,
    );
    assert_eq!(values[0], " me");
    assert_eq!(
        ui.take_clipboard_requests(),
        [ClipboardRequest {
            from: key,
            op: ClipboardOp::Offer("copy".to_owned()),
        }]
    );

    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([step(Motion::End, true), Edit::Paste]),
        &mut values,
    );
    assert_eq!(
        ui.take_clipboard_requests(),
        [ClipboardRequest {
            from: key,
            op: ClipboardOp::Read,
        }]
    );
    let answer = |to, reply| TextInput {
        clipboard: vec![ClipboardAnswer { to, reply }],
        ..quiet()
    };
    let other = page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        answer(copied.pass.key, ClipboardReply::Text("wrong".to_owned())),
        &mut values,
    );
    assert_eq!(values, [" me", ""], "an answer to another input landed");
    let pasted = page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        answer(key, ClipboardReply::Text("pasted".to_owned())),
        &mut values,
    );
    assert!(pasted.name.changed && !other.name.changed);
    assert_eq!(
        values[0], "pasted",
        "the read did not replace the selection"
    );

    let refused_border = linear("#e5484d");
    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        answer(key, ClipboardReply::Refused),
        &mut values,
    );
    let node = ui.nodes.iter().find(|node| node.key == key).expect("built");
    assert!(node.pseudo.contains(PseudoClasses::REFUSED), "no :refused");
    assert_eq!(style_of(&ui, key).border_color, refused_border);
    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([insert("!")]),
        &mut values,
    );
    assert_ne!(
        style_of(&ui, key).border_color,
        refused_border,
        "an edit did not clear :refused"
    );

    page(&mut ui, idle(), NavInput::ACCEPT, quiet(), &mut values);
    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        answer(key, ClipboardReply::Text("late".to_owned())),
        &mut values,
    );
    assert_eq!(values[0], "pasted!", "a read landed in a committed input");

    // The masked input: engaged, typed into, and asked to copy and cut.
    page(&mut ui, idle(), DOWN, quiet(), &mut values);
    page(&mut ui, idle(), NavInput::ACCEPT, quiet(), &mut values);
    let masked = page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        typed([insert("secret"), Edit::SelectAll, Edit::Copy, Edit::Cut]),
        &mut values,
    );
    assert!(
        ui.take_clipboard_requests().is_empty(),
        "a masked input offered"
    );
    assert_eq!(values[1], "secret", "a masked input cut");
    let span = part(&ui, masked.pass.key, ".text-input-text").expect("text");
    assert_eq!(shown(&ui, span), "******", "the value was drawn unmasked");
}

/// **The placeholder shows while the value is empty, in `default.css`'s
/// dimmed colour, and the text replaces it once typed.**
#[test]
fn the_placeholder_shows_while_the_value_is_empty() {
    let mut ui = Ui::new();
    let mut values = [String::new(), String::new()];
    let first = page(&mut ui, idle(), NavInput::default(), quiet(), &mut values);
    let placeholder = part(&ui, first.pass.key, ".text-input-placeholder").expect("placeholder");
    assert_eq!(shown(&ui, placeholder), "password");
    assert_eq!(style_of(&ui, placeholder).color, linear("#7a8190"));
    assert!(part(&ui, first.pass.key, ".text-input-text").is_none());
    let empty = part(&ui, first.name.key, ".text-input-text").expect("an empty span");
    assert_eq!(
        shown(&ui, empty),
        "",
        "no placeholder asked for, and one shown"
    );
    let (min, max) = rect(&ui, empty);
    assert!(max.y > min.y, "an empty input collapsed to no line");

    values[1] = "ab".to_owned();
    let typed_in = page(&mut ui, idle(), NavInput::default(), quiet(), &mut values);
    assert!(part(&ui, typed_in.pass.key, ".text-input-placeholder").is_none());
    let text = part(&ui, typed_in.pass.key, ".text-input-text").expect("text");
    assert_eq!(shown(&ui, text), "**");
}

/// **The caret blinks on the frame clock and shows solid from an edit**: it
/// is hidden once [`CARET_BLINK`] has passed with nothing typed, back after
/// another, and shown the frame an edit lands — its background transparent
/// while hidden, so the blink moves no layout.
#[test]
fn the_caret_blinks_on_the_frame_clock() {
    let (mut ui, mut values) = engaged_on("abc");
    let shown_colour = linear("#3d8bfd");
    let caret_colour =
        |ui: &Ui, key| style_of(ui, part(ui, key, ".text-input-caret").expect("caret")).background;
    let tick = |dt| TextInput { dt, ..quiet() };
    let key = page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        tick(CARET_BLINK / 2),
        &mut values,
    )
    .name
    .key;
    assert_eq!(
        caret_colour(&ui, key),
        shown_colour,
        "hidden before the interval"
    );
    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        tick(CARET_BLINK / 2),
        &mut values,
    );
    assert_eq!(caret_colour(&ui, key), [0.0; 4], "shown past the interval");
    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        tick(CARET_BLINK),
        &mut values,
    );
    assert_eq!(
        caret_colour(&ui, key),
        shown_colour,
        "not back after another"
    );
    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        tick(CARET_BLINK),
        &mut values,
    );
    assert_eq!(caret_colour(&ui, key), [0.0; 4]);
    page(
        &mut ui,
        idle(),
        NavInput::NAVIGATION,
        TextInput {
            dt: CARET_BLINK,
            ..typed([step(Motion::Left, false)])
        },
        &mut values,
    );
    assert_eq!(
        caret_colour(&ui, key),
        shown_colour,
        "a caret move left it hidden"
    );
}

/// **A value changed from outside is taken as it stands**, the caret and the
/// selection's anchor held inside it, and a disabled input takes no edits.
#[test]
fn an_outside_change_and_a_disabled_input() {
    let (mut ui, mut values) = engaged_on("abcdef");
    values[0] = "ab".to_owned();
    let shrunk = page(&mut ui, idle(), NavInput::NAVIGATION, quiet(), &mut values);
    assert_eq!(ui.text_caret(shrunk.name.key), Some((2, 0)));
    assert!(
        !shrunk.name.changed,
        "the caller's own change was reported back"
    );

    let mut values = [String::from("keep"), String::new()];
    let mut ui = Ui::new();
    let build = |ui: &mut Ui, nav, text, values: &mut [String; 2]| {
        frame_with_text(ui, idle(), nav, text, |ui| {
            ui.enabled(false, |ui| {
                ui.text_input("#off", &mut values[0]);
            });
        })
    };
    build(&mut ui, NavInput::default(), quiet(), &mut values);
    build(&mut ui, NavInput::ACCEPT, typed([insert("x")]), &mut values);
    build(
        &mut ui,
        NavInput::NAVIGATION,
        typed([insert("x")]),
        &mut values,
    );
    assert_eq!(values[0], "keep");
    assert_eq!(ui.engaged(), None, "a disabled input engaged");
}

/// **A value set from outside loses its line breaks**, as HTML strips them
/// from a text input's value: the input draws one line, hands the caller the
/// stripped value and reports that change once — not on every later frame.
#[test]
fn an_outside_value_with_a_line_break_draws_on_one_line() {
    let mut ui = Ui::new();
    let mut values = [String::from("ab"), String::new()];
    page(&mut ui, idle(), NavInput::default(), quiet(), &mut values);
    values[0] = "ab\ncd\r\n".to_owned();
    let stripped = page(&mut ui, idle(), NavInput::default(), quiet(), &mut values);
    let text = part(&ui, stripped.name.key, ".text-input-text").expect("text");
    assert_eq!(shown(&ui, text), "abcd");
    assert_eq!(values[0], "abcd");
    assert!(stripped.name.changed, "the stripped value went unreported");
    let steady = page(&mut ui, idle(), NavInput::default(), quiet(), &mut values);
    assert!(!steady.name.changed, "a stripped value changed again");
}

/// **What the input method is told follows the field**: nothing while no
/// field is typed into, ordinary text for a plain input, and a password for a
/// masked one, so a platform input method learns nothing typed there.
#[test]
fn a_masked_input_is_a_password_to_the_input_method_and_nothing_is_none() {
    use crcbl_core::input::TextPurpose;
    let (mut ui, mut values) = engaged_on("ada");
    assert_eq!(ui.text_editing_purpose(), Some(TextPurpose::Normal));
    page(&mut ui, idle(), NavInput::ACCEPT, quiet(), &mut values);
    assert_eq!(
        ui.text_editing_purpose(),
        None,
        "committed, so nothing types"
    );
    page(&mut ui, idle(), DOWN, quiet(), &mut values);
    let engaged = page(&mut ui, idle(), NavInput::ACCEPT, quiet(), &mut values);
    assert_eq!(engaged.pass.engagement, Engagement::Began);
    assert_eq!(ui.text_editing_purpose(), Some(TextPurpose::Password));
}