From d612bf56784b41346f18ad9eda4f6d54d699dcb5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 25 Nov 2020 05:31:45 +0100 Subject: Rename `keyboard::ModifiersState` to `Modifiers` --- core/src/keyboard.rs | 4 +-- core/src/keyboard/event.rs | 8 +++--- core/src/keyboard/modifiers.rs | 45 ++++++++++++++++++++++++++++++++ core/src/keyboard/modifiers_state.rs | 50 ------------------------------------ 4 files changed, 51 insertions(+), 56 deletions(-) create mode 100644 core/src/keyboard/modifiers.rs delete mode 100644 core/src/keyboard/modifiers_state.rs (limited to 'core') diff --git a/core/src/keyboard.rs b/core/src/keyboard.rs index b26bdb3d..61e017ad 100644 --- a/core/src/keyboard.rs +++ b/core/src/keyboard.rs @@ -1,8 +1,8 @@ //! Reuse basic keyboard types. mod event; mod key_code; -mod modifiers_state; +mod modifiers; pub use event::Event; pub use key_code::KeyCode; -pub use modifiers_state::ModifiersState; +pub use modifiers::Modifiers; diff --git a/core/src/keyboard/event.rs b/core/src/keyboard/event.rs index d142c3bc..0564c171 100644 --- a/core/src/keyboard/event.rs +++ b/core/src/keyboard/event.rs @@ -1,4 +1,4 @@ -use super::{KeyCode, ModifiersState}; +use super::{KeyCode, Modifiers}; /// A keyboard event. /// @@ -14,7 +14,7 @@ pub enum Event { key_code: KeyCode, /// The state of the modifier keys - modifiers: ModifiersState, + modifiers: Modifiers, }, /// A keyboard key was released. @@ -23,12 +23,12 @@ pub enum Event { key_code: KeyCode, /// The state of the modifier keys - modifiers: ModifiersState, + modifiers: Modifiers, }, /// A unicode character was received. CharacterReceived(char), /// The keyboard modifiers have changed. - ModifiersChanged(ModifiersState), + ModifiersChanged(Modifiers), } diff --git a/core/src/keyboard/modifiers.rs b/core/src/keyboard/modifiers.rs new file mode 100644 index 00000000..d2a0500e --- /dev/null +++ b/core/src/keyboard/modifiers.rs @@ -0,0 +1,45 @@ +/// The current state of the keyboard modifiers. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct Modifiers { + /// Whether a shift key is pressed + pub shift: bool, + + /// Whether a control key is pressed + pub control: bool, + + /// Whether an alt key is pressed + pub alt: bool, + + /// Whether a logo key is pressed (e.g. windows key, command key...) + pub logo: bool, +} + +impl Modifiers { + /// Returns true if a "command key" is pressed in the [`Modifiers`]. + /// + /// The "command key" is the main modifier key used to issue commands in the + /// current platform. Specifically: + /// + /// - It is the `logo` or command key (⌘) on macOS + /// - It is the `control` key on other platforms + pub fn is_command_pressed(self) -> bool { + #[cfg(target_os = "macos")] + let is_pressed = self.logo; + + #[cfg(not(target_os = "macos"))] + let is_pressed = self.control; + + is_pressed + } + + /// Returns true if the current [`Modifiers`] have at least the same + /// keys pressed as the provided ones, and false otherwise. + pub fn matches(&self, modifiers: Self) -> bool { + let shift = !modifiers.shift || self.shift; + let control = !modifiers.control || self.control; + let alt = !modifiers.alt || self.alt; + let logo = !modifiers.logo || self.logo; + + shift && control && alt && logo + } +} diff --git a/core/src/keyboard/modifiers_state.rs b/core/src/keyboard/modifiers_state.rs deleted file mode 100644 index 254013c3..00000000 --- a/core/src/keyboard/modifiers_state.rs +++ /dev/null @@ -1,50 +0,0 @@ -/// The current state of the keyboard modifiers. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub struct ModifiersState { - /// Whether a shift key is pressed - pub shift: bool, - - /// Whether a control key is pressed - pub control: bool, - - /// Whether an alt key is pressed - pub alt: bool, - - /// Whether a logo key is pressed (e.g. windows key, command key...) - pub logo: bool, -} - -impl ModifiersState { - /// Returns true if the current [`ModifiersState`] has a "command key" - /// pressed. - /// - /// The "command key" is the main modifier key used to issue commands in the - /// current platform. Specifically: - /// - /// - It is the `logo` or command key (⌘) on macOS - /// - It is the `control` key on other platforms - /// - /// [`ModifiersState`]: struct.ModifiersState.html - pub fn is_command_pressed(self) -> bool { - #[cfg(target_os = "macos")] - let is_pressed = self.logo; - - #[cfg(not(target_os = "macos"))] - let is_pressed = self.control; - - is_pressed - } - - /// Returns true if the current [`ModifiersState`] has at least the same - /// modifiers enabled as the given value, and false otherwise. - /// - /// [`ModifiersState`]: struct.ModifiersState.html - pub fn matches(&self, modifiers: ModifiersState) -> bool { - let shift = !modifiers.shift || self.shift; - let control = !modifiers.control || self.control; - let alt = !modifiers.alt || self.alt; - let logo = !modifiers.logo || self.logo; - - shift && control && alt && logo - } -} -- cgit