A horizontal bar of menus where hovering another trigger switches the open menu.
Loading GPUI…
use gpui::{div, prelude::*, px, Context, Window};
use crate::ui::dropdown_menu::MenuItem;
use crate::ui::menubar::{Menubar, MenubarMenu};
#[derive(Default)]
pub struct AppMenubar {
open: Option<usize>,
}
impl Render for AppMenubar {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
// Top-aligned so the menus have room to open downward in the preview.
div().h(px(300.0)).flex().items_start().child(
Menubar::new("app-menubar", self.open)
.on_open_change(cx.listener(|this, open: &Option<usize>, _, cx| {
this.open = *open;
cx.notify();
}))
.menu(
MenubarMenu::new("File")
.item(MenuItem::new("New Tab").shortcut("Ctrl+T"))
.item(MenuItem::new("New Window").shortcut("Ctrl+N"))
.item(MenuItem::new("New Incognito Window").disabled(true))
.separator()
.item(MenuItem::new("Share"))
.separator()
.item(MenuItem::new("Print...").shortcut("Ctrl+P")),
)
.menu(
MenubarMenu::new("Edit")
.item(MenuItem::new("Undo").shortcut("Ctrl+Z"))
.item(MenuItem::new("Redo").shortcut("Ctrl+Y"))
.separator()
.item(MenuItem::new("Cut"))
.item(MenuItem::new("Copy"))
.item(MenuItem::new("Paste")),
)
.menu(
MenubarMenu::new("View")
.item(MenuItem::new("Reload").shortcut("Ctrl+R"))
.item(MenuItem::new("Force Reload").shortcut("Ctrl+Shift+R").disabled(true))
.separator()
.item(MenuItem::new("Toggle Fullscreen"))
.separator()
.item(MenuItem::new("Hide Sidebar")),
)
.menu(
MenubarMenu::new("Profiles")
.label("Switch profile")
.item(MenuItem::new("Andy"))
.item(MenuItem::new("Benoit"))
.item(MenuItem::new("Luis"))
.separator()
.item(MenuItem::new("Edit...")),
),
)
}
}Installation
$ cargo hyuskui add menubar
Copies src/ui/menubar.rs into your project, along with any components it depends on, and registers the module in src/ui/mod.rs.
Install the theme
Every component reads colors from
crate::theme. Follow the installation guide if you have not added it yet.Copy the source
Paste the code below into
src/ui/menubar.rs.src/ui/menubar.rs use std::{rc::Rc, sync::Arc}; use gpui::{div, prelude::*, px, App, ElementId, FontWeight, SharedString, Window}; use crate::theme::ActiveTheme; use crate::ui::dropdown_menu::{menu_list, MenuEntry, MenuItem}; use crate::ui::popover::{Align, Popover, Side}; type OpenChangeHandler = Rc<dyn Fn(&Option<usize>, &mut Window, &mut App) + 'static>; /// One top-level menu: its trigger label and the rows it opens. pub struct MenubarMenu { label: SharedString, entries: Vec<MenuEntry>, } impl MenubarMenu { pub fn new(label: impl Into<SharedString>) -> Self { Self { label: label.into(), entries: Vec::new(), } } pub fn item(mut self, item: MenuItem) -> Self { self.entries.push(MenuEntry::Item(item)); self } /// A non-interactive heading. pub fn label(mut self, label: impl Into<SharedString>) -> Self { self.entries.push(MenuEntry::Label(label.into())); self } pub fn separator(mut self) -> Self { self.entries.push(MenuEntry::Separator); self } } /// A horizontal bar of menus, like a desktop app's File / Edit / View. The parent view owns /// which menu is open. Once one is open, hovering another trigger switches to it. #[derive(IntoElement)] pub struct Menubar { id: ElementId, open: Option<usize>, menus: Vec<MenubarMenu>, on_open_change: Option<OpenChangeHandler>, } impl Menubar { pub fn new(id: impl Into<ElementId>, open: Option<usize>) -> Self { Self { id: id.into(), open, menus: Vec::new(), on_open_change: None, } } pub fn menu(mut self, menu: MenubarMenu) -> Self { self.menus.push(menu); self } /// Receives the index of the menu to open next, or `None` to close. pub fn on_open_change( mut self, handler: impl Fn(&Option<usize>, &mut Window, &mut App) + 'static, ) -> Self { self.on_open_change = Some(Rc::new(handler)); self } } impl RenderOnce for Menubar { fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { let theme = cx.theme(); let (accent, accent_fg, border, bg) = (theme.accent, theme.accent_foreground, theme.border, theme.background); let open = self.open; let group = Arc::new(self.id.clone()); let on_open_change = self.on_open_change; let menus: Vec<_> = self .menus .into_iter() .enumerate() .map(|(ix, menu)| { let is_open = open == Some(ix); let close = on_open_change.clone().map(|handler| { Rc::new(move |next: &bool, window: &mut Window, cx: &mut App| { handler(&next.then_some(ix), window, cx) }) as Rc<dyn Fn(&bool, &mut Window, &mut App)> }); let list = menu_list(menu.entries, close.clone(), cx); let hover = on_open_change.clone(); let trigger = div() .id(("menubar-trigger", ix)) .flex() .items_center() .h(px(28.0)) .px_2() .rounded(px(4.0)) .text_sm() .font_weight(FontWeight::MEDIUM) .cursor_pointer() .when(is_open, |this| this.bg(accent).text_color(accent_fg)) .hover(move |style| style.bg(accent).text_color(accent_fg)) // Radix menubar behavior: while any menu is open, pointing at another // trigger opens that one instead. .when_some(hover.filter(|_| open.is_some() && !is_open), |this, handler| { this.on_hover(move |hovered, window, cx| { if *hovered { handler(&Some(ix), window, cx); } }) }) .child(menu.label); Popover::new(ElementId::NamedChild(group.clone(), format!("menu-{ix}").into()), is_open) .side(Side::Bottom) .align(Align::Start) .width(px(192.0)) .padded(false) .trigger(trigger) .when_some(close, |this, close| { this.on_open_change(move |next, window, cx| close(next, window, cx)) }) .child(list) }) .collect(); div() .id(self.id) .flex() .items_center() .gap_1() .h(px(36.0)) .p_1() .rounded(theme.radius) .border_1() .border_color(border) .bg(bg) .shadow_xs() .children(menus) } }Register the module
// src/ui/mod.rs pub mod menubar;
Usage
use crate::ui::menubar::*;API Reference
MenubarMenu
One top-level menu: its trigger label and the rows it opens.
Method
MenubarMenu::new(label: impl Into<SharedString>) -> Self.item(item: MenuItem).label(label: impl Into<SharedString>)A non-interactive heading.
.separator()Menubar
A horizontal bar of menus, like a desktop app's File / Edit / View. The parent view owns which menu is open. Once one is open, hovering another trigger switches to it.
Method
Menubar::new(id: impl Into<ElementId>, open: Option<usize>) -> Self.menu(menu: MenubarMenu).on_open_change(handler: impl Fn(&Option<usize>, &mut Window, &mut App) + 'static)Receives the index of the menu to open next, or None to close.