#[cfg(target_os = "macos")]
use gpui_kit::KeyBinding;
use gpui_kit::component::input::{Copy, Cut, Paste, Redo, SelectAll, Undo};
use gpui_kit::{App, Menu, MenuItem, OsAction, SystemMenuType, Window, actions};
use super::{
MoveTerminalLeft, MoveTerminalRight, MoveTerminalToBottom, NextDashboard, OpenAbout,
OpenLogView, OpenSearchView, OpenSettings, PreviousDashboard, Quit, Reload, SearchItems,
ToggleChat, ToggleHelp, ToggleJig, ToggleNavigationMode, TogglePalette, ToggleShortcuts,
ToggleSpaceInspector, ToggleSpaceNavigator, ToggleTerminal, ToggleTerminalFullWidth,
ToggleTerminalMaximized, ToggleWrap,
};
actions!(
subspace_app,
[
ToggleFullScreen,
HideApp,
HideOtherApps,
ShowAllApps,
MinimizeWindow,
ZoomWindow
]
);
pub(crate) struct MenuBar {
pub name: String,
pub terminal: bool,
pub report_issue: bool,
pub developer: Option<Menu>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Layout {
Mac,
Windows,
Linux,
}
impl Layout {
pub(crate) const fn current() -> Self {
if cfg!(target_os = "macos") {
Self::Mac
} else if cfg!(windows) {
Self::Windows
} else {
Self::Linux
}
}
}
pub(crate) fn install_menus(cx: &mut App) {
if cx
.try_global::<subspace_dashboard::CurrentProduct>()
.is_none()
{
return;
}
let bar = MenuBar {
name: crate::product::desktop_name(cx).to_owned(),
terminal: crate::terminal::registration(cx).is_some(),
report_issue: report_issue_available(cx),
developer: developer_menu(cx),
};
cx.set_menus(menus(bar, Layout::current()));
}
#[cfg(feature = "report-issue")]
fn report_issue_available(cx: &App) -> bool {
super::report_issue::available(cx)
}
#[cfg(not(feature = "report-issue"))]
fn report_issue_available(_: &App) -> bool {
false
}
#[cfg(debug_assertions)]
fn developer_menu(cx: &App) -> Option<Menu> {
crate::settings::developer_mode(cx).then(crate::inspector::menu)
}
#[cfg(not(debug_assertions))]
fn developer_menu(_: &App) -> Option<Menu> {
None
}
pub(crate) fn menus(bar: MenuBar, layout: Layout) -> Vec<Menu> {
let (first, window) = framing_menus(&bar.name, layout);
let help = help_menu(&bar.name, bar.report_issue, layout);
[first, edit_menu(), view_menu(bar.terminal, layout)]
.into_iter()
.chain(bar.developer)
.chain(window)
.chain([help])
.collect()
}
fn framing_menus(name: &str, layout: Layout) -> (Menu, Option<Menu>) {
match layout {
Layout::Mac => (app_menu(name), Some(window_menu())),
Layout::Windows => (file_menu("Exit"), None),
Layout::Linux => (file_menu("Quit"), None),
}
}
fn app_menu(name: &str) -> Menu {
Menu::new(name.to_owned()).items([
MenuItem::action(format!("About {name}"), OpenAbout),
MenuItem::separator(),
MenuItem::action("Settings…", OpenSettings),
MenuItem::separator(),
MenuItem::os_submenu("Services", SystemMenuType::Services),
MenuItem::separator(),
MenuItem::action(format!("Hide {name}"), HideApp),
MenuItem::action("Hide Others", HideOtherApps),
MenuItem::action("Show All", ShowAllApps),
MenuItem::separator(),
MenuItem::action(format!("Quit {name}"), Quit),
])
}
fn file_menu(quit: &'static str) -> Menu {
Menu::new("File").items([
MenuItem::action("Settings…", OpenSettings),
MenuItem::separator(),
MenuItem::action(quit, Quit),
])
}
fn edit_menu() -> Menu {
Menu::new("Edit").items([
MenuItem::os_action("Undo", Undo, OsAction::Undo),
MenuItem::os_action("Redo", Redo, OsAction::Redo),
MenuItem::separator(),
MenuItem::os_action("Cut", Cut, OsAction::Cut),
MenuItem::os_action("Copy", Copy, OsAction::Copy),
MenuItem::os_action("Paste", Paste, OsAction::Paste),
MenuItem::os_action("Select All", SelectAll, OsAction::SelectAll),
])
}
fn view_menu(terminal: bool, layout: Layout) -> Menu {
let full_screen = (layout != Layout::Mac).then(|| {
[
MenuItem::separator(),
MenuItem::action("Full Screen", ToggleFullScreen),
]
});
Menu::new("View").items(
[
MenuItem::action("Command Palette", TogglePalette),
MenuItem::action("Search Items", SearchItems),
MenuItem::action("Search Content", OpenSearchView),
MenuItem::separator(),
MenuItem::action("Chat", ToggleChat),
]
.into_iter()
.chain(terminal.then(terminal_items).into_iter().flatten())
.chain([
MenuItem::action("Navigator", ToggleSpaceNavigator),
MenuItem::action("Inspector", ToggleSpaceInspector),
MenuItem::action("Vertical Space Navigation", ToggleNavigationMode),
MenuItem::separator(),
MenuItem::action("Next Space", NextDashboard),
MenuItem::action("Previous Space", PreviousDashboard),
MenuItem::separator(),
MenuItem::action("Wrap Titles", ToggleWrap),
MenuItem::action("Theme Designer", ToggleJig),
MenuItem::action("Reload", Reload),
])
.chain(full_screen.into_iter().flatten()),
)
}
fn terminal_items() -> [MenuItem; 2] {
[
MenuItem::action("Terminal", ToggleTerminal),
MenuItem::submenu(Menu::new("Terminal Layout").items([
MenuItem::action("Move Left", MoveTerminalLeft),
MenuItem::action("Move to Bottom", MoveTerminalToBottom),
MenuItem::action("Move Right", MoveTerminalRight),
MenuItem::action("Full Window Width", ToggleTerminalFullWidth),
MenuItem::action("Maximize", ToggleTerminalMaximized),
])),
]
}
fn window_menu() -> Menu {
Menu::new("Window").items([
MenuItem::action("Minimize", MinimizeWindow),
MenuItem::action("Zoom", ZoomWindow),
])
}
fn help_menu(name: &str, report_issue: bool, layout: Layout) -> Menu {
let about = (layout != Layout::Mac).then(|| {
[
MenuItem::separator(),
MenuItem::action(format!("About {name}"), OpenAbout),
]
});
Menu::new("Help").items(
[
MenuItem::action(format!("{name} Help"), ToggleHelp),
MenuItem::action("Keyboard Shortcuts", ToggleShortcuts),
MenuItem::separator(),
MenuItem::action("Show Logs", OpenLogView),
]
.into_iter()
.chain(report_issue.then(report_issue_item).flatten())
.chain(about.into_iter().flatten()),
)
}
#[cfg(feature = "report-issue")]
fn report_issue_item() -> Option<MenuItem> {
Some(MenuItem::action("Report an Issue…", super::OpenReportIssue))
}
#[cfg(not(feature = "report-issue"))]
fn report_issue_item() -> Option<MenuItem> {
None
}
#[cfg(target_os = "macos")]
pub(super) fn bind_keys(cx: &mut App) {
let outside_chat = format!("!{}", subspace_dashboard::chat_input::CHAT_PANEL_CONTEXT);
let outside_chat = Some(outside_chat.as_str());
cx.bind_keys([
KeyBinding::new("cmd-h", HideApp, None),
KeyBinding::new("cmd-alt-h", HideOtherApps, None),
KeyBinding::new("cmd-m", MinimizeWindow, None),
KeyBinding::new("cmd-ctrl-f", ToggleFullScreen, None),
KeyBinding::new("cmd-shift-]", NextDashboard, outside_chat),
KeyBinding::new("cmd-}", NextDashboard, outside_chat),
KeyBinding::new("cmd-shift-[", PreviousDashboard, outside_chat),
KeyBinding::new("cmd-{", PreviousDashboard, outside_chat),
]);
}
#[cfg(not(target_os = "macos"))]
pub(super) fn bind_keys(_: &mut App) {}
pub(super) fn help_keys() -> &'static [(&'static str, &'static str)] {
if cfg!(target_os = "macos") {
&[
("⌘⇧[ / ⌘⇧]", "Previous / next Space"),
("⌃⌘F", "Enter or leave full screen"),
]
} else {
&[]
}
}
pub(super) fn register_actions(cx: &mut App) {
cx.on_action(|_: &ToggleFullScreen, cx| {
with_active_window(cx, Window::toggle_fullscreen);
});
register_mac_actions(cx);
}
#[cfg(target_os = "macos")]
fn register_mac_actions(cx: &mut App) {
cx.on_action(|_: &HideApp, cx| cx.hide());
cx.on_action(|_: &HideOtherApps, cx| cx.hide_other_apps());
cx.on_action(|_: &ShowAllApps, cx| cx.unhide_other_apps());
cx.on_action(|_: &MinimizeWindow, cx| {
with_active_window(cx, Window::minimize_window);
});
cx.on_action(|_: &ZoomWindow, cx| with_active_window(cx, Window::zoom_window));
}
#[cfg(not(target_os = "macos"))]
fn register_mac_actions(_: &mut App) {}
fn with_active_window(cx: &mut App, act: fn(&Window)) {
if let Some(window) = cx.active_window() {
cx.defer(move |cx| {
_ = window.update(cx, |_, window, _| act(window));
});
}
}
#[cfg(test)]
#[path = "menu_bar_tests.rs"]
mod tests;
#[cfg(test)]
#[path = "menu_bar_key_tests.rs"]
mod key_tests;