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