//! The floating menu panel shared by menus, dropdowns, and context menus. use std::cell::RefCell; use std::rc::Rc; use gpui::prelude::FluentBuilder; use gpui::{ Animation, AnimationExt as _, AnyElement, App, Bounds, BoxShadow, DefiniteLength, DispatchPhase, ElementId, Entity, FocusHandle, InteractiveElement as _, IntoElement, KeyDownEvent, MouseButton, MouseDownEvent, ParentElement as _, Pixels, Point, RenderOnce, Role, ScrollHandle, SharedString, StatefulInteractiveElement as _, Styled as _, Window, anchored, canvas, deferred, div, ease_out_quint, point, px, relative, }; use gpui_native_icons::{Icon, IconRole, icon as icon_glyph}; use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, POPUP_MAX_HEIGHT}; use gpui_native_utils::focus_handle; use crate::common::{OVERLAY_LAYER, v_flex}; use crate::scroll_area::{ScrollArea, ScrollAxes}; /// Runs the item's action. The menu closes itself before the handler runs. pub type ItemHandler = Rc; /// Reports the next open state of a menu: `true` to open, `false` to close. pub(crate) type ChangeHandler = Rc; /// Flips a panel's flyout open state; `true` arms the first row for keyboard. type FlyoutSetter = Rc, bool, &mut App)>; /// Bounds captured during the last paint, shared with the dismissal listener. pub(crate) type TriggerBoundsCell = Rc>>>; type TriggerFn = Box AnyElement>; type ItemsFn = Box Vec>; /// One row of a [`Menu`]: an action, a checkable action, a submenu, or a separator. #[derive(Clone)] #[must_use] pub struct MenuItem { id: Option, kind: MenuItemKind, label: SharedString, icon: Option, shortcut: Option, handler: Option, disabled: bool, destructive: bool, checked: bool, } #[derive(Clone)] enum MenuItemKind { Action, Separator, Submenu(Rc<[MenuItem]>), } impl MenuItem { /// An action row with this label. pub fn new(label: L) -> Self where L: Into, { Self { id: None, kind: MenuItemKind::Action, label: label.into(), icon: None, shortcut: None, handler: None, disabled: false, destructive: false, checked: false, } } /// A divider between groups of actions. pub fn separator() -> Self { Self { id: None, kind: MenuItemKind::Separator, label: SharedString::default(), icon: None, shortcut: None, handler: None, disabled: false, destructive: false, checked: false, } } /// Overrides the row's element id, for stable identity across reorderings. pub fn id(mut self, id: I) -> Self where I: Into, { self.id = Some(id.into()); self } /// Runs this action when the item is activated. The menu closes first. pub fn handler(mut self, handler: F) -> Self where F: Fn(&mut Window, &mut App) + 'static, { self.handler = Some(Rc::new(handler)); self } /// Shows an icon before the label. pub fn icon(mut self, icon: I) -> Self where I: Into, { self.icon = Some(icon.into()); self } /// Shows a shortcut hint after the label. pub fn shortcut(mut self, shortcut: S) -> Self where S: Into, { self.shortcut = Some(shortcut.into()); self } /// Marks the item non-selectable and non-activatable. pub fn disabled(mut self, disabled: bool) -> Self { self.disabled = disabled; self } /// Paints the row in the danger role. pub fn destructive(mut self, destructive: bool) -> Self { self.destructive = destructive; self } /// Shows a check slot before the label. pub fn checked(mut self, checked: bool) -> Self { self.checked = checked; self } /// Opens a submenu with these rows instead of running an action. pub fn submenu(mut self, items: I) -> Self where I: IntoIterator, { self.kind = MenuItemKind::Submenu(items.into_iter().collect::>().into()); self } /// The submenu's rows, when this item opens one. fn submenu_items(&self) -> Option<&[MenuItem]> { match &self.kind { MenuItemKind::Submenu(items) => Some(items), _ => None, } } /// Whether keyboard navigation and activation may land on this item. fn selectable(&self) -> bool { matches!(self.kind, MenuItemKind::Action | MenuItemKind::Submenu(_)) && !self.disabled } } // MenuItem is a data builder, not an element, adopt the combinators explicitly. impl FluentBuilder for MenuItem {} /// Menu chrome styles. #[derive(Clone)] pub(crate) struct MenuChrome { surface: gpui::Hsla, border: gpui::Hsla, panel_border: gpui::Hsla, foreground: gpui::Hsla, muted_foreground: gpui::Hsla, muted_hover: gpui::Hsla, primary: gpui::Hsla, primary_foreground: gpui::Hsla, destructive: gpui::Hsla, destructive_foreground: gpui::Hsla, radius_overlay: Pixels, radius_sm: Pixels, icon_size: Pixels, dense_icon_size: Pixels, menu_item_height: Pixels, spacing_unit: Pixels, text_size: Pixels, shadow: Vec, } /// A floating menu of actions anchored below its trigger. #[derive(IntoElement)] #[must_use] pub struct Menu { id: ElementId, open: bool, initial_selection: Option, trigger_button: MouseButton, open_at: Option>, a11y_label: Option, on_change: Option, trigger: Option, items: Option, min_width: Option, } impl MenuChrome { /// Reads the menu's chrome and enter duration from the active theme. pub(crate) fn read(cx: &App) -> (Self, std::time::Duration) { let theme = cx.theme(); ( Self { surface: theme.colors.surface, border: theme.colors.border, panel_border: if theme.borders.overlays { theme.colors.border } else { theme.colors.surface }, foreground: theme.colors.foreground, muted_foreground: theme.colors.muted_foreground, muted_hover: theme.colors.muted_hover, primary: theme.ramps.primary.base, primary_foreground: theme.colors.primary_foreground, destructive: theme.ramps.destructive.base, destructive_foreground: theme.colors.destructive_foreground, radius_overlay: theme.radius.overlay, radius_sm: theme.radius.sm, icon_size: theme.sizes.medium.icon_size, dense_icon_size: theme.sizes.medium.dense_icon_size, menu_item_height: theme.sizes.medium.menu_item_height, spacing_unit: theme.spacing.unit, text_size: theme.sizes.medium.text_size, shadow: theme.elevations.popover.shadows(), }, theme.motion.overlay, ) } } impl Menu { /// Creates a closed menu. pub fn new(id: I) -> Self where I: Into, { Self { id: id.into(), open: false, initial_selection: None, trigger_button: MouseButton::Left, open_at: None, a11y_label: None, on_change: None, trigger: None, items: None, min_width: None, } } /// Sets the row keyboard-highlighted when the menu opens. pub fn initial_selection(mut self, index: usize) -> Self { self.initial_selection = Some(index); self } /// Sets the mouse button that toggles the menu; right for context menus. pub fn trigger_button(mut self, button: MouseButton) -> Self { self.trigger_button = button; self } /// Positions the panel at this window point when it opens. pub fn open_at

