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 ------------------------------------ native/src/widget/text_input.rs | 10 +++----- src/keyboard.rs | 2 +- winit/src/conversion.rs | 12 ++++----- 7 files changed, 61 insertions(+), 70 deletions(-) create mode 100644 core/src/keyboard/modifiers.rs delete mode 100644 core/src/keyboard/modifiers_state.rs 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 - } -} diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index a302e483..e67ea365 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -570,7 +570,7 @@ where self.state.is_pasting = None; self.state.keyboard_modifiers = - keyboard::ModifiersState::default(); + keyboard::Modifiers::default(); } _ => {} } @@ -734,7 +734,7 @@ pub struct State { is_pasting: Option, last_click: Option, cursor: Cursor, - keyboard_modifiers: keyboard::ModifiersState, + keyboard_modifiers: keyboard::Modifiers, // TODO: Add stateful horizontal scrolling offset } @@ -756,7 +756,7 @@ impl State { is_pasting: None, last_click: None, cursor: Cursor::default(), - keyboard_modifiers: keyboard::ModifiersState::default(), + keyboard_modifiers: keyboard::Modifiers::default(), } } @@ -873,9 +873,7 @@ fn find_cursor_position( mod platform { use crate::keyboard; - pub fn is_jump_modifier_pressed( - modifiers: keyboard::ModifiersState, - ) -> bool { + pub fn is_jump_modifier_pressed(modifiers: keyboard::Modifiers) -> bool { if cfg!(target_os = "macos") { modifiers.alt } else { diff --git a/src/keyboard.rs b/src/keyboard.rs index 0b3e894d..2134a66b 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -1,2 +1,2 @@ //! Listen and react to keyboard events. -pub use crate::runtime::keyboard::{Event, KeyCode, ModifiersState}; +pub use crate::runtime::keyboard::{Event, KeyCode, Modifiers}; diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index de38f246..a9fa2ffc 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -3,7 +3,7 @@ //! [`winit`]: https://github.com/rust-windowing/winit //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native use crate::{ - keyboard::{self, KeyCode, ModifiersState}, + keyboard::{self, KeyCode, Modifiers}, mouse, window, Event, Mode, Point, }; @@ -89,7 +89,7 @@ pub fn window_event( .. } => Some(Event::Keyboard({ let key_code = key_code(*virtual_keycode); - let modifiers = modifiers_state(modifiers); + let modifiers = self::modifiers(modifiers); match state { winit::event::ElementState::Pressed => { @@ -107,7 +107,7 @@ pub fn window_event( } })), WindowEvent::ModifiersChanged(new_modifiers) => Some(Event::Keyboard( - keyboard::Event::ModifiersChanged(modifiers_state(*new_modifiers)), + keyboard::Event::ModifiersChanged(self::modifiers(*new_modifiers)), )), WindowEvent::HoveredFile(path) => { Some(Event::Window(window::Event::FileHovered(path.clone()))) @@ -180,10 +180,8 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { /// /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native -pub fn modifiers_state( - modifiers: winit::event::ModifiersState, -) -> ModifiersState { - ModifiersState { +pub fn modifiers(modifiers: winit::event::ModifiersState) -> Modifiers { + Modifiers { shift: modifiers.shift(), control: modifiers.ctrl(), alt: modifiers.alt(), -- cgit