Skip to main content

gpui_deck/
navigation_menu.rs

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