use gpui_kit::component::input::Enter; use gpui_kit::*; use crate::i18n::tr; use super::actions::{ COMPOSER_KEY_CONTEXT as COMPOSER_CONTEXT, EditLastMessage, EditLink, InsertTableLine, NewChat, OpenSwitcher, PastePlainText, RemoveTableLine, ReplyToHovered, ScheduleSend, SetHeading1, SetHeading2, SetHeading3, SetParagraph, ToggleBold, ToggleCallMute, ToggleCode, ToggleCodeBlock, ToggleFormatBar, ToggleItalic, ToggleQuote, ToggleStrike, ToggleSubscript, ToggleSuperscript, ToggleUnderline, }; const TEXT_FIELD_CONTEXT: &str = "Input"; const OUTSIDE_COMPOSER_CONTEXT: &str = "!Composer"; const OUTSIDE_TEXT_FIELD_CONTEXT: &str = "!Input && !Composer"; const ALTERNATE_KEYS: &[(&str, &str)] = &[ ("Zoom in on shared content", "alt-shift-+"), ("Insert column or row", "ctrl-alt-+"), ("Zoom in", "ctrl-+"), ("Zoom in", "ctrl-shift-="), ("Zoom in", "ctrl-shift-+"), ("Zoom out", "ctrl-_"), ("Zoom out", "ctrl-shift--"), ]; actions!( teams, [ OpenActivity, OpenChatsTab, OpenChannelsTab, OpenSaved, OpenCalendar, OpenCallsTab, OpenAgents, OpenChatFilter, PopOutSelectedChat, FindInChat, FilterUnreadChats, FilterNonMeetingChats, FilterMeetingChats, FilterMutedChats, ClearChatFilters, PreviousConversation, NextConversation, JumpToLastReadOrNewest, GoToComposeBox, ReplyToLastMessage, PreviousUnreadConversation, NextUnreadConversation, SetPresenceAvailable, SetPresenceBusy, SetPresenceDoNotDisturb, SetPresenceBeRightBack, SetPresenceAway, AcceptAudioCall, ToggleBackgroundBlur, ToggleLiveCaptions, AcceptCall, DeclineCall, JoinMeetingFromToast, HangUpCall, ToggleCallCamera, ToggleCallShare, ToggleCallHand, OpenLobbyList, StartAudioCall, StartVideoCall, ToggleCallFullscreen, PushToTalk, ZoomShareIn, ZoomShareOut, ZoomShareReset, ShowShortcuts, OpenSettings, ZoomIn, ZoomOut, ZoomReset ] ); #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Group { General, Navigation, Composer, Calls, } impl Group { pub const ALL: [Group; 4] = [ Group::General, Group::Navigation, Group::Composer, Group::Calls, ]; pub fn title(self) -> String { match self { Group::General => tr("shortcuts.group.general"), Group::Navigation => tr("shortcuts.group.navigation"), Group::Composer => tr("shortcuts.group.composer"), Group::Calls => tr("shortcuts.group.calls"), } } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Scope { Global, OutsideComposer, GlobalOverTextFields, OutsideTextFields, Composer, } pub struct Shortcut { pub group: Group, pub label: &'static str, pub keys: &'static str, pub mac_keys: Option<&'static str>, pub scope: Scope, bind: fn(&str, Option<&str>) -> KeyBinding, } macro_rules! shortcut { ($group:ident, $scope:ident, $label:expr, $keys:expr, $mac_keys:expr, $action:expr) => { Shortcut { group: Group::$group, label: $label, keys: $keys, mac_keys: $mac_keys, scope: Scope::$scope, bind: |keys, context| KeyBinding::new(keys, $action, context), } }; } #[rustfmt::skip] pub const SHORTCUTS: &[Shortcut] = &[ shortcut!(General, OutsideComposer, "Search and switch chats", "ctrl-k", Some("cmd-k"), OpenSwitcher), shortcut!(General, Global, "New chat", "ctrl-n", Some("cmd-n"), NewChat), shortcut!(General, OutsideTextFields, "Pop out selected chat", "ctrl-enter", Some("cmd-enter"), PopOutSelectedChat), shortcut!(General, GlobalOverTextFields, "Keyboard shortcuts", "ctrl-.", Some("cmd-."), ShowShortcuts), shortcut!(General, GlobalOverTextFields, "Settings", "ctrl-,", Some("cmd-,"), OpenSettings), shortcut!(General, OutsideComposer, "Zoom in", "ctrl-=", Some("cmd-="), ZoomIn), shortcut!(General, GlobalOverTextFields, "Zoom out", "ctrl--", Some("cmd--"), ZoomOut), shortcut!(General, GlobalOverTextFields, "Reset zoom level", "ctrl-0", Some("cmd-0"), ZoomReset), shortcut!(General, Global, "Set presence status to Available", "alt-shift-q", None, SetPresenceAvailable), shortcut!(General, Global, "Set presence status to Busy", "alt-shift-y", None, SetPresenceBusy), shortcut!(General, Global, "Set presence status to Do Not Disturb", "alt-shift-u", None, SetPresenceDoNotDisturb), shortcut!(General, Global, "Set presence status to Be Right Back", "alt-shift-k", None, SetPresenceBeRightBack), shortcut!(General, Global, "Set presence status to Away", "alt-shift-w", None, SetPresenceAway), shortcut!(Navigation, GlobalOverTextFields, "Open Activity", "ctrl-1", Some("cmd-1"), OpenActivity), shortcut!(Navigation, GlobalOverTextFields, "Show Chats", "ctrl-2", Some("cmd-2"), OpenChatsTab), shortcut!(Navigation, GlobalOverTextFields, "Show Channels", "ctrl-3", Some("cmd-3"), OpenChannelsTab), shortcut!(Navigation, GlobalOverTextFields, "Open Saved", "ctrl-4", Some("cmd-4"), OpenSaved), shortcut!(Navigation, GlobalOverTextFields, "Open Calendar", "ctrl-shift-4", Some("cmd-shift-4"), OpenCalendar), shortcut!(Navigation, GlobalOverTextFields, "Show Calls", "ctrl-5", Some("cmd-5"), OpenCallsTab), shortcut!(Navigation, GlobalOverTextFields, "Open Agents", "ctrl-6", Some("cmd-6"), OpenAgents), shortcut!(Navigation, GlobalOverTextFields, "Previous chat or channel", "alt-up", None, PreviousConversation), shortcut!(Navigation, GlobalOverTextFields, "Next chat or channel", "alt-down", None, NextConversation), shortcut!(Navigation, GlobalOverTextFields, "Go to previous unread conversation", "ctrl-alt-up", None, PreviousUnreadConversation), shortcut!(Navigation, GlobalOverTextFields, "Go to next unread conversation", "ctrl-alt-down", None, NextUnreadConversation), shortcut!(Navigation, GlobalOverTextFields, "Go to a specific chat or channel", "ctrl-g", Some("cmd-g"), OpenSwitcher), shortcut!(Navigation, Global, "Go to compose box", "ctrl-r", Some("cmd-r"), GoToComposeBox), shortcut!(Navigation, GlobalOverTextFields, "Open filter", "ctrl-shift-f", Some("cmd-shift-f"), OpenChatFilter), shortcut!(Navigation, OutsideTextFields, "Filter chat list by unread chats only", "ctrl-alt-u", None, FilterUnreadChats), shortcut!(Navigation, OutsideTextFields, "Filter chat list by chats only", "ctrl-alt-c", None, FilterNonMeetingChats), shortcut!(Navigation, OutsideTextFields, "Filter chat list by meeting chats only", "ctrl-alt-b", None, FilterMeetingChats), shortcut!(Navigation, OutsideTextFields, "Filter chat list by muted chats only", "ctrl-alt-q", None, FilterMutedChats), shortcut!(Navigation, OutsideTextFields, "Remove all filters in the chat list", "ctrl-alt-z", None, ClearChatFilters), shortcut!(Composer, GlobalOverTextFields, "Find in chat", "ctrl-f", Some("cmd-f"), FindInChat), shortcut!(Composer, Global, "Reply to the message under the pointer", "alt-r", None, ReplyToHovered), shortcut!(Composer, Global, "Reply to the last message", "alt-shift-r", None, ReplyToLastMessage), shortcut!(Composer, Composer, "Edit last message", "ctrl-up", Some("cmd-up"), EditLastMessage), shortcut!(Composer, Global, "Jump to last read or newest message", "ctrl-j", Some("cmd-j"), JumpToLastReadOrNewest), shortcut!(Composer, Composer, "Bold", "ctrl-b", Some("cmd-b"), ToggleBold), shortcut!(Composer, Composer, "Italic", "ctrl-i", Some("cmd-i"), ToggleItalic), shortcut!(Composer, Composer, "Underline", "ctrl-u", Some("cmd-u"), ToggleUnderline), shortcut!(Composer, Composer, "Expand compose box", "ctrl-shift-x", Some("cmd-shift-x"), ToggleFormatBar), shortcut!(Composer, Composer, "Send (expanded compose box)", "ctrl-enter", Some("cmd-enter"), Enter { secondary: true, shift: false }), shortcut!(Composer, Composer, "Strikethrough", "ctrl-alt-x", None, ToggleStrike), shortcut!(Composer, Composer, "Superscript", "ctrl-shift-=", None, ToggleSuperscript), shortcut!(Composer, Composer, "Subscript", "ctrl-=", None, ToggleSubscript), shortcut!(Composer, Composer, "Code", "ctrl-shift-c", None, ToggleCode), shortcut!(Composer, Composer, "Insert link", "ctrl-k", Some("cmd-k"), EditLink), shortcut!(Composer, Composer, "Paste as plain text", "ctrl-shift-v", Some("cmd-shift-v"), PastePlainText), shortcut!(Composer, Composer, "Schedule send", "ctrl-shift-enter", None, ScheduleSend), shortcut!(Composer, Composer, "Insert column or row", "ctrl-alt-=", None, InsertTableLine), shortcut!(Composer, Composer, "Delete column, row, or table", "ctrl-alt--", None, RemoveTableLine), shortcut!(Composer, Composer, "Code block", "ctrl-shift-b", None, ToggleCodeBlock), shortcut!(Composer, Composer, "Quote", "ctrl-alt-4", None, ToggleQuote), shortcut!(Composer, Composer, "Heading 1", "ctrl-alt-1", None, SetHeading1), shortcut!(Composer, Composer, "Heading 2", "ctrl-alt-2", None, SetHeading2), shortcut!(Composer, Composer, "Heading 3", "ctrl-alt-3", None, SetHeading3), shortcut!(Composer, Composer, "Paragraph", "ctrl-alt-p", None, SetParagraph), shortcut!(Calls, GlobalOverTextFields, "Accept video call", "ctrl-shift-a", Some("cmd-shift-a"), AcceptCall), shortcut!(Calls, GlobalOverTextFields, "Accept audio call", "ctrl-shift-s", Some("cmd-shift-s"), AcceptAudioCall), shortcut!(Calls, GlobalOverTextFields, "Decline call", "ctrl-shift-d", Some("cmd-shift-d"), DeclineCall), shortcut!(Calls, GlobalOverTextFields, "Join the meeting from its reminder", "ctrl-shift-j", Some("cmd-shift-j"), JoinMeetingFromToast), shortcut!(Calls, GlobalOverTextFields, "Hang up or leave", "ctrl-shift-h", Some("cmd-shift-h"), HangUpCall), shortcut!(Calls, GlobalOverTextFields, "Mute or unmute", "ctrl-shift-m", None, ToggleCallMute), shortcut!(Calls, GlobalOverTextFields, "Turn camera on or off", "ctrl-shift-o", Some("cmd-shift-o"), ToggleCallCamera), shortcut!(Calls, GlobalOverTextFields, "Open the share menu or stop sharing", "ctrl-shift-e", Some("cmd-shift-e"), ToggleCallShare), shortcut!(Calls, GlobalOverTextFields, "Raise or lower hand", "ctrl-shift-k", Some("cmd-shift-k"), ToggleCallHand), shortcut!(Calls, GlobalOverTextFields, "Toggle background blur", "ctrl-shift-p", Some("cmd-shift-p"), ToggleBackgroundBlur), shortcut!(Calls, GlobalOverTextFields, "Toggle live captions", "alt-shift-c", None, ToggleLiveCaptions), shortcut!(Calls, GlobalOverTextFields, "Open the lobby list", "ctrl-shift-y", Some("cmd-shift-y"), OpenLobbyList), shortcut!(Calls, GlobalOverTextFields, "Start an audio call in this chat", "alt-shift-a", None, StartAudioCall), shortcut!(Calls, GlobalOverTextFields, "Start a video call in this chat", "alt-shift-v", None, StartVideoCall), shortcut!(Calls, GlobalOverTextFields, "Full screen", "f11", None, ToggleCallFullscreen), shortcut!(Calls, GlobalOverTextFields, "Zoom in on shared content", "alt-shift-=", None, ZoomShareIn), shortcut!(Calls, GlobalOverTextFields, "Zoom out on shared content", "alt-shift--", None, ZoomShareOut), shortcut!(Calls, GlobalOverTextFields, "Reset zoom on shared content", "alt-shift-0", None, ZoomShareReset), shortcut!(Calls, OutsideTextFields, "Hold to talk while muted", "ctrl-space", None, PushToTalk), ]; impl Shortcut { pub fn keys_for_platform(&self) -> &'static str { match self.mac_keys { Some(mac_keys) if cfg!(target_os = "macos") => mac_keys, _ => self.keys, } } pub fn display_keys(&self) -> String { display_keys(self.keys_for_platform()) } fn bindings(&self) -> Vec { let contexts: &[Option<&str>] = match self.scope { Scope::Global => &[None], Scope::OutsideComposer => &[Some(OUTSIDE_COMPOSER_CONTEXT)], Scope::GlobalOverTextFields => &[None, Some(TEXT_FIELD_CONTEXT)], Scope::OutsideTextFields => &[Some(OUTSIDE_TEXT_FIELD_CONTEXT)], Scope::Composer => &[Some(COMPOSER_CONTEXT)], }; let mut keys = vec![self.keys]; keys.extend( ALTERNATE_KEYS .iter() .filter(|(label, _)| *label == self.label) .map(|(_, alternate)| *alternate), ); keys.extend(self.mac_keys.filter(|_| cfg!(target_os = "macos"))); keys.iter() .flat_map(|keys| contexts.iter().map(|context| (self.bind)(keys, *context))) .collect() } } pub fn keys_for_label(label: &str) -> Option { SHORTCUTS .iter() .find(|shortcut| shortcut.label == label) .map(Shortcut::display_keys) } pub fn display_keys(keys: &str) -> String { let (modifiers, key) = match keys.strip_suffix("--") { Some(modifiers) => (modifiers, "-"), None => keys.rsplit_once('-').unwrap_or(("", keys)), }; modifiers .split('-') .filter(|part| !part.is_empty()) .chain([key]) .map(|part| match part { "ctrl" => "Ctrl".to_owned(), "cmd" => "Cmd".to_owned(), "alt" => "Alt".to_owned(), "shift" => "Shift".to_owned(), "up" => "Up".to_owned(), "down" => "Down".to_owned(), "enter" => "Enter".to_owned(), "space" => "Space".to_owned(), "=" => "Plus".to_owned(), "-" => "Minus".to_owned(), other => other.to_uppercase(), }) .collect::>() .join("+") } pub fn bind_scopes(cx: &mut App, scopes: &[Scope]) { cx.bind_keys( SHORTCUTS .iter() .filter(|shortcut| scopes.contains(&shortcut.scope)) .flat_map(Shortcut::bindings), ); } pub fn shortcuts_in(group: Group) -> impl Iterator { SHORTCUTS .iter() .filter(move |shortcut| shortcut.group == group) } #[cfg(test)] mod tests { use std::collections::HashSet; use gpui_kit::{KeyContext, Keystroke, TestAppContext}; use super::{Group, SHORTCUTS, Scope, Shortcut, display_keys, shortcuts_in}; use crate::views::shell::bind_keys; fn bound_action(cx: &mut TestAppContext, keys: &str, contexts: &[&str]) -> Option { let stack: Vec = contexts .iter() .map(|context| KeyContext::parse(context).unwrap()) .collect(); let keystroke = Keystroke::parse(keys).unwrap(); cx.update(|cx| { let keymap = cx.key_bindings(); let (bindings, _) = keymap.borrow().bindings_for_input(&[keystroke], &stack); bindings .first() .map(|binding| binding.action().name().to_owned()) }) } fn registered(cx: &mut TestAppContext) { cx.update(gpui_kit::init); cx.update(bind_keys); } fn action_name(shortcut: &Shortcut) -> String { shortcut.bindings()[0].action().name().to_owned() } #[gpui_kit::test] fn every_listed_shortcut_resolves_to_its_action(cx: &mut TestAppContext) { registered(cx); for shortcut in SHORTCUTS { let contexts: &[&str] = match shortcut.scope { Scope::Global | Scope::OutsideComposer => &["AppShell"], Scope::GlobalOverTextFields | Scope::OutsideTextFields => &["AppShell"], Scope::Composer => &["AppShell", "Composer"], }; assert_eq!( bound_action(cx, shortcut.keys, contexts), Some(action_name(shortcut)), "{} ({})", shortcut.label, shortcut.keys ); } } #[gpui_kit::test] fn global_shortcuts_survive_typing_in_the_composer(cx: &mut TestAppContext) { registered(cx); for shortcut in SHORTCUTS .iter() .filter(|shortcut| shortcut.scope == Scope::GlobalOverTextFields) { assert_eq!( bound_action(cx, shortcut.keys, &["AppShell", "Composer", "Input"]), Some(action_name(shortcut)), "{} ({})", shortcut.label, shortcut.keys ); } } #[gpui_kit::test] fn push_to_talk_is_not_bound_while_typing_in_a_text_field(cx: &mut TestAppContext) { registered(cx); assert!(bound_action(cx, "ctrl-space", &["AppShell"]).is_some()); assert_eq!( bound_action(cx, "ctrl-space", &["AppShell", "Composer", "Input"]), None ); } #[gpui_kit::test] fn composer_link_shortcut_still_beats_the_switcher_while_typing(cx: &mut TestAppContext) { registered(cx); let typing = ["AppShell", "Composer", "Input"]; assert_eq!( bound_action(cx, "ctrl-k", &typing), Some(action_name( SHORTCUTS .iter() .find(|shortcut| shortcut.label == "Insert link") .unwrap() )) ); assert_eq!( bound_action(cx, "ctrl-k", &["AppShell"]), Some(action_name( SHORTCUTS .iter() .find(|shortcut| shortcut.label == "Search and switch chats") .unwrap() )) ); } #[test] fn no_two_shortcuts_share_keys_in_the_same_scope() { let mut seen = HashSet::new(); for shortcut in SHORTCUTS { let scope = matches!(shortcut.scope, Scope::Composer); assert!(seen.insert((shortcut.keys, scope)), "{}", shortcut.keys); } } #[gpui_kit::test] fn teams_message_presence_and_call_keys_resolve(cx: &mut TestAppContext) { registered(cx); let action_of = |label: &str| { action_name( SHORTCUTS .iter() .find(|shortcut| shortcut.label == label) .unwrap(), ) }; let typing = ["AppShell", "Composer", "Input"]; for (keys, label) in [ ("ctrl-r", "Go to compose box"), ("alt-shift-r", "Reply to the last message"), ("ctrl-up", "Edit last message"), ("ctrl-alt-up", "Go to previous unread conversation"), ("ctrl-alt-down", "Go to next unread conversation"), ("ctrl-g", "Go to a specific chat or channel"), ("alt-shift-q", "Set presence status to Available"), ("alt-shift-y", "Set presence status to Busy"), ("alt-shift-u", "Set presence status to Do Not Disturb"), ("alt-shift-k", "Set presence status to Be Right Back"), ("alt-shift-w", "Set presence status to Away"), ("ctrl-shift-a", "Accept video call"), ("ctrl-shift-s", "Accept audio call"), ("ctrl-shift-p", "Toggle background blur"), ("alt-shift-c", "Toggle live captions"), ] { assert_eq!( bound_action(cx, keys, &typing), Some(action_of(label)), "{keys}" ); } } #[test] fn every_group_lists_shortcuts_and_keys_read_like_teams() { for group in Group::ALL { assert!(shortcuts_in(group).next().is_some(), "{group:?}"); } assert_eq!(display_keys("ctrl-shift-a"), "Ctrl+Shift+A"); assert_eq!(display_keys("alt-up"), "Alt+Up"); assert_eq!(display_keys("ctrl-."), "Ctrl+."); assert_eq!(display_keys("ctrl-space"), "Ctrl+Space"); assert_eq!(display_keys("alt-shift-="), "Alt+Shift+Plus"); assert_eq!(display_keys("alt-shift--"), "Alt+Shift+Minus"); assert_eq!(display_keys("alt-shift-0"), "Alt+Shift+0"); assert_eq!(display_keys("ctrl-alt--"), "Ctrl+Alt+Minus"); } #[gpui_kit::test] fn zoom_keys_match_teams_and_leave_the_composer_subscript_alone(cx: &mut TestAppContext) { registered(cx); let action_of = |label: &str| { action_name( SHORTCUTS .iter() .find(|shortcut| shortcut.label == label) .unwrap(), ) }; for (keys, label) in [ ("ctrl-=", "Zoom in"), ("ctrl-+", "Zoom in"), ("ctrl-shift-=", "Zoom in"), ("ctrl--", "Zoom out"), ("ctrl-_", "Zoom out"), ("ctrl-0", "Reset zoom level"), ] { assert_eq!( bound_action(cx, keys, &["AppShell"]), Some(action_of(label)), "{keys}" ); } let typing = ["AppShell", "Composer", "Input"]; assert_eq!( bound_action(cx, "ctrl-=", &typing), Some(action_of("Subscript")) ); assert_eq!( bound_action(cx, "ctrl--", &typing), Some(action_of("Zoom out")) ); assert_eq!(display_keys("ctrl-="), "Ctrl+Plus"); } #[gpui_kit::test] fn composer_format_and_table_keys_match_teams(cx: &mut TestAppContext) { registered(cx); let typing = ["AppShell", "Composer", "Input"]; let action_of = |label: &str| { action_name( SHORTCUTS .iter() .find(|shortcut| shortcut.label == label) .unwrap(), ) }; for (keys, label) in [ ("ctrl-shift-x", "Expand compose box"), ("ctrl-enter", "Send (expanded compose box)"), ("ctrl-alt-x", "Strikethrough"), ("ctrl-alt-=", "Insert column or row"), ("ctrl-alt-+", "Insert column or row"), ("ctrl-alt--", "Delete column, row, or table"), ("ctrl-shift-b", "Code block"), ("ctrl-alt-4", "Quote"), ("ctrl-alt-1", "Heading 1"), ("ctrl-alt-3", "Heading 3"), ("ctrl-alt-p", "Paragraph"), ] { assert_eq!( bound_action(cx, keys, &typing), Some(action_of(label)), "{keys}" ); } } }