quickgui 0.1.4

A damage-driven, GPU-accelerated desktop GUI foundation
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
use crate::{
    AccessibilityOrientation, AccessibilityPopover, AccessibilityRole, Element, ElementId,
    EventContext, FocusHandle, KeyBinding, StateAccessor, ViewContext, div,
};

/// Maximum top-level menus one in-window menubar manages.
///
/// A menubar is a small fixed set of application commands. The bound keeps roving navigation,
/// hover switching, and the retained indices constant-size whatever the application declares.
pub const MAX_MENUBAR_MENUS: usize = 64;

const MENUBAR_KEY_CONTEXT: &str = "Menubar";
const MENUBAR_ITEM_ID_TAG: u64 = 0x4d17_ec6b_9a20_35f1;

/// Move menubar focus to the previous menu, wrapping at the start.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct MenubarPrevious;
/// Move menubar focus to the next menu, wrapping at the end.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct MenubarNext;
/// Move menubar focus to the first menu.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct MenubarFirst;
/// Move menubar focus to the final menu.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct MenubarLast;
/// Open the focused menu.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct MenubarOpen;
/// Close the open menu and keep focus on the menubar.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct MenubarClose;

/// Contextual bindings used by [`MenubarItem::key_with`].
///
/// Left and Right move between menus while the bar has focus, and they keep working while a menu is
/// open because an open menu switches instead of closing — the desktop menubar convention. Down,
/// Return, and Space open the focused menu; Escape closes it without leaving the bar.
pub fn menubar_key_bindings() -> [KeyBinding; 8] {
    [
        KeyBinding::new("left", MenubarPrevious, Some(MENUBAR_KEY_CONTEXT)),
        KeyBinding::new("right", MenubarNext, Some(MENUBAR_KEY_CONTEXT)),
        KeyBinding::new("home", MenubarFirst, Some(MENUBAR_KEY_CONTEXT)),
        KeyBinding::new("end", MenubarLast, Some(MENUBAR_KEY_CONTEXT)),
        KeyBinding::new("down", MenubarOpen, Some(MENUBAR_KEY_CONTEXT)),
        KeyBinding::new("enter", MenubarOpen, Some(MENUBAR_KEY_CONTEXT)),
        KeyBinding::new("space", MenubarOpen, Some(MENUBAR_KEY_CONTEXT)),
        KeyBinding::new("escape", MenubarClose, Some(MENUBAR_KEY_CONTEXT)),
    ]
}

/// Controlled roving-focus and open state for one in-window menubar.
///
/// The application declares the menus and composes each surface from
/// [`crate::PopoverMenu`]; QuickGUI retains only which menu owns the bar's single Tab stop and
/// which one is open. The state is a plain copyable value with no item registry, allocation, task,
/// timer, observer, or idle scheduler source.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct MenubarState {
    count: usize,
    focused: usize,
    open: Option<usize>,
    disabled: bool,
}

impl Default for MenubarState {
    fn default() -> Self {
        Self::new(0)
    }
}

impl MenubarState {
    /// A closed menubar over `menu_count` menus, focused on the first one.
    ///
    /// A larger count than [`MAX_MENUBAR_MENUS`] is clamped rather than rejected, so a dynamic
    /// application menu can never grow the retained state without bound.
    pub const fn new(menu_count: usize) -> Self {
        Self {
            count: if menu_count > MAX_MENUBAR_MENUS {
                MAX_MENUBAR_MENUS
            } else {
                menu_count
            },
            focused: 0,
            open: None,
            disabled: false,
        }
    }

    #[must_use]
    pub const fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    pub const fn is_disabled(self) -> bool {
        self.disabled
    }

    pub const fn menu_count(self) -> usize {
        self.count
    }

