kui-core 0.1.0-alpha.46

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
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
//! Submenus (backlog F128, from Noticon): a row that opens a menu of its
//! own beside it — "Move to ▸", "Sort by ▸" — in the drawn context menu
//! and the drawn menu bar, by the pointer and by the keyboard, and the
//! doors a host that shows menus itself reports a row inside one through.

use kui_core::testing::{click, edit_key, hover};
use kui_core::{
    BarMenu, Core, EditKey, Key, Menu, MenuBar, MenuItem, Mods, NodeSpec, Rect, Role, Size,
    TextStyle, Value, Vec2,
};

/// A card of text the menus are about, with whatever the core has open
/// drawn over it by `Ui::finish`; the bar when one is given.
fn frame_with(core: &mut Core, bar: Option<MenuBar>) -> Key {
    let mut ui = core.frame(Size::new(600.0, 400.0), 1.0);
    ui.configure_root(NodeSpec::column().fill());
    if let Some(bar) = bar {
        kui_core::widgets::menu_bar(&mut ui, bar);
    }
    let card = ui.with_keyed("card", NodeSpec::column().grow_width(), |ui| {
        ui.text("a note", TextStyle::new(14.0));
    });
    ui.finish();
    card
}

fn frame(core: &mut Core) -> Key {
    frame_with(core, None)
}

/// Noticon's note menu, cut down: a plain row, a submenu of folders with
/// one disabled, a submenu nested in a submenu, and a plain row after.
fn items() -> Vec<MenuItem> {
    vec![
        MenuItem::new("Open").id("open"),
        MenuItem::submenu(
            "Move to",
            vec![
                MenuItem::new("Inbox").id("move.inbox"),
                MenuItem::new("Archive").id("move.archive").enabled(false),
                MenuItem::submenu(
                    "Projects",
                    vec![MenuItem::new("kui").id("move.kui").accel("mod+shift+k")],
                ),
            ],
        ),
        MenuItem::new("Delete").id("delete"),
    ]
}

fn open(core: &mut Core) -> Key {
    let card = frame(core);
    core.open_menu(Menu::new(card, Vec2::new(20.0, 20.0), items()));
    frame(core);
    card
}

/// The access node for the menu row `label`, if it is drawn.
fn row(core: &mut Core, label: &str) -> Option<kui_core::AccessNode> {
    core.access_tree()
        .nodes
        .iter()
        .find(|n| n.role == Role::MenuItem && n.name.as_deref() == Some(label))
        .cloned()
}

fn center(r: Rect) -> Vec2 {
    Vec2::new(r.x + r.w / 2.0, r.y + r.h / 2.0)
}

fn row_center(core: &mut Core, label: &str) -> Vec2 {
    center(
        row(core, label)
            .unwrap_or_else(|| panic!("no menu row named {label:?}"))
            .rect,
    )
}

fn menus(core: &mut Core) -> Vec<String> {
    core.access_tree()
        .nodes
        .iter()
        .filter(|n| n.role == Role::Menu)
        .map(|n| n.name.clone().unwrap_or_default())
        .collect()
}

fn key(core: &mut Core, k: EditKey) -> Vec<kui_core::UiEvent> {
    edit_key(core, k, Mods::default())
}

fn focused_row(core: &mut Core) -> Option<String> {
    core.access_tree()
        .nodes
        .iter()
        .find(|n| n.focused)
        .and_then(|n| n.name.clone())
}

#[test]
fn a_submenu_row_draws_closed_and_reads_as_collapsed() {
    let mut core = Core::new();
    open(&mut core);
    assert_eq!(menus(&mut core), ["Menu"], "only the menu itself is drawn");
    let move_to = row(&mut core, "Move to").expect("the row is drawn");
    assert_eq!(
        move_to.expanded,
        Some(false),
        "a reader hears it opens something"
    );
    assert!(
        row(&mut core, "Inbox").is_none(),
        "nothing inside is drawn yet"
    );
    assert_eq!(core.menu_submenus(), &[] as &[usize]);
}

