use gpui_kit::{Action, App, KeyBinding, Menu, MenuItem};
use subspace_dashboard::{AddToChat, app_keys, prefs, secondary, view_keys};
use super::{
CloseBoardHelp, CloseHelp, CloseJig, MoveTerminalLeft, MoveTerminalRight, MoveTerminalToBottom,
NextDashboard, OpenAbout, OpenLogView, OpenSearchView, OpenSettings, PaletteBackspace,
PaletteEscape, PreviousDashboard, Quit, Reload, SearchItems, ToggleChat, ToggleHelp, ToggleJig,
TogglePalette, ToggleShortcuts, ToggleTerminal, ToggleTerminalFullWidth,
ToggleTerminalMaximized, ToggleWrap,
};
#[derive(Action, Clone, PartialEq)]
#[action(namespace = subspace_app, no_json)]
pub struct ShowDashboard {
pub index: usize,
}
const FUNCTION_KEYS: [&str; 12] = [
"f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12",
];
const BOARD_KEYS: [&str; 9] = [
secondary!("1"),
secondary!("2"),
secondary!("3"),
secondary!("4"),
secondary!("5"),
secondary!("6"),
secondary!("7"),
secondary!("8"),
secondary!("9"),
];
pub const APP_CONTEXT: &str = "Subspace";
pub const HELP_CONTEXT: &str = "SubspaceHelp";
pub const BOARD_HELP_CONTEXT: &str = "SubspaceBoardHelp";
pub const PALETTE_CONTEXT: &str = "SubspacePalette";
const PALETTE_INPUT_CONTEXT: &str = "SubspacePalette > Input";
fn bind_reference_keys(cx: &mut App) {
let shell = view_keys(APP_CONTEXT);
let shell = Some(shell.as_str());
cx.bind_keys([
KeyBinding::new(secondary!("?"), ToggleShortcuts, None),
KeyBinding::new(secondary!("shift-/"), ToggleShortcuts, None),
KeyBinding::new("w", ToggleWrap, shell),
KeyBinding::new("r", Reload, shell),
KeyBinding::new("?", ToggleHelp, shell),
KeyBinding::new("`", ToggleJig, shell),
KeyBinding::new("/", SearchItems, shell),
KeyBinding::new("escape", CloseHelp, Some(HELP_CONTEXT)),
KeyBinding::new("escape", CloseHelp, Some("SubspaceHelp > Input")),
KeyBinding::new(
secondary!("f"),
super::help_dock::FocusShortcutFilter,
Some(HELP_CONTEXT),
),
KeyBinding::new("escape", CloseBoardHelp, Some(BOARD_HELP_CONTEXT)),
KeyBinding::new("escape", CloseJig, Some(crate::jig::JIG_CONTEXT)),
KeyBinding::new("escape", PaletteEscape, Some(PALETTE_INPUT_CONTEXT)),
KeyBinding::new("backspace", PaletteBackspace, Some(PALETTE_INPUT_CONTEXT)),
]);
}
pub fn bind_keys(cx: &mut App) {
bind_reference_keys(cx);
#[cfg(target_os = "linux")]
cx.bind_keys([
KeyBinding::new("ctrl-shift-q", Quit, None),
KeyBinding::new("ctrl-shift-k", TogglePalette, None),
KeyBinding::new("ctrl-shift-r", Reload, None),
KeyBinding::new("ctrl-shift-,", OpenSettings, None),
KeyBinding::new("super-shift-c", ToggleChat, None),
KeyBinding::new("ctrl-shift-a", AddToChat, None),
KeyBinding::new("ctrl-shift-f", OpenSearchView, None),
KeyBinding::new("ctrl-shift-l", OpenLogView, None),
]);
#[cfg(not(target_os = "linux"))]
cx.bind_keys([
KeyBinding::new(secondary!("q"), Quit, None),
KeyBinding::new(secondary!("k"), TogglePalette, None),
KeyBinding::new(secondary!("r"), Reload, None),
KeyBinding::new(secondary!(","), OpenSettings, None),
KeyBinding::new(secondary!("shift-c"), ToggleChat, None),
KeyBinding::new(secondary!("shift-a"), AddToChat, None),
KeyBinding::new(secondary!("shift-f"), OpenSearchView, None),
KeyBinding::new(secondary!("shift-l"), OpenLogView, None),
]);
cx.bind_keys(
BOARD_KEYS
.into_iter()
.enumerate()
.map(|(index, keys)| KeyBinding::new(keys, ShowDashboard { index }, None)),
);
let app_context = app_keys();
let app_context = Some(app_context.as_str());
cx.bind_keys(
FUNCTION_KEYS
.into_iter()
.enumerate()
.map(|(index, keys)| KeyBinding::new(keys, ShowDashboard { index }, app_context)),
);
}
pub fn save_prefs(cx: &mut App) {
if let Err(error) = prefs::save(cx) {
eprintln!("subspace: could not save the state file: {error}");
}
}
pub fn palette_key() -> &'static str {
if cfg!(target_os = "macos") {
"⌘K"
} else if cfg!(target_os = "linux") {
"Ctrl+Shift+K"
} else {
"Ctrl K"
}
}
pub fn shortcuts_key() -> &'static str {
if cfg!(target_os = "macos") {
"⌘⇧/"
} else {
"Ctrl+Shift+/"
}
}
pub fn settings_key() -> &'static str {
if cfg!(target_os = "macos") {
"⌘,"
} else if cfg!(target_os = "linux") {
"Ctrl+Shift+,"
} else {
"Ctrl+,"
}
}
pub fn add_to_chat_key() -> &'static str {
if cfg!(target_os = "macos") {
"⌘⇧A"
} else {
"Ctrl+Shift+A"
}
}
pub fn chat_key() -> &'static str {
if cfg!(target_os = "macos") {
"⌘⇧C"
} else if cfg!(target_os = "linux") {
"Super+Shift+C"
} else {
"Ctrl+Shift+C"
}
}
pub(crate) fn install_menus(cx: &mut App) {
let Some(product) = cx.try_global::<subspace_dashboard::CurrentProduct>() else {
return;
};
let name = product.0.product.display_name.clone();
#[cfg_attr(not(debug_assertions), expect(unused_mut))]
let mut menus = menus(&name, crate::terminal::registration(cx).is_some());
#[cfg(debug_assertions)]
if crate::settings::developer_mode(cx) {
menus.push(crate::inspector::menu());
}
cx.set_menus(menus);
}
pub fn menus(name: &str, terminal: bool) -> Vec<Menu> {
let terminal_items = terminal.then(|| {
[
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),
])),
]
});
vec![
Menu::new(name.to_owned()).items([
MenuItem::action("Command palette", TogglePalette),
MenuItem::action("Search items", SearchItems),
MenuItem::action("Search content", OpenSearchView),
MenuItem::action("Show logs", OpenLogView),
MenuItem::action("Help", ToggleHelp),
MenuItem::action("Keyboard Shortcuts", ToggleShortcuts),
MenuItem::action("Settings…", OpenSettings),
MenuItem::action("About…", OpenAbout),
MenuItem::separator(),
MenuItem::action(format!("Quit {name}"), Quit),
]),
Menu::new("View").items(
[
MenuItem::action("Reload", Reload),
MenuItem::action("Wrap titles", ToggleWrap),
MenuItem::action("Theme designer", ToggleJig),
MenuItem::action("Chat", ToggleChat),
]
.into_iter()
.chain(terminal_items.into_iter().flatten())
.chain([
MenuItem::separator(),
MenuItem::action("Next Space", NextDashboard),
MenuItem::action("Previous Space", PreviousDashboard),
]),
),
]
}
#[cfg(test)]
mod tests {
use super::{
ShowDashboard, add_to_chat_key, bind_keys, chat_key, menus, palette_key, settings_key,
};
use gpui_kit::{Keystroke, TestAppContext};
#[gpui_kit::test]
fn digit_and_function_keys_show_dashboards_in_switcher_order(cx: &mut TestAppContext) {
cx.update(|cx| {
bind_keys(cx);
let os = cx.platform_info().os;
for (keys, index) in [
("secondary-1", 0),
("secondary-9", 8),
("f1", 0),
("f12", 11),
] {
let input = [Keystroke::parse_with_os(keys, os).unwrap()];
let bindings = cx.all_bindings_for_input(&input);
assert!(
bindings
.iter()
.any(|binding| binding.action().partial_eq(&ShowDashboard { index })),
"{keys} shows Space {index}"
);
}
});
}
#[test]
fn the_view_menu_lists_the_terminal_only_for_a_product_with_one() {
let names = |terminal| {
menus("Subspace", terminal)[1]
.items
.iter()
.filter_map(|item| match item {
gpui_kit::MenuItem::Action { name, .. } => Some(name.to_string()),
_ => None,
})
.collect::<Vec<_>>()
};
assert!(!names(false).iter().any(|name| name.contains("erminal")));
let with = names(true);
assert!(with.contains(&"Terminal".to_owned()));
let layout = menus("Subspace", true)[1]
.items
.iter()
.any(|item| matches!(item, gpui_kit::MenuItem::Submenu(menu) if menu.name == "Terminal layout"));
assert!(layout);
}
#[test]
pub(super) fn the_shown_keys_follow_the_platform() {
let mac = cfg!(target_os = "macos");
assert_eq!(palette_key() == "⌘K", mac);
assert_eq!(settings_key() == "⌘,", mac);
assert_eq!(add_to_chat_key() == "⌘⇧A", mac);
assert_eq!(chat_key() == "⌘⇧C", mac);
if cfg!(target_os = "linux") {
assert_eq!(chat_key(), "Super+Shift+C");
assert_eq!(palette_key(), "Ctrl+Shift+K");
}
}
}