    /// Replace the declared menu count, clamping focus and closing a menu that no longer exists.
    pub const fn set_menu_count(&mut self, menu_count: usize) -> bool {
        let count = if menu_count > MAX_MENUBAR_MENUS {
            MAX_MENUBAR_MENUS
        } else {
            menu_count
        };
        if self.count == count {
            return false;
        }
        self.count = count;
        if self.focused >= count {
            self.focused = if count == 0 { 0 } else { count - 1 };
        }
        if let Some(open) = self.open
            && open >= count
        {
            self.open = None;
        }
        true
    }

    /// The menu that owns the menubar's single Tab stop.
    pub const fn focused_menu(self) -> usize {
        self.focused
    }

    pub const fn open_menu(self) -> Option<usize> {
        self.open
    }

    pub const fn is_open(self, index: usize) -> bool {
        matches!(self.open, Some(open) if open == index)
    }

    /// Move the roving Tab stop, keeping an already open menu in sync.
    pub const fn focus_menu(&mut self, index: usize) -> bool {
        if self.disabled || index >= self.count || self.focused == index {
            return false;
        }
        self.focused = index;
        if self.open.is_some() {
            self.open = Some(index);
        }
        true
    }

    /// Move focus one menu along the bar, wrapping at both ends.
    pub const fn move_focus(&mut self, forward: bool) -> bool {
        if self.disabled || self.count == 0 {
            return false;
        }
        let next = if forward {
            (self.focused + 1) % self.count
        } else {
            (self.focused + self.count - 1) % self.count
        };
        self.focus_menu(next)
    }

    /// Move focus to the first or last menu.
    pub const fn focus_edge(&mut self, last: bool) -> bool {
        if self.disabled || self.count == 0 {
            return false;
        }
        self.focus_menu(if last { self.count - 1 } else { 0 })
    }

    /// Open one menu and give it the roving Tab stop.
    pub const fn open_menu_at(&mut self, index: usize) -> bool {
        if self.disabled || index >= self.count {
            return false;
        }
        let changed = self.focused != index || !self.is_open(index);
        self.focused = index;
        self.open = Some(index);
        changed
    }

    /// Open the focused menu.
    pub const fn open_focused(&mut self) -> bool {
        if self.disabled || self.count == 0 || self.is_open(self.focused) {
            return false;
        }
        self.open_menu_at(self.focused)
    }

    /// Open a closed menu or close the one that is open.
    pub const fn toggle_menu(&mut self, index: usize) -> bool {
        if self.disabled || index >= self.count {
            return false;
        }
        if self.is_open(index) {
            return self.close();
        }
        self.open_menu_at(index)
    }

    /// Close whichever menu is open. Focus stays on the bar.
    pub const fn close(&mut self) -> bool {
        if self.open.is_none() {
            return false;
        }
        self.open = None;
        true
    }

    /// Switch menus on hover.
    ///
    /// Hovering does nothing while the bar is closed, and switches the open menu while one is
    /// open, which is exactly how a desktop menubar behaves.
    pub const fn hover_menu(&mut self, index: usize) -> bool {
        if self.disabled || index >= self.count || self.open.is_none() || self.is_open(index) {
            return false;
        }
        self.focused = index;
        self.open = Some(index);
        true
    }
}

/// A copyable declaration for one unstyled in-window menubar.
///
/// The descriptor retains no allocation, task, timer, observer, or idle scheduler source.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[must_use = "a Menubar descriptor has no effect until one of its parts is mounted"]
pub struct Menubar {
    root_id: ElementId,
}

impl Menubar {
    pub fn new(root_id: impl Into<ElementId>) -> Self {
        Self {
            root_id: root_id.into(),
        }
    }

    pub const fn root_id(self) -> ElementId {
        self.root_id
    }

    pub fn item_id(self, index: usize) -> ElementId {
        derived_menubar_id(self.root_id, MENUBAR_ITEM_ID_TAG, index as u64)
    }

    /// Describe one top-level menu trigger.
    ///
    /// Indices at or past [`MenubarState::menu_count`] return `None`, so a caller-driven loop stays
    /// bounded by the state instead of by its own arithmetic.
    pub const fn item(self, state: MenubarState, index: usize) -> Option<MenubarItem> {
        if index >= state.count {
            return None;
        }
        Some(MenubarItem {
            bar: self,
            state,
            index,
        })
    }

