1use iced_core::keyboard::{Key, Modifiers};
2use std::fmt;
34/// 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}
1819/// 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.
24pub modifiers: Vec<Modifier>,
25/// The key for the key binding.
26pub key: Key,
27}
2829impl 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.
40pub fn matches(&self, modifiers: Modifiers, key: &Key) -> bool {
41let 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}
5354impl fmt::Display for KeyBind {
55fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56for modifier in self.modifiers.iter() {
57write!(f, "{:?} + ", modifier)?;
58 }
59match &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}