(mut self, position: P) -> Self where P: Into>>, { self.open_at = position.into(); self } /// Sets the current open state. pub fn open(mut self, open: bool) -> Self { self.open = open; self } /// Reports the next open state, from dismissals, toggles, and activations alike. pub fn on_change(mut self, handler: F) -> Self where F: Fn(bool, &mut Window, &mut App) + 'static, { self.on_change = Some(Rc::new(handler)); self } /// Sets the element the panel anchors below. pub fn trigger(mut self, trigger: F) -> Self where F: Fn(&mut Window, &mut App) -> AnyElement + 'static, { self.trigger = Some(Box::new(trigger)); self } /// Sets the menu's items, built fresh each render. pub fn items(mut self, items: F) -> Self where F: Fn(&mut Window, &mut App) -> Vec + 'static, { self.items = Some(Box::new(items)); self } /// Overrides the accessibility label of the panel. pub fn accessibility_label(mut self, label: L) -> Self where L: Into, { self.a11y_label = Some(label.into()); self } /// Sets the panel's minimum width; defaults to the standard menu width. pub fn min_width(mut self, width: W) -> Self where W: Into, { self.min_width = Some(width.into()); self } } impl RenderOnce for Menu { fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { let (chrome, enter_duration) = MenuChrome::read(cx); let trigger_bounds: Rc>>> = Rc::new(RefCell::new(None)); let on_change = self.on_change.clone(); let fallback_label = self .a11y_label .clone() .unwrap_or_else(|| SharedString::from(self.id.to_string())); let items = if self.open { match self.items { Some(build) => Rc::from(build(window, cx)), None => Rc::from([]), } } else { Rc::from([]) }; let spec = MenuPanelSpec { open: self.open, items, initial_selection: self.initial_selection, preselect: false, min_width: self.min_width, label: fallback_label, position: self.open_at, trigger_bounds: trigger_bounds.clone(), on_dismiss: on_change.clone(), on_activate: self.on_change, }; div() .relative() .child( canvas( { let cell = trigger_bounds.clone(); move |bounds, _window, _cx| *cell.borrow_mut() = Some(bounds) }, { let on_change = on_change; let open = self.open; let trigger_button = self.trigger_button; let cell = trigger_bounds.clone(); move |_, _, window, _cx| { let trigger_bounds = cell; window.on_mouse_event({ let on_change = on_change.clone(); move |event: &MouseDownEvent, phase, window, cx| { if phase != DispatchPhase::Bubble || event.button != trigger_button { return; } let on_trigger = trigger_bounds .borrow() .is_some_and(|bounds| bounds.contains(&event.position)); if !on_trigger { return; } if let Some(on_change) = &on_change { on_change(!open, window, cx); } cx.stop_propagation(); } }); } }, ) .absolute() .inset_0(), ) .when_some(self.trigger, |wrapper, trigger| { wrapper.child(trigger(window, cx)) }) .when_some( anchored_menu_panel(window, cx, &self.id, spec, &chrome, enter_duration), |wrapper, panel| wrapper.child(panel), ) } } /// Everything the floating menu panel needs from its owner. pub(crate) struct MenuPanelSpec { pub open: bool, pub items: Rc<[MenuItem]>, pub initial_selection: Option, pub preselect: bool, pub min_width: Option, pub label: SharedString, pub position: Option>, pub trigger_bounds: TriggerBoundsCell, /// Closes this panel: outside clicks, `escape`, and `left` past a closed flyout. pub on_dismiss: Option, /// Closes the whole menu when a row inside activates. pub on_activate: Option, } /// Per-panel state that survives across frames while the panel stays mounted. struct MenuPanelState { focus: FocusHandle, was_open: Entity, scroll: Entity, selection: Entity>, } fn create_menu_panel_state(id: &ElementId, window: &mut Window, cx: &mut App) -> MenuPanelState { MenuPanelState { focus: focus_handle(&(id.clone(), "panel").into(), window, cx), was_open: window.use_keyed_state((id.clone(), "menu-was-open"), cx, |_, _| false), scroll: window.use_keyed_state((id.clone(), "menu-scroll"), cx, |_, _| ScrollHandle::new()), selection: window.use_keyed_state((id.clone(), "menu-selection"), cx, |_, _| None), } } struct MenuPanelParams<'a> { id: &'a ElementId, items: Rc<[MenuItem]>, initial_selection: Option, preselect: bool, min_width: Option, label: SharedString, /// Clicks here do not dismiss the panel: the trigger or the owning row. dismiss_bounds: &'a TriggerBoundsCell, /// Receives the panel's painted bounds, for a parent's dismissal checks. bounds_sink: Option<&'a TriggerBoundsCell>, /// This panel's flyout bounds are pushed here. outer_stack: Option<&'a Rc>>>, on_dismiss: Option, on_activate: Option, /// Flyout panels dismiss themselves on `left`; top-level panels ignore it. is_flyout: bool, chrome: &'a MenuChrome, enter_duration: std::time::Duration, debug_prefix: &'static str, } /// Wiring a panel's rows need to open their submenus. struct FlyoutContext<'a> { state: &'a MenuPanelState, open: &'a Entity>, preselect: &'a Entity, /// Opens or closes this level's flyout, re-arming it on every switch. set_flyout: FlyoutSetter, /// The open flyout's painted bounds; a parent ignores clicks inside them. bounds: &'a TriggerBoundsCell, /// Bounds of every open flyout below this panel, this level's included. stack: &'a Rc>>, /// Closes this level's flyout and returns focus to this panel. dismiss: ChangeHandler, on_activate: Option, id: ElementId, enter_duration: std::time::Duration, min_width: Option, } struct RowEnv<'a> { chrome: &'a MenuChrome, selection: &'a Entity>, on_activate: Option, flyout: &'a FlyoutContext<'a>, debug_prefix: &'static str, } /// Assembles the floating menu panel anchored to its trigger. pub(crate) fn anchored_menu_panel( window: &mut Window, cx: &mut App, id: &ElementId, spec: MenuPanelSpec, chrome: &MenuChrome, enter_duration: std::time::Duration, ) -> Option { let was_open = window.use_keyed_state((id.clone(), "menu-was-open"), cx, |_, _| false); // Render-phase residue: trigger-toggle and owner-driven closes bypass every // dismissal handler, so this closed frame is the only universal close hook. if !spec.open { was_open.update(cx, |was_open, _| *was_open = false); // A closed menu drops any open flyout; fresh state on the next open. window .use_keyed_state::>((id.clone(), "submenu-open"), cx, |_, _| None) .update(cx, |open, _| *open = None); window .use_keyed_state((id.clone(), "submenu-preselect"), cx, |_, _| false) .update(cx, |preselect, _| *preselect = false); return None; } let state = create_menu_panel_state(id, window, cx); let panel = menu_panel( window, cx, &state, MenuPanelParams { id, items: spec.items, initial_selection: spec.initial_selection, preselect: spec.preselect, min_width: spec.min_width, label: spec.label, dismiss_bounds: &spec.trigger_bounds, bounds_sink: None, outer_stack: None, on_dismiss: spec.on_dismiss, on_activate: spec.on_activate, is_flyout: false, chrome, enter_duration, debug_prefix: "menu", }, ); let anchored = match spec.position { Some(position) => anchored().position(position), None => anchored(), } .offset(point(px(0.), chrome.spacing_unit)); Some( deferred(anchored.child(panel)) .with_priority(OVERLAY_LAYER) .into_any_element(), ) } /// Builds one menu panel: chrome, rows, focus, navigation, and dismissals. fn menu_panel( window: &mut Window, cx: &mut App, state: &MenuPanelState, params: MenuPanelParams, ) -> AnyElement { let MenuPanelState { focus, was_open, scroll, selection, } = state; // Render-phase residue: panels open from owner state alone, so this mount // frame is the only place fresh-selection init can read its params. let selected = if *was_open.read(cx) { *selection.read(cx) } else { let requested = params .initial_selection .map(|index| index.min(params.items.len().saturating_sub(1))) .or_else(|| { params .preselect .then(|| params.items.iter().position(MenuItem::selectable)) .flatten() }); selection.update(cx, |selection, _| *selection = requested); requested }; was_open.update(cx, |was_open, _| *was_open = true); // Submenu plumbing for this level; a flyout panel recurses with its own. let flyout_id: ElementId = (params.id.clone(), "submenu").into(); let flyout_state = create_menu_panel_state(&flyout_id, window, cx); let flyout_open = window.use_keyed_state((params.id.clone(), "submenu-open"), cx, |_, _| None); let flyout_preselect = window.use_keyed_state((params.id.clone(), "submenu-preselect"), cx, |_, _| false); let flyout_bounds: TriggerBoundsCell = Rc::new(RefCell::new(None)); let flyout_stack: Rc>> = Rc::new(RefCell::new(Vec::new())); flyout_stack.borrow_mut().push(flyout_bounds.clone()); if let Some(outer_stack) = params.outer_stack { outer_stack.borrow_mut().push(flyout_bounds.clone()); } // Every open flip re-arms a clean flyout, so render never reconciles one. let set_flyout: FlyoutSetter = { let flyout_open = flyout_open.clone(); let flyout_preselect = flyout_preselect.clone(); let flyout_was_open = flyout_state.was_open.clone(); let flyout_selection = flyout_state.selection.clone(); Rc::new(move |index, opens_preselected, cx| { if index.is_some() && *flyout_open.read(cx) == index { return; } flyout_open.update(cx, |open, _| *open = index); flyout_preselect.update(cx, |preselect, _| *preselect = opens_preselected); flyout_was_open.update(cx, |was_open, _| *was_open = false); flyout_selection.update(cx, |selection, _| *selection = None); }) }; let dismiss_flyout: ChangeHandler = { let set_flyout = set_flyout.clone(); let focus = focus.clone(); Rc::new(move |_, window, cx| { set_flyout(None, false, cx); window.focus(&focus, cx); }) }; let flyout = FlyoutContext { state: &flyout_state, open: &flyout_open, preselect: &flyout_preselect, set_flyout: set_flyout.clone(), bounds: &flyout_bounds, stack: &flyout_stack, dismiss: dismiss_flyout, on_activate: params.on_activate.clone(), id: flyout_id, enter_duration: params.enter_duration, min_width: params.min_width, }; let rows: Vec = params .items .iter() .enumerate() .map(|(index, item)| { render_item( window, cx, item, index, selected == Some(index), &RowEnv { chrome: params.chrome, selection, on_activate: params.on_activate.clone(), flyout: &flyout, debug_prefix: params.debug_prefix, }, ) }) .collect(); v_flex() .id("menu-panel") .debug_selector(move || format!("{}-panel", params.debug_prefix)) .occlude() .track_focus(focus) .role(Role::Menu) .aria_label(params.label) .when_some(params.min_width, |panel, width| panel.min_w(width)) .when(params.min_width.is_none(), |panel| panel.min_w_56()) .p(params.chrome.spacing_unit) .bg(params.chrome.surface) .text_color(params.chrome.foreground) .rounded(params.chrome.radius_overlay) .border_1() // A flyout overlaps its parent, so it always wears a visible border. .border_color(if params.is_flyout { params.chrome.border } else { params.chrome.panel_border }) .shadow(params.chrome.shadow.clone()) .overflow_hidden() .on_mouse_down_out({ let on_dismiss = params.on_dismiss.clone(); let dismiss_bounds = params.dismiss_bounds.clone(); let flyout_stack = flyout_stack.clone(); move |event, window, cx| { if dismiss_bounds .borrow() .is_some_and(|bounds| bounds.contains(&event.position)) { return; } // Clicks inside any open flyout belong to that flyout. if flyout_stack.borrow().iter().any(|cell| { cell.borrow() .is_some_and(|bounds| bounds.contains(&event.position)) }) { return; } if let Some(on_dismiss) = &on_dismiss { on_dismiss(false, window, cx); } } }) .on_key_down({ let items = params.items.clone(); let selection = selection.clone(); let set_flyout = set_flyout.clone(); let on_dismiss = params.on_dismiss.clone(); let on_activate = params.on_activate.clone(); let left_dismisses = params.is_flyout; move |event: &KeyDownEvent, window, cx| { handle_menu_key( event, MenuKeyEnv { items: &items, selection: &selection, set_flyout: &set_flyout, on_dismiss: &on_dismiss, on_activate: &on_activate, left_dismisses, }, window, cx, ); } }) .child( ScrollArea::new("menu-items") .handle(scroll.read(cx).clone()) .axes(ScrollAxes::Vertical) .max_h(POPUP_MAX_HEIGHT) .child(v_flex().gap_0p5().children(rows)), ) .child( canvas( { let bounds_sink = params.bounds_sink.cloned(); move |bounds, _window, _cx| { if let Some(cell) = &bounds_sink { *cell.borrow_mut() = Some(bounds); } } }, { let focus = focus.clone(); let flyout_open = flyout_open.clone(); move |_, _, window, cx| { if flyout_open.read(cx).is_none() && !focus.contains_focused(window, cx) { window.focus(&focus, cx); } } }, ) .absolute() .inset_0(), ) .with_animation( "menu-enter", Animation::new(params.enter_duration).with_easing(ease_out_quint()), |panel, delta| panel.opacity(delta), ) .into_any_element() } /// Renders one menu row. fn render_item( window: &mut Window, cx: &mut App, item: &MenuItem, index: usize, selected: bool, env: &RowEnv, ) -> AnyElement { match &item.kind { MenuItemKind::Separator => div() .h(BORDER_WIDTH) .debug_selector(move || format!("{}-separator-{index}", env.debug_prefix)) .mx(env.chrome.spacing_unit * 2.0) .my(env.chrome.spacing_unit) .bg(env.chrome.border) .into_any_element(), MenuItemKind::Action => { let label_color = row_color(item, selected, false, env.chrome); let disabled = item.disabled; let destructive = item.destructive; let checked = item.checked; let shortcut = item.shortcut.clone(); let icon = item.icon.clone(); row_shell(cx, item, index, selected, env) .on_click({ let handler = item.handler.clone(); let on_activate = env.on_activate.clone(); move |_, window, cx| { if disabled { return; } if let Some(on_activate) = &on_activate { on_activate(false, window, cx); } if let Some(handler) = &handler { handler(window, cx); } } }) .when_some(icon, |row, icon| { row.child(icon.size(env.chrome.icon_size).color(label_color)) }) .child( div() .flex_1() .text_color(label_color) .child(item.label.clone()), ) .when_some(shortcut, |row, shortcut| { row.child( div() .text_color(if selected && !destructive && !disabled { env.chrome.primary_foreground } else { env.chrome.muted_foreground }) .child(shortcut), ) }) .when(checked, |row| { row.child( icon_glyph(IconRole::Check) .size(env.chrome.dense_icon_size) .color(label_color), ) }) .into_any_element() } MenuItemKind::Submenu(items) => { let chrome = env.chrome; let flyout = env.flyout; let disabled = item.disabled; let open = *flyout.open.read(cx) == Some(index); let label_color = row_color(item, selected, open, chrome); let row_bounds: TriggerBoundsCell = Rc::new(RefCell::new(None)); row_shell(cx, item, index, selected, env) .on_click({ let set_flyout = flyout.set_flyout.clone(); move |_, _window, cx| { if disabled { return; } // Hover already opens the flyout; a click never closes it. set_flyout(Some(index), false, cx); } }) .child( div() .flex_1() .text_color(label_color) .child(item.label.clone()), ) .child( icon_glyph(IconRole::ChevronRight) .size(chrome.dense_icon_size) .color(label_color), ) .aria_expanded(open) .when(open, |row| { row.child( canvas( { let cell = row_bounds.clone(); move |bounds, _window, _cx| *cell.borrow_mut() = Some(bounds) }, |_, _, _, _| {}, ) .absolute() .inset_0(), ) .child( deferred(div().absolute().left_full().top_0().child(menu_panel( window, cx, flyout.state, MenuPanelParams { id: &flyout.id, items: items.clone(), initial_selection: None, preselect: *flyout.preselect.read(cx), min_width: flyout.min_width, label: item.label.clone(), dismiss_bounds: &row_bounds, bounds_sink: Some(flyout.bounds), outer_stack: Some(flyout.stack), on_dismiss: Some(flyout.dismiss.clone()), on_activate: flyout.on_activate.clone(), is_flyout: true, chrome, enter_duration: flyout.enter_duration, debug_prefix: "submenu", }, ))) .with_priority(OVERLAY_LAYER), ) }) .into_any_element() } } } /// The shared row shell: metrics, highlight, and hover-selection. fn row_shell( cx: &App, item: &MenuItem, index: usize, selected: bool, env: &RowEnv, ) -> gpui::Stateful { let chrome = env.chrome; let selectable = item.selectable(); let opens_submenu = item.submenu_items().is_some(); let flyout_open = *env.flyout.open.read(cx) == Some(index); let row_id = item .id .clone() .unwrap_or_else(|| ("menu-item", index).into()); div() .id(row_id) .role(Role::MenuItem) .debug_selector(move || format!("{}-item-{index}", env.debug_prefix)) .flex() .items_center() .gap_2() .min_h(chrome.menu_item_height) .px(chrome.spacing_unit * 2.0) .text_size(chrome.text_size) .line_height(relative(1.0)) .rounded(chrome.radius_sm) .map(|row| { if item.disabled { row } else if selected && flyout_open { row.bg(chrome.muted_hover) } else if selected { let highlight = if item.destructive { chrome.destructive } else { chrome.primary }; row.bg(highlight) } else { row.hover(|row| row.bg(chrome.muted_hover)) } }) .on_mouse_move({ let selection = env.selection.clone(); let flyout_open = env.flyout.open.clone(); let set_flyout = env.flyout.set_flyout.clone(); let dismiss = env.flyout.dismiss.clone(); move |_, window, cx| { if !selectable { return; } if *selection.read(cx) != Some(index) { selection.update(cx, |selection, _| *selection = Some(index)); } if opens_submenu { set_flyout(Some(index), false, cx); } else if flyout_open.read(cx).is_some() { dismiss(false, window, cx); } } }) } /// The row's label/icon color. fn row_color( item: &MenuItem, selected: bool, flyout_open: bool, chrome: &MenuChrome, ) -> gpui::Hsla { if item.disabled { return chrome.muted_foreground; } if item.destructive { return if selected && !flyout_open { chrome.destructive_foreground } else { chrome.destructive }; } if selected && !flyout_open { chrome.primary_foreground } else { chrome.foreground } } /// Everything key navigation needs from the panel. struct MenuKeyEnv<'a> { items: &'a [MenuItem], selection: &'a Entity>, set_flyout: &'a FlyoutSetter, on_dismiss: &'a Option, on_activate: &'a Option, left_dismisses: bool, } /// Handles the panel's navigation keys. fn handle_menu_key(event: &KeyDownEvent, env: MenuKeyEnv, window: &mut Window, cx: &mut App) { let selectable: Vec = env .items .iter() .enumerate() .filter(|(_, item)| item.selectable()) .map(|(index, _)| index) .collect(); let current = *env.selection.read(cx); let position = current.and_then(|current| selectable.iter().position(|index| *index == current)); match event.keystroke.key.as_str() { "down" | "up" | "home" | "end" => { if selectable.is_empty() { return; } let next = match event.keystroke.key.as_str() { "down" => match position { Some(pos) => (pos + 1).min(selectable.len() - 1), None => 0, }, "up" => match position { Some(pos) => pos.saturating_sub(1), None => selectable.len() - 1, }, "home" => 0, _ => selectable.len() - 1, }; env.selection .update(cx, |selection, _| *selection = Some(selectable[next])); } "right" => { let opens = current .and_then(|current| env.items.get(current)) .is_some_and(|item| item.selectable() && item.submenu_items().is_some()); if !opens { return; } (env.set_flyout)(current, true, cx); } "enter" | "space" => { if let Some(current) = current && let Some(item) = env.items.get(current) && item.selectable() { if item.submenu_items().is_some() { (env.set_flyout)(Some(current), true, cx); } else { if let Some(on_activate) = env.on_activate { on_activate(false, window, cx); } if let Some(handler) = &item.handler { handler(window, cx); } } } } "escape" => { if let Some(on_dismiss) = env.on_dismiss { on_dismiss(false, window, cx); } } "left" => { if !env.left_dismisses { return; } if let Some(on_dismiss) = env.on_dismiss { on_dismiss(false, window, cx); } } _ => return, } cx.stop_propagation(); }