    /// Decorate an application-owned menubar root without adding layout or appearance.
    pub fn root_with(self, state: MenubarState, root: Element) -> Element {
        let disabled = state.disabled || root.accessibility.disabled;
        let root = root
            .id(self.root_id)
            .accessibility_role(AccessibilityRole::MenuBar)
            .accessibility_orientation(AccessibilityOrientation::Horizontal)
            .accessibility_size_of_set(state.count)
            .cursor_default()
            .app_region_no_drag()
            .user_select_none()
            .disabled(disabled);
        match state.open {
            Some(open) => root.accessibility_active_descendant(self.item_id(open)),
            None => root.accessibility_active_descendant(self.item_id(state.focused)),
        }
    }
    /// Create the unstyled root part. Use [`Self::root_with`] to supply an existing element.
    pub fn root(self, state: MenubarState) -> Element {
        self.root_with(state, crate::div())
    }
}

/// A copyable declaration for one menubar trigger.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[must_use = "a MenubarItem descriptor has no effect until its part is mounted"]
pub struct MenubarItem {
    bar: Menubar,
    state: MenubarState,
    index: usize,
}

impl MenubarItem {
    pub const fn index(self) -> usize {
        self.index
    }

    pub const fn is_open(self) -> bool {
        self.state.is_open(self.index)
    }

    pub const fn is_focused(self) -> bool {
        self.state.focused == self.index
    }

    pub fn item_id(self) -> ElementId {
        self.bar.item_id(self.index)
    }

    /// Decorate an application-owned trigger without adding layout or appearance.
    ///
    /// Exactly one trigger carries the bar's Tab stop; the rest stay reachable only through the
    /// arrow keys, which is the roving pattern desktop menubars use.
    pub fn item_with(self, item: Element) -> Element {
        let disabled = self.state.disabled || item.accessibility.disabled;
        item.id(self.item_id())
            .accessibility_role(AccessibilityRole::MenuItem)
            .accessibility_has_popover(AccessibilityPopover::Menu)
            .accessibility_expanded(self.is_open())
            .accessibility_position_in_set(self.index)
            .accessibility_size_of_set(self.state.count)
            .selected(self.is_open())
            .clickable()
            .tab_index(if self.is_focused() { 0 } else { -1 })
            .key_context(MENUBAR_KEY_CONTEXT)
            .cursor_default()
            .app_region_no_drag()
            .user_select_none()
            .disabled(disabled)
    }
    /// Create the unstyled item part. Use [`Self::item_with`] to supply an existing element.
    pub fn item(self) -> Element {
        self.item_with(crate::div())
    }

