1use gpui::{
4 Anchor, AnyElement, App, ClickEvent, Context, ElementId, Entity, FocusHandle,
5 InteractiveElement as _, IntoElement, KeyBinding, MouseButton, ParentElement, Pixels,
6 RenderOnce, Role, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled,
7 Subscription, Window, div, prelude::FluentBuilder as _, px,
8};
9
10use crate::{
11 Button, Direction, MenuState, Popover, StyledExt as _, TestSupportExt as _,
12 actions::{SelectDown, SelectFirst, SelectLast, SelectLeft, SelectRight, SelectUp},
13};
14
15const TRIGGER_CONTEXT: &str = "MenubarTrigger";
16const CONTENT_CONTEXT: &str = "MenubarContent";
17
18pub(crate) fn init(cx: &mut App) {
19 for context in [TRIGGER_CONTEXT, CONTENT_CONTEXT] {
20 cx.bind_keys([
21 KeyBinding::new("left", SelectLeft, Some(context)),
22 KeyBinding::new("right", SelectRight, Some(context)),
23 ]);
24 }
25 cx.bind_keys([
26 KeyBinding::new("home", SelectFirst, Some(TRIGGER_CONTEXT)),
27 KeyBinding::new("end", SelectLast, Some(TRIGGER_CONTEXT)),
28 KeyBinding::new("down", SelectDown, Some(TRIGGER_CONTEXT)),
29 KeyBinding::new("up", SelectUp, Some(TRIGGER_CONTEXT)),
30 ]);
31}
32
33#[derive(Clone)]
35pub struct MenubarItem {
36 label: SharedString,
37 menu: Entity<MenuState>,
38}
39
40impl MenubarItem {
41 pub fn new(label: impl Into<SharedString>, menu: &Entity<MenuState>) -> Self {
42 Self {
43 label: label.into(),
44 menu: menu.clone(),
45 }
46 }
47
48 pub fn label(&self) -> &SharedString {
49 &self.label
50 }
51
52 pub fn menu(&self) -> &Entity<MenuState> {
53 &self.menu
54 }
55}
56
57pub struct MenubarState {
63 items: Vec<MenubarItem>,
64 active: Option<usize>,
65 direction: Option<Direction>,
66 resolved_direction: Direction,
67 pressed_open: bool,
68 _menu_subscriptions: Vec<Subscription>,
69 focus_subscriptions: Vec<Subscription>,
70}
71
72impl MenubarState {
73 pub fn new(items: impl IntoIterator<Item = MenubarItem>, cx: &mut Context<Self>) -> Self {
74 let items: Vec<_> = items.into_iter().collect();
75 for (index, item) in items.iter().enumerate() {
76 assert!(
77 items[..index].iter().all(|other| other.menu != item.menu),
78 "A MenuState can appear only once in a MenubarState"
79 );
80 }
81 let active = items
82 .iter()
83 .position(|item| !item.menu.read(cx).is_disabled(cx));
84 let subscriptions = items
85 .iter()
86 .enumerate()
87 .map(|(index, item)| {
88 cx.observe(&item.menu, move |state, menu, cx| {
89 if menu.read(cx).is_open(cx) {
90 state.active = Some(index);
91 }
92 cx.notify();
93 })
94 })
95 .collect();
96 Self {
97 items,
98 active,
99 direction: None,
100 resolved_direction: Direction::Ltr,
101 pressed_open: false,
102 _menu_subscriptions: subscriptions,
103 focus_subscriptions: Vec::new(),
104 }
105 }
106
107 pub fn direction(mut self, direction: Direction) -> Self {
109 self.direction = Some(direction);
110 self
111 }
112
113 pub fn items(&self) -> &[MenubarItem] {
114 &self.items
115 }
116
117 pub fn active_index(&self, cx: &App) -> Option<usize> {
118 self.active
119 .filter(|&index| !self.items[index].menu.read(cx).is_disabled(cx))
120 .or_else(|| {
121 self.items
122 .iter()
123 .position(|item| !item.menu.read(cx).is_disabled(cx))
124 })
125 }
126
127 pub fn open_index(&self, cx: &App) -> Option<usize> {
128 self.items
129 .iter()
130 .position(|item| item.menu.read(cx).is_open(cx))
131 }
132
133 pub fn focus_handle(&self, index: usize, cx: &App) -> FocusHandle {
134 self.items[index].menu.read(cx).focus_handle().clone()
135 }
136
137 pub fn open(&mut self, index: usize, window: &mut Window, cx: &mut Context<Self>) {
139 self.select(index, true, false, window, cx);
140 }
141
142 pub fn close(&mut self, window: &mut Window, cx: &mut Context<Self>) {
144 for item in &self.items {
145 item.menu.update(cx, |menu, cx| menu.close(window, cx));
146 }
147 cx.notify();
148 }
149
150 fn bind_focus(&mut self, direction: Direction, window: &mut Window, cx: &mut Context<Self>) {
151 self.resolved_direction = self.direction.unwrap_or(direction);
152 if !self.focus_subscriptions.is_empty() {
153 return;
154 }
155 self.focus_subscriptions = self
156 .items
157 .iter()
158 .enumerate()
159 .map(|(index, item)| {
160 let focus = item.menu.read(cx).focus_handle().clone();
161 cx.on_focus(&focus, window, move |_, window, cx| {
162 cx.defer_in(window, move |state, window, cx| {
163 if state.focus_handle(index, cx).is_focused(window)
164 && state.active != Some(index)
165 {
166 state.active = Some(index);
167 cx.notify();
168 }
169 });
170 })
171 })
172 .collect();
173 }
174
175 fn select(
176 &mut self,
177 index: usize,
178 open: bool,
179 last: bool,
180 window: &mut Window,
181 cx: &mut Context<Self>,
182 ) {
183 let Some(item) = self.items.get(index) else {
184 return;
185 };
186 if item.menu.read(cx).is_disabled(cx) {
187 return;
188 }
189 let menu = item.menu.clone();
190 for (other_index, item) in self.items.iter().enumerate() {
191 if other_index != index {
192 item.menu.update(cx, |menu, cx| menu.close(window, cx));
193 }
194 }
195 self.active = Some(index);
196 if open {
197 menu.update(cx, |menu, cx| menu.open_at(last, window, cx));
198 } else {
199 let focus = menu.read(cx).focus_handle().clone();
200 focus.focus(window, cx);
201 }
202 cx.notify();
203 window.refresh();
204 }
205
206 fn move_trigger(&mut self, right: bool, window: &mut Window, cx: &mut Context<Self>) {
207 let count = self.items.len();
208 if count == 0 {
209 return;
210 }
211 let forward = self.resolved_direction.horizontal_forward(right);
212 let start = self
213 .active_index(cx)
214 .unwrap_or(if forward { count - 1 } else { 0 });
215 for distance in 1..=count {
216 let index = if forward {
217 (start + distance) % count
218 } else {
219 (start + count - distance) % count
220 };
221 if !self.items[index].menu.read(cx).is_disabled(cx) {
222 let open = self.open_index(cx).is_some();
223 self.select(index, open, false, window, cx);
224 return;
225 }
226 }
227 }
228
229 fn edge(&mut self, last: bool, window: &mut Window, cx: &mut Context<Self>) {
230 let index = if last {
231 self.items
232 .iter()
233 .rposition(|item| !item.menu.read(cx).is_disabled(cx))
234 } else {
235 self.items
236 .iter()
237 .position(|item| !item.menu.read(cx).is_disabled(cx))
238 };
239 if let Some(index) = index {
240 self.select(index, false, false, window, cx);
241 }
242 }
243
244 fn activate(
245 &mut self,
246 index: usize,
247 event: &ClickEvent,
248 window: &mut Window,
249 cx: &mut Context<Self>,
250 ) {
251 let was_open = match event {
252 ClickEvent::Mouse(_) => self.pressed_open,
253 _ => self.open_index(cx) == Some(index),
254 };
255 if was_open {
256 self.close(window, cx);
257 } else {
258 self.open(index, window, cx);
259 }
260 }
261}
262
263#[derive(IntoElement)]
265pub struct Menubar {
266 state: Entity<MenubarState>,
267 label: Option<SharedString>,
268 children: Vec<AnyElement>,
269 style: StyleRefinement,
270}
271
272impl Menubar {
273 pub fn new(state: &Entity<MenubarState>) -> Self {
274 Self {
275 state: state.clone(),
276 label: None,
277 children: Vec::new(),
278 style: StyleRefinement::default(),
279 }
280 }
281
282 pub fn accessibility_label(mut self, label: impl Into<SharedString>) -> Self {
283 self.label = Some(label.into());
284 self
285 }
286}
287
288impl ParentElement for Menubar {
289 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
290 self.children.extend(elements);
291 }
292}
293
294impl Styled for Menubar {
295 fn style(&mut self) -> &mut StyleRefinement {
296 &mut self.style
297 }
298}
299
300impl RenderOnce for Menubar {
301 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
302 let direction = Direction::current(cx);
303 self.state
304 .update(cx, |state, cx| state.bind_focus(direction, window, cx));
305 div()
306 .id(("menubar", self.state.entity_id()))
307 .test_support()
308 .role(Role::MenuBar)
309 .when_some(self.label, |this, label| this.aria_label(label))
310 .children(self.children)
311 .refine_style(&self.style)
312 }
313}
314
315#[derive(IntoElement)]
320pub struct MenubarMenu {
321 state: Entity<MenubarState>,
322 index: usize,
323 trigger: Button,
324 content: Option<AnyElement>,
325 anchor: Anchor,
326 offset: Pixels,
327 style: StyleRefinement,
328}
329
330impl MenubarMenu {
331 pub fn new(state: &Entity<MenubarState>, index: usize) -> Self {
332 Self {
333 state: state.clone(),
334 index,
335 trigger: Button::new(("menubar-trigger", index)),
336 content: None,
337 anchor: Anchor::TopLeft,
338 offset: px(0.),
339 style: StyleRefinement::default(),
340 }
341 }
342
343 pub fn trigger(mut self, build: impl FnOnce(Button) -> Button) -> Self {
344 self.trigger = build(self.trigger);
345 self
346 }
347 pub fn content(mut self, content: impl IntoElement) -> Self {
348 self.content = Some(content.into_any_element());
349 self
350 }
351 pub fn anchor(mut self, anchor: impl Into<Anchor>) -> Self {
352 self.anchor = anchor.into();
353 self
354 }
355 pub fn offset(mut self, offset: Pixels) -> Self {
356 self.offset = offset;
357 self
358 }
359}
360
361impl Styled for MenubarMenu {
362 fn style(&mut self) -> &mut StyleRefinement {
363 &mut self.style
364 }
365}
366
367impl RenderOnce for MenubarMenu {
368 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
369 let state = self.state.read(cx);
370 let item = state
371 .items
372 .get(self.index)
373 .expect("MenubarMenu index is out of bounds");
374 let menu = item.menu.clone();
375 let label = item.label.clone();
376 let focus = menu.read(cx).focus_handle().clone();
377 let popover = menu.read(cx).popover.clone();
378 let disabled = menu.read(cx).is_disabled(cx);
379 let open = menu.read(cx).is_open(cx);
380 let tab_stop = state.active_index(cx) == Some(self.index);
381 let direction = state.resolved_direction;
382 let index = self.index;
383 let press = self.state.clone();
384 let activate = self.state.clone();
385 let hover = self.state.clone();
386 let left = self.state.clone();
387 let right = self.state.clone();
388 let first = self.state.clone();
389 let last = self.state.clone();
390 let down = self.state.clone();
391 let up = self.state.clone();
392 let content_left = self.state.clone();
393 let content_right = self.state.clone();
394 let trigger = self
395 .trigger
396 .role(Role::MenuItem)
397 .accessibility_label(label.clone())
398 .track_focus(&focus)
399 .focusable(true)
400 .tab_stop(tab_stop)
401 .disabled(disabled)
402 .aria_expanded(open)
403 .key_context(TRIGGER_CONTEXT)
404 .on_mouse_down(MouseButton::Left, move |_, _, cx| {
405 press.update(cx, |state, cx| {
406 state.pressed_open = state.open_index(cx) == Some(index)
407 });
408 })
409 .on_click(move |event, window, cx| {
410 activate.update(cx, |state, cx| state.activate(index, event, window, cx));
411 })
412 .on_hover(move |hovered, window, cx| {
413 if *hovered {
414 hover.update(cx, |state, cx| {
415 if state.open_index(cx).is_some() && state.open_index(cx) != Some(index) {
416 state.open(index, window, cx);
417 }
418 });
419 }
420 })
421 .on_action(move |_: &SelectLeft, window, cx| {
422 left.update(cx, |state, cx| state.move_trigger(false, window, cx))
423 })
424 .on_action(move |_: &SelectRight, window, cx| {
425 right.update(cx, |state, cx| state.move_trigger(true, window, cx))
426 })
427 .on_action(move |_: &SelectFirst, window, cx| {
428 first.update(cx, |state, cx| state.edge(false, window, cx))
429 })
430 .on_action(move |_: &SelectLast, window, cx| {
431 last.update(cx, |state, cx| state.edge(true, window, cx))
432 })
433 .on_action(move |_: &SelectDown, window, cx| {
434 down.update(cx, |state, cx| state.select(index, true, false, window, cx))
435 })
436 .on_action(move |_: &SelectUp, window, cx| {
437 up.update(cx, |state, cx| state.select(index, true, true, window, cx))
438 });
439 let content = div()
440 .key_context(CONTENT_CONTEXT)
441 .on_action(move |_: &SelectLeft, window, cx| {
442 content_left.update(cx, |state, cx| state.move_trigger(false, window, cx))
443 })
444 .on_action(move |_: &SelectRight, window, cx| {
445 content_right.update(cx, |state, cx| state.move_trigger(true, window, cx))
446 })
447 .child(crate::menu::menu_content_with_direction(
448 &menu,
449 Some(label),
450 self.content,
451 direction,
452 window,
453 cx,
454 ));
455 Popover::new(ElementId::from(("menubar-menu", index)), &popover)
456 .supplied_trigger(trigger)
457 .content_role(None)
458 .anchor(self.anchor)
459 .offset(self.offset)
460 .content(content)
461 .refine_style(&self.style)
462 }
463}
464
465#[cfg(test)]
466mod tests {
467 use super::*;
468 use crate::{MenuEvent, MenuItem, MenuOption, MenuSubmenu, Root};
469 use gpui::{
470 AppContext as _, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers, Render, TestAppContext,
471 VisualTestContext,
472 };
473
474 struct Harness {
475 bar: Entity<MenubarState>,
476 nested: Entity<MenuState>,
477 before: FocusHandle,
478 after: FocusHandle,
479 commands: Vec<SharedString>,
480 _subscriptions: Vec<Subscription>,
481 }
482
483 impl Render for Harness {
484 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
485 div()
486 .flex()
487 .flex_col()
488 .gap(px(8.))
489 .size_full()
490 .child(
491 Button::new("before")
492 .track_focus(&self.before)
493 .h(px(30.))
494 .child("Before"),
495 )
496 .child(
497 Menubar::new(&self.bar)
498 .accessibility_label("Application")
499 .flex()
500 .gap(px(4.))
501 .children(self.bar.read(cx).items().iter().enumerate().map(
502 |(index, item)| {
503 MenubarMenu::new(&self.bar, index)
504 .trigger(|button| {
505 button
506 .debug_selector(move || format!("bar-{index}").into())
507 .w(px(90.))
508 .h(px(30.))
509 .child(item.label().clone())
510 })
511 .content(div().flex().flex_col().w(px(160.)).children(
512 item.menu().read(cx).items().iter().enumerate().map(
513 |(index, command)| {
514 if let Some(child) = command.submenu_state() {
515 MenuSubmenu::new(item.menu(), index)
516 .trigger(|option| option.h(px(30.)).child(
517 div().debug_selector(|| "submenu-row".into())
518 .size_full().child(command.label().clone())))
519 .content(div().flex().flex_col().w(px(150.)).children(
520 child.read(cx).items().iter().enumerate().map(|(index, item)|
521 MenuOption::new(child, index).h(px(30.)).child(
522 div().debug_selector(move || format!("submenu-command-{index}").into())
523 .size_full().child(item.label().clone())))))
524 .into_any_element()
525 } else {
526 MenuOption::new(item.menu(), index)
527 .h(px(30.))
528 .child(command.label().clone()).into_any_element()
529 }
530 },
531 ),
532 ))
533 },
534 )),
535 )
536 .child(
537 Button::new("after")
538 .track_focus(&self.after)
539 .h(px(30.))
540 .child("After"),
541 )
542 }
543 }
544
545 fn settle(cx: &mut VisualTestContext) {
546 cx.update(|window, cx| window.draw(cx).clear(cx));
547 cx.run_until_parked();
548 cx.update(|window, cx| window.draw(cx).clear(cx));
549 }
550
551 fn press(cx: &mut VisualTestContext, key: &str) {
552 let keystroke =
553 cx.update(|_, cx| Keystroke::parse_with_os(key, cx.platform_info().os).unwrap());
554 cx.simulate_event(KeyDownEvent {
555 keystroke: keystroke.clone(),
556 is_held: false,
557 prefer_character_input: false,
558 });
559 cx.simulate_event(KeyUpEvent { keystroke });
560 settle(cx);
561 }
562
563 #[gpui::test]
564 fn coordinates_roving_focus_open_menus_pointer_switching_and_commands(cx: &mut TestAppContext) {
565 cx.update(crate::init);
566 let (root, cx) = cx.add_window_view(|window, cx| {
567 let view = cx.new(|cx| {
568 let nested = cx.new(|cx| {
569 MenuState::new(
570 [
571 MenuItem::new("nested-first", "First nested command"),
572 MenuItem::new("nested-disabled", "Disabled nested command")
573 .disabled(true),
574 MenuItem::new("nested-last", "Last nested command"),
575 ],
576 cx,
577 )
578 });
579 let menus: Vec<_> = ["File", "Edit", "View"]
580 .into_iter()
581 .map(|label| {
582 cx.new(|cx| {
583 MenuState::new(
584 [
585 MenuItem::new(format!("{label}-first"), "First"),
586 MenuItem::new("disabled", "Disabled").disabled(true),
587 if label == "File" {
588 MenuItem::new("more", "More").submenu(&nested)
589 } else {
590 MenuItem::new(format!("{label}-last"), "Last")
591 },
592 ],
593 cx,
594 )
595 })
596 })
597 .collect();
598 menus[1].update(cx, |menu, cx| menu.set_disabled(true, window, cx));
599 let bar = cx.new(|cx| {
600 MenubarState::new(
601 ["File", "Edit", "View"]
602 .into_iter()
603 .zip(&menus)
604 .map(|(label, menu)| MenubarItem::new(label, menu)),
605 cx,
606 )
607 });
608 let mut subscriptions = vec![cx.observe(&bar, |_, _, cx| cx.notify())];
609 for menu in &menus {
610 subscriptions.push(cx.subscribe(menu, |this: &mut Harness, _, event, cx| {
611 let MenuEvent::Action(value) = event;
612 this.commands.push(value.clone());
613 cx.notify();
614 }));
615 }
616 Harness {
617 bar,
618 nested,
619 before: cx.focus_handle(),
620 after: cx.focus_handle(),
621 commands: Vec::new(),
622 _subscriptions: subscriptions,
623 }
624 });
625 Root::new(view, window, cx)
626 });
627 let view = root
628 .read_with(cx, |root, _| root.view().clone())
629 .downcast::<Harness>()
630 .unwrap();
631 cx.update(|window, cx| {
632 window.activate_window();
633 view.read(cx).before.clone().focus(window, cx);
634 });
635 settle(cx);
636 press(cx, "tab");
637 cx.update(|window, cx| {
638 assert!(
639 view.read(cx)
640 .bar
641 .read(cx)
642 .focus_handle(0, cx)
643 .is_focused(window)
644 )
645 });
646 press(cx, "right");
647 cx.update(|window, cx| {
648 let bar = view.read(cx).bar.read(cx);
649 assert_eq!(bar.active_index(cx), Some(2));
650 assert_eq!(bar.open_index(cx), None);
651 assert!(bar.focus_handle(2, cx).is_focused(window));
652 });
653 press(cx, "tab");
654 cx.update(|window, cx| assert!(view.read(cx).after.is_focused(window)));
655 press(cx, "shift-tab");
656 press(cx, "up");
657 cx.update(|_, cx| {
658 let bar = view.read(cx).bar.read(cx);
659 assert_eq!(bar.open_index(cx), Some(2));
660 assert_eq!(bar.items[2].menu.read(cx).highlighted_index(), Some(2));
661 });
662 press(cx, "left");
663 cx.update(|window, cx| {
664 let bar = view.read(cx).bar.read(cx);
665 assert_eq!(bar.open_index(cx), Some(0));
666 assert!(!bar.items[2].menu.read(cx).is_open(cx));
667 assert!(
668 bar.items[0]
669 .menu
670 .read(cx)
671 .content_focus_handle()
672 .is_focused(window)
673 );
674 #[cfg(feature = "test-support")]
675 {
676 let scope = crate::test_support::find(
677 window,
678 &[],
679 &("menubar", view.read(cx).bar.entity_id()).into(),
680 )
681 .unwrap();
682 assert_eq!(scope.role(), Some(Role::MenuBar));
683 let trigger =
684 crate::test_support::find(window, &[], &("menubar-trigger", 0usize).into())
685 .unwrap();
686 assert_eq!(trigger.role(), Some(Role::MenuItem));
687 assert_eq!(trigger.expanded(), Some(true));
688 }
689 });
690 press(cx, "enter");
691 cx.update(|window, cx| {
692 let harness = view.read(cx);
693 let bar = harness.bar.read(cx);
694 assert_eq!(harness.commands, vec![SharedString::from("File-first")]);
695 assert_eq!(bar.open_index(cx), None);
696 assert!(bar.focus_handle(0, cx).is_focused(window));
697 });
698 cx.update(|_, cx| {
700 view.read(cx).bar.clone().update(cx, |bar, cx| {
701 bar.direction = Some(Direction::Rtl);
702 cx.notify();
703 })
704 });
705 settle(cx);
706 press(cx, "right");
707 cx.update(|_, cx| assert_eq!(view.read(cx).bar.read(cx).active_index(cx), Some(2)));
708 press(cx, "down");
709 let file = cx.debug_bounds("bar-0").unwrap().center();
710 cx.simulate_mouse_move(file, None, Modifiers::default());
711 settle(cx);
712 cx.update(|_, cx| assert_eq!(view.read(cx).bar.read(cx).open_index(cx), Some(0)));
713 cx.simulate_click(file, Modifiers::default());
714 settle(cx);
715 cx.update(|_, cx| assert_eq!(view.read(cx).bar.read(cx).open_index(cx), None));
716 cx.simulate_click(file, Modifiers::default());
717 settle(cx);
718 cx.update(|_, cx| assert_eq!(view.read(cx).bar.read(cx).open_index(cx), Some(0)));
719 press(cx, "escape");
720 cx.update(|window, cx| {
721 let bar = view.read(cx).bar.read(cx);
722 assert_eq!(bar.open_index(cx), None);
723 assert!(bar.focus_handle(0, cx).is_focused(window));
724 });
725 press(cx, "down");
728 press(cx, "end");
729 press(cx, "left");
730 cx.update(|window, cx| {
731 let harness = view.read(cx);
732 assert!(harness.nested.read(cx).is_open(cx));
733 assert!(
734 harness
735 .nested
736 .read(cx)
737 .content_focus_handle()
738 .is_focused(window)
739 );
740 assert_eq!(harness.bar.read(cx).open_index(cx), Some(0));
741 });
742 press(cx, "right");
743 cx.update(|window, cx| {
744 let harness = view.read(cx);
745 let bar = harness.bar.read(cx);
746 assert!(!harness.nested.read(cx).is_open(cx));
747 assert!(
748 bar.items[0]
749 .menu
750 .read(cx)
751 .content_focus_handle()
752 .is_focused(window)
753 );
754 assert_eq!(bar.items[0].menu.read(cx).highlighted_index(), Some(2));
755 });
756 press(cx, "home");
758 cx.update(|window, cx| {
759 let focus = view.read(cx).nested.read(cx).focus_handle().clone();
760 focus.focus(window, cx);
761 });
762 settle(cx);
763 press(cx, "left");
764 cx.update(|_, cx| assert!(view.read(cx).nested.read(cx).is_open(cx)));
765 press(cx, "escape");
766 cx.update(|_, cx| {
767 let harness = view.read(cx);
768 assert!(!harness.nested.read(cx).is_open(cx));
769 assert_eq!(harness.bar.read(cx).open_index(cx), Some(0));
770 });
771 press(cx, "home");
772 cx.update(|window, cx| {
773 let focus = view.read(cx).nested.read(cx).focus_handle().clone();
774 focus.focus(window, cx);
775 });
776 settle(cx);
777 press(cx, "enter");
778 cx.update(|_, cx| {
779 let harness = view.read(cx);
780 assert!(harness.nested.read(cx).is_open(cx));
781 assert_eq!(harness.commands, ["File-first"]);
782 });
783 press(cx, "down");
784 press(cx, "enter");
785 cx.update(|window, cx| {
786 let harness = view.read(cx);
787 let bar = harness.bar.read(cx);
788 assert_eq!(harness.commands, ["File-first", "nested-last"]);
789 assert!(!harness.nested.read(cx).is_open(cx));
790 assert_eq!(bar.open_index(cx), None);
791 assert!(bar.focus_handle(0, cx).is_focused(window));
792 });
793 press(cx, "down");
794 press(cx, "end");
795 press(cx, "left");
796 press(cx, "tab");
797 cx.update(|window, cx| {
798 let harness = view.read(cx);
799 assert!(!harness.nested.read(cx).is_open(cx));
800 assert_eq!(harness.bar.read(cx).open_index(cx), None);
801 assert!(harness.after.is_focused(window));
802 });
803 press(cx, "shift-tab");
804 press(cx, "down");
805 let row = cx.debug_bounds("submenu-row").unwrap().center();
806 cx.simulate_mouse_move(row, None, Modifiers::default());
807 settle(cx);
808 cx.update(|_, cx| assert!(view.read(cx).nested.read(cx).is_open(cx)));
809 let command = cx.debug_bounds("submenu-command-0").unwrap().center();
810 cx.simulate_click(command, Modifiers::default());
811 settle(cx);
812 cx.update(|window, cx| {
813 let harness = view.read(cx);
814 assert_eq!(
815 harness.commands,
816 ["File-first", "nested-last", "nested-first"]
817 );
818 assert_eq!(harness.bar.read(cx).open_index(cx), None);
819 assert!(harness.bar.read(cx).focus_handle(0, cx).is_focused(window));
820 });
821 }
822}