/// Resting the pointer on the row opens its submenu beside it, level with
/// it; resting on another row of the same menu closes it again.
#[test]
fn the_pointer_opens_a_submenu_and_another_row_closes_it() {
    let mut core = Core::new();
    open(&mut core);
    let at = row_center(&mut core, "Move to");
    hover(&mut core, at);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1]);
    assert_eq!(menus(&mut core), ["Menu", "Move to"]);
    let parent = row(&mut core, "Move to").unwrap();
    assert_eq!(parent.expanded, Some(true));
    let inbox = row(&mut core, "Inbox")
        .expect("the submenu's rows are drawn")
        .rect;
    assert!(
        inbox.x >= parent.rect.x + parent.rect.w,
        "beside its row: {inbox:?} against {:?}",
        parent.rect
    );
    assert!(
        (inbox.y - parent.rect.y).abs() < 1.0,
        "its first row level with it: {} against {}",
        inbox.y,
        parent.rect.y
    );

    // Into the submenu and down a level.
    let at = row_center(&mut core, "Projects");
    hover(&mut core, at);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1, 2]);
    assert!(row(&mut core, "kui").is_some());

    // Back on a plain row of the first submenu: the nested one closes.
    let at = row_center(&mut core, "Inbox");
    hover(&mut core, at);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1]);
    assert!(row(&mut core, "kui").is_none());

    // And on a plain row of the menu itself: everything closes.
    let at = row_center(&mut core, "Delete");
    hover(&mut core, at);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[] as &[usize]);
    assert_eq!(menus(&mut core), ["Menu"]);
}

/// A row inside a submenu is chosen as any row is: its own `menu` event,
/// on the node the menu is about, and the whole menu closes. A click on a
/// row that opens a submenu chooses nothing and leaves the menu open.
#[test]
fn choosing_a_row_inside_posts_it_and_closes_every_level() {
    let mut core = Core::new();
    let card = open(&mut core);
    let at = row_center(&mut core, "Move to");
    let evs = click(&mut core, at);
    assert!(
        evs.is_empty(),
        "the submenu's row is nobody's click: {evs:?}"
    );
    assert!(core.menu().is_some(), "still open");
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1], "and the click opened it");

    let at = row_center(&mut core, "Projects");
    click(&mut core, at);
    frame(&mut core);
    let at = row_center(&mut core, "kui");
    let evs = click(&mut core, at);
    assert_eq!(evs.len(), 1, "{evs:?}");
    assert_eq!(evs[0].key, card, "on the node the menu is about");
    assert_eq!(evs[0].kind(), Some("menu"));
    assert_eq!(evs[0].payload.get_str("item"), Some("move.kui"));
    assert!(core.menu().is_none(), "every level closes");
    assert_eq!(core.menu_submenus(), &[] as &[usize]);
}

/// A disabled row inside a submenu is inert, as one outside is.
#[test]
fn a_disabled_row_inside_is_inert() {
    let mut core = Core::new();
    open(&mut core);
    let at = row_center(&mut core, "Move to");
    click(&mut core, at);
    frame(&mut core);
    let at = row_center(&mut core, "Archive");
    let evs = click(&mut core, at);
    assert!(evs.is_empty(), "{evs:?}");
    assert!(core.menu().is_some());
}

