use super::overlay::{Callback, rem};
use crate::{
icons::{CarbonIcon, Icon, IconSize},
motion::{self, Curve, Motion, Speed},
theme::color,
tokens::{ColorRole, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
Anchor, AnyElement, App, BoxShadow, CursorStyle, ElementId, Entity, FocusHandle, FontWeight,
Hsla, InteractiveElement, IntoElement, KeyDownEvent, ParentElement, RenderOnce, SharedString,
StatefulInteractiveElement as _, Styled, TestSupportExt as _, Window, div, hsla,
prelude::FluentBuilder as _, px,
};
use gpui_kit::{KeyBinding, actions};
use std::rc::Rc;
actions!(
u0ui_navigation_menu,
[
TabNext,
TabPrev
]
);
const KEY_CONTEXT: &str = "U0NavigationMenu";
pub(crate) fn init(cx: &mut App) {
cx.bind_keys([
KeyBinding::new("tab", TabNext, Some(KEY_CONTEXT)),
KeyBinding::new("shift-tab", TabPrev, Some(KEY_CONTEXT)),
]);
}
pub const ITEM_MOTION: Motion = Motion::new(Curve::ProductiveStandard, Speed::Fast02);
pub const HEADER_HEIGHT: f32 = 48.;
pub const MENU_WIDTH: f32 = 200.;
#[derive(Clone)]
pub struct NavItem {
label: SharedString,
active: bool,
on_click: Option<Callback>,
items: Vec<NavItem>,
}
impl NavItem {
pub fn link(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
active: false,
on_click: None,
items: Vec::new(),
}
}
pub fn menu(label: impl Into<SharedString>, items: impl IntoIterator<Item = NavItem>) -> Self {
Self {
items: items.into_iter().collect(),
..Self::link(label)
}
}
pub fn active(mut self, active: bool) -> Self {
self.active = active;
self
}
pub fn on_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
}
pub fn item_colors(in_menu: bool, current: bool, hovered: bool, pressed: bool) -> (Hsla, Hsla) {
use ColorRole::*;
if in_menu {
let bg = match (current, hovered, pressed) {
(_, _, true) => LayerActive01,
(true, true, _) => LayerSelectedHover01,
(true, false, _) => LayerSelected01,
(false, true, _) => LayerHover01,
_ => Layer01,
};
let text = if hovered || pressed || current {
TextPrimary
} else {
TextSecondary
};
return (color(bg), color(text));
}
let bg = if pressed {
BackgroundActive
} else if hovered {
BackgroundHover
} else {
Background
};
let text = if hovered || pressed {
TextPrimary
} else {
TextSecondary
};
(color(bg), color(text))
}
type KeyHandler = Rc<dyn Fn(&KeyDownEvent, &mut Window, &mut App)>;
type SetMenu = Rc<dyn Fn(bool, Option<usize>, &mut Window, &mut App)>;
const MENU_SHADOW_REACH: f32 = 4. + 3. * 8.;
pub fn menu_shadow() -> Vec<BoxShadow> {
vec![BoxShadow::new(px(0.), px(4.), hsla(0., 0., 0., 0.5)).blur_radius(px(8.))]
}
const NAME_SHRINK: f32 = 1000.;
#[derive(IntoElement)]
pub struct NavigationMenu {
id: ElementId,
brand_prefix: SharedString,
brand: SharedString,
on_brand_click: Option<Callback>,
items: Vec<NavItem>,
actions: Vec<AnyElement>,
}
impl NavigationMenu {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
brand_prefix: SharedString::default(),
brand: SharedString::default(),
on_brand_click: None,
items: Vec::new(),
actions: Vec::new(),
}
}
pub fn brand_prefix(mut self, prefix: impl Into<SharedString>) -> Self {
self.brand_prefix = prefix.into();
self
}
pub fn brand(mut self, brand: impl Into<SharedString>) -> Self {
self.brand = brand.into();
self
}
pub fn on_brand_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_brand_click = Some(Rc::new(f));
self
}
pub fn item(mut self, item: NavItem) -> Self {
self.items.push(item);
self
}
pub fn action(mut self, action: impl IntoElement) -> Self {
self.actions.push(action.into_any_element());
self
}
}
#[derive(Default)]
struct Pointer {
hovered: bool,
pressed: bool,
}
#[allow(clippy::too_many_arguments)]
fn header_item(
id: ElementId,
label: SharedString,
current: bool,
in_menu: bool,
expanded: Option<bool>,
focus: FocusHandle,
on_click: Callback,
on_key: Option<KeyHandler>,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
let pointer: Entity<Pointer> =
window.use_keyed_state((id.clone(), "pointer"), cx, |_, _| Pointer::default());
let (hovered, pressed) = {
let p = pointer.read(cx);
(p.hovered, p.pressed)
};
let (mut bg, text) = item_colors(in_menu, current, hovered, pressed);
if expanded == Some(true) {
bg = color(ColorRole::Layer01);
}
let bg = motion::animate_color((id.clone(), "bg"), bg, ITEM_MOTION, window, cx);
let text = motion::animate_color((id.clone(), "text"), text, ITEM_MOTION, window, cx);
let focused = focus.is_focused(window);
let rotation =
expanded.map(|open| motion::chevron_rotation((id.clone(), "arrow"), open, window, cx));
gpui_kit::base::Button::new(id.clone())
.track_focus(&focus)
.accessibility_label(label.clone())
.relative()
.map(|this| {
if in_menu {
this.h(rem(HEADER_HEIGHT)).w_full()
} else {
this.h_full()
}
})
.flex()
.flex_row()
.items_center()
.justify_start()
.px(rem(16.))
.bg(bg)
.text_color(text)
.type_style(TypeStyle::BodyCompact01)
.whitespace_nowrap()
.border_2()
.border_color(if focused {
color(ColorRole::Focus)
} else {
Hsla::transparent_black()
})
.cursor(CursorStyle::PointingHand)
.on_hover({
let pointer = pointer.clone();
move |h, _, cx| {
pointer.update(cx, |p, cx| {
p.hovered = *h;
if !*h {
p.pressed = false;
}
cx.notify();
})
}
})
.on_mouse_down(gpui_kit::MouseButton::Left, {
let pointer = pointer.clone();
move |_, _, cx| {
pointer.update(cx, |p, cx| {
p.pressed = true;
cx.notify();
})
}
})
.on_mouse_up(gpui_kit::MouseButton::Left, move |_, _, cx| {
pointer.update(cx, |p, cx| {
p.pressed = false;
cx.notify();
})
})
.on_click(move |_, window, cx| on_click(window, cx))
.when_some(on_key, |this, on_key| {
this.on_key_down(move |event, window, cx| on_key(event, window, cx))
})
.child(label)
.when_some(rotation, |this, rotation| {
this.child(
div().ml(rem(8.)).child(
Icon::carbon(CarbonIcon::ChevronDown)
.size(IconSize::S16)
.color(color(ColorRole::IconSecondary))
.rotate(gpui_kit::radians(rotation)),
),
)
})
.when(current, |this| {
this.child(if in_menu {
div()
.absolute()
.top(px(-2.))
.bottom(px(-2.))
.left(px(-2.))
.w(px(3.))
.bg(color(ColorRole::BorderInteractive))
} else {
div()
.absolute()
.left(px(-2.))
.right(px(-2.))
.bottom(px(-2.))
.h(px(3.))
.bg(color(ColorRole::BorderInteractive))
})
})
.into_any_element()
}
struct MenuState {
open: bool,
title: FocusHandle,
items: Vec<FocusHandle>,
}
impl RenderOnce for NavigationMenu {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let mut nav = div().flex().flex_row().h_full();
for (index, item) in self.items.into_iter().enumerate() {
let item_id = ElementId::NamedChild(
std::sync::Arc::new(id.clone()),
SharedString::from(format!("item-{index}")),
);
if item.items.is_empty() {
let focus = window
.use_keyed_state((item_id.clone(), "focus"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
let on_click = item.on_click.clone();
nav = nav.child(header_item(
item_id,
item.label,
item.active,
false,
None,
focus,
Rc::new(move |window, cx| {
if let Some(f) = &on_click {
f(window, cx)
}
}),
None,
window,
cx,
));
continue;
}
let count = item.items.len();
let state = window.use_keyed_state((item_id.clone(), "menu"), cx, |_, cx| MenuState {
open: false,
title: cx.focus_handle(),
items: Vec::new(),
});
if state.read(cx).items.len() < count {
state.update(cx, |s, cx| {
while s.items.len() < count {
s.items.push(cx.focus_handle());
}
});
}
let (open, title_focus, item_focus) = {
let s = state.read(cx);
(s.open, s.title.clone(), s.items.clone())
};
let set_open: SetMenu = {
let state = state.clone();
Rc::new(move |open, focus_item, window, cx| {
let (title, items) = state.update(cx, |s, cx| {
s.open = open;
cx.notify();
(s.title.clone(), s.items.clone())
});
match (open, focus_item) {
(true, Some(i)) => {
let handle = items[i].clone();
window.defer(cx, move |window, cx| handle.focus(window, cx));
}
(false, _) if items.iter().any(|h| h.is_focused(window)) => {
title.focus(window, cx)
}
_ => {}
}
window.refresh();
})
};
let title = header_item(
(item_id.clone(), "title").into(),
item.label.clone(),
item.active || item.items.iter().any(|i| i.active),
false,
Some(open),
title_focus.clone(),
{
let set_open = set_open.clone();
Rc::new(move |window, cx| set_open(!open, None, window, cx))
},
Some({
let set_open = set_open.clone();
Rc::new(
move |event: &KeyDownEvent, window: &mut Window, cx: &mut App| {
match event.keystroke.key.as_str() {
"down" => set_open(true, Some(0), window, cx),
"escape" if open => set_open(false, None, window, cx),
_ => return,
}
cx.stop_propagation();
},
)
}),
window,
cx,
);
let on_tab = {
let set_open = set_open.clone();
let title = title_focus.clone();
let items = item_focus.clone();
move |forward: bool, window: &mut Window, cx: &mut App| {
let at = if title.is_focused(window) {
None
} else if let Some(i) = items.iter().position(|h| h.is_focused(window)) {
Some(i)
} else {
cx.propagate();
return;
};
match (at, forward) {
(None, _) if !open => {
cx.propagate();
return;
}
(None, true) => set_open(true, Some(0), window, cx),
(None, false) => {
set_open(false, None, window, cx);
window.focus_prev(cx);
}
(Some(i), true) if i + 1 < count => set_open(true, Some(i + 1), window, cx),
(Some(_), true) => {
set_open(false, None, window, cx);
for _ in 0..=count {
window.focus_next(cx);
if !items.iter().any(|h| h.is_focused(window)) {
break;
}
}
}
(Some(0), false) => set_open(false, None, window, cx),
(Some(i), false) => set_open(true, Some(i - 1), window, cx),
}
cx.stop_propagation();
}
};
let on_tab = Rc::new(on_tab);
let popup = gpui_kit::base::Popup::new((item_id.clone(), "popup"), title)
.anchor(Anchor::TopLeft)
.h_full()
.key_context(KEY_CONTEXT)
.on_action({
let on_tab = on_tab.clone();
move |_: &TabNext, window, cx| on_tab(true, window, cx)
})
.on_action(move |_: &TabPrev, window, cx| on_tab(false, window, cx));
if !open {
nav = nav.child(popup);
continue;
}
let mut menu = div()
.id((item_id.clone(), "list"))
.w(rem(MENU_WIDTH))
.flex()
.flex_col()
.bg(color(ColorRole::Layer01))
.shadow(menu_shadow())
.on_mouse_down_out({
let set_open = set_open.clone();
move |_, window, cx| {
if open {
set_open(false, None, window, cx)
}
}
});
for (i, link) in item.items.into_iter().enumerate() {
let on_click = link.on_click.clone();
let set_close = set_open.clone();
let set_key = set_open.clone();
menu = menu.child(header_item(
ElementId::NamedChild(
std::sync::Arc::new(item_id.clone()),
SharedString::from(format!("link-{i}")),
),
link.label,
link.active,
true,
None,
item_focus[i].clone(),
Rc::new(move |window, cx| {
if let Some(f) = &on_click {
f(window, cx)
}
set_close(false, None, window, cx);
}),
Some(Rc::new(
move |event: &KeyDownEvent, window: &mut Window, cx: &mut App| {
match event.keystroke.key.as_str() {
"down" => set_key(true, Some((i + 1) % count), window, cx),
"up" => set_key(true, Some((i + count - 1) % count), window, cx),
"escape" => set_key(false, None, window, cx),
_ => return,
}
cx.stop_propagation();
},
)),
window,
cx,
));
}
let pad = rem(MENU_SHADOW_REACH);
let menu = div()
.overflow_hidden()
.ml(rem(-MENU_SHADOW_REACH))
.pl(pad)
.pr(pad)
.pb(pad)
.child(menu.test_support());
nav = nav.child(popup.content(menu));
}
let brand_focus = window
.use_keyed_state((id.clone(), "brand-focus"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
let brand_focused = brand_focus.is_focused(window);
let on_brand = self.on_brand_click.clone();
div()
.id(id.clone())
.w_full()
.h(rem(HEADER_HEIGHT))
.flex()
.flex_row()
.items_center()
.bg(color(ColorRole::Background))
.border_b_1()
.border_color(color(ColorRole::BorderSubtle01))
.child(
gpui_kit::base::Button::new((id.clone(), "name"))
.track_focus(&brand_focus)
.accessibility_label(self.brand.clone())
.h_full()
.min_w_0()
.flex_shrink(NAME_SHRINK)
.flex()
.flex_row()
.items_center()
.pl(rem(16.))
.pr(rem(32.))
.border_2()
.border_color(if brand_focused {
color(ColorRole::Focus)
} else {
Hsla::transparent_black()
})
.type_style(TypeStyle::BodyCompact01)
.text_color(color(ColorRole::TextPrimary))
.whitespace_nowrap()
.cursor(CursorStyle::PointingHand)
.on_click(move |_, window, cx| {
if let Some(f) = &on_brand {
f(window, cx)
}
})
.when(!self.brand_prefix.is_empty(), |this| {
this.child(
div()
.flex_none()
.mr(rem(4.))
.child(self.brand_prefix.clone()),
)
})
.child(
div()
.min_w_0()
.overflow_hidden()
.text_ellipsis()
.font_weight(FontWeight::SEMIBOLD)
.child(self.brand.clone()),
),
)
.child(
div()
.relative()
.h_full()
.min_w_0()
.flex_shrink(1.)
.pl(rem(16.))
.child(
div()
.absolute()
.left_0()
.top(rem(12.))
.h(rem(24.))
.w(px(1.))
.bg(color(ColorRole::BorderSubtle01)),
)
.child(nav),
)
.child(
div()
.flex_1()
.flex_shrink_0()
.h_full()
.flex()
.flex_row()
.justify_end()
.items_start()
.bg(color(ColorRole::Background))
.children(self.actions),
)
}
}
#[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))
}
#[test]
fn item_colors_follow_header_css() {
crate::theme::tests_set(CarbonTheme::White);
let (bg, text) = item_colors(false, false, false, false);
assert_eq!((hex(bg), hex(text)), ("#ffffff".into(), "#525252".into()));
let (_, text) = item_colors(false, true, false, false);
assert_eq!(hex(text), "#525252");
let (_, text) = item_colors(false, true, true, false);
assert_eq!(hex(text), "#161616");
let (bg, _) = item_colors(true, true, false, false);
assert_eq!(hex(bg), "#e0e0e0");
let (bg, _) = item_colors(true, false, true, false);
assert_eq!(hex(bg), "#e8e8e8");
assert_eq!((HEADER_HEIGHT, MENU_WIDTH), (48., 200.));
let shadow = &menu_shadow()[0];
assert_eq!((shadow.offset.y, shadow.blur_radius), (px(4.), px(8.)));
}
#[test]
fn menu_clip_spares_the_whole_shadow() {
let shadow = &menu_shadow()[0];
let reach = px(MENU_SHADOW_REACH);
assert!(reach >= shadow.offset.x.abs() + shadow.blur_radius * 3. + shadow.spread_radius);
assert!(reach >= shadow.offset.y + shadow.blur_radius * 3. + shadow.spread_radius);
}
}