use gpui::{
App, Context, EventEmitter, FocusHandle, Focusable, KeyBinding, SharedString, Window, actions,
div, prelude::*, px,
};
use theme::{TextStyle, Theme, Typeset};
use crate::{
menu::{self, Item},
popover,
};
#[derive(Clone, Debug)]
pub struct Menu {
pub title: SharedString,
pub items: Vec<Item>,
}
impl Menu {
pub fn new(title: impl Into<SharedString>, items: Vec<Item>) -> Self {
Self {
title: title.into(),
items,
}
}
}
actions!(
bezel_menubar,
[PrevMenu, NextMenu, PrevItem, NextItem, Confirm, Dismiss]
);
pub const KEY_CONTEXT: &str = "Menubar";
pub fn init(cx: &mut App) {
let ctx = Some(KEY_CONTEXT);
cx.bind_keys([
KeyBinding::new("left", PrevMenu, ctx),
KeyBinding::new("right", NextMenu, ctx),
KeyBinding::new("up", PrevItem, ctx),
KeyBinding::new("down", NextItem, ctx),
KeyBinding::new("enter", Confirm, ctx),
KeyBinding::new("escape", Dismiss, ctx),
]);
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MenubarEvent {
Selected { menu: usize, item: usize },
}
pub struct Menubar {
menus: Vec<Menu>,
open: popover::Popup<usize>,
highlighted: Option<usize>,
focus_handle: FocusHandle,
}
impl EventEmitter<MenubarEvent> for Menubar {}
impl Menubar {
pub fn new(menus: Vec<Menu>, cx: &mut Context<Self>) -> Self {
Self {
menus,
open: popover::Popup::default(),
highlighted: None,
focus_handle: cx.focus_handle().tab_stop(true),
}
}
pub fn open_menu(&self) -> Option<usize> {
self.open.as_open().copied()
}
pub fn menus(&self) -> &[Menu] {
&self.menus
}
fn show(&mut self, menu: usize, window: &mut Window, cx: &mut Context<Self>) {
self.open.open(menu);
self.highlighted = None;
window.focus(&self.focus_handle, cx);
cx.notify();
}
fn toggle(&mut self, menu: usize, window: &mut Window, cx: &mut Context<Self>) {
if self.open.take_press_was_open() {
self.close(cx);
} else {
self.show(menu, window, cx);
}
}
fn hover_switch(&mut self, menu: usize, cx: &mut Context<Self>) {
if self.open.is_open() && self.open_menu() != Some(menu) {
self.open.open(menu);
self.highlighted = None;
cx.notify();
}
}
fn close(&mut self, cx: &mut Context<Self>) {
if self.open.begin_close() {
popover::reap_popup(cx, |bar: &mut Self| &mut bar.open);
}
self.highlighted = None;
cx.notify();
}
fn choose(&mut self, menu: usize, item: usize, cx: &mut Context<Self>) {
cx.emit(MenubarEvent::Selected { menu, item });
self.close(cx);
}
fn step_item(&mut self, delta: isize, cx: &mut Context<Self>) {
let Some(menu) = self.open_menu() else { return };
self.highlighted = menu::next_selectable(&self.menus[menu].items, self.highlighted, delta);
cx.notify();
}
fn step_menu(&mut self, delta: isize, cx: &mut Context<Self>) {
let Some(menu) = self.open_menu() else { return };
let count = self.menus.len() as isize;
if count == 0 {
return;
}
self.open
.open((menu as isize + delta).rem_euclid(count) as usize);
self.highlighted = None;
cx.notify();
}
fn confirm(&mut self, _: &Confirm, window: &mut Window, cx: &mut Context<Self>) {
match (self.open_menu(), self.highlighted) {
(Some(menu), Some(item)) => self.choose(menu, item, cx),
(None, _) if !self.menus.is_empty() => self.show(0, window, cx),
_ => {}
}
}
fn dismiss(&mut self, _: &Dismiss, _: &mut Window, cx: &mut Context<Self>) {
self.close(cx);
}
fn card(&self, menu: usize, theme: &Theme, cx: &mut Context<Self>) -> gpui::AnyElement {
menu::card(
theme,
SharedString::from(format!("menu-{menu}")),
&self.menus[menu].items,
self.highlighted,
cx,
move |bar, item, _, cx| bar.choose(menu, item, cx),
)
.on_mouse_down_out(cx.listener(|bar, _, _, cx| bar.close(cx)))
.into_any_element()
}
}
pub fn menubar_title(theme: &Theme, label: impl Into<SharedString>, open: bool) -> gpui::Div {
let title = div()
.px(px(8.0))
.py(px(3.0))
.rounded(px(Theme::control_radius()))
.text_style(TextStyle::Body)
.cursor_pointer()
.child(label.into());
if open {
title.bg(theme.element_active).text_color(theme.text)
} else {
title
.text_color(theme.text_muted)
.hover(|s| s.bg(theme.element_hover).text_color(theme.text))
}
}
pub fn menubar() -> gpui::Div {
div().flex().flex_row().items_center().gap(px(2.0))
}
impl Focusable for Menubar {
fn focus_handle(&self, _: &App) -> FocusHandle {
self.focus_handle.clone()
}
}
impl Render for Menubar {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = Theme::of(cx).clone();
let mounted = self.open.get().copied();
let closing = self.open.closing_since();
menubar()
.key_context(KEY_CONTEXT)
.track_focus(&self.focus_handle)
.on_action(cx.listener(|bar, _: &PrevMenu, _, cx| bar.step_menu(-1, cx)))
.on_action(cx.listener(|bar, _: &NextMenu, _, cx| bar.step_menu(1, cx)))
.on_action(cx.listener(|bar, _: &PrevItem, _, cx| bar.step_item(-1, cx)))
.on_action(cx.listener(|bar, _: &NextItem, _, cx| bar.step_item(1, cx)))
.on_action(cx.listener(Self::confirm))
.on_action(cx.listener(Self::dismiss))
.children((0..self.menus.len()).map(|menu| {
let down = mounted == Some(menu);
let card = down.then(|| self.card(menu, &theme, cx));
div()
.relative()
.id(SharedString::from(format!("menubar-title-{menu}")))
.on_mouse_down(
gpui::MouseButton::Left,
cx.listener(move |bar, _, _, _| {
bar.open.note_trigger_press_matching(|open| *open == menu)
}),
)
.on_click(cx.listener(move |bar, _, window, cx| bar.toggle(menu, window, cx)))
.on_hover(cx.listener(move |bar, hovered: &bool, _, cx| {
if *hovered {
bar.hover_switch(menu, cx);
}
}))
.child(menubar_title(&theme, self.menus[menu].title.clone(), down))
.when_some(card, |title, card| {
title.child(popover::anchored_menu_below(
SharedString::from(format!("menubar-menu-{menu}")),
card,
closing,
))
})
}))
}
}