/// The keyboard: Right on a row with a submenu opens it with focus on its
/// first row, the arrows walk that submenu's rows, Left closes it with
/// focus back on its row, and Escape closes the innermost submenu before
/// it closes the menu.
#[test]
fn the_arrows_open_and_close_a_submenu_and_escape_closes_one_level() {
    let mut core = Core::new();
    open(&mut core);
    assert_eq!(
        focused_row(&mut core).as_deref(),
        Some("Open"),
        "the menu opens on its first row"
    );
    key(&mut core, EditKey::Down);
    frame(&mut core);
    assert_eq!(focused_row(&mut core).as_deref(), Some("Move to"));

    // Right opens it, and the first row inside has the keyboard.
    let evs = key(&mut core, EditKey::Right);
    assert!(evs.is_empty(), "{evs:?}");
    assert_eq!(core.menu_submenus(), &[1], "opened by the key");
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1]);
    assert_eq!(focused_row(&mut core).as_deref(), Some("Inbox"));

    // The arrows walk the submenu's rows, stepping over the dead one.
    key(&mut core, EditKey::Down);
    frame(&mut core);
    assert_eq!(focused_row(&mut core).as_deref(), Some("Projects"));
    key(&mut core, EditKey::Right);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1, 2]);
    assert_eq!(focused_row(&mut core).as_deref(), Some("kui"));

    // Left: back out a level, on the row it hung from.
    key(&mut core, EditKey::Left);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1]);
    assert_eq!(focused_row(&mut core).as_deref(), Some("Projects"));

    // Escape: the submenu, not the menu.
    let evs = key(&mut core, EditKey::Escape);
    assert!(evs.is_empty(), "{evs:?}");
    frame(&mut core);
    assert!(core.menu().is_some(), "the menu is still open");
    assert_eq!(core.menu_submenus(), &[] as &[usize]);
    assert_eq!(focused_row(&mut core).as_deref(), Some("Move to"));
    // And again: now the menu.
    key(&mut core, EditKey::Escape);
    assert!(core.menu().is_none());

    // Enter on the row opens it too, from the keyboard, focus inside.
    let mut core = Core::new();
    open(&mut core);
    key(&mut core, EditKey::Down);
    frame(&mut core);
    assert_eq!(focused_row(&mut core).as_deref(), Some("Move to"));
    key(&mut core, EditKey::Enter);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1]);
    assert_eq!(focused_row(&mut core).as_deref(), Some("Inbox"));
    let evs = key(&mut core, EditKey::Enter);
    assert_eq!(evs.len(), 1, "{evs:?}");
    assert_eq!(evs[0].payload.get_str("item"), Some("move.inbox"));
}

/// A pointer resting on a row does not undo what the keyboard did: the
/// hover opens and closes on a change of row only.
#[test]
fn a_resting_pointer_does_not_undo_the_keyboard() {
    let mut core = Core::new();
    open(&mut core);
    let at = row_center(&mut core, "Open");
    hover(&mut core, at);
    frame(&mut core);
    key(&mut core, EditKey::Down);
    frame(&mut core);
    key(&mut core, EditKey::Right);
    frame(&mut core);
    frame(&mut core);
    assert_eq!(
        core.menu_submenus(),
        &[1],
        "still open under a pointer on Open"
    );
}

/// A submenu's accelerator is the platform's spelling too, and the row
/// with the submenu draws a chevron where an accelerator would be.
#[test]
fn a_row_inside_reads_its_accelerator_in_the_platforms_spelling() {
    let mut core = Core::new();
    open(&mut core);
    let kui = MenuItem::at_path(&core.menu().unwrap().items, &[1, 2, 0])
        .unwrap()
        .accel
        .clone();
    let want = if cfg!(target_os = "macos") {
        "\u{21e7}\u{2318}K"
    } else {
        "Ctrl+Shift+K"
    };
    assert_eq!(kui.as_deref(), Some(want));
}

/// A host that shows menus itself reports a row inside a submenu by its
/// path; a row that opens one is refused, as the platform never reports
/// it, and so is a dead one.
#[test]
fn a_host_reports_a_row_inside_by_its_path() {
    let mut core = Core::new();
    let card = open(&mut core);
    core.set_native_menus(true);
    assert_eq!(
        core.activate_menu_item(1),
        None,
        "the row that opens a submenu"
    );
    assert_eq!(core.activate_menu_path(&[1]), None);
    assert_eq!(core.activate_menu_path(&[1, 1]), None, "a dead row inside");
    assert!(core.menu().is_some());
    let evs = core.activate_menu_path(&[1, 2, 0]).expect("taken");
    assert_eq!(evs.len(), 1);
    assert_eq!(evs[0].key, card);
    assert_eq!(evs[0].payload.get_str("item"), Some("move.kui"));
    assert!(core.menu().is_none());
}

