cosmic/widget/menu/
key_bind.rs

1use iced_core::keyboard::{Key, Modifiers};
2use std::fmt;
3
4/// Represents the modifier keys on a keyboard.
5///
6/// It has four variants:
7/// * `Super`: Represents the Super key (also known as the Windows key on Windows, Command key on macOS).
8/// * `Ctrl`: Represents the Control key.
9/// * `Alt`: Represents the Alt key.
10/// * `Shift`: Represents the Shift key.
11#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
12pub enum Modifier {
13    Super,
14    Ctrl,
15    Alt,
16    Shift,
17}
18
19/// Represents a combination of a key and modifiers.
20/// It is used to define keyboard shortcuts.
21#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
22pub struct KeyBind {
23    /// A vector of modifiers for the key binding.
24    pub modifiers: Vec<Modifier>,
25    /// The key for the key binding.
26    pub key: Key,
27}
28
29impl KeyBind {
30    /// Checks if the given key and modifiers match the `KeyBind`.
31    ///
32    /// # Arguments
33    ///
34    /// * `modifiers` - A `Modifiers` instance representing the current active modifiers.
35    /// * `key` - A reference to the `Key` that is being checked.
36    ///
37    /// # Returns
38    ///
39    /// * `bool` - `true` if the key and modifiers match the `KeyBind`, `false` otherwise.
40    pub fn matches(&self, modifiers: Modifiers, key: &Key) -> bool {
41        let key_eq = match (key, &self.key) {
42            // CapsLock and Shift change the case of Key::Character, so we compare these in a case insensitive way
43            (Key::Character(a), Key::Character(b)) => a.eq_ignore_ascii_case(b),
44            (a, b) => a.eq(b),
45        };
46        key_eq
47            && modifiers.logo() == self.modifiers.contains(&Modifier::Super)
48            && modifiers.control() == self.modifiers.contains(&Modifier::Ctrl)
49            && modifiers.alt() == self.modifiers.contains(&Modifier::Alt)
50            && modifiers.shift() == self.modifiers.contains(&Modifier::Shift)
51    }
52}
53
54impl fmt::Display for KeyBind {
55    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56        for modifier in self.modifiers.iter() {
57            write!(f, "{:?} + ", modifier)?;
58        }
59        match &self.key {
60            Key::Character(c) => write!(f, "{}", c.to_uppercase()),
61            Key::Named(named) => write!(f, "{:?}", named),
62            other => write!(f, "{:?}", other),
63        }
64    }
65}