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) if c.as_str() == " " => write!(f, "Space"),
150            Key::Character(c) => write!(f, "{}", c.to_uppercase()),
151            Key::Named(named) => write!(f, "{:?}", named),
152            other => write!(f, "{:?}", other),
153        }
154    }
155}
156
157#[cfg(test)]
158mod test {
159    use super::*;
160
161    fn bind_ctrl_w() -> KeyBind {
162        KeyBind {
163            modifiers: vec![Modifier::Ctrl],
164            key: Key::Character("w".into()),
165        }
166    }
167
168    #[test]
169    fn ctrl_w() {
170        assert!(bind_ctrl_w().matches(
171            Modifiers::CTRL,
172            &Key::Character("w".into()),
173            Some(&Physical::Code(Code::KeyW)),
174        ));
175    }
176
177    #[test]
178    fn ctrl_w_no_fallback_to_dvorak_comma() {
179        assert!(!bind_ctrl_w().matches(
180            Modifiers::CTRL,
181            &Key::Character(",".into()),
182            Some(&Physical::Code(Code::KeyW)),
183        ));
184    }
185
186    #[test]
187    fn non_latin_layout_fallback() {
188        assert!(bind_ctrl_w().matches(
189            Modifiers::CTRL,
190            &Key::Character("ц".into()),
191            Some(&Physical::Code(Code::KeyW)),
192        ));
193
194        let bind = KeyBind {
195            modifiers: vec![Modifier::Ctrl],
196            key: Key::Character("s".into()),
197        };
198
199        assert!(bind.matches(
200            Modifiers::CTRL,
201            &Key::Character("ы".into()),
202            Some(&Physical::Code(Code::KeyS)),
203        ));
204
205        assert!(!bind.matches(
206            Modifiers::CTRL,
207            &Key::Character("ц".into()),
208            Some(&Physical::Code(Code::KeyQ)),
209        ));
210    }
211
212    #[test]
213    fn ctrl_space() {
214        let bind = KeyBind {
215            modifiers: vec![Modifier::Ctrl],
216            key: Key::Character(" ".into()),
217        };
218
219        assert!(bind.matches(Modifiers::CTRL, &Key::Character(" ".into()), None,));
220        assert!(format!("{}", bind) == String::from("Ctrl + Space"))
221    }
222
223    #[test]
224    fn ctrl_space_no_fallback() {
225        assert!(!bind_ctrl_w().matches(
226            Modifiers::CTRL,
227            &Key::Character(" ".into()),
228            Some(&Physical::Code(Code::KeyW)),
229        ));
230    }
231
232    #[test]
233    fn ctrl_a_no_fallback_to_french_azerty_q() {
234        let bind = KeyBind {
235            modifiers: vec![Modifier::Ctrl],
236            key: Key::Character("a".into()),
237        };
238
239        assert!(!bind.matches(
240            Modifiers::CTRL,
241            &Key::Character("q".into()),
242            Some(&Physical::Code(Code::KeyA)),
243        ));
244    }
245}