/// The rows come from plain data the way every binding sends them: a
/// row's `items` are its submenu.
#[test]
fn a_rows_items_are_its_submenu() {
    let v = Value::List(vec![Value::map([
        ("label", Value::str("Sort by")),
        (
            "items",
            Value::List(vec![
                Value::map([
                    ("label", Value::str("Name")),
                    ("checked", Value::Bool(true)),
                ]),
                Value::map([("label", Value::str("Date"))]),
            ]),
        ),
    ])]);
    let rows = MenuItem::list_from_value(&v).unwrap();
    assert!(rows[0].has_submenu());
    assert_eq!(rows[0].submenu.len(), 2);
    assert!(rows[0].submenu[0].checked);
    assert!(MenuItem::KEYS.contains(&"items"));
    // A row in a submenu is read with the same rules as any row.
    let bad = Value::List(vec![Value::map([
        ("label", Value::str("Sort by")),
        (
            "items",
            Value::List(vec![Value::map([("role", Value::str("custom"))])]),
        ),
    ])]);
    assert!(MenuItem::list_from_value(&bad).is_err());
}

/// The drawn menu bar's menus nest the same way: a title opens its menu,
/// the pointer opens a submenu in it, and a row inside posts its item.
#[test]
fn the_drawn_bar_opens_submenus_too() {
    let bar = || {
        MenuBar::new(vec![BarMenu::new(
            "View",
            vec![
                MenuItem::new("Zoom in").id("zoom"),
                MenuItem::submenu(
                    "Sort by",
                    vec![
                        MenuItem::new("Name").id("sort.name").checked(true),
                        MenuItem::new("Date").id("sort.date"),
                    ],
                ),
            ],
        )])
    };
    let mut core = Core::new();
    frame_with(&mut core, Some(bar()));
    let at = row_center(&mut core, "View");
    click(&mut core, at);
    frame_with(&mut core, Some(bar()));
    assert_eq!(core.menu_bar_open(), Some(0));
    let at = row_center(&mut core, "Sort by");
    hover(&mut core, at);
    frame_with(&mut core, Some(bar()));
    assert_eq!(core.menu_bar_submenus(), &[1]);
    let at = row_center(&mut core, "Date");
    let evs = click(&mut core, at);
    assert_eq!(evs.len(), 1, "{evs:?}");
    assert_eq!(evs[0].payload.get_str("item"), Some("sort.date"));
    assert_eq!(core.menu_bar_open(), None, "the bar's menu closes");
    assert_eq!(core.menu_bar_submenus(), &[] as &[usize]);

    // And the platform's bar reports a row inside by its path.
    let evs = core.activate_menu_bar_path(0, &[1, 0]);
    assert_eq!(evs[0].payload.get_str("item"), Some("sort.name"));
    assert!(
        core.activate_menu_bar_path(0, &[1]).is_empty(),
        "the submenu's own row"
    );
}

