Skip to main content

crisol_core/commands/key/
modifiers.rs

1//! The set of pressed modifier keys.
2
3/// A set of pressed modifier keys.
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
5/// # Example
6///
7/// ```
8/// use crisol_core::commands::Modifiers;
9/// ```
10pub struct Modifiers {
11    /// Alt / Option.
12    pub alt: bool,
13    /// Control.
14    pub ctrl: bool,
15    /// Shift.
16    pub shift: bool,
17    /// The Command key on macOS.
18    pub mac_cmd: bool,
19    /// The platform command key: Ctrl elsewhere, Command on macOS.
20    pub command: bool,
21}
22
23impl Modifiers {
24    /// No modifiers.
25    pub const NONE: Self = Self {
26        alt: false,
27        ctrl: false,
28        shift: false,
29        mac_cmd: false,
30        command: false,
31    };
32    /// Alt / Option.
33    pub const ALT: Self = Self {
34        alt: true,
35        ..Self::NONE
36    };
37    /// Control.
38    pub const CTRL: Self = Self {
39        ctrl: true,
40        ..Self::NONE
41    };
42    /// Shift.
43    pub const SHIFT: Self = Self {
44        shift: true,
45        ..Self::NONE
46    };
47    /// The platform command key.
48    pub const COMMAND: Self = Self {
49        command: true,
50        ..Self::NONE
51    };
52    /// The macOS Command key.
53    pub const MAC_CMD: Self = Self {
54        mac_cmd: true,
55        ..Self::NONE
56    };
57
58    /// Whether no modifier is set.
59    pub const fn is_none(self) -> bool {
60        !self.alt && !self.ctrl && !self.shift && !self.mac_cmd && !self.command
61    }
62
63    /// Whether the pressed set satisfies the pattern, treating `command` as the
64    /// platform key, exactly as `egui::Modifiers::matches_logically` does.
65    pub fn matches_logically(self, pattern: Self) -> bool {
66        if pattern.alt && !self.alt {
67            return false;
68        }
69        if pattern.shift && !self.shift {
70            return false;
71        }
72        if pattern.mac_cmd {
73            return self.mac_cmd && pattern.ctrl == self.ctrl;
74        }
75        if !pattern.ctrl && !pattern.command {
76            return !self.ctrl && !self.command;
77        }
78        (!pattern.ctrl || self.ctrl) && (!pattern.command || self.command)
79    }
80}
81
82impl std::ops::BitOr for Modifiers {
83    type Output = Self;
84
85    fn bitor(self, other: Self) -> Self {
86        Self {
87            alt: self.alt || other.alt,
88            ctrl: self.ctrl || other.ctrl,
89            shift: self.shift || other.shift,
90            mac_cmd: self.mac_cmd || other.mac_cmd,
91            command: self.command || other.command,
92        }
93    }
94}
95
96impl std::ops::BitOrAssign for Modifiers {
97    fn bitor_assign(&mut self, other: Self) {
98        *self = *self | other;
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105
106    #[test]
107    fn logical_matching_follows_the_platform_key() {
108        assert!(Modifiers::COMMAND.matches_logically(Modifiers::COMMAND));
109        assert!(Modifiers::CTRL.matches_logically(Modifiers::CTRL));
110        assert!((Modifiers::CTRL | Modifiers::COMMAND).matches_logically(Modifiers::COMMAND));
111        assert!(!Modifiers::NONE.matches_logically(Modifiers::COMMAND));
112        assert!(Modifiers::MAC_CMD.matches_logically(Modifiers::MAC_CMD));
113        assert!(!Modifiers::CTRL.matches_logically(Modifiers::MAC_CMD));
114    }
115}