notcurses/input/
key_mod.rs

1// notcurses::input::key_mod
2//
3//!
4//
5
6use crate::sys::NcKeyMod;
7
8/// A bitmask of keyboard modifiers.
9#[derive(Clone, Copy, PartialEq, Eq)]
10pub struct KeyMod(u32);
11
12/// # Flags
13#[allow(non_upper_case_globals)]
14impl KeyMod {
15    ///
16    pub const Shift: Self = Self(NcKeyMod::Shift.0);
17
18    ///
19    pub const Alt: Self = Self(NcKeyMod::Alt.0);
20
21    ///
22    pub const Ctrl: Self = Self(NcKeyMod::Ctrl.0);
23
24    ///
25    pub const Super: Self = Self(NcKeyMod::Super.0);
26
27    ///
28    pub const Hyper: Self = Self(NcKeyMod::Hyper.0);
29
30    ///
31    pub const Meta: Self = Self(NcKeyMod::Meta.0);
32
33    ///
34    pub const CapsLock: Self = Self(NcKeyMod::CapsLock.0);
35
36    ///
37    pub const NumLock: Self = Self(NcKeyMod::NumLock.0);
38
39    /// None of the modifiers (all bits set to 0).
40    pub const None: Self = Self(0);
41
42    /// The modifier mask (all bits set to 1).
43    pub const Mask: Self = Self(u32::MAX);
44}
45
46mod core_impls {
47    use super::{KeyMod, NcKeyMod};
48    use core::fmt;
49
50    impl Default for KeyMod {
51        fn default() -> Self {
52            Self::None
53        }
54    }
55
56    impl fmt::Display for KeyMod {
57        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58            let mut string = String::new();
59
60            if self.has_none() {
61                string += "None ";
62            } else {
63                if self.has_capslock() {
64                    string += "CapsLock+";
65                }
66                if self.has_numlock() {
67                    string += "NumLock+";
68                }
69                if self.has_ctrl() {
70                    string += "Ctrl+";
71                }
72                if self.has_shift() {
73                    string += "Shift+";
74                }
75                if self.has_alt() {
76                    string += "Alt+";
77                }
78                if self.has_meta() {
79                    string += "Meta+";
80                }
81                if self.has_super() {
82                    string += "Super+";
83                }
84                if self.has_hyper() {
85                    string += "Hyper+";
86                }
87            }
88            string.pop();
89
90            write!(f, "{}", string)
91        }
92    }
93
94    impl fmt::Debug for KeyMod {
95        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96            write!(f, "KeyMod::{}", self)
97        }
98    }
99    crate::from_primitive![KeyMod, u32];
100    crate::unit_impl_ops![bitwise; KeyMod, u32];
101    crate::unit_impl_fmt![bases; KeyMod];
102
103    impl From<NcKeyMod> for KeyMod {
104        fn from(nc: NcKeyMod) -> Self {
105            Self(nc.into())
106        }
107    }
108    impl From<KeyMod> for NcKeyMod {
109        fn from(k: KeyMod) -> Self {
110            k.0.into()
111        }
112    }
113
114    impl From<u32> for KeyMod {
115        fn from(u: u32) -> KeyMod {
116            Self(u)
117        }
118    }
119    impl From<KeyMod> for u32 {
120        fn from(km: KeyMod) -> u32 {
121            km.0
122        }
123    }
124}
125
126/// # methods
127impl KeyMod {
128    /// Returns `true` if no modifiers are present.
129    #[inline]
130    pub fn has_none(&self) -> bool {
131        *self == KeyMod::None
132    }
133
134    /// Returns `true` if the `Shift` modifier is present.
135    #[inline]
136    pub fn has_shift(&self) -> bool {
137        *self & KeyMod::Shift != KeyMod::None
138    }
139
140    /// Returns `true` if the `Alt` modifier is present.
141    #[inline]
142    pub fn has_alt(&self) -> bool {
143        *self & KeyMod::Alt != KeyMod::None
144    }
145
146    /// Returns `true` if the `Ctrl` modifier is present.
147    #[inline]
148    pub fn has_ctrl(&self) -> bool {
149        *self & KeyMod::Ctrl != KeyMod::None
150    }
151
152    /// Returns `true` if the `Super` modifier is present.
153    #[inline]
154    pub fn has_super(&self) -> bool {
155        *self & KeyMod::Super != KeyMod::None
156    }
157
158    /// Returns `true` if the `Hyper` modifier is present.
159    #[inline]
160    pub fn has_hyper(&self) -> bool {
161        *self & KeyMod::Hyper != KeyMod::None
162    }
163
164    /// Returns `true` if the `Meta` modifier is present.
165    #[inline]
166    pub fn has_meta(&self) -> bool {
167        *self & KeyMod::Meta != KeyMod::None
168    }
169
170    /// Returns `true` if the `CapsLock` modifier is present.
171    #[inline]
172    pub fn has_capslock(&self) -> bool {
173        *self & KeyMod::CapsLock != KeyMod::None
174    }
175
176    /// Returns `true` if the `NumLock` modifier is present.
177    #[inline]
178    pub fn has_numlock(&self) -> bool {
179        *self & KeyMod::NumLock != KeyMod::None
180    }
181}