crisol_core/commands/key/
modifiers.rs1#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
5pub struct Modifiers {
11 pub alt: bool,
13 pub ctrl: bool,
15 pub shift: bool,
17 pub mac_cmd: bool,
19 pub command: bool,
21}
22
23impl Modifiers {
24 pub const NONE: Self = Self {
26 alt: false,
27 ctrl: false,
28 shift: false,
29 mac_cmd: false,
30 command: false,
31 };
32 pub const ALT: Self = Self {
34 alt: true,
35 ..Self::NONE
36 };
37 pub const CTRL: Self = Self {
39 ctrl: true,
40 ..Self::NONE
41 };
42 pub const SHIFT: Self = Self {
44 shift: true,
45 ..Self::NONE
46 };
47 pub const COMMAND: Self = Self {
49 command: true,
50 ..Self::NONE
51 };
52 pub const MAC_CMD: Self = Self {
54 mac_cmd: true,
55 ..Self::NONE
56 };
57
58 pub const fn is_none(self) -> bool {
60 !self.alt && !self.ctrl && !self.shift && !self.mac_cmd && !self.command
61 }
62
63 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}