/// A host's report of a row the menu could not have shown it — a dead row,
/// one under a dead submenu row, one in a dead bar menu — is refused, as a
/// dead top-level row always was in the context menu; the bar's path door
/// performed them all.
#[test]
fn a_host_cannot_choose_a_row_the_menu_would_not_reach() {
    let rows = || {
        vec![
            MenuItem::new("Undo").id("undo").enabled(false),
            MenuItem::submenu("Sort by", vec![MenuItem::new("Name").id("sort.name")])
                .enabled(false),
            MenuItem::submenu("Group by", vec![MenuItem::new("Day").id("group.day")]),
        ]
    };
    let bar = MenuBar::new(vec![
        BarMenu::new("View", rows()),
        BarMenu::new("Dead", vec![MenuItem::new("Go").id("go")]).enabled(false),
    ]);
    let mut core = Core::new();
    frame_with(&mut core, Some(bar));
    assert!(
        core.activate_menu_bar_path(0, &[0]).is_empty(),
        "a dead row"
    );
    assert!(
        core.activate_menu_bar_item(0, 0).is_empty(),
        "a dead row, by index"
    );
    assert!(
        core.activate_menu_bar_path(0, &[1, 0]).is_empty(),
        "under a dead row"
    );
    assert!(
        core.activate_menu_bar_path(1, &[0]).is_empty(),
        "in a dead menu"
    );
    let evs = core.activate_menu_bar_path(0, &[2, 0]);
    assert_eq!(evs[0].payload.get_str("item"), Some("group.day"));

    let card = frame(&mut core);
    core.open_menu(Menu::new(card, Vec2::new(20.0, 20.0), rows()));
    frame(&mut core);
    assert_eq!(core.activate_menu_path(&[1, 0]), None, "under a dead row");
    assert!(core.menu().is_some(), "and the menu stays open");
    let evs = core.activate_menu_path(&[2, 0]).expect("taken");
    assert_eq!(evs[0].payload.get_str("item"), Some("group.day"));
}

/// An app drawing `widgets::context_menu` itself opens no submenu, but the
/// pointer resting on one of its submenu rows used to note one open in the
/// core's state all the same — and with no menu of the core's left to
/// close, the next Escape was spent on it and never reached the modal.
#[test]
fn an_app_drawn_menu_leaves_no_submenu_for_escape_to_spend() {
    let draw = |core: &mut Core, with_menu: bool| -> Key {
        let mut ui = core.frame(Size::new(600.0, 400.0), 1.0);
        ui.configure_root(NodeSpec::column().fill());
        let dialog = ui.leaf_keyed(
            "dialog",
            NodeSpec::column()
                .size(200.0, 100.0)
                .float(kui_core::FloatConfig::viewport())
                .modal(Value::Null)
                .label("Dialog"),
        );
        if with_menu {
            kui_core::widgets::context_menu(&mut ui, Vec2::new(20.0, 20.0), &items());
        }
        ui.finish();
        dialog
    };
    let mut core = Core::new();
    draw(&mut core, true);
    let at = row_center(&mut core, "Move to");
    hover(&mut core, at);
    draw(&mut core, true);
    hover(&mut core, Vec2::new(500.0, 350.0));
    let dialog = draw(&mut core, false);
    let evs = key(&mut core, EditKey::Escape);
    assert_eq!(evs.len(), 1, "{evs:?}");
    assert_eq!(evs[0].key, dialog);
    assert_eq!(evs[0].kind(), Some("dismiss"));
}

/// On the frame clock, the pointer crossing a row on its way into an open
/// submenu does not close it: a row below "Move to" is what a diagonal
/// path to a lower row of its submenu passes over, and each one used to
/// close the submenu the pointer was heading for. Resting on that row
/// does switch, once the wait is up, and the frames for it are owed
/// meanwhile (backlog RG150).
#[test]
fn passing_over_a_row_on_the_way_into_a_submenu_keeps_it_open() {
    let mut core = Core::new();
    core.set_time(10.0);
    open(&mut core);
    let at = row_center(&mut core, "Move to");
    hover(&mut core, at);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1]);

    // Over "Delete", then on into the submenu's last row.
    core.set_time(10.05);
    let at = row_center(&mut core, "Delete");
    hover(&mut core, at);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1], "passing over Delete");
    assert!(core.animating(), "the wait owes its frames");
    core.set_time(10.1);
    let at = row_center(&mut core, "Projects");
    hover(&mut core, at);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1, 2], "reached, and opened");
    core.set_time(11.0);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1, 2], "nothing left waiting");

    // Resting on Delete: the submenus give way once the wait is up.
    core.set_time(12.0);
    let at = row_center(&mut core, "Delete");
    hover(&mut core, at);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1, 2]);
    core.set_time(12.2);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1, 2], "not yet");
    core.set_time(12.4);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[] as &[usize], "rested long enough");
    assert_eq!(menus(&mut core), ["Menu"]);
    assert!(!core.animating(), "and nothing owed after");
}

