From e139aae1439d362ada017a05c9554eaae0883888 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 04:34:29 +0200 Subject: Split `Input` keyboard event by `ButtonState` --- native/src/input/keyboard/event.rs | 15 ++++++++++----- native/src/widget/pane_grid.rs | 26 +++++++++++++------------- native/src/widget/text_input.rs | 9 +++------ 3 files changed, 26 insertions(+), 24 deletions(-) (limited to 'native') diff --git a/native/src/input/keyboard/event.rs b/native/src/input/keyboard/event.rs index 862f30c4..bc8437a8 100644 --- a/native/src/input/keyboard/event.rs +++ b/native/src/input/keyboard/event.rs @@ -1,5 +1,4 @@ use super::{KeyCode, ModifiersState}; -use crate::input::ButtonState; /// A keyboard event. /// @@ -9,11 +8,17 @@ use crate::input::ButtonState; /// [open an issue]: https://github.com/hecrj/iced/issues #[derive(Debug, Clone, Copy, PartialEq)] pub enum Event { - /// A keyboard key was pressed or released. - Input { - /// The state of the key - state: ButtonState, + /// A keyboard key was pressed. + KeyPressed { + /// The key identifier + key_code: KeyCode, + + /// The state of the modifier keys + modifiers: ModifiersState, + }, + /// A keyboard key was released. + KeyReleased { /// The key identifier key_code: KeyCode, diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index f84775ed..0b735ad3 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -532,24 +532,19 @@ where Event::Mouse(mouse::Event::CursorMoved { .. }) => { self.trigger_resize(layout, cursor_position, messages); } - Event::Keyboard(keyboard::Event::Input { + Event::Keyboard(keyboard::Event::KeyPressed { modifiers, key_code, - state, }) => { if let Some(on_key_press) = &self.on_key_press { // TODO: Discard when event is captured - if state == ButtonState::Pressed { - if let Some(_) = self.state.active_pane() { - if modifiers.matches(self.modifier_keys) { - if let Some(message) = - on_key_press(KeyPressEvent { - key_code, - modifiers, - }) - { - messages.push(message); - } + if let Some(_) = self.state.active_pane() { + if modifiers.matches(self.modifier_keys) { + if let Some(message) = on_key_press(KeyPressEvent { + key_code, + modifiers, + }) { + messages.push(message); } } } @@ -557,6 +552,11 @@ where *self.pressed_modifiers = modifiers; } + Event::Keyboard(keyboard::Event::KeyReleased { + modifiers, .. + }) => { + *self.pressed_modifiers = modifiers; + } _ => {} } diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 7d1a7415..ea6921b5 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -327,9 +327,8 @@ where let message = (self.on_change)(editor.contents()); messages.push(message); } - Event::Keyboard(keyboard::Event::Input { + Event::Keyboard(keyboard::Event::KeyPressed { key_code, - state: ButtonState::Pressed, modifiers, }) if self.state.is_focused => match key_code { keyboard::KeyCode::Enter => { @@ -473,10 +472,8 @@ where } _ => {} }, - Event::Keyboard(keyboard::Event::Input { - key_code, - state: ButtonState::Released, - .. + Event::Keyboard(keyboard::Event::KeyReleased { + key_code, .. }) => match key_code { keyboard::KeyCode::V => { self.state.is_pasting = None; -- cgit