use super::overlay::{self, Callback, Translate, rem};
use crate::{
density::{self, Density},
icons::{CarbonIcon, Icon, IconSize},
motion::{self, Curve, Motion, Speed},
theme::color,
tokens::{ColorRole, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
Anchor, AnyElement, App, Bounds, CursorStyle, ElementId, Entity, FocusHandle, Hsla,
InteractiveElement, IntoElement, KeyDownEvent, MouseButton, ParentElement, Pixels, RenderOnce,
SharedString, StatefulInteractiveElement as _, Styled, Window, canvas, div, point,
prelude::FluentBuilder as _, px,
};
use gpui_kit::{KeyBinding, TestSupportExt as _, actions};
use std::{
cell::RefCell,
collections::HashMap,
rc::Rc,
time::{Duration, Instant},
};
actions!(
u0ui_menu,
[
TabOut
]
);
pub(crate) const KEY_CONTEXT: &str = "U0Menu";
pub(crate) fn init(cx: &mut App) {
cx.bind_keys([
KeyBinding::new("tab", TabOut, Some(KEY_CONTEXT)),
KeyBinding::new("shift-tab", TabOut, Some(KEY_CONTEXT)),
]);
}
pub const ITEM_MOTION: Motion = Motion::new(Curve::ProductiveStandard, Speed::Fast01);
pub const TYPEAHEAD: Duration = Duration::from_millis(500);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum MenuSize {
Xs,
#[default]
Sm,
Md,
Lg,
}
impl MenuSize {
pub const ALL: [MenuSize; 4] = [Self::Xs, Self::Sm, Self::Md, Self::Lg];
pub const fn name(self) -> &'static str {
match self {
Self::Xs => "xs",
Self::Sm => "sm",
Self::Md => "md",
Self::Lg => "lg",
}
}
pub fn item_height(self, density: Density) -> f32 {
match density {
Density::Compact => crate::tokens::DENSITY_ROW_MD.px_value(),
Density::Comfortable => match self {
Self::Xs => 24.,
Self::Sm => 32.,
Self::Md => 40.,
Self::Lg => 48.,
},
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MenuGeometry {
pub padding_block: f32,
pub min_width: f32,
pub max_width: f32,
pub item_padding: f32,
pub column_gap: f32,
pub divider_margin: f32,
}
impl MenuGeometry {
pub const fn of(with_icons: bool) -> Self {
Self {
padding_block: 4.,
min_width: if with_icons { 192. } else { 160. },
max_width: 288.,
item_padding: 16.,
column_gap: 8.,
divider_margin: 4.,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ItemState {
pub hovered: bool,
pub focused: bool,
pub danger: bool,
pub disabled: bool,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MenuLook {
pub background: Hsla,
pub divider: Hsla,
}
impl MenuLook {
pub fn current() -> Self {
Self {
background: color(ColorRole::Layer01),
divider: color(ColorRole::BorderSubtle00),
}
}
pub fn item(state: ItemState) -> (Hsla, Hsla, Option<Hsla>) {
use ColorRole::*;
let clear = Hsla::transparent_black();
if state.disabled {
return (clear, color(TextDisabled), None);
}
let outline = state.focused.then(|| color(Focus));
if state.danger && (state.hovered || state.focused) {
return (color(ButtonDangerPrimary), color(TextOnColor), outline);
}
if state.hovered {
return (color(LayerHover01), color(TextPrimary), outline);
}
(clear, color(TextSecondary), outline)
}
pub fn label(state: ItemState) -> Hsla {
let (_, text, _) = Self::item(state);
if state.danger && !state.disabled && !state.hovered && !state.focused {
color(ColorRole::TextError)
} else {
text
}
}
}
type Toggle = Rc<dyn Fn(bool, &mut Window, &mut App)>;
type Pick = Rc<dyn Fn(usize, &mut Window, &mut App)>;
pub(crate) type KeyHook = Rc<dyn Fn(&str, &mut Window, &mut App) -> bool>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Kind {
Item,
Divider,
Checkbox,
Radio,
Group,
}
#[derive(Clone)]
pub struct MenuItem {
kind: Kind,
label: SharedString,
shortcut: Option<SharedString>,
icon: Option<Icon>,
disabled: bool,
danger: bool,
on_select: Option<Callback>,
children: Vec<MenuItem>,
selected: Option<bool>,
default_selected: bool,
on_toggle: Option<Toggle>,
options: Vec<SharedString>,
picked: Option<Option<usize>>,
default_picked: Option<usize>,
on_pick: Option<Pick>,
}
impl MenuItem {
fn base(kind: Kind, label: SharedString) -> Self {
Self {
kind,
label,
shortcut: None,
icon: None,
disabled: false,
danger: false,
on_select: None,
children: Vec::new(),
selected: None,
default_selected: false,
on_toggle: None,
options: Vec::new(),
picked: None,
default_picked: None,
on_pick: None,
}
}
pub fn item(label: impl Into<SharedString>) -> Self {
Self::base(Kind::Item, label.into())
}
pub fn divider() -> Self {
Self::base(Kind::Divider, SharedString::default())
}
pub fn checkbox(label: impl Into<SharedString>) -> Self {
Self::base(Kind::Checkbox, label.into())
}
pub fn radio_group<S: Into<SharedString>>(
label: impl Into<SharedString>,
options: impl IntoIterator<Item = S>,
) -> Self {
let mut item = Self::base(Kind::Radio, label.into());
item.options = options.into_iter().map(Into::into).collect();
item
}
pub fn group(
label: impl Into<SharedString>,
items: impl IntoIterator<Item = MenuItem>,
) -> Self {
let mut item = Self::base(Kind::Group, label.into());
item.children = items.into_iter().collect();
item
}
pub fn shortcut(mut self, shortcut: impl Into<SharedString>) -> Self {
self.shortcut = Some(shortcut.into());
self
}
pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
self.icon = Some(icon.into());
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn danger(mut self, danger: bool) -> Self {
self.danger = danger;
self
}
pub fn on_select(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_select = Some(Rc::new(f));
self
}
pub fn submenu(mut self, items: impl IntoIterator<Item = MenuItem>) -> Self {
self.children = items.into_iter().collect();
self
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = Some(selected);
self
}
pub fn default_selected(mut self, selected: bool) -> Self {
self.default_selected = selected;
self
}
pub fn on_change(mut self, f: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
self.on_toggle = Some(Rc::new(f));
self
}
pub fn selected_item(mut self, index: Option<usize>) -> Self {
self.picked = Some(index);
self
}
pub fn default_selected_item(mut self, index: usize) -> Self {
self.default_picked = Some(index);
self
}
pub fn on_select_item(mut self, f: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
self.on_pick = Some(Rc::new(f));
self
}
}
#[derive(Clone)]
enum Action {
Select(Option<Callback>),
Submenu(Vec<MenuItem>),
Toggle {
key: String,
checked: bool,
controlled: bool,
on_change: Option<Toggle>,
},
Pick {
key: String,
index: usize,
controlled: bool,
on_change: Option<Pick>,
},
}
#[derive(Clone)]
#[allow(clippy::large_enum_variant)]
enum Row {
Divider,
Entry {
label: SharedString,
shortcut: Option<SharedString>,
icon: Option<Icon>,
disabled: bool,
danger: bool,
checked: Option<bool>,
action: Action,
},
}
impl Row {
fn enabled(&self) -> bool {
matches!(
self,
Row::Entry {
disabled: false,
..
}
)
}
fn label(&self) -> &str {
match self {
Row::Entry { label, .. } => label,
Row::Divider => "",
}
}
}
pub(crate) type Selections = Entity<HashMap<String, usize>>;
fn flatten(
items: &[MenuItem],
prefix: &str,
selections: &HashMap<String, usize>,
out: &mut Vec<Row>,
) {
for (i, item) in items.iter().enumerate() {
let key = format!("{prefix}{i}");
match item.kind {
Kind::Divider => out.push(Row::Divider),
Kind::Group => flatten(&item.children, &format!("{key}."), selections, out),
Kind::Item => out.push(Row::Entry {
label: item.label.clone(),
shortcut: item.shortcut.clone(),
icon: item.icon.clone(),
disabled: item.disabled,
danger: item.danger,
checked: None,
action: if item.children.is_empty() {
Action::Select(item.on_select.clone())
} else {
Action::Submenu(item.children.clone())
},
}),
Kind::Checkbox => {
let checked = item.selected.unwrap_or_else(|| {
selections
.get(&key)
.map_or(item.default_selected, |v| *v == 1)
});
out.push(Row::Entry {
label: item.label.clone(),
shortcut: item.shortcut.clone(),
icon: item.icon.clone(),
disabled: item.disabled,
danger: false,
checked: Some(checked),
action: Action::Toggle {
key,
checked,
controlled: item.selected.is_some(),
on_change: item.on_toggle.clone(),
},
})
}
Kind::Radio => {
let picked = item
.picked
.unwrap_or_else(|| selections.get(&key).copied().or(item.default_picked));
for (index, option) in item.options.iter().enumerate() {
out.push(Row::Entry {
label: option.clone(),
shortcut: None,
icon: None,
disabled: item.disabled,
danger: false,
checked: Some(picked == Some(index)),
action: Action::Pick {
key: key.clone(),
index,
controlled: item.picked.is_some(),
on_change: item.on_pick.clone(),
},
});
}
}
}
}
}
#[derive(Default)]
struct LevelState {
active: Option<usize>,
hovered: Option<usize>,
submenu: Option<usize>,
submenu_focus: bool,
submenu_first: bool,
typed: String,
typed_at: Option<Instant>,
}
#[derive(Clone)]
pub(crate) struct Chain {
pub close: Callback,
pub selections: Selections,
pub bounds: Rc<RefCell<Vec<Bounds<Pixels>>>>,
}
fn step(rows: &[Row], from: Option<usize>, direction: isize) -> Option<usize> {
let n = rows.len() as isize;
if n == 0 {
return None;
}
let mut i = match from {
Some(i) => i as isize,
None if direction > 0 => -1,
None => n,
};
for _ in 0..n {
i = (i + direction).rem_euclid(n);
if rows[i as usize].enabled() {
return Some(i as usize);
}
}
None
}
fn initial_row(rows: &[Row]) -> Option<usize> {
let only_picks = rows.iter().all(|r| {
matches!(
r,
Row::Entry {
action: Action::Pick { .. },
..
}
)
});
let picked = only_picks
.then(|| {
rows.iter().position(|r| {
r.enabled()
&& matches!(
r,
Row::Entry {
checked: Some(true),
..
}
)
})
})
.flatten();
picked.or_else(|| step(rows, None, 1))
}
fn find_typed(rows: &[Row], from: Option<usize>, typed: &str) -> Option<usize> {
let typed = typed.to_lowercase();
let n = rows.len();
let start = from.map_or(0, |i| if typed.chars().count() > 1 { i } else { i + 1 });
(0..n)
.map(|k| (start + k) % n)
.find(|&i| rows[i].enabled() && rows[i].label().to_lowercase().starts_with(&typed))
}
#[derive(IntoElement)]
pub(crate) struct Menu {
id: ElementId,
items: Vec<MenuItem>,
size: MenuSize,
label: SharedString,
focus: FocusHandle,
chain: Chain,
parent: Option<(FocusHandle, Callback)>,
min_width: Option<f32>,
path: String,
on_key: Option<KeyHook>,
focus_first: Option<Entity<LevelState>>,
opening: Option<Entity<bool>>,
}
impl Menu {
pub fn new(
id: impl Into<ElementId>,
items: Vec<MenuItem>,
focus: FocusHandle,
chain: Chain,
) -> Self {
Self {
id: id.into(),
items,
size: MenuSize::Sm,
label: SharedString::default(),
focus,
chain,
parent: None,
min_width: None,
path: String::new(),
on_key: None,
focus_first: None,
opening: None,
}
}
pub fn size(mut self, size: MenuSize) -> Self {
self.size = size;
self
}
pub fn label(mut self, label: SharedString) -> Self {
self.label = label;
self
}
pub fn min_width(mut self, width: Option<f32>) -> Self {
self.min_width = width;
self
}
}
fn activate(
row: &Row,
index: usize,
state: &Entity<LevelState>,
chain: &Chain,
from_keyboard: bool,
window: &mut Window,
cx: &mut App,
) {
let Row::Entry {
action, disabled, ..
} = row
else {
return;
};
if *disabled {
return;
}
match action {
Action::Submenu(_) => state.update(cx, |s, cx| {
s.submenu = Some(index);
s.submenu_focus = from_keyboard;
s.submenu_first = from_keyboard;
cx.notify();
}),
Action::Select(on_select) => {
if let Some(f) = on_select {
f(window, cx);
}
(chain.close)(window, cx);
}
Action::Toggle {
key,
checked,
controlled,
on_change,
} => {
if !controlled {
chain.selections.update(cx, |map, cx| {
map.insert(key.clone(), usize::from(!checked));
cx.notify();
});
}
if let Some(f) = on_change {
f(!checked, window, cx);
}
(chain.close)(window, cx);
}
Action::Pick {
key,
index,
controlled,
on_change,
} => {
if !controlled {
chain.selections.update(cx, |map, cx| {
map.insert(key.clone(), *index);
cx.notify();
});
}
if let Some(f) = on_change {
f(*index, window, cx);
}
(chain.close)(window, cx);
}
}
}
impl RenderOnce for Menu {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let rows = {
let mut rows = Vec::new();
flatten(
&self.items,
&self.path,
self.chain.selections.read(cx),
&mut rows,
);
Rc::new(rows)
};
let state = window.use_keyed_state((id.clone(), "level"), cx, |_, _| LevelState::default());
let opening = self.opening.as_ref().is_some_and(|o| *o.read(cx));
if let Some(flag) = &self.opening
&& opening
{
flag.update(cx, |v, _| *v = false);
}
if opening {
let first = initial_row(&rows);
state.update(cx, |s, _| {
*s = LevelState {
active: first,
..LevelState::default()
}
});
}
if let Some(parent) = &self.focus_first {
let first = step(&rows, None, 1);
state.update(cx, |s, _| {
s.active = first;
s.hovered = None;
});
parent.update(cx, |s, _| s.submenu_first = false);
}
let sub_focus = window
.use_keyed_state((id.clone(), "sub-focus"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
let density = density::current();
let height = self.size.item_height(density);
let with_icons = rows
.iter()
.any(|r| matches!(r, Row::Entry { icon: Some(_), .. }));
let with_selection = rows.iter().any(|r| {
matches!(
r,
Row::Entry {
checked: Some(_),
..
}
)
});
let geometry = MenuGeometry::of(with_icons);
let look = MenuLook::current();
let (active, hovered, submenu, submenu_focus) = {
let s = state.read(cx);
(s.active, s.hovered, s.submenu, s.submenu_focus)
};
let keyboard_focus = self.focus.is_focused(window);
let on_key = {
let rows = rows.clone();
let state = state.clone();
let chain = self.chain.clone();
let parent = self.parent.clone();
let extra = self.on_key.clone();
move |event: &KeyDownEvent, window: &mut Window, cx: &mut App| {
let key = event.keystroke.key.as_str();
let current = state.read(cx).active.or(state.read(cx).hovered);
let set_active = |index: Option<usize>, cx: &mut App| {
state.update(cx, |s, cx| {
s.active = index;
s.hovered = None;
cx.notify();
})
};
match key {
"down" => set_active(step(&rows, current, 1), cx),
"up" => set_active(step(&rows, current, -1), cx),
"home" => set_active(step(&rows, None, 1), cx),
"end" => set_active(step(&rows, None, -1), cx),
"enter" | "space" => {
if let Some(i) = current {
activate(&rows[i], i, &state, &chain, true, window, cx);
}
}
"right" => {
if let Some(i) = current
&& matches!(
&rows[i],
Row::Entry {
action: Action::Submenu(_),
disabled: false,
..
}
)
{
activate(&rows[i], i, &state, &chain, true, window, cx);
} else if let Some(extra) = &extra {
extra(key, window, cx);
}
}
"left" if parent.is_some() => {
if let Some((_, close)) = &parent {
close(window, cx);
}
}
"escape" => match &parent {
Some((_, close)) => close(window, cx),
None => (chain.close)(window, cx),
},
"tab" => (chain.close)(window, cx),
_ => {
if let Some(extra) = &extra
&& extra(key, window, cx)
{
cx.stop_propagation();
return;
}
let Some(ch) = event.keystroke.key_char.as_deref() else {
return;
};
if ch.chars().any(|c| c.is_control()) || ch.trim().is_empty() {
return;
}
let now = Instant::now();
let typed = state.update(cx, |s, _| {
if s.typed_at.is_none_or(|t| now.duration_since(t) > TYPEAHEAD) {
s.typed.clear();
}
s.typed.push_str(ch);
s.typed_at = Some(now);
s.typed.clone()
});
if let Some(i) = find_typed(&rows, current, &typed) {
set_active(Some(i), cx);
}
}
}
cx.stop_propagation();
}
};
let mut children: Vec<AnyElement> = Vec::new();
for (index, row) in rows.iter().enumerate() {
let Row::Entry {
label,
shortcut,
icon,
disabled,
danger,
checked,
action,
} = row
else {
children.push(
div()
.my(rem(geometry.divider_margin))
.h(px(1.))
.w_full()
.bg(look.divider)
.into_any_element(),
);
continue;
};
let is_submenu = matches!(action, Action::Submenu(_));
let item_state = ItemState {
hovered: hovered == Some(index) || submenu == Some(index),
focused: keyboard_focus && active == Some(index),
danger: *danger,
disabled: *disabled,
};
let (bg, text, outline) = MenuLook::item(item_state);
let label_color = MenuLook::label(item_state);
let row_id = ElementId::NamedChild(
std::sync::Arc::new(id.clone()),
SharedString::from(format!("row-{index}")),
);
let bg = motion::animate_color((row_id.clone(), "bg"), bg, ITEM_MOTION, window, cx);
let icon_color = text;
let mut line = div()
.id(row_id.clone())
.relative()
.h(rem(height))
.flex_shrink_0()
.px(rem(geometry.item_padding))
.flex()
.flex_row()
.items_center()
.gap(rem(geometry.column_gap))
.bg(bg)
.text_color(text)
.type_style(TypeStyle::BodyCompact01)
.cursor(if *disabled {
CursorStyle::OperationNotAllowed
} else {
CursorStyle::PointingHand
});
if with_selection {
line = line.child(div().size(rem(16.)).flex_shrink_0().when(
checked == &Some(true),
|this| {
this.child(
Icon::carbon(CarbonIcon::Checkmark)
.size(IconSize::S16)
.color(icon_color),
)
},
));
}
if with_icons {
line = line.child(
div()
.size(rem(16.))
.flex_shrink_0()
.when_some(icon.clone(), |this, icon| {
this.child(icon.size(IconSize::S16).color(icon_color))
}),
);
}
line = line
.child(
div()
.flex_1()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.text_color(label_color)
.child(label.clone()),
)
.child(
div()
.flex_shrink_0()
.flex()
.items_center()
.when(!is_submenu, |this| this.children(shortcut.clone()))
.when(is_submenu, |this| {
this.child(
Icon::carbon(CarbonIcon::CaretRight)
.size(IconSize::S16)
.color(icon_color),
)
}),
)
.when_some(outline, |this, outline| {
this.child(
div()
.absolute()
.top_0()
.left_0()
.size_full()
.border_2()
.border_color(outline)
.when(*danger, |this| {
this.child(
div()
.size_full()
.border_1()
.border_color(color(ColorRole::Background)),
)
}),
)
});
if !*disabled {
let state_hover = state.clone();
let state_click = state.clone();
let chain = self.chain.clone();
let row_owned = row.clone();
line = line
.on_hover(move |hovering, _, cx| {
state_hover.update(cx, |s, cx| {
if *hovering {
s.hovered = Some(index);
s.active = None;
s.submenu = is_submenu.then_some(index);
s.submenu_focus = false;
s.submenu_first = false;
} else if s.hovered == Some(index) {
s.hovered = None;
}
cx.notify();
})
})
.on_click(move |_, window, cx| {
activate(&row_owned, index, &state_click, &chain, false, window, cx)
});
}
let line = line.into_any_element();
if submenu == Some(index)
&& let Action::Submenu(items) = action
{
let close_sub = {
let state = state.clone();
let focus = self.focus.clone();
Rc::new(move |window: &mut Window, cx: &mut App| {
state.update(cx, |s, cx| {
s.submenu = None;
s.active = Some(index);
cx.notify();
});
focus.focus(window, cx);
}) as Callback
};
if submenu_focus {
state.update(cx, |s, _| s.submenu_focus = false);
let sub_focus = sub_focus.clone();
window.defer(cx, move |window, cx| sub_focus.focus(window, cx));
}
let mut sub = Menu::new(
(row_id.clone(), "sub"),
items.clone(),
sub_focus.clone(),
self.chain.clone(),
)
.size(self.size)
.label(label.clone());
sub.parent = Some((self.focus.clone(), close_sub));
sub.focus_first = state.read(cx).submenu_first.then(|| state.clone());
sub.path = format!("{}{index}.", self.path);
let bounds = self.chain.bounds.clone();
let sub = div()
.child(sub)
.child(
canvas(move |b, _, _| bounds.borrow_mut().push(b), |_, _, _, _| {})
.absolute()
.top_0()
.left_0()
.size_full(),
)
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation());
children.push(
gpui_kit::base::Popup::new((row_id, "popup"), line)
.anchor(Anchor::LeftCenter)
.content(Translate::new(
sub,
point(0., 0.5),
point(px(0.), px(-height / 2. - geometry.padding_block)),
))
.into_any_element(),
);
} else {
children.push(line);
}
}
let tab_out = {
let chain = self.chain.clone();
move |_: &TabOut, window: &mut Window, cx: &mut App| {
(chain.close)(window, cx);
cx.stop_propagation();
}
};
div()
.id(id.clone())
.test_support()
.key_context(KEY_CONTEXT)
.track_focus(&self.focus)
.on_key_down(on_key)
.on_action(tab_out)
.flex()
.flex_col()
.py(rem(geometry.padding_block))
.min_w(rem(self.min_width.unwrap_or(0.).max(geometry.min_width)))
.max_w(rem(geometry.max_width.max(self.min_width.unwrap_or(0.))))
.bg(look.background)
.shadow(overlay::floating_shadow())
.occlude()
.children(children)
}
}
pub(crate) struct MenuSpec {
pub items: Vec<MenuItem>,
pub size: MenuSize,
pub label: SharedString,
pub open: bool,
pub set_open: overlay::OpenChange,
pub min_width: Option<f32>,
pub on_unhandled_key: Option<KeyHook>,
}
pub(crate) fn menu_surface(
id: &ElementId,
spec: MenuSpec,
window: &mut Window,
cx: &mut App,
) -> Option<AnyElement> {
menu_surface_returning(id, spec, None, window, cx)
}
pub(crate) fn menu_surface_returning(
id: &ElementId,
spec: MenuSpec,
return_to: Option<FocusHandle>,
window: &mut Window,
cx: &mut App,
) -> Option<AnyElement> {
let open = spec.open;
let focus = overlay::surface_focus_returning(id, open, None, return_to, window, cx);
let selections = window.use_keyed_state((id.clone(), "selections"), cx, |_, _| HashMap::new());
let bounds = window
.use_keyed_state((id.clone(), "sub-bounds"), cx, |_, _| {
Rc::new(RefCell::new(Vec::<Bounds<Pixels>>::new()))
})
.read(cx)
.clone();
let was_open = window.use_keyed_state((id.clone(), "was-open"), cx, |_, _| false);
let opening = open && !*was_open.read(cx);
if open != *was_open.read(cx) {
was_open.update(cx, |v, _| *v = open);
}
let had_focus = window.use_keyed_state((id.clone(), "had-focus"), cx, |_, _| false);
if !open {
if *had_focus.read(cx) {
had_focus.update(cx, |v, _| *v = false);
}
return None;
}
let set_open = spec.set_open;
let pending = window.use_keyed_state((id.clone(), "focus-pending"), cx, |_, _| false);
let fresh = window.use_keyed_state((id.clone(), "fresh"), cx, |_, _| false);
if opening {
had_focus.update(cx, |v, _| *v = false);
pending.update(cx, |v, _| *v = true);
fresh.update(cx, |v, _| *v = true);
} else if focus.contains_focused(window, cx) {
if !*had_focus.read(cx) {
had_focus.update(cx, |v, _| *v = true);
}
} else if *had_focus.read(cx)
&& window.focused(cx).is_none_or(|f| f.is_rendered(window))
{
had_focus.update(cx, |v, _| *v = false);
let set_open = set_open.clone();
window.defer(cx, move |window, cx| set_open(false, window, cx));
}
let chain = Chain {
close: {
let set_open = set_open.clone();
Rc::new(move |window, cx| set_open(false, window, cx))
},
selections,
bounds: bounds.clone(),
};
let outside = {
let bounds = bounds.clone();
move |event: &gpui_kit::MouseDownEvent, window: &mut Window, cx: &mut App| {
if open && !bounds.borrow().iter().any(|b| b.contains(&event.position)) {
set_open(false, window, cx);
}
}
};
bounds.borrow_mut().clear();
let mut menu = Menu::new((id.clone(), "menu"), spec.items, focus, chain)
.size(spec.size)
.label(spec.label)
.min_width(spec.min_width);
menu.on_key = spec.on_unhandled_key;
menu.opening = Some(fresh);
let take_focus = {
let focus = menu.focus.clone();
canvas(
|_, _, _| {},
move |_, _, window, cx| {
if !*pending.read(cx) {
return;
}
pending.update(cx, |v, _| *v = false);
let focus = focus.clone();
window.defer(cx, move |window, cx| {
if !focus.contains_focused(window, cx) {
focus.focus(window, cx);
}
});
},
)
.absolute()
.size_0()
};
Some(
div()
.id((id.clone(), "surface"))
.on_mouse_down_out(outside)
.child(menu)
.child(take_focus)
.into_any_element(),
)
}
pub(crate) fn anchored_menu(
id: &ElementId,
trigger: AnyElement,
spec: MenuSpec,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
anchored_menu_aligned(id, trigger, spec, false, window, cx)
}
pub(crate) fn anchored_menu_aligned(
id: &ElementId,
trigger: AnyElement,
spec: MenuSpec,
end: bool,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
anchored_menu_impl(
id,
trigger,
spec,
end,
false,
None,
None,
Shift::NONE,
window,
cx,
)
}
pub(crate) fn anchored_menu_offset(
id: &ElementId,
trigger: AnyElement,
spec: MenuSpec,
dx: f32,
gap: f32,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
anchored_menu_impl(
id,
trigger,
spec,
false,
false,
None,
None,
Shift {
dx: dx.min(0.),
gap,
caret: true,
},
window,
cx,
)
}
pub(crate) fn anchored_menu_full_height(
id: &ElementId,
trigger: AnyElement,
trigger_focus: FocusHandle,
spec: MenuSpec,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
anchored_menu_impl(
id,
trigger,
spec,
false,
true,
Some(trigger_focus),
None,
Shift::NONE,
window,
cx,
)
}
pub(crate) fn anchored_button_menu(
id: &ElementId,
trigger: AnyElement,
spec: MenuSpec,
trigger_width: f32,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
let width = button_menu_width(&spec.items, trigger_width);
anchored_menu_impl(
id,
trigger,
spec,
false,
false,
None,
Some(width),
Shift::NONE,
window,
cx,
)
}
pub fn button_menu_width(items: &[MenuItem], trigger_width: f32) -> f32 {
let mut rows = Vec::new();
flatten(items, "", &HashMap::new(), &mut rows);
let with_icons = rows
.iter()
.any(|r| matches!(r, Row::Entry { icon: Some(_), .. }));
let geometry = MenuGeometry::of(with_icons);
trigger_width
.min(geometry.max_width)
.max(geometry.min_width)
}
#[derive(Clone, Copy)]
struct Shift {
dx: f32,
gap: f32,
caret: bool,
}
impl Shift {
const NONE: Self = Self {
dx: 0.,
gap: 0.,
caret: false,
};
}
fn breadcrumb_caret() -> impl IntoElement {
let fill = color(ColorRole::Field01);
canvas(
|_, _, _| {},
move |bounds, _, window, _| {
let o = bounds.origin;
let w = bounds.size.width;
let h = bounds.size.height;
let mut path = gpui_kit::PathBuilder::fill();
path.move_to(gpui_kit::point(o.x, o.y + h));
path.line_to(gpui_kit::point(o.x + w / 2., o.y));
path.line_to(gpui_kit::point(o.x + w, o.y + h));
path.close();
if let Ok(path) = path.build() {
window.paint_path(path, fill);
}
},
)
.absolute()
.top(rem(-7.))
.left(rem(14.))
.w(rem(14.))
.h(rem(7.))
}
#[allow(clippy::too_many_arguments)]
fn anchored_menu_impl(
id: &ElementId,
trigger: AnyElement,
spec: MenuSpec,
end: bool,
full_height: bool,
return_to: Option<FocusHandle>,
centered_width: Option<f32>,
offset: Shift,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
let anchor = super::overlay::drop_anchor((id.clone(), "drop"), window, cx);
let trigger = div()
.relative()
.when(full_height, |this| this.h_full())
.child(trigger)
.child(anchor.measure_trigger());
let side = anchor.place_measured(window);
let popup = gpui_kit::base::Popup::new((id.clone(), "popup"), trigger)
.anchor(if centered_width.is_some() {
side.anchor_center()
} else if end {
side.anchor_end()
} else {
side.anchor()
})
.when(full_height, |this| this.h_full())
.when(offset.dx < 0., |this| {
this.ml(gpui_kit::px(offset.dx))
.pl(gpui_kit::px(-offset.dx))
})
.offset(gpui_kit::px(offset.gap));
let surface =
menu_surface_returning(id, spec, return_to, window, cx).map(
|surface| match centered_width {
Some(width) => div().w(rem(width)).child(surface).into_any_element(),
None => surface,
},
);
let surface = surface.map(|surface| {
if offset.caret && side == super::overlay::DropSide::Below {
div()
.relative()
.child(surface)
.child(breadcrumb_caret())
.into_any_element()
} else {
surface
}
});
match surface {
Some(surface) => popup.content(anchor.surface(surface)).into_any_element(),
None => popup.into_any_element(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
fn hex(c: Hsla) -> String {
let c = c.to_rgb();
let b = |v: f32| (v * 255.).round() as u8;
format!("#{:02x}{:02x}{:02x}", b(c.r), b(c.g), b(c.b))
}
fn rows(items: &[MenuItem]) -> Vec<Row> {
let mut out = Vec::new();
flatten(items, "", &HashMap::new(), &mut out);
out
}
#[test]
fn item_heights_follow_menu_size_and_density() {
let c = Density::Comfortable;
assert_eq!(
MenuSize::ALL.map(|s| s.item_height(c)),
[24., 32., 40., 48.]
);
assert_eq!(MenuSize::Lg.item_height(Density::Compact), 32.);
let g = MenuGeometry::of(false);
assert_eq!(
(g.padding_block, g.min_width, g.max_width),
(4., 160., 288.)
);
assert_eq!(MenuGeometry::of(true).min_width, 192.);
}
#[test]
fn item_colors_follow_menu_item_rules() {
crate::theme::tests_set(CarbonTheme::White);
let (bg, text, outline) = MenuLook::item(ItemState::default());
assert_eq!((bg.a, hex(text), outline), (0., "#525252".into(), None));
let (bg, text, _) = MenuLook::item(ItemState {
hovered: true,
..Default::default()
});
assert_eq!((hex(bg), hex(text)), ("#e8e8e8".into(), "#161616".into()));
let (bg, text, _) = MenuLook::item(ItemState {
hovered: true,
danger: true,
..Default::default()
});
assert_eq!((hex(bg), hex(text)), ("#da1e28".into(), "#ffffff".into()));
let (_, _, outline) = MenuLook::item(ItemState {
focused: true,
..Default::default()
});
assert_eq!(outline.map(hex).as_deref(), Some("#0f62fe"));
let (bg, _, _) = MenuLook::item(ItemState {
hovered: true,
disabled: true,
..Default::default()
});
assert_eq!(bg.a, 0.);
assert_eq!(hex(MenuLook::current().background), "#f4f4f4");
}
#[test]
fn danger_items_are_red_at_rest_and_dividers_use_border_subtle_00() {
let danger = ItemState {
danger: true,
..Default::default()
};
crate::theme::tests_set(CarbonTheme::White);
let (bg, text, _) = MenuLook::item(danger);
assert_eq!((bg.a, hex(text)), (0., "#525252".into()));
assert_eq!(hex(MenuLook::label(danger)), "#da1e28");
let hovered = ItemState {
hovered: true,
..danger
};
assert_eq!(hex(MenuLook::label(hovered)), "#ffffff");
assert_eq!(hex(MenuLook::current().divider), "#e0e0e0");
let (_, text, _) = MenuLook::item(ItemState {
disabled: true,
..danger
});
assert_eq!(text, color(ColorRole::TextDisabled));
crate::theme::tests_set(CarbonTheme::G90);
assert_eq!(hex(MenuLook::label(danger)), "#ffb3b8");
crate::theme::tests_set(CarbonTheme::G100);
assert_eq!(hex(MenuLook::label(danger)), "#ff8389");
crate::theme::tests_set(CarbonTheme::G90);
assert_eq!(hex(MenuLook::current().divider), "#525252");
crate::theme::tests_set(CarbonTheme::White);
}
#[test]
fn keyboard_skips_disabled_items_and_dividers_and_wraps() {
let rows = rows(&[
MenuItem::item("Restart"),
MenuItem::item("Resize").disabled(true),
MenuItem::divider(),
MenuItem::item("Delete").danger(true),
]);
assert_eq!(step(&rows, None, 1), Some(0));
assert_eq!(step(&rows, Some(0), 1), Some(3));
assert_eq!(step(&rows, Some(3), 1), Some(0));
assert_eq!(step(&rows, None, -1), Some(3));
assert_eq!(step(&rows, Some(0), -1), Some(3));
}
#[test]
fn typeahead_finds_the_next_match() {
let rows = rows(&[
MenuItem::item("Restart"),
MenuItem::item("Resize"),
MenuItem::item("Move to"),
]);
assert_eq!(find_typed(&rows, None, "r"), Some(0));
assert_eq!(find_typed(&rows, Some(0), "r"), Some(1));
assert_eq!(find_typed(&rows, Some(1), "r"), Some(0));
assert_eq!(find_typed(&rows, Some(0), "res"), Some(0));
assert_eq!(find_typed(&rows, Some(0), "resi"), Some(1));
assert_eq!(find_typed(&rows, None, "m"), Some(2));
}
#[test]
fn selectable_and_radio_rows_carry_their_state() {
let rows = rows(&[
MenuItem::checkbox("Show archived").default_selected(true),
MenuItem::divider(),
MenuItem::radio_group("Sort", ["Name", "Date"]).default_selected_item(1),
]);
let checked: Vec<_> = rows
.iter()
.map(|r| match r {
Row::Entry { checked, .. } => *checked,
Row::Divider => None,
})
.collect();
assert_eq!(checked, [Some(true), None, Some(false), Some(true)]);
}
#[test]
fn opening_focuses_the_first_enabled_item_or_the_picked_option() {
let actions = rows(&[
MenuItem::item("Restart").disabled(true),
MenuItem::item("Resize"),
MenuItem::divider(),
MenuItem::item("Delete").danger(true),
]);
assert_eq!(initial_row(&actions), Some(1));
let mixed = rows(&[
MenuItem::checkbox("Show archived"),
MenuItem::divider(),
MenuItem::radio_group("Sort", ["Name", "Date"]).default_selected_item(1),
]);
assert_eq!(initial_row(&mixed), Some(0));
let select = rows(&[MenuItem::radio_group("Page", ["1", "2", "3"]).selected_item(Some(2))]);
assert_eq!(initial_row(&select), Some(2));
let none = rows(&[MenuItem::radio_group("Page", ["1", "2"]).selected_item(None)]);
assert_eq!(initial_row(&none), Some(0));
}
}