libnotcurses_sys/key/
keymod.rs

1//!
2
3/// A bitmask of [`NcKey`][crate::NcKey] modifiers.
4///
5/// # Default
6/// *[`NcKeyMod::None`]
7///
8/// # Flags
9/// - [`Shift`][NcKeyMod::Shift]
10/// - [`Alt`][NcKeyMod::Alt]
11/// - [`Ctrl`][NcKeyMod::Ctrl]
12/// - [`Super`][NcKeyMod::Super]
13/// - [`Hyper`][NcKeyMod::Hyper]
14/// - [`Meta`][NcKeyMod::Meta]
15/// - [`CapsLock`][NcKeyMod::CapsLock]
16/// - [`NumLock`][NcKeyMod::NumLock]
17/// - [`None`][NcKeyMod::None]
18/// - [`Mask`][NcKeyMod::Mask]
19#[repr(transparent)]
20#[derive(Clone, Copy, PartialEq, Eq)]
21pub struct NcKeyMod(pub u32);
22
23/// # Flags
24impl NcKeyMod {
25    ///
26    pub const Shift: Self = Self(c_api::NCKEY_MOD_SHIFT);
27
28    ///
29    pub const Alt: Self = Self(c_api::NCKEY_MOD_ALT);
30
31    ///
32    pub const Ctrl: Self = Self(c_api::NCKEY_MOD_CTRL);
33
34    ///
35    pub const Super: Self = Self(c_api::NCKEY_MOD_SUPER);
36
37    ///
38    pub const Hyper: Self = Self(c_api::NCKEY_MOD_HYPER);
39
40    ///
41    pub const Meta: Self = Self(c_api::NCKEY_MOD_META);
42
43    ///
44    pub const CapsLock: Self = Self(c_api::NCKEY_MOD_CAPSLOCK);
45
46    ///
47    pub const NumLock: Self = Self(c_api::NCKEY_MOD_NUMLOCK);
48
49    /// None of the modifiers (all bits set to 0).
50    pub const None: Self = Self(0);
51
52    /// The modifier mask (all bits set to 1).
53    pub const Mask: Self = Self(u32::MAX);
54}
55
56/// # Aliases
57impl NcKeyMod {
58    ///
59    pub const Control: Self = Self::Ctrl;
60}
61
62/// # Methods
63impl NcKeyMod {
64    /// Returns true if no modifiers are present.
65    pub fn none_p(&self) -> bool {
66        *self == NcKeyMod::None
67    }
68
69    /// Returns true if the `Shift` modifier is present.
70    pub fn shift_p(&self) -> bool {
71        *self & NcKeyMod::Shift != NcKeyMod::None
72    }
73
74    /// Returns true if the `Alt` modifier is present.
75    pub fn alt_p(&self) -> bool {
76        *self & NcKeyMod::Alt != NcKeyMod::None
77    }
78
79    /// Returns true if the `Ctrl` modifier is present.
80    pub fn ctrl_p(&self) -> bool {
81        *self & NcKeyMod::Ctrl != NcKeyMod::None
82    }
83
84    /// Returns true if the `Super` modifier is present.
85    pub fn super_p(&self) -> bool {
86        *self & NcKeyMod::Super != NcKeyMod::None
87    }
88
89    /// Returns true if the `Hyper` modifier is present.
90    pub fn hyper_p(&self) -> bool {
91        *self & NcKeyMod::Hyper != NcKeyMod::None
92    }
93
94    /// Returns true if the `Meta` modifier is present.
95    pub fn meta_p(&self) -> bool {
96        *self & NcKeyMod::Meta != NcKeyMod::None
97    }
98
99    /// Returns true if the `CapsLock` modifier is present.
100    pub fn capslock_p(&self) -> bool {
101        *self & NcKeyMod::CapsLock != NcKeyMod::None
102    }
103
104    /// Returns true if the `NumLock` modifier is present.
105    pub fn numlock_p(&self) -> bool {
106        *self & NcKeyMod::NumLock != NcKeyMod::None
107    }
108}
109
110mod core_impls {
111    use core::fmt;
112
113    #[cfg(not(feature = "std"))]
114    use alloc::string::String;
115
116    use super::NcKeyMod;
117
118    impl Default for NcKeyMod {
119        fn default() -> Self {
120            Self::None
121        }
122    }
123
124    impl fmt::Display for NcKeyMod {
125        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126            let mut string = String::new();
127
128            if self.none_p() {
129                string += "None ";
130            } else {
131                if self.capslock_p() {
132                    string += "CapsLock+";
133                }
134                if self.numlock_p() {
135                    string += "NumLock+";
136                }
137                if self.ctrl_p() {
138                    string += "Ctrl+";
139                }
140                if self.shift_p() {
141                    string += "Shift+";
142                }
143                if self.alt_p() {
144                    string += "Alt+";
145                }
146                if self.meta_p() {
147                    string += "Meta+";
148                }
149                if self.super_p() {
150                    string += "Super+";
151                }
152                if self.hyper_p() {
153                    string += "Hyper+";
154                }
155            }
156            string.pop();
157
158            write!(f, "{}", string)
159        }
160    }
161
162    impl fmt::Debug for NcKeyMod {
163        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
164            write!(f, "NcKeyMod::{}", self)
165        }
166    }
167
168    crate::from_primitive![NcKeyMod, u32];
169    crate::unit_impl_from![NcKeyMod, u32];
170
171    crate::unit_impl_ops![bitwise; NcKeyMod, u32];
172}
173
174pub(crate) mod c_api {
175    use crate::c_api::ffi;
176
177    ///
178    pub const NCKEY_MOD_SHIFT: u32 = ffi::NCKEY_MOD_SHIFT;
179
180    ///
181    pub const NCKEY_MOD_ALT: u32 = ffi::NCKEY_MOD_ALT;
182
183    ///
184    pub const NCKEY_MOD_CTRL: u32 = ffi::NCKEY_MOD_CTRL;
185
186    ///
187    pub const NCKEY_MOD_SUPER: u32 = ffi::NCKEY_MOD_SUPER;
188
189    ///
190    pub const NCKEY_MOD_HYPER: u32 = ffi::NCKEY_MOD_HYPER;
191
192    ///
193    pub const NCKEY_MOD_META: u32 = ffi::NCKEY_MOD_META;
194
195    ///
196    pub const NCKEY_MOD_CAPSLOCK: u32 = ffi::NCKEY_MOD_CAPSLOCK;
197
198    ///
199    pub const NCKEY_MOD_NUMLOCK: u32 = ffi::NCKEY_MOD_NUMLOCK;
200}