    /// Attach QuickGUI's typed menubar actions, click opening, and hover switching.
    ///
    /// Install [`menubar_key_bindings`] once on the application keymap. Compose the surface itself
    /// from [`crate::PopoverMenu`] anchored to [`Self::item_id`]; the menubar owns which menu
    /// is open, never the menu's own contents.
    pub fn key_with<V: 'static>(
        self,
        cx: &mut ViewContext<'_, V>,
        item: Element,
        access: fn(&mut V) -> &mut MenubarState,
    ) -> Element {
        self.key_with_accessor(cx, item, StateAccessor::from(access))
    }
    /// Create the unstyled key part. Use [`Self::key_with`] to supply an existing element.
    pub fn key<V: 'static>(
        self,
        cx: &mut ViewContext<'_, V>,
        access: fn(&mut V) -> &mut MenubarState,
    ) -> Element {
        self.key_with(cx, crate::div(), access)
    }

    /// Attach the typed menubar actions against a per-instance state accessor.
    ///
    /// A host that renders many declared menubars through one view passes an accessor that
    /// captures which [`MenubarState`] this trigger belongs to.
    pub fn key_with_accessor<V: 'static>(
        self,
        cx: &mut ViewContext<'_, V>,
        item: Element,
        access_source: StateAccessor<V, MenubarState>,
    ) -> Element {
        let id = self.item_id();
        let bar = self.bar;
        let index = self.index;
        let access = access_source.clone();
        let previous = cx.action_listener(id, move |view, _: &MenubarPrevious, cx| {
            move_menubar_focus(view, cx, &access, bar, index, false);
        });
        let access = access_source.clone();
        let next = cx.action_listener(id, move |view, _: &MenubarNext, cx| {
            move_menubar_focus(view, cx, &access, bar, index, true);
        });
        let access = access_source.clone();
        let first = cx.action_listener(id, move |view, _: &MenubarFirst, cx| {
            let state = access.get(view);
            state.focus_menu(index);
            if state.focus_edge(false) {
                let focused = state.focused_menu();
                cx.focus(FocusHandle::new(bar.item_id(focused)));
                cx.invalidate();
            }
        });
        let access = access_source.clone();
        let last = cx.action_listener(id, move |view, _: &MenubarLast, cx| {
            let state = access.get(view);
            state.focus_menu(index);
            if state.focus_edge(true) {
                let focused = state.focused_menu();
                cx.focus(FocusHandle::new(bar.item_id(focused)));
                cx.invalidate();
            }
        });
        let access = access_source.clone();
        let open = cx.action_listener(id, move |view, _: &MenubarOpen, cx| {
            let state = access.get(view);
            state.focus_menu(index);
            if state.open_menu_at(index) {
                cx.invalidate();
            }
        });
        let access = access_source.clone();
        let close = cx.action_listener(id, move |view, _: &MenubarClose, cx| {
            if access.get(view).close() {
                cx.focus(FocusHandle::new(id));
                cx.invalidate();
            }
        });
        let access = access_source.clone();
        let clicked = cx.listener(id, move |view, cx| {
            let state = access.get(view);
            state.focus_menu(index);
            if state.toggle_menu(index) {
                cx.invalidate();
            }
        });
        let access = access_source.clone();
        let hovered = cx.hover_listener(id, move |view, hovered, cx| {
            if *hovered && access.get(view).hover_menu(index) {
                cx.focus(FocusHandle::new(id));
                cx.invalidate();
            }
        });

        item.on_action(previous)
            .on_action(next)
            .on_action(first)
            .on_action(last)
            .on_action(open)
            .on_action(close)
            .on_click(clicked)
            .on_hover(hovered)
    }
}

fn move_menubar_focus<V: 'static>(
    view: &mut V,
    cx: &mut EventContext,
    access: &StateAccessor<V, MenubarState>,
    bar: Menubar,
    index: usize,
    forward: bool,
) {
    let state = access.get(view);
    state.focus_menu(index);
    if state.move_focus(forward) {
        let focused = state.focused_menu();
        cx.focus(FocusHandle::new(bar.item_id(focused)));
        cx.invalidate();
    }
}

/// Create an unstyled semantic menubar root.
///
/// This shorthand is equivalent to `Menubar::new(id).root_with(state, div())`.
pub fn menubar(id: impl Into<ElementId>, state: MenubarState) -> Element {
    Menubar::new(id).root_with(state, div())
}

