Skip to main content

cosmic/widget/menu/
key_bind.rs

1use iced_core::keyboard::key::{Code, Physical};
2use iced_core::keyboard::{Key, Modifiers};
3use std::fmt;
4
5/// Represents the modifier keys on a keyboard.
6///
7/// It has four variants:
8/// * `Super`: Represents the Super key (also known as the Windows key on Windows, Command key on macOS).
9/// * `Ctrl`: Represents the Control key.
10/// * `Alt`: Represents the Alt key.
11/// * `Shift`: Represents the Shift key.
12#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
13pub enum Modifier {
14    Super,
15    Ctrl,
16    Alt,
17    Shift,
18}
19
20/// Represents a combination of a key and modifiers.
21/// It is used to define keyboard shortcuts.
22#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
23pub struct KeyBind {
24    /// A vector of modifiers for the key binding.
25    pub modifiers: Vec<Modifier>,
26    /// The key for the key binding.
27    pub key: Key,
28}
29
30impl KeyBind {
31    /// Checks if the given key and modifiers match the `KeyBind`, with an
32    /// optional fallback to the physical key position for non-Latin keyboard
33    /// layouts.
34    ///
35    /// # Arguments
36    ///
37    /// * `modifiers` - A `Modifiers` instance representing the current active modifiers.
38    /// * `key` - A reference to the `Key` that is being checked.
39    /// * `physical_key` - An optional reference to the physical key position,
40    ///   used as a fallback when the logical `key` does not match (e.g. on
41    ///   Cyrillic or other non-Latin layouts). Can be `None` for keys where
42    ///   the physical position is not relevant (e.g. `Key::Named`).
43    ///
44    /// # Returns
45    ///
46    /// * `bool` - `true` if the key and modifiers match the `KeyBind`, `false` otherwise.
47    pub fn matches(
48        &self,
49        modifiers: Modifiers,
50        key: &Key,
51        physical_key: Option<&Physical>,
52    ) -> bool {
53        let key_eq = self.key_eq(key)
54            || (!is_latin_shortcut_key(key)
55                && physical_key
56                    .and_then(physical_key_to_latin)
57                    .is_some_and(|latin| self.key_eq(&latin)));
58        key_eq
59            && modifiers.logo() == self.modifiers.contains(&Modifier::Super)
60            && modifiers.control() == self.modifiers.contains(&Modifier::Ctrl)
61            && modifiers.alt() == self.modifiers.contains(&Modifier::Alt)
62            && modifiers.shift() == self.modifiers.contains(&Modifier::Shift)
63    }
64
65    fn key_eq(&self, key: &Key) -> bool {
66        match (key, &self.key) {
67            // CapsLock and Shift change the case of Key::Character, so we compare these in a case insensitive way
68            (Key::Character(a), Key::Character(b)) => a.eq_ignore_ascii_case(b),
69            (a, b) => a.eq(b),
70        }
71    }
72}
73
74fn is_latin_shortcut_key(key: &Key) -> bool {
75    let Key::Character(s) = key else {
76        return false;
77    };
78
79    let mut chars = s.chars();
80    let Some(ch) = chars.next() else {
81        return false;
82    };
83
84    chars.next().is_none() && (ch.is_ascii_graphic() || ch == ' ')
85}
86
87/// Converts a physical key code to the corresponding US-layout Latin `Key`.
88///
89/// This mapping is intentionally limited to keys that may produce different
90/// characters on non-Latin keyboard layouts (letters and punctuation). Keys
91/// like digits are not included because they remain the same across layouts.
92///
93/// Only used as a fallback when the primary key comparison in
94/// [`KeyBind::matches`] does not match.
95fn physical_key_to_latin(physical_key: &Physical) -> Option<Key> {
96    let code = match physical_key {
97        Physical::Code(code) => code,
98        Physical::Unidentified(_) => return None,
99    };
100    let ch = match code {
101        Code::KeyA => "a",
102        Code::KeyB => "b",
103        Code::KeyC => "c",
104        Code::KeyD => "d",
105        Code::KeyE => "e",
106        Code::KeyF => "f",
107        Code::KeyG => "g",
108        Code::KeyH => "h",
109        Code::KeyI => "i",
110        Code::KeyJ => "j",
111        Code::KeyK => "k",
112        Code::KeyL => "l",
113        Code::KeyM => "m",
114        Code::KeyN => "n",
115        Code::KeyO => "o",
116        Code::KeyP => "p",
117        Code::KeyQ => "q",
118        Code::KeyR => "r",
119        Code::KeyS => "s",
120        Code::KeyT => "t",
121        Code::KeyU => "u",
122        Code::KeyV => "v",
123        Code::KeyW => "w",
124        Code::KeyX => "x",
125        Code::KeyY => "y",
126        Code::KeyZ => "z",
127        Code::Minus => "-",
128        Code::Equal => "=",
129        Code::BracketLeft => "[",
130        Code::BracketRight => "]",
131        Code::Backslash => "\\",
132        Code::Semicolon => ";",
133        Code::Quote => "'",
134        Code::Backquote => "`",
135        Code::Comma => ",",
136        Code::Period => ".",
137        Code::Slash => "/",
138        _ => return None,
139    };
140    Some(Key::Character(ch.into()))
141}
142
143impl fmt::Display for KeyBind {
144    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
145        for modifier in self.modifiers.iter() {
146            write!(f, "{:?} + ", modifier)?;
147        }
148        match &self.key {
149            Key::Character(c) => write!(f, "{}", c.to_uppercase()),
150            Key::Named(named) => write!(f, "{:?}", named),
151            other => write!(f, "{:?}", other),
152        }
153    }
154}
155
156#[cfg(test)]
157mod test {
158    use super::*;
159
160    fn bind_ctrl_w() -> KeyBind {
161        KeyBind {
162            modifiers: vec![Modifier::Ctrl],
163            key: Key::Character("w".into()),
164        }
165    }
166
167    #[test]
168    fn ctrl_w() {
169        assert!(bind_ctrl_w().matches(
170            Modifiers::CTRL,
171            &Key::Character("w".into()),
172            Some(&Physical::Code(Code::KeyW)),
173        ));
174    }
175
176    #[test]
177    fn ctrl_w_no_fallback_to_dvorak_comma() {
178        assert!(!bind_ctrl_w().matches(
179            Modifiers::CTRL,
180            &Key::Character(",".into()),
181            Some(&Physical::Code(Code::KeyW)),
182        ));
183    }
184
185    #[test]
186    fn non_latin_layout_fallback() {
187        assert!(bind_ctrl_w().matches(
188            Modifiers::CTRL,
189            &Key::Character("ц".into()),
190            Some(&Physical::Code(Code::KeyW)),
191        ));
192
193        let bind = KeyBind {
194            modifiers: vec![Modifier::Ctrl],
195            key: Key::Character("s".into()),
196        };
197
198        assert!(bind.matches(
199            Modifiers::CTRL,
200            &Key::Character("ы".into()),
201            Some(&Physical::Code(Code::KeyS)),
202        ));
203
204        assert!(!bind.matches(
205            Modifiers::CTRL,
206            &Key::Character("ц".into()),
207            Some(&Physical::Code(Code::KeyQ)),
208        ));
209    }
210
211    #[test]
212    fn ctrl_space() {
213        let bind = KeyBind {
214            modifiers: vec![Modifier::Ctrl],
215            key: Key::Character(" ".into()),
216        };
217
218        assert!(bind.matches(Modifiers::CTRL, &Key::Character(" ".into()), None,));
219    }
220
221    #[test]
222    fn ctrl_space_no_fallback() {
223        assert!(!bind_ctrl_w().matches(
224            Modifiers::CTRL,
225            &Key::Character(" ".into()),
226            Some(&Physical::Code(Code::KeyW)),
227        ));
228    }
229
230    #[test]
231    fn ctrl_a_no_fallback_to_french_azerty_q() {
232        let bind = KeyBind {
233            modifiers: vec![Modifier::Ctrl],
234            key: Key::Character("a".into()),
235        };
236
237        assert!(!bind.matches(
238            Modifiers::CTRL,
239            &Key::Character("q".into()),
240            Some(&Physical::Code(Code::KeyA)),
241        ));
242    }
243}