Rename `keyboard::ModifiersState` to `Modifiers`

This commit is contained in:
Héctor Ramón Jiménez 2020-11-25 05:31:45 +01:00
parent 08e0b9ffbd
commit d612bf5678
6 changed files with 22 additions and 31 deletions

View File

@ -1,8 +1,8 @@
//! Reuse basic keyboard types. //! Reuse basic keyboard types.
mod event; mod event;
mod key_code; mod key_code;
mod modifiers_state; mod modifiers;
pub use event::Event; pub use event::Event;
pub use key_code::KeyCode; pub use key_code::KeyCode;
pub use modifiers_state::ModifiersState; pub use modifiers::Modifiers;

View File

@ -1,4 +1,4 @@
use super::{KeyCode, ModifiersState}; use super::{KeyCode, Modifiers};
/// A keyboard event. /// A keyboard event.
/// ///
@ -14,7 +14,7 @@ pub enum Event {
key_code: KeyCode, key_code: KeyCode,
/// The state of the modifier keys /// The state of the modifier keys
modifiers: ModifiersState, modifiers: Modifiers,
}, },
/// A keyboard key was released. /// A keyboard key was released.
@ -23,12 +23,12 @@ pub enum Event {
key_code: KeyCode, key_code: KeyCode,
/// The state of the modifier keys /// The state of the modifier keys
modifiers: ModifiersState, modifiers: Modifiers,
}, },
/// A unicode character was received. /// A unicode character was received.
CharacterReceived(char), CharacterReceived(char),
/// The keyboard modifiers have changed. /// The keyboard modifiers have changed.
ModifiersChanged(ModifiersState), ModifiersChanged(Modifiers),
} }

View File

@ -1,6 +1,6 @@
/// The current state of the keyboard modifiers. /// The current state of the keyboard modifiers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ModifiersState { pub struct Modifiers {
/// Whether a shift key is pressed /// Whether a shift key is pressed
pub shift: bool, pub shift: bool,
@ -14,17 +14,14 @@ pub struct ModifiersState {
pub logo: bool, pub logo: bool,
} }
impl ModifiersState { impl Modifiers {
/// Returns true if the current [`ModifiersState`] has a "command key" /// Returns true if a "command key" is pressed in the [`Modifiers`].
/// pressed.
/// ///
/// The "command key" is the main modifier key used to issue commands in the /// The "command key" is the main modifier key used to issue commands in the
/// current platform. Specifically: /// current platform. Specifically:
/// ///
/// - It is the `logo` or command key (⌘) on macOS /// - It is the `logo` or command key (⌘) on macOS
/// - It is the `control` key on other platforms /// - It is the `control` key on other platforms
///
/// [`ModifiersState`]: struct.ModifiersState.html
pub fn is_command_pressed(self) -> bool { pub fn is_command_pressed(self) -> bool {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
let is_pressed = self.logo; let is_pressed = self.logo;
@ -35,11 +32,9 @@ impl ModifiersState {
is_pressed is_pressed
} }
/// Returns true if the current [`ModifiersState`] has at least the same /// Returns true if the current [`Modifiers`] have at least the same
/// modifiers enabled as the given value, and false otherwise. /// keys pressed as the provided ones, and false otherwise.
/// pub fn matches(&self, modifiers: Self) -> bool {
/// [`ModifiersState`]: struct.ModifiersState.html
pub fn matches(&self, modifiers: ModifiersState) -> bool {
let shift = !modifiers.shift || self.shift; let shift = !modifiers.shift || self.shift;
let control = !modifiers.control || self.control; let control = !modifiers.control || self.control;
let alt = !modifiers.alt || self.alt; let alt = !modifiers.alt || self.alt;

View File

@ -570,7 +570,7 @@ where
self.state.is_pasting = None; self.state.is_pasting = None;
self.state.keyboard_modifiers = self.state.keyboard_modifiers =
keyboard::ModifiersState::default(); keyboard::Modifiers::default();
} }
_ => {} _ => {}
} }
@ -734,7 +734,7 @@ pub struct State {
is_pasting: Option<Value>, is_pasting: Option<Value>,
last_click: Option<mouse::Click>, last_click: Option<mouse::Click>,
cursor: Cursor, cursor: Cursor,
keyboard_modifiers: keyboard::ModifiersState, keyboard_modifiers: keyboard::Modifiers,
// TODO: Add stateful horizontal scrolling offset // TODO: Add stateful horizontal scrolling offset
} }
@ -756,7 +756,7 @@ impl State {
is_pasting: None, is_pasting: None,
last_click: None, last_click: None,
cursor: Cursor::default(), cursor: Cursor::default(),
keyboard_modifiers: keyboard::ModifiersState::default(), keyboard_modifiers: keyboard::Modifiers::default(),
} }
} }
@ -873,9 +873,7 @@ fn find_cursor_position<Renderer: self::Renderer>(
mod platform { mod platform {
use crate::keyboard; use crate::keyboard;
pub fn is_jump_modifier_pressed( pub fn is_jump_modifier_pressed(modifiers: keyboard::Modifiers) -> bool {
modifiers: keyboard::ModifiersState,
) -> bool {
if cfg!(target_os = "macos") { if cfg!(target_os = "macos") {
modifiers.alt modifiers.alt
} else { } else {

View File

@ -1,2 +1,2 @@
//! Listen and react to keyboard events. //! Listen and react to keyboard events.
pub use crate::runtime::keyboard::{Event, KeyCode, ModifiersState}; pub use crate::runtime::keyboard::{Event, KeyCode, Modifiers};

View File

@ -3,7 +3,7 @@
//! [`winit`]: https://github.com/rust-windowing/winit //! [`winit`]: https://github.com/rust-windowing/winit
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
use crate::{ use crate::{
keyboard::{self, KeyCode, ModifiersState}, keyboard::{self, KeyCode, Modifiers},
mouse, window, Event, Mode, Point, mouse, window, Event, Mode, Point,
}; };
@ -89,7 +89,7 @@ pub fn window_event(
.. ..
} => Some(Event::Keyboard({ } => Some(Event::Keyboard({
let key_code = key_code(*virtual_keycode); let key_code = key_code(*virtual_keycode);
let modifiers = modifiers_state(modifiers); let modifiers = self::modifiers(modifiers);
match state { match state {
winit::event::ElementState::Pressed => { winit::event::ElementState::Pressed => {
@ -107,7 +107,7 @@ pub fn window_event(
} }
})), })),
WindowEvent::ModifiersChanged(new_modifiers) => Some(Event::Keyboard( WindowEvent::ModifiersChanged(new_modifiers) => Some(Event::Keyboard(
keyboard::Event::ModifiersChanged(modifiers_state(*new_modifiers)), keyboard::Event::ModifiersChanged(self::modifiers(*new_modifiers)),
)), )),
WindowEvent::HoveredFile(path) => { WindowEvent::HoveredFile(path) => {
Some(Event::Window(window::Event::FileHovered(path.clone()))) 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 /// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
pub fn modifiers_state( pub fn modifiers(modifiers: winit::event::ModifiersState) -> Modifiers {
modifiers: winit::event::ModifiersState, Modifiers {
) -> ModifiersState {
ModifiersState {
shift: modifiers.shift(), shift: modifiers.shift(),
control: modifiers.ctrl(), control: modifiers.ctrl(),
alt: modifiers.alt(), alt: modifiers.alt(),