BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / shortcuts.rs

shortcuts.rs

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