fn derived_menubar_id(parent: ElementId, tag: u64, index: u64) -> ElementId {
    let mut hash = parent.as_u64() ^ tag ^ index.wrapping_mul(0x9e37_79b9_7f4a_7c15);
    hash ^= hash >> 30;
    hash = hash.wrapping_mul(0xbf58_476d_1ce4_e5b9);
    hash ^= hash >> 27;
    hash = hash.wrapping_mul(0x94d0_49bb_1331_11eb);
    hash ^= hash >> 31;
    if hash == parent.as_u64() || hash == u64::MAX {
        hash ^= tag.rotate_left(23);
    }
    ElementId::new(hash)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        AppRegion, Application, Color, CursorStyle, IntoElement, UserSelect, View, WindowOptions,
        text,
    };

    #[test]
    fn roving_focus_open_and_hover_switching_stay_bounded() {
        let mut state = MenubarState::new(4);
        assert_eq!(state.menu_count(), 4);
        assert_eq!(state.focused_menu(), 0);
        assert_eq!(state.open_menu(), None);

        assert!(state.move_focus(true));
        assert_eq!(state.focused_menu(), 1);
        assert!(state.move_focus(false));
        assert!(state.move_focus(false));
        assert_eq!(state.focused_menu(), 3, "focus wraps at the start");
        assert!(state.move_focus(true));
        assert_eq!(state.focused_menu(), 0, "focus wraps at the end");
        assert!(state.focus_edge(true));
        assert_eq!(state.focused_menu(), 3);
        assert!(state.focus_edge(false));
        assert!(!state.focus_menu(9), "an index past the end is refused");

        // Hover does nothing until a menu is open, then it switches menus.
        assert!(!state.hover_menu(2));
        assert!(state.open_focused());
        assert!(state.is_open(0));
        assert!(!state.open_focused(), "the focused menu is already open");
        assert!(state.hover_menu(2));
        assert!(state.is_open(2));
        assert_eq!(state.focused_menu(), 2);
        assert!(!state.hover_menu(2));

        // Arrow movement while open keeps the open menu under the roving focus.
        assert!(state.move_focus(true));
        assert!(state.is_open(3));
        assert!(state.toggle_menu(3));
        assert_eq!(state.open_menu(), None);
        assert!(state.toggle_menu(1));
        assert!(state.is_open(1));
        assert!(state.close());
        assert!(!state.close());

        // A shrinking declaration clamps focus and closes a menu that no longer exists.
        assert!(state.open_menu_at(3));
        assert!(state.set_menu_count(2));
        assert_eq!(state.focused_menu(), 1);
        assert_eq!(state.open_menu(), None);
        assert!(!state.set_menu_count(2));

        let mut oversized = MenubarState::new(MAX_MENUBAR_MENUS + 10);
        assert_eq!(oversized.menu_count(), MAX_MENUBAR_MENUS);
        assert!(
            !oversized.set_menu_count(usize::MAX),
            "the clamped count is already at the bound"
        );
        assert_eq!(oversized.menu_count(), MAX_MENUBAR_MENUS);

        let mut disabled = MenubarState::new(3).disabled(true);
        assert!(disabled.is_disabled());
        assert!(!disabled.move_focus(true));
        assert!(!disabled.open_focused());
        assert!(!disabled.toggle_menu(1));
        assert!(!disabled.hover_menu(1));
    }

    #[test]
    fn parts_add_exact_semantics_without_layout_or_appearance() {
        let mut state = MenubarState::new(3);
        state.open_menu_at(1);
        let bar = Menubar::new("menubar");

        let root = bar.root_with(
            state,
            div()
                .h(28.0)
                .bg(Color::rgb8(1, 2, 3))
                .child("Application-owned bar"),
        );
        assert_eq!(root.accessibility.role, AccessibilityRole::MenuBar);
        assert_eq!(root.visual.background, Some(Color::rgb8(1, 2, 3)));
        assert_eq!(root.children.len(), 1);
        assert_eq!(
            root.accessibility.relations.active_descendant(),
            Some(bar.item_id(1))
        );
        assert_eq!(root.app_region, Some(AppRegion::NoDrag));
        assert_eq!(root.user_select, UserSelect::None);

        let open = bar.item(state, 1).expect("a declared menu");
        assert!(open.is_open());
        assert!(open.is_focused());
        let item = open.item_with(div().px(8.0).child(text("Edit")));
        assert_eq!(item.accessibility.role, AccessibilityRole::MenuItem);
        assert_eq!(item.accessibility.expanded, Some(true));
        assert!(item.clickable);
        assert!(item.focusable);
        assert_eq!(item.tab_index, 0, "the focused menu owns the Tab stop");
        assert_eq!(item.cursor_style, Some(CursorStyle::Arrow));
        assert_eq!(item.visual.background, None);
        assert_eq!(item.children.len(), 1);

        let closed = bar.item(state, 2).expect("a declared menu");
        let closed_item = closed.item_with(div());
        assert_eq!(closed_item.accessibility.expanded, Some(false));
        assert_eq!(
            closed_item.tab_index, -1,
            "unfocused menus are reached with the arrow keys"
        );

        assert!(
            bar.item(state, 3).is_none(),
            "indices stay inside the state"
        );
        assert_eq!(
            menubar("menubar", state).accessibility.role,
            AccessibilityRole::MenuBar
        );
        assert!(menubar("menubar", state).children.is_empty());
    }

    #[test]
    fn menubar_bindings_are_contextual_and_complete() {
        let bindings = menubar_key_bindings();
        assert_eq!(bindings.len(), 8);
        assert!(bindings.iter().all(|binding| {
            binding.context_predicate().is_some_and(|predicate| {
                predicate
                    .depth_of(&[crate::KeyContext::parse(MENUBAR_KEY_CONTEXT).unwrap()])
                    .is_some()
            })
        }));
    }

    #[derive(Default)]
    struct MenubarView {
        menubar: MenubarState,
    }

    impl MenubarView {
        fn menubar(view: &mut Self) -> &mut MenubarState {
            &mut view.menubar
        }
    }

    impl View for MenubarView {
        fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
            let bar = Menubar::new("menubar");
            let mut root = bar
                .root_with(self.menubar, div().flex_row())
                .accessibility_label("Application menus");
            for index in 0..self.menubar.menu_count() {
                let item = bar.item(self.menubar, index).expect("a declared menu");
                let element = item.item_with(div().child(text(match index {
                    0 => "File",
                    1 => "Edit",
                    _ => "View",
                })));
                root = root.child(item.key_with(cx, element, Self::menubar));
            }
            root
        }
    }

    #[test]
    fn menubar_uses_existing_focus_click_hover_and_idle_paths() {
        let (mut cx, view) = Application::new()
            .bind_keys(menubar_key_bindings())
            .into_test_context(
                WindowOptions::default(),
                MenubarView {
                    menubar: MenubarState::new(3),
                },
            )
            .unwrap();
        let window = view.window_handle();
        let bar = Menubar::new("menubar");

        cx.simulate_keystrokes(window, "tab").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some(bar.item_id(0)));

        cx.simulate_keystrokes(window, "right").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some(bar.item_id(1)));
        assert_eq!(
            cx.read(view, |view| view.menubar.open_menu()).unwrap(),
            None
        );

        cx.simulate_keystrokes(window, "down").unwrap();
        assert_eq!(
            cx.read(view, |view| view.menubar.open_menu()).unwrap(),
            Some(1)
        );

        // Arrows keep switching menus while one is open.
        cx.simulate_keystrokes(window, "right").unwrap();
        assert_eq!(
            cx.read(view, |view| view.menubar.open_menu()).unwrap(),
            Some(2)
        );
        assert_eq!(cx.focused(window).unwrap(), Some(bar.item_id(2)));

        cx.simulate_keystrokes(window, "escape").unwrap();
        assert_eq!(
            cx.read(view, |view| view.menubar.open_menu()).unwrap(),
            None
        );
        assert_eq!(
            cx.focused(window).unwrap(),
            Some(bar.item_id(2)),
            "closing keeps focus on the bar"
        );

        cx.click(window, bar.item_id(0)).unwrap();
        assert_eq!(
            cx.read(view, |view| view.menubar.open_menu()).unwrap(),
            Some(0)
        );
        cx.click(window, bar.item_id(0)).unwrap();
        assert_eq!(
            cx.read(view, |view| view.menubar.open_menu()).unwrap(),
            None
        );

        let renders = cx.render_count(window).unwrap();
        cx.run_until_idle().unwrap();
        assert_eq!(cx.render_count(window).unwrap(), renders);
    }
}