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
//! The context menu: opened at the pointer by a secondary press and below the
//! focused widget by `ui_menu`, picked by a click or by moves and accept,
//! reported once, with submenus beside their items — and closed a level at a
//! time by back, or all at once by a pick or a press outside. The arrows wrap
//! at a menu's ends; Home, End and typeahead move through the topmost level.

use super::*;
use crate::edit::Edit;
use crate::style::Sides;
use crate::tree::{Behavior, ContextItem, ContextMenuResponse, Jump, Position, Ui};

/// What the fixture's items report.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Pick {
    Cut,
    Copy,
    Locked,
    Small,
    Large,
    Panel,
}

/// The size submenu.
const SIZES: [ContextItem<'static, Pick>; 2] = [
    ContextItem::action("Small", Pick::Small),
    ContextItem::action("Large", Pick::Large),
];

/// The panel's own menu, under every target.
const PANEL: [ContextItem<'static, Pick>; 1] = [ContextItem::action("Panel", Pick::Panel)];

/// A target's menu: two actions, the first with a shortcut, a disabled one
/// with a shortcut too, a separator and a submenu.
fn items() -> [ContextItem<'static, Pick>; 5] {
    [
        ContextItem::action("Cut", Pick::Cut).with_shortcut(CUT_KEY),
        ContextItem::action("Copy", Pick::Copy),
        ContextItem::action("Locked", Pick::Locked)
            .with_shortcut(CUT_KEY)
            .enabled(false),
        ContextItem::Separator,
        ContextItem::submenu("Size", &SIZES),
    ]
}

/// The shortcut the fixture's Cut item shows.
const CUT_KEY: &str = "Ctrl+X";

/// The targets' size.
const TARGET: Vec2 = Vec2::new(100.0, 30.0);

/// What a page built.
struct Page {
    /// A focusable block near the top left with [`items`] as its menu.
    target: NodeKey,
    /// The same, at the page's bottom right corner.
    corner: NodeKey,
    /// A button with no menu of its own, inside the panel.
    other: NodeKey,
    /// The block around them all, with [`PANEL`] as its menu.
    panel: NodeKey,
    /// What each call reported, in that order: the target's, the corner's
    /// and the panel's.
    menus: [ContextMenuResponse<Pick>; 3],
}

/// One frame of the fixture.
fn page(ui: &mut Ui, pointer: PointerInput, nav: NavInput) -> Page {
    frame(ui, pointer, nav, |ui| {
        let size = [
            Declaration::Width(LengthAuto::Px(TARGET.x)),
            Declaration::Height(LengthAuto::Px(TARGET.y)),
            Declaration::FlexShrink(0.0),
        ];
        let fill = [
            Declaration::FlexGrow(1.0),
            Declaration::FlexDirection(FlexDirection::Column),
        ];
        let mut keys = None;
        let panel = ui.block("#panel", &fill, |ui| {
            let target = ui
                .block_with("#target", &size, Behavior::BUTTON, |_| {})
                .key;
            let target_menu = ui.context_menu(target, &items());
            let other = ui.block_with("#other", &size, Behavior::BUTTON, |_| {}).key;
            let corner_at = [
                Declaration::Position(Position::Absolute),
                Declaration::Inset(Sides::Left, LengthAuto::Px(PAGE - TARGET.x)),
                Declaration::Inset(Sides::Top, LengthAuto::Px(PAGE - TARGET.y)),
            ];
            let corner = ui
                .block_with(
                    "#corner",
                    &[&size[..], &corner_at[..]].concat(),
                    Behavior::BUTTON,
                    |_| {},
                )
                .key;
            let corner_menu = ui.context_menu(corner, &items());
            keys = Some((target, other, corner, target_menu, corner_menu));
        });
        let panel_menu = ui.context_menu(panel.key, &PANEL);
        let (target, other, corner, target_menu, corner_menu) = keys.expect("built");
        Page {
            target,
            corner,
            other,
            panel: panel.key,
            menus: [target_menu, corner_menu, panel_menu],
        }
    })
}

/// A secondary press at `pos`.
fn secondary(pos: Vec2) -> PointerInput {
    PointerInput {
        secondary_pressed: true,
        ..PointerInput::hovering(pos)
    }
}

/// The key of the item reading `label` in the menu hanging from `anchor`: the
/// parent of its label.
fn item(ui: &Ui, anchor: NodeKey, label: &str) -> NodeKey {
    children_of(ui, Ui::popup_key(anchor))
        .into_iter()
        .find(|&item| {
            children_of(ui, item)
                .first()
                .is_some_and(|&span| ui.text(span) == Some(label))
        })
        .unwrap_or_else(|| panic!("the menu holds no {label:?}"))
}

/// A point inside `key` a little way from its top-left corner.
fn inside(ui: &Ui, key: NodeKey) -> Vec2 {
    rect(ui, key).0 + Vec2::splat(5.0)
}

/// The menu opened on the target by a secondary press, at the point it was
/// opened at.
fn opened(ui: &mut Ui) -> (Page, Vec2) {
    let built = page(ui, idle(), NavInput::default());
    let at = inside(ui, built.target);
    (page(ui, secondary(at), NavInput::default()), at)
}

/// **A secondary press opens the target's menu with its corner at the
/// pointer**, reported as opened that frame and that frame only — the
/// target's and not the panel's around it, the innermost node that asked
/// winning — and a press on the panel itself closes it and opens the panel's
/// there instead.
#[test]
fn a_secondary_press_opens_the_menu_at_the_pointer() {
    let mut ui = Ui::new();
    let (built, at) = opened(&mut ui);
    assert!(built.menus[0].opened, "the press did not report opening");
    assert!(!built.menus[2].opened, "the panel's menu opened too");
    assert!(ui.is_popup_open(built.target));
    assert_eq!(rect(&ui, Ui::popup_key(built.target)).0, at);

    let next = page(&mut ui, idle(), NavInput::default());
    assert!(!next.menus[0].opened, "opening was reported twice");
    assert!(ui.is_popup_open(next.target), "the menu did not stay open");

    // On the target again, clear of the menu hanging from `at`.
    let again = rect(&ui, next.target).0 + Vec2::splat(2.0);
    let reopened = page(&mut ui, secondary(again), NavInput::default());
    assert!(
        reopened.menus[0].opened,
        "the second press did not reopen it"
    );
    assert_eq!(rect(&ui, Ui::popup_key(next.target)).0, again);

    // On the panel, clear of the targets and of the open menu.
    let beside = Vec2::splat(PAGE * 0.5);
    let (menu_min, menu_max) = rect(&ui, Ui::popup_key(next.target));
    assert!(!(beside.cmpge(menu_min).all() && beside.cmplt(menu_max).all()));
    let moved = page(&mut ui, secondary(beside), NavInput::default());
    assert!(moved.menus[2].opened, "the panel's menu did not open");
    assert!(!ui.is_popup_open(moved.target), "the target's stayed open");
    assert_eq!(rect(&ui, Ui::popup_key(moved.panel)).0, beside);
}

/// **A disabled widget's menu never opens**, and disabling a widget whose
/// menu is open closes it.
#[test]
fn a_disabled_widgets_menu_never_opens() {
    let build = |ui: &mut Ui, pointer: PointerInput, enabled: bool| {
        frame(ui, pointer, NavInput::default(), |ui| {
            let size = [
                Declaration::Width(LengthAuto::Px(TARGET.x)),
                Declaration::Height(LengthAuto::Px(TARGET.y)),
            ];
            let mut key = None;
            ui.enabled(enabled, |ui| {
                let target = ui
                    .block_with("#target", &size, Behavior::BUTTON, |_| {})
                    .key;
                ui.context_menu(target, &items());
                key = Some(target);
            });
            key.expect("built")
        })
    };
    let mut ui = Ui::new();
    let target = build(&mut ui, idle(), false);
    let at = inside(&ui, target);
    build(&mut ui, secondary(at), false);
    assert!(!ui.is_popup_open(target), "a disabled widget's menu opened");
    // A press is matched against last frame's marks, so one enabled frame
    // first.
    build(&mut ui, idle(), true);
    build(&mut ui, secondary(at), true);
    assert!(
        ui.is_popup_open(target),
        "the enabled widget's menu did not open"
    );
    build(&mut ui, idle(), false);
    assert!(!ui.is_popup_open(target), "disabling it left the menu open");
}

/// **A menu of nothing but disabled items keeps focus on its widget**, so
/// `ui_menu` can come again: the menu stays open and is not reported opened a
/// second time.
#[test]
fn ui_menu_again_over_a_menu_focus_cannot_enter_reports_nothing() {
    let disabled = [ContextItem::action("Locked", Pick::Locked).enabled(false)];
    let build = |ui: &mut Ui, nav: NavInput| {
        frame(ui, idle(), nav, |ui| {
            let size = [
                Declaration::Width(LengthAuto::Px(TARGET.x)),
                Declaration::Height(LengthAuto::Px(TARGET.y)),
            ];
            let target = ui
                .block_with("#target", &size, Behavior::BUTTON, |_| {})
                .key;
            (target, ui.context_menu(target, &disabled))
        })
    };
    let mut ui = Ui::new();
    build(&mut ui, NavInput::default());
    let (target, _) = build(&mut ui, NavInput::NEXT);
    let (_, first) = build(&mut ui, NavInput::MENU);
    assert!(first.opened);
    build(&mut ui, NavInput::NAVIGATION);
    assert_eq!(
        ui.focused(),
        Some(target),
        "focus entered a menu of nothing"
    );
    let (_, again) = build(&mut ui, NavInput::MENU);
    assert!(!again.opened, "an open menu was reported opened again");
    assert!(ui.is_popup_open(target));
}

/// **A primary press does not open it**, nor does its release.
#[test]
fn a_primary_press_does_not_open_the_menu() {
    let mut ui = Ui::new();
    let built = page(&mut ui, idle(), NavInput::default());
    let at = inside(&ui, built.target);
    let pressed = page(&mut ui, press(at), NavInput::default());
    let released = page(&mut ui, release(at), NavInput::default());
    assert!(!pressed.menus[0].opened && !released.menus[0].opened);
    assert!(!ui.is_popup_open(built.target), "a primary press opened it");
}

/// **At the viewport's bottom right it flips up and to the left of the
/// pointer**, ending at it on both axes, and stays inside the viewport.
#[test]
fn at_the_bottom_right_the_menu_flips_to_end_at_the_pointer() {
    let mut ui = Ui::new();
    let built = page(&mut ui, idle(), NavInput::default());
    let (_, corner_max) = rect(&ui, built.corner);
    let at = corner_max - Vec2::splat(5.0);
    page(&mut ui, secondary(at), NavInput::default());
    let (min, max) = rect(&ui, Ui::popup_key(built.corner));
    assert_eq!(max, at, "the menu did not flip to end at the pointer");
    assert!(min.cmpge(Vec2::ZERO).all(), "it left the viewport");
}

/// **`ui_menu` opens the focused widget's menu below it**, and nothing
/// opens when focus is on a widget no one asked a menu for.
#[test]
fn ui_menu_opens_the_focused_widgets_menu_below_it() {
    let mut ui = Ui::new();
    page(&mut ui, idle(), NavInput::default());
    let built = page(&mut ui, idle(), NavInput::NEXT);
    assert_eq!(ui.focused(), Some(built.target));
    let menu = page(&mut ui, idle(), NavInput::MENU);
    assert!(
        menu.menus[0].opened,
        "ui_menu did not open the target's menu"
    );
    let (target_min, target_max) = rect(&ui, built.target);
    assert_eq!(
        rect(&ui, Ui::popup_key(built.target)).0,
        Vec2::new(target_min.x, target_max.y)
    );

    let mut ui = Ui::new();
    page(&mut ui, idle(), NavInput::default());
    page(&mut ui, idle(), NavInput::NEXT);
    let other = page(&mut ui, idle(), NavInput::NEXT);
    assert_eq!(ui.focused(), Some(other.other));
    let menu = page(&mut ui, idle(), NavInput::MENU);
    assert!(
        menu.menus[2].opened,
        "ui_menu on a widget with no menu did not reach the panel around it"
    );
    assert!(!ui.is_popup_open(other.target));
}

/// **Moves and accept pick an item, reported once, closing the menu**: focus
/// lands on the first item, a move reaches the next, accept picks it — the
/// one frame that reports it — and focus is back on the target.
#[test]
fn moves_and_accept_pick_an_item_once_and_close_the_menu() {
    let mut ui = Ui::new();
    let (built, _) = opened(&mut ui);
    let mut picks = Vec::new();
    let mut step = |ui: &mut Ui, nav: NavInput| picks.extend(page(ui, idle(), nav).menus[0].picked);
    step(&mut ui, NavInput::NAVIGATION);
    assert_eq!(ui.focused(), Some(item(&ui, built.target, "Cut")));
    step(&mut ui, DOWN);
    assert_eq!(ui.focused(), Some(item(&ui, built.target, "Copy")));

    step(&mut ui, NavInput::ACCEPT);
    assert!(!ui.is_popup_open(built.target), "the pick left it open");
    step(&mut ui, NavInput::NAVIGATION);
    step(&mut ui, NavInput::ACCEPT);
    assert_eq!(picks, [Pick::Copy], "one pick was not reported once");
    assert_eq!(ui.focused(), Some(built.target), "focus did not return");
}

/// **A disabled item is never picked or focused**: a click on it leaves the
/// menu open with nothing reported, and a move from the item above it goes
/// past it and the separator to the submenu's item.
#[test]
fn a_disabled_item_is_neither_picked_nor_focused() {
    let mut ui = Ui::new();
    let (built, _) = opened(&mut ui);
    let on = centre(&ui, item(&ui, built.target, "Locked"));
    page(&mut ui, press(on), NavInput::default());
    let released = page(&mut ui, release(on), NavInput::default());
    assert_eq!(
        released.menus[0].picked, None,
        "the disabled item was picked"
    );
    assert!(ui.is_popup_open(built.target), "the click closed the menu");

    page(&mut ui, idle(), NavInput::NAVIGATION);
    page(&mut ui, idle(), DOWN);
    assert_eq!(ui.focused(), Some(item(&ui, built.target, "Copy")));
    page(&mut ui, idle(), DOWN);
    assert_eq!(
        ui.focused(),
        Some(item(&ui, built.target, "Size")),
        "a move stopped on the disabled item or the separator"
    );
}

/// **A click on a submenu's item opens it beside the item, and a click in
/// it picks and closes the whole chain**, reported once.
#[test]
fn a_submenu_opens_beside_its_item_and_its_pick_closes_the_chain() {
    let mut ui = Ui::new();
    let (built, _) = opened(&mut ui);
    let size = item(&ui, built.target, "Size");
    let on = centre(&ui, size);
    page(&mut ui, press(on), NavInput::default());
    page(&mut ui, release(on), NavInput::default());
    assert!(ui.is_popup_open(size), "the click did not open the submenu");
    let (item_min, item_max) = rect(&ui, size);
    assert_eq!(
        rect(&ui, Ui::popup_key(size)).0,
        Vec2::new(item_max.x, item_min.y),
        "the submenu is not beside its item"
    );

    let on = centre(&ui, item(&ui, size, "Large"));
    page(&mut ui, press(on), NavInput::default());
    let picked = page(&mut ui, release(on), NavInput::default());
    assert_eq!(picked.menus[0].picked, Some(Pick::Large));
    assert!(!ui.is_popup_open(size), "the submenu stayed open");
    assert!(!ui.is_popup_open(built.target), "the menu stayed open");
}

/// **At the viewport's right edge a submenu opens to the left of its
/// item**, ending at the item's left edge — reached by the right arrow, which
/// opens it as a click does.
#[test]
fn a_submenu_flips_left_at_the_right_edge() {
    let mut ui = Ui::new();
    let built = page(&mut ui, idle(), NavInput::default());
    let at = inside(&ui, built.corner);
    page(&mut ui, secondary(at), NavInput::default());
    page(&mut ui, idle(), NavInput::NAVIGATION);
    for _ in 0..2 {
        page(&mut ui, idle(), DOWN);
    }
    let size = item(&ui, built.corner, "Size");
    assert_eq!(ui.focused(), Some(size));
    page(&mut ui, idle(), RIGHT);
    assert!(ui.is_popup_open(size), "the right arrow did not open it");
    let (item_min, _) = rect(&ui, size);
    let (_, sub_max) = rect(&ui, Ui::popup_key(size));
    assert_eq!(
        sub_max.x, item_min.x,
        "the submenu did not flip to the left"
    );
}

/// **Back closes only the top submenu, then the menu**, focus going back to
/// the submenu's item and then to the target; left inside a submenu closes
/// it the same way.
#[test]
fn back_closes_the_top_submenu_first_then_the_menu() {
    let mut ui = Ui::new();
    let (built, _) = opened(&mut ui);
    page(&mut ui, idle(), NavInput::NAVIGATION);
    page(&mut ui, idle(), DOWN);
    page(&mut ui, idle(), DOWN);
    let size = item(&ui, built.target, "Size");
    page(&mut ui, idle(), RIGHT);
    page(&mut ui, idle(), NavInput::NAVIGATION);
    assert_eq!(
        ui.focused(),
        Some(item(&ui, size, "Small")),
        "focus did not move into the submenu"
    );

    page(&mut ui, idle(), NavInput::BACK);
    assert!(!ui.is_popup_open(size), "back left the submenu open");
    assert!(ui.is_popup_open(built.target), "back closed the menu too");
    assert_eq!(ui.focused(), Some(size));

    page(&mut ui, idle(), RIGHT);
    page(&mut ui, idle(), NavInput::NAVIGATION);
    page(&mut ui, idle(), LEFT);
    assert!(!ui.is_popup_open(size), "left left the submenu open");
    assert!(ui.is_popup_open(built.target), "left closed the menu");
    page(&mut ui, idle(), NavInput::NAVIGATION);
    assert_eq!(
        ui.focused(),
        Some(size),
        "left did not put focus on the item"
    );

    page(&mut ui, idle(), NavInput::BACK);
    assert!(!ui.is_popup_open(built.target), "back left the menu open");
    assert_eq!(ui.focused(), Some(built.target));
    assert!(
        !ui.back_requested(),
        "a back spent on the menu was reported"
    );
}

/// **A press outside closes the menu and is swallowed**: the block under it
/// is neither pressed nor clicked, and nothing is reported picked.
#[test]
fn a_press_outside_closes_the_menu_and_is_swallowed() {
    let mut ui = Ui::new();
    let (built, _) = opened(&mut ui);
    let on = centre(&ui, built.corner);
    let (menu_min, menu_max) = rect(&ui, Ui::popup_key(built.target));
    assert!(
        !(on.cmpge(menu_min).all() && on.cmplt(menu_max).all()),
        "the fixture's press lands on the menu"
    );
    let interaction = |ui: &Ui| ui.interaction_of(built.corner);
    page(&mut ui, press(on), NavInput::default());
    assert!(!ui.is_popup_open(built.target), "the press left it open");
    assert!(!interaction(&ui).pressed, "the dismissing press pressed it");
    let released = page(&mut ui, release(on), NavInput::default());
    assert_eq!(released.menus[0].picked, None);
    assert!(!interaction(&ui).clicked, "the dismissing press clicked it");
}

/// One frame of a page holding one focusable target with `items` as its
/// menu, with `text` as the frame's text input. Returns the target's key.
fn menu_page(
    ui: &mut Ui,
    nav: NavInput,
    text: TextInput,
    items: &[ContextItem<'_, Pick>],
) -> NodeKey {
    frame_with_text(ui, idle(), nav, text, |ui| {
        let size = [
            Declaration::Width(LengthAuto::Px(TARGET.x)),
            Declaration::Height(LengthAuto::Px(TARGET.y)),
            Declaration::FlexShrink(0.0),
        ];
        let target = ui
            .block_with("#target", &size, Behavior::BUTTON, |_| {})
            .key;
        ui.context_menu(target, items);
        target
    })
}

/// A frame's text input typing `text`.
fn typing(text: &str) -> TextInput {
    TextInput {
        edits: vec![Edit::Insert(text.to_owned())],
        ..TextInput::default()
    }
}

/// The target's menu over `items`, opened by `ui_menu` with focus moved into
/// it. Returns the target's key.
fn opened_by_key(ui: &mut Ui, items: &[ContextItem<'_, Pick>]) -> NodeKey {
    menu_page(ui, NavInput::default(), TextInput::default(), items);
    let target = menu_page(ui, NavInput::NEXT, TextInput::default(), items);
    menu_page(ui, NavInput::MENU, TextInput::default(), items);
    menu_page(ui, NavInput::NAVIGATION, TextInput::default(), items);
    assert!(ui.is_popup_open(target), "ui_menu did not open the menu");
    target
}

/// **Home and End go to the first and last item focus can rest on**, past a
/// disabled item at either end and the separator before the last.
#[test]
fn home_and_end_skip_disabled_items() {
    let items = [
        ContextItem::action("Locked", Pick::Locked).enabled(false),
        ContextItem::action("Cut", Pick::Cut),
        ContextItem::action("Copy", Pick::Copy),
        ContextItem::Separator,
        ContextItem::submenu("Size", &SIZES),
        ContextItem::action("Panel", Pick::Panel).enabled(false),
    ];
    let mut ui = Ui::new();
    let target = opened_by_key(&mut ui, &items);
    menu_page(&mut ui, DOWN, TextInput::default(), &items);
    assert_eq!(ui.focused(), Some(item(&ui, target, "Copy")));
    let end = NavInput::jumping(Jump::Last);
    menu_page(&mut ui, end, TextInput::default(), &items);
    assert_eq!(ui.focused(), Some(item(&ui, target, "Size")), "End");
    let home = NavInput::jumping(Jump::First);
    menu_page(&mut ui, home, TextInput::default(), &items);
    assert_eq!(ui.focused(), Some(item(&ui, target, "Cut")), "Home");
    assert!(ui.is_popup_open(target), "a jump closed the menu");
}

/// **Up and down wrap at the menu's ends**, as a Windows menu's arrows do:
/// up from the first item reaches the last, past the disabled item and the
/// separator, and down from it comes back.
#[test]
fn the_arrows_wrap_at_the_menus_ends() {
    let mut ui = Ui::new();
    let target = opened_by_key(&mut ui, &items());
    assert_eq!(ui.focused(), Some(item(&ui, target, "Cut")));
    menu_page(&mut ui, UP, TextInput::default(), &items());
    assert_eq!(
        ui.focused(),
        Some(item(&ui, target, "Size")),
        "up did not wrap to the last item"
    );
    menu_page(&mut ui, DOWN, TextInput::default(), &items());
    assert_eq!(
        ui.focused(),
        Some(item(&ui, target, "Cut")),
        "down did not wrap to the first item"
    );
}

/// **A menu longer than the viewport scrolls, and wraps to its first item
/// rather than the first in view**: End scrolls the last item into view, and
/// down from it reaches the first, scrolled back into view.
#[test]
fn a_long_menu_wraps_to_its_first_item_and_scrolls_it_into_view() {
    let labels: Vec<String> = (0..40).map(|index| format!("item {index:02}")).collect();
    let items: Vec<ContextItem<'_, Pick>> = labels
        .iter()
        .map(|label| ContextItem::action(label.as_str(), Pick::Cut))
        .collect();
    let mut ui = Ui::new();
    let target = opened_by_key(&mut ui, &items);
    let menu = Ui::popup_key(target);
    let (menu_min, menu_max) = rect(&ui, menu);
    assert!(menu_max.y - menu_min.y < PAGE, "the menu was not capped");

    menu_page(
        &mut ui,
        NavInput::jumping(Jump::Last),
        TextInput::default(),
        &items,
    );
    let last = item(&ui, target, "item 39");
    assert_eq!(ui.focused(), Some(last));
    assert!(ui.scroll_offset_of(menu).y > 0.0, "End did not scroll");
    menu_page(&mut ui, DOWN, TextInput::default(), &items);
    let first = item(&ui, target, "item 00");
    assert_eq!(
        ui.focused(),
        Some(first),
        "down wrapped to an item other than the first"
    );
    assert_eq!(
        ui.scroll_offset_of(menu).y,
        0.0,
        "the first item is not in view"
    );
}

/// **Typeahead skips a disabled item**: `c` from Cut steps past the disabled
/// Copy to Crop.
#[test]
fn typeahead_skips_disabled_items() {
    let items = [
        ContextItem::action("Cut", Pick::Cut),
        ContextItem::action("Copy", Pick::Copy).enabled(false),
        ContextItem::action("Crop", Pick::Small),
    ];
    let mut ui = Ui::new();
    let target = opened_by_key(&mut ui, &items);
    assert_eq!(ui.focused(), Some(item(&ui, target, "Cut")));
    menu_page(&mut ui, NavInput::NAVIGATION, typing("c"), &items);
    menu_page(&mut ui, NavInput::NAVIGATION, TextInput::default(), &items);
    assert_eq!(ui.focused(), Some(item(&ui, target, "Crop")));
}

/// **A submenu takes the typing while it is the topmost menu**, and the
/// menu under it does not: `l` reaches Large, `c` — only the menu under it
/// has a C — moves nothing, and the right arrow, the pick and its report work
/// as they did.
#[test]
fn a_submenu_takes_typeahead_and_still_picks() {
    let mut ui = Ui::new();
    let target = opened_by_key(&mut ui, &items());
    menu_page(
        &mut ui,
        NavInput::jumping(Jump::Last),
        TextInput::default(),
        &items(),
    );
    let size = item(&ui, target, "Size");
    menu_page(&mut ui, RIGHT, TextInput::default(), &items());
    menu_page(
        &mut ui,
        NavInput::NAVIGATION,
        TextInput::default(),
        &items(),
    );
    assert_eq!(ui.focused(), Some(item(&ui, size, "Small")));
    assert!(ui.popup_list_open(), "the submenu does not take the keys");

    menu_page(&mut ui, NavInput::NAVIGATION, typing("l"), &items());
    menu_page(
        &mut ui,
        NavInput::NAVIGATION,
        TextInput::default(),
        &items(),
    );
    let large = item(&ui, size, "Large");
    assert_eq!(ui.focused(), Some(large), "the submenu did not take the l");
    menu_page(&mut ui, NavInput::NAVIGATION, typing("c"), &items());
    menu_page(
        &mut ui,
        NavInput::NAVIGATION,
        TextInput::default(),
        &items(),
    );
    assert_eq!(ui.focused(), Some(large), "the menu under it took the c");

    let picked = frame(&mut ui, idle(), NavInput::ACCEPT, |ui| {
        let size = [
            Declaration::Width(LengthAuto::Px(TARGET.x)),
            Declaration::Height(LengthAuto::Px(TARGET.y)),
            Declaration::FlexShrink(0.0),
        ];
        let target = ui
            .block_with("#target", &size, Behavior::BUTTON, |_| {})
            .key;
        ui.context_menu(target, &items()).picked
    });
    assert_eq!(picked, Some(Pick::Large));
    assert!(!ui.is_popup_open(target), "the pick left the menu open");
}

/// **An action given a shortcut shows it after its label, at the item's right
/// end**, in a span of its own; one without shows its label alone.
#[test]
fn a_shortcut_is_shown_after_the_label() {
    let mut ui = Ui::new();
    let (built, _) = opened(&mut ui);
    let cut = item(&ui, built.target, "Cut");
    let [label, shortcut] = children_of(&ui, cut)[..] else {
        panic!("Cut holds no label and shortcut");
    };
    assert_eq!(ui.text(shortcut), Some(CUT_KEY));
    let (label_min, label_max) = rect(&ui, label);
    let (shortcut_min, shortcut_max) = rect(&ui, shortcut);
    assert!(
        shortcut_min.x >= label_max.x,
        "the shortcut is not after the label"
    );
    let copy = item(&ui, built.target, "Copy");
    let (_, copy_max) = rect(&ui, copy);
    assert!(
        copy_max.x - shortcut_max.x < shortcut_min.x - label_min.x,
        "the shortcut ends at {shortcut_max}, short of the item's end at {copy_max}"
    );
    assert_eq!(children_of(&ui, copy).len(), 1, "Copy shows a shortcut");
    let locked = item(&ui, built.target, "Locked");
    assert_eq!(
        children_of(&ui, locked).len(),
        2,
        "disabling an item lost its shortcut"
    );
}