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