| 1 | use gpui_kit::*; |
| 2 | |
| 3 | use super::composer::{ |
| 4 | EditLink, KEY_CONTEXT as COMPOSER_CONTEXT, ScheduleSend, ToggleBold, ToggleCode, ToggleItalic, |
| 5 | ToggleStrike, ToggleSubscript, ToggleSuperscript, ToggleUnderline, |
| 6 | }; |
| 7 | use super::conversation::ReplyToHovered; |
| 8 | use super::shell::{NewChat, OpenSwitcher, ToggleCallMute}; |
| 9 | |
| 10 | const TEXT_FIELD_CONTEXT: &str = "Input"; |
| 11 | const OUTSIDE_COMPOSER_CONTEXT: &str = "!Composer"; |
| 12 | const OUTSIDE_TEXT_FIELD_CONTEXT: &str = "!Input"; |
| 13 | const ALTERNATE_KEYS: &[(&str, &str)] = &[("alt-shift-=", "alt-shift-+")]; |
| 14 | |
| 15 | actions!( |
| 16 | teams, |
| 17 | [ |
| 18 | OpenActivity, |
| 19 | OpenChatsTab, |
| 20 | OpenChannelsTab, |
| 21 | OpenSaved, |
| 22 | OpenCallsTab, |
| 23 | PreviousConversation, |
| 24 | NextConversation, |
| 25 | AcceptCall, |
| 26 | DeclineCall, |
| 27 | JoinMeetingFromToast, |
| 28 | HangUpCall, |
| 29 | ToggleCallCamera, |
| 30 | ToggleCallShare, |
| 31 | ToggleCallHand, |
| 32 | OpenLobbyList, |
| 33 | StartAudioCall, |
| 34 | StartVideoCall, |
| 35 | ToggleCallFullscreen, |
| 36 | PushToTalk, |
| 37 | ZoomShareIn, |
| 38 | ZoomShareOut, |
| 39 | ZoomShareReset, |
| 40 | ShowShortcuts |
| 41 | ] |
| 42 | ); |
| 43 | |
| 44 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 45 | pub enum Group { |
| 46 | General, |
| 47 | Navigation, |
| 48 | Composer, |
| 49 | Calls, |
| 50 | } |
| 51 | |
| 52 | impl Group { |
| 53 | pub const ALL: [Group; 4] = [ |
| 54 | Group::General, |
| 55 | Group::Navigation, |
| 56 | Group::Composer, |
| 57 | Group::Calls, |
| 58 | ]; |
| 59 | |
| 60 | pub fn title(self) -> &'static str { |
| 61 | match self { |
| 62 | Group::General => "General", |
| 63 | Group::Navigation => "Navigation", |
| 64 | Group::Composer => "Messages and composer", |
| 65 | Group::Calls => "Calls", |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 71 | pub enum Scope { |
| 72 | Global, |
| 73 | OutsideComposer, |
| 74 | GlobalOverTextFields, |
| 75 | OutsideTextFields, |
| 76 | Composer, |
| 77 | } |
| 78 | |
| 79 | pub struct Shortcut { |
| 80 | pub group: Group, |
| 81 | pub label: &'static str, |
| 82 | pub keys: &'static str, |
| 83 | pub mac_keys: Option<&'static str>, |
| 84 | pub scope: Scope, |
| 85 | bind: fn(&str, Option<&str>) -> KeyBinding, |
| 86 | } |
| 87 | |
| 88 | macro_rules! shortcut { |
| 89 | ($group:ident, $scope:ident, $label:expr, $keys:expr, $mac_keys:expr, $action:expr) => { |
| 90 | Shortcut { |
| 91 | group: Group::$group, |
| 92 | label: $label, |
| 93 | keys: $keys, |
| 94 | mac_keys: $mac_keys, |
| 95 | scope: Scope::$scope, |
| 96 | bind: |keys, context| KeyBinding::new(keys, $action, context), |
| 97 | } |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | #[rustfmt::skip] |
| 102 | pub const SHORTCUTS: &[Shortcut] = &[ |
| 103 | shortcut!(General, OutsideComposer, "Search and switch chats", "ctrl-k", Some("cmd-k"), OpenSwitcher), |
| 104 | shortcut!(General, Global, "New chat", "ctrl-n", Some("cmd-n"), NewChat), |
| 105 | shortcut!(General, GlobalOverTextFields, "Keyboard shortcuts", "ctrl-.", Some("cmd-."), ShowShortcuts), |
| 106 | shortcut!(Navigation, GlobalOverTextFields, "Open Activity", "ctrl-1", Some("cmd-1"), OpenActivity), |
| 107 | shortcut!(Navigation, GlobalOverTextFields, "Show Chats", "ctrl-2", Some("cmd-2"), OpenChatsTab), |
| 108 | shortcut!(Navigation, GlobalOverTextFields, "Show Channels", "ctrl-3", Some("cmd-3"), OpenChannelsTab), |
| 109 | shortcut!(Navigation, GlobalOverTextFields, "Open Saved", "ctrl-4", Some("cmd-4"), OpenSaved), |
| 110 | shortcut!(Navigation, GlobalOverTextFields, "Show Calls", "ctrl-5", Some("cmd-5"), OpenCallsTab), |
| 111 | shortcut!(Navigation, GlobalOverTextFields, "Previous chat or channel", "alt-up", None, PreviousConversation), |
| 112 | shortcut!(Navigation, GlobalOverTextFields, "Next chat or channel", "alt-down", None, NextConversation), |
| 113 | shortcut!(Composer, Global, "Reply to the message under the pointer", "alt-r", None, ReplyToHovered), |
| 114 | shortcut!(Composer, Composer, "Bold", "ctrl-b", Some("cmd-b"), ToggleBold), |
| 115 | shortcut!(Composer, Composer, "Italic", "ctrl-i", Some("cmd-i"), ToggleItalic), |
| 116 | shortcut!(Composer, Composer, "Underline", "ctrl-u", Some("cmd-u"), ToggleUnderline), |
| 117 | shortcut!(Composer, Composer, "Strikethrough", "ctrl-shift-x", None, ToggleStrike), |
| 118 | shortcut!(Composer, Composer, "Superscript", "ctrl-shift-=", None, ToggleSuperscript), |
| 119 | shortcut!(Composer, Composer, "Subscript", "ctrl-=", None, ToggleSubscript), |
| 120 | shortcut!(Composer, Composer, "Code", "ctrl-shift-c", None, ToggleCode), |
| 121 | shortcut!(Composer, Composer, "Insert link", "ctrl-k", Some("cmd-k"), EditLink), |
| 122 | shortcut!(Composer, Composer, "Schedule send", "ctrl-shift-enter", None, ScheduleSend), |
| 123 | shortcut!(Calls, GlobalOverTextFields, "Accept incoming call", "ctrl-shift-a", Some("cmd-shift-a"), AcceptCall), |
| 124 | shortcut!(Calls, GlobalOverTextFields, "Decline incoming call", "ctrl-shift-d", Some("cmd-shift-d"), DeclineCall), |
| 125 | shortcut!(Calls, GlobalOverTextFields, "Join the meeting from its reminder", "ctrl-shift-j", Some("cmd-shift-j"), JoinMeetingFromToast), |
| 126 | shortcut!(Calls, GlobalOverTextFields, "Hang up or leave", "ctrl-shift-h", Some("cmd-shift-h"), HangUpCall), |
| 127 | shortcut!(Calls, GlobalOverTextFields, "Mute or unmute", "ctrl-shift-m", None, ToggleCallMute), |
| 128 | shortcut!(Calls, GlobalOverTextFields, "Turn camera on or off", "ctrl-shift-o", Some("cmd-shift-o"), ToggleCallCamera), |
| 129 | shortcut!(Calls, GlobalOverTextFields, "Open the share menu or stop sharing", "ctrl-shift-e", Some("cmd-shift-e"), ToggleCallShare), |
| 130 | shortcut!(Calls, GlobalOverTextFields, "Raise or lower hand", "ctrl-shift-k", Some("cmd-shift-k"), ToggleCallHand), |
| 131 | shortcut!(Calls, GlobalOverTextFields, "Open the lobby list", "ctrl-shift-y", Some("cmd-shift-y"), OpenLobbyList), |
| 132 | shortcut!(Calls, GlobalOverTextFields, "Start an audio call in this chat", "alt-shift-a", None, StartAudioCall), |
| 133 | shortcut!(Calls, GlobalOverTextFields, "Start a video call in this chat", "alt-shift-v", None, StartVideoCall), |
| 134 | shortcut!(Calls, GlobalOverTextFields, "Full screen", "f11", None, ToggleCallFullscreen), |
| 135 | shortcut!(Calls, GlobalOverTextFields, "Zoom in on shared content", "alt-shift-=", None, ZoomShareIn), |
| 136 | shortcut!(Calls, GlobalOverTextFields, "Zoom out on shared content", "alt-shift--", None, ZoomShareOut), |
| 137 | shortcut!(Calls, GlobalOverTextFields, "Reset zoom on shared content", "alt-shift-0", None, ZoomShareReset), |
| 138 | shortcut!(Calls, OutsideTextFields, "Hold to talk while muted", "ctrl-space", None, PushToTalk), |
| 139 | ]; |
| 140 | |
| 141 | impl Shortcut { |
| 142 | pub fn keys_for_platform(&self) -> &'static str { |
| 143 | match self.mac_keys { |
| 144 | Some(mac_keys) if cfg!(target_os = "macos") => mac_keys, |
| 145 | _ => self.keys, |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | pub fn display_keys(&self) -> String { |
| 150 | display_keys(self.keys_for_platform()) |
| 151 | } |
| 152 | |
| 153 | fn bindings(&self) -> Vec<KeyBinding> { |
| 154 | let contexts: &[Option<&str>] = match self.scope { |
| 155 | Scope::Global => &[None], |
| 156 | Scope::OutsideComposer => &[Some(OUTSIDE_COMPOSER_CONTEXT)], |
| 157 | Scope::GlobalOverTextFields => &[None, Some(TEXT_FIELD_CONTEXT)], |
| 158 | Scope::OutsideTextFields => &[Some(OUTSIDE_TEXT_FIELD_CONTEXT)], |
| 159 | Scope::Composer => &[Some(COMPOSER_CONTEXT)], |
| 160 | }; |
| 161 | let mut keys = vec![self.keys]; |
| 162 | keys.extend(ALTERNATE_KEYS.iter().filter(|(primary, _)| *primary == self.keys).map(|(_, alternate)| *alternate)); |
| 163 | keys.extend(self.mac_keys.filter(|_| cfg!(target_os = "macos"))); |
| 164 | keys.iter() |
| 165 | .flat_map(|keys| contexts.iter().map(|context| (self.bind)(keys, *context))) |
| 166 | .collect() |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | pub fn keys_for_label(label: &str) -> Option<String> { |
| 171 | SHORTCUTS |
| 172 | .iter() |
| 173 | .find(|shortcut| shortcut.label == label) |
| 174 | .map(Shortcut::display_keys) |
| 175 | } |
| 176 | |
| 177 | pub fn display_keys(keys: &str) -> String { |
| 178 | let (modifiers, key) = match keys.strip_suffix("--") { |
| 179 | Some(modifiers) => (modifiers, "-"), |
| 180 | None => keys.rsplit_once('-').unwrap_or(("", keys)), |
| 181 | }; |
| 182 | modifiers |
| 183 | .split('-') |
| 184 | .filter(|part| !part.is_empty()) |
| 185 | .chain([key]) |
| 186 | .map(|part| match part { |
| 187 | "ctrl" => "Ctrl".to_owned(), |
| 188 | "cmd" => "Cmd".to_owned(), |
| 189 | "alt" => "Alt".to_owned(), |
| 190 | "shift" => "Shift".to_owned(), |
| 191 | "up" => "Up".to_owned(), |
| 192 | "down" => "Down".to_owned(), |
| 193 | "enter" => "Enter".to_owned(), |
| 194 | "space" => "Space".to_owned(), |
| 195 | "=" => "Plus".to_owned(), |
| 196 | "-" => "Minus".to_owned(), |
| 197 | other => other.to_uppercase(), |
| 198 | }) |
| 199 | .collect::<Vec<_>>() |
| 200 | .join("+") |
| 201 | } |
| 202 | |
| 203 | pub fn bind_scopes(cx: &mut App, scopes: &[Scope]) { |
| 204 | cx.bind_keys( |
| 205 | SHORTCUTS |
| 206 | .iter() |
| 207 | .filter(|shortcut| scopes.contains(&shortcut.scope)) |
| 208 | .flat_map(Shortcut::bindings), |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | pub fn shortcuts_in(group: Group) -> impl Iterator<Item = &'static Shortcut> { |
| 213 | SHORTCUTS |
| 214 | .iter() |
| 215 | .filter(move |shortcut| shortcut.group == group) |
| 216 | } |
| 217 | |
| 218 | #[cfg(test)] |
| 219 | mod tests { |
| 220 | use std::collections::HashSet; |
| 221 | |
| 222 | use gpui_kit::{KeyContext, Keystroke, TestAppContext}; |
| 223 | |
| 224 | use super::{Group, SHORTCUTS, Scope, Shortcut, display_keys, shortcuts_in}; |
| 225 | use crate::views::shell::bind_keys; |
| 226 | |
| 227 | fn bound_action(cx: &mut TestAppContext, keys: &str, contexts: &[&str]) -> Option<String> { |
| 228 | let stack: Vec<KeyContext> = contexts |
| 229 | .iter() |
| 230 | .map(|context| KeyContext::parse(context).unwrap()) |
| 231 | .collect(); |
| 232 | let keystroke = Keystroke::parse(keys).unwrap(); |
| 233 | cx.update(|cx| { |
| 234 | let keymap = cx.key_bindings(); |
| 235 | let (bindings, _) = keymap.borrow().bindings_for_input(&[keystroke], &stack); |
| 236 | bindings |
| 237 | .first() |
| 238 | .map(|binding| binding.action().name().to_owned()) |
| 239 | }) |
| 240 | } |
| 241 | |
| 242 | fn registered(cx: &mut TestAppContext) { |
| 243 | cx.update(gpui_kit::init); |
| 244 | cx.update(bind_keys); |
| 245 | } |
| 246 | |
| 247 | fn action_name(shortcut: &Shortcut) -> String { |
| 248 | shortcut.bindings()[0].action().name().to_owned() |
| 249 | } |
| 250 | |
| 251 | #[gpui_kit::test] |
| 252 | fn every_listed_shortcut_resolves_to_its_action(cx: &mut TestAppContext) { |
| 253 | registered(cx); |
| 254 | for shortcut in SHORTCUTS { |
| 255 | let contexts: &[&str] = match shortcut.scope { |
| 256 | Scope::Global | Scope::OutsideComposer => &["AppShell"], |
| 257 | Scope::GlobalOverTextFields | Scope::OutsideTextFields => &["AppShell"], |
| 258 | Scope::Composer => &["AppShell", "Composer"], |
| 259 | }; |
| 260 | assert_eq!( |
| 261 | bound_action(cx, shortcut.keys, contexts), |
| 262 | Some(action_name(shortcut)), |
| 263 | "{} ({})", |
| 264 | shortcut.label, |
| 265 | shortcut.keys |
| 266 | ); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | #[gpui_kit::test] |
| 271 | fn global_shortcuts_survive_typing_in_the_composer(cx: &mut TestAppContext) { |
| 272 | registered(cx); |
| 273 | for shortcut in SHORTCUTS |
| 274 | .iter() |
| 275 | .filter(|shortcut| shortcut.scope == Scope::GlobalOverTextFields) |
| 276 | { |
| 277 | assert_eq!( |
| 278 | bound_action(cx, shortcut.keys, &["AppShell", "Composer", "Input"]), |
| 279 | Some(action_name(shortcut)), |
| 280 | "{} ({})", |
| 281 | shortcut.label, |
| 282 | shortcut.keys |
| 283 | ); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | #[gpui_kit::test] |
| 288 | fn push_to_talk_is_not_bound_while_typing_in_a_text_field(cx: &mut TestAppContext) { |
| 289 | registered(cx); |
| 290 | assert!(bound_action(cx, "ctrl-space", &["AppShell"]).is_some()); |
| 291 | assert_eq!( |
| 292 | bound_action(cx, "ctrl-space", &["AppShell", "Composer", "Input"]), |
| 293 | None |
| 294 | ); |
| 295 | } |
| 296 | |
| 297 | #[gpui_kit::test] |
| 298 | fn composer_link_shortcut_still_beats_the_switcher_while_typing(cx: &mut TestAppContext) { |
| 299 | registered(cx); |
| 300 | let typing = ["AppShell", "Composer", "Input"]; |
| 301 | assert_eq!( |
| 302 | bound_action(cx, "ctrl-k", &typing), |
| 303 | Some(action_name( |
| 304 | SHORTCUTS |
| 305 | .iter() |
| 306 | .find(|shortcut| shortcut.label == "Insert link") |
| 307 | .unwrap() |
| 308 | )) |
| 309 | ); |
| 310 | assert_eq!( |
| 311 | bound_action(cx, "ctrl-k", &["AppShell"]), |
| 312 | Some(action_name( |
| 313 | SHORTCUTS |
| 314 | .iter() |
| 315 | .find(|shortcut| shortcut.label == "Search and switch chats") |
| 316 | .unwrap() |
| 317 | )) |
| 318 | ); |
| 319 | } |
| 320 | |
| 321 | #[test] |
| 322 | fn no_two_shortcuts_share_keys_in_the_same_scope() { |
| 323 | let mut seen = HashSet::new(); |
| 324 | for shortcut in SHORTCUTS { |
| 325 | let scope = matches!(shortcut.scope, Scope::Composer); |
| 326 | assert!(seen.insert((shortcut.keys, scope)), "{}", shortcut.keys); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | #[test] |
| 331 | fn every_group_lists_shortcuts_and_keys_read_like_teams() { |
| 332 | for group in Group::ALL { |
| 333 | assert!(shortcuts_in(group).next().is_some(), "{group:?}"); |
| 334 | } |
| 335 | assert_eq!(display_keys("ctrl-shift-a"), "Ctrl+Shift+A"); |
| 336 | assert_eq!(display_keys("alt-up"), "Alt+Up"); |
| 337 | assert_eq!(display_keys("ctrl-."), "Ctrl+."); |
| 338 | assert_eq!(display_keys("ctrl-space"), "Ctrl+Space"); |
| 339 | assert_eq!(display_keys("alt-shift-="), "Alt+Shift+Plus"); |
| 340 | assert_eq!(display_keys("alt-shift--"), "Alt+Shift+Minus"); |
| 341 | assert_eq!(display_keys("alt-shift-0"), "Alt+Shift+0"); |
| 342 | } |
| 343 | } |