egui/data/input/
keyboard_shortcut.rs1use crate::Key;
2
3use super::{ModifierNames, Modifiers};
4
5#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
10#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
11pub struct KeyboardShortcut {
12 pub modifiers: Modifiers,
13
14 pub logical_key: Key,
15}
16
17impl KeyboardShortcut {
18 pub const fn new(modifiers: Modifiers, logical_key: Key) -> Self {
19 Self {
20 modifiers,
21 logical_key,
22 }
23 }
24
25 pub fn format(&self, names: &ModifierNames<'_>, is_mac: bool) -> String {
26 let mut s = names.format(&self.modifiers, is_mac);
27 if !s.is_empty() {
28 s += names.concat;
29 }
30 if names.is_short {
31 s += self.logical_key.symbol_or_name();
32 } else {
33 s += self.logical_key.name();
34 }
35 s
36 }
37}
38
39#[test]
40fn format_kb_shortcut() {
41 let cmd_shift_f = KeyboardShortcut::new(Modifiers::COMMAND | Modifiers::SHIFT, Key::F);
42 assert_eq!(
43 cmd_shift_f.format(&ModifierNames::NAMES, false),
44 "Ctrl+Shift+F"
45 );
46 assert_eq!(
47 cmd_shift_f.format(&ModifierNames::NAMES, true),
48 "Shift+Cmd+F"
49 );
50 assert_eq!(cmd_shift_f.format(&ModifierNames::SYMBOLS, false), "⌃⇧F");
51 assert_eq!(cmd_shift_f.format(&ModifierNames::SYMBOLS, true), "⇧⌘F");
52}