/// A submenu the keyboard closed opens again when the pointer leaves the
/// menu and comes back to its row: the row the pointer was last on is
/// forgotten once it is on none, where it used to need another row
/// visited first (backlog RG150).
#[test]
fn a_row_the_keyboard_closed_reopens_when_the_pointer_comes_back() {
    let mut core = Core::new();
    open(&mut core);
    let at = row_center(&mut core, "Move to");
    hover(&mut core, at);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1]);
    key(&mut core, EditKey::Escape);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[] as &[usize]);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[] as &[usize], "a resting pointer");

    hover(&mut core, Vec2::new(590.0, 390.0));
    frame(&mut core);
    hover(&mut core, at);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1], "open again");
}

/// The pointer leaving the menu with a switch waiting: the switch is
/// dropped — the one path a build with no row hovered clears it on — the
/// submenu stays open, and nothing is owed (backlog RG154).
#[test]
fn the_pointer_leaving_the_menu_drops_a_waiting_switch() {
    let mut core = Core::new();
    core.set_time(10.0);
    open(&mut core);
    let at = row_center(&mut core, "Move to");
    hover(&mut core, at);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1]);
    core.set_time(10.05);
    let at = row_center(&mut core, "Delete");
    hover(&mut core, at);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1]);
    assert!(core.animating(), "waiting");
    core.set_time(10.1);
    hover(&mut core, Vec2::new(590.0, 390.0));
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1], "still open");
    assert!(!core.animating(), "nothing waiting once the pointer is off");
    core.set_time(11.0);
    frame(&mut core);
    assert_eq!(core.menu_submenus(), &[1], "no switch ripened off the menu");
}

/// A bar the app stops drawing while a switch waits — or hands to the
/// platform — has no rows for the switch to ripen on: the switch is
/// dropped at the frame's end, so the frames it owed are not owed for
/// good (backlog RG154).
#[test]
fn a_bar_the_app_stops_drawing_owes_nothing_for_a_waiting_switch() {
    let bar = || {
        MenuBar::new(vec![BarMenu::new(
            "View",
            vec![
                MenuItem::new("Zoom in").id("zoom"),
                MenuItem::submenu("Sort by", vec![MenuItem::new("Name").id("sort.name")]),
            ],
        )])
    };
    let mut core = Core::new();
    core.set_time(10.0);
    frame_with(&mut core, Some(bar()));
    let at = row_center(&mut core, "View");
    click(&mut core, at);
    frame_with(&mut core, Some(bar()));
    let at = row_center(&mut core, "Sort by");
    hover(&mut core, at);
    frame_with(&mut core, Some(bar()));
    assert_eq!(core.menu_bar_submenus(), &[1]);
    core.set_time(10.05);
    let at = row_center(&mut core, "Zoom in");
    hover(&mut core, at);
    frame_with(&mut core, Some(bar()));
    assert_eq!(core.menu_bar_submenus(), &[1], "waiting");
    assert!(core.animating());
    // The app stops drawing the bar; the declaration stands.
    frame_with(&mut core, None);
    assert!(
        !core.animating(),
        "nothing owed for a bar that is not drawn"
    );
    core.set_time(11.0);
    frame_with(&mut core, None);
    assert!(!core.animating());
    assert_eq!(core.menu_bar_submenus(), &[1], "nothing switched either");
}