summaryrefslogtreecommitdiffstats
path: root/widget/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-16 13:28:00 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-01-16 13:28:00 +0100
commit64d1ce5532f55d152fa5819532a138da2dca1a39 (patch)
tree1a242bde122b5e715f2e980c5541353e2ae8b048 /widget/src
parent534c7dd7b0bc515c31b6de87b4aa6a35b44c46a0 (diff)
downloadiced-64d1ce5532f55d152fa5819532a138da2dca1a39.tar.gz
iced-64d1ce5532f55d152fa5819532a138da2dca1a39.tar.bz2
iced-64d1ce5532f55d152fa5819532a138da2dca1a39.zip
Refactor `KeyCode` into `Key` and `Location`
Diffstat (limited to 'widget/src')
-rw-r--r--widget/src/combo_box.rs14
-rw-r--r--widget/src/text_editor.rs67
-rw-r--r--widget/src/text_input.rs54
3 files changed, 76 insertions, 59 deletions
diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs
index 1b2fa947..73beeac3 100644
--- a/widget/src/combo_box.rs
+++ b/widget/src/combo_box.rs
@@ -1,6 +1,7 @@
//! Display a dropdown list of searchable and selectable options.
use crate::core::event::{self, Event};
use crate::core::keyboard;
+use crate::core::keyboard::key;
use crate::core::layout::{self, Layout};
use crate::core::mouse;
use crate::core::overlay;
@@ -436,14 +437,14 @@ where
}
if let Event::Keyboard(keyboard::Event::KeyPressed {
- key_code,
+ key: keyboard::Key::Named(named_key),
modifiers,
..
}) = event
{
let shift_modifer = modifiers.shift();
- match (key_code, shift_modifer) {
- (keyboard::KeyCode::Enter, _) => {
+ match (named_key, shift_modifer) {
+ (key::Named::Enter, _) => {
if let Some(index) = &menu.hovered_option {
if let Some(option) =
state.filtered_options.options.get(*index)
@@ -455,8 +456,7 @@ where
event_status = event::Status::Captured;
}
- (keyboard::KeyCode::Up, _)
- | (keyboard::KeyCode::Tab, true) => {
+ (key::Named::ArrowUp, _) | (key::Named::Tab, true) => {
if let Some(index) = &mut menu.hovered_option {
if *index == 0 {
*index = state
@@ -492,8 +492,8 @@ where
event_status = event::Status::Captured;
}
- (keyboard::KeyCode::Down, _)
- | (keyboard::KeyCode::Tab, false)
+ (key::Named::ArrowDown, _)
+ | (key::Named::Tab, false)
if !modifiers.shift() =>
{
if let Some(index) = &mut menu.hovered_option {
diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs
index b95a45e4..09a0cac0 100644
--- a/widget/src/text_editor.rs
+++ b/widget/src/text_editor.rs
@@ -1,6 +1,7 @@
//! Display a multi-line text input for text editing.
use crate::core::event::{self, Event};
use crate::core::keyboard;
+use crate::core::keyboard::key;
use crate::core::layout::{self, Layout};
use crate::core::mouse;
use crate::core::renderer;
@@ -646,34 +647,48 @@ impl Update {
},
Event::Keyboard(event) => match event {
keyboard::Event::KeyPressed {
- key_code,
+ key,
modifiers,
text,
+ ..
} if state.is_focused => {
- if let Some(motion) = motion(key_code) {
- let motion =
- if platform::is_jump_modifier_pressed(modifiers) {
+ if let keyboard::Key::Named(named_key) = key.as_ref() {
+ if let Some(motion) = motion(named_key) {
+ let motion = if platform::is_jump_modifier_pressed(
+ modifiers,
+ ) {
motion.widen()
} else {
motion
};
- return action(if modifiers.shift() {
- Action::Select(motion)
- } else {
- Action::Move(motion)
- });
+ return action(if modifiers.shift() {
+ Action::Select(motion)
+ } else {
+ Action::Move(motion)
+ });
+ }
}
- match key_code {
- keyboard::KeyCode::Enter => edit(Edit::Enter),
- keyboard::KeyCode::Backspace => edit(Edit::Backspace),
- keyboard::KeyCode::Delete => edit(Edit::Delete),
- keyboard::KeyCode::Escape => Some(Self::Unfocus),
- keyboard::KeyCode::C if modifiers.command() => {
+ match key.as_ref() {
+ keyboard::Key::Named(key::Named::Enter) => {
+ edit(Edit::Enter)
+ }
+ keyboard::Key::Named(key::Named::Backspace) => {
+ edit(Edit::Backspace)
+ }
+ keyboard::Key::Named(key::Named::Delete) => {
+ edit(Edit::Delete)
+ }
+ keyboard::Key::Named(key::Named::Escape) => {
+ Some(Self::Unfocus)
+ }
+ keyboard::Key::Character("c")
+ if modifiers.command() =>
+ {
Some(Self::Copy)
}
- keyboard::KeyCode::V
+ keyboard::Key::Character("v")
if modifiers.command() && !modifiers.alt() =>
{
Some(Self::Paste)
@@ -694,16 +709,16 @@ impl Update {
}
}
-fn motion(key_code: keyboard::KeyCode) -> Option<Motion> {
- match key_code {
- keyboard::KeyCode::Left => Some(Motion::Left),
- keyboard::KeyCode::Right => Some(Motion::Right),
- keyboard::KeyCode::Up => Some(Motion::Up),
- keyboard::KeyCode::Down => Some(Motion::Down),
- keyboard::KeyCode::Home => Some(Motion::Home),
- keyboard::KeyCode::End => Some(Motion::End),
- keyboard::KeyCode::PageUp => Some(Motion::PageUp),
- keyboard::KeyCode::PageDown => Some(Motion::PageDown),
+fn motion(key: key::Named) -> Option<Motion> {
+ match key {
+ key::Named::ArrowLeft => Some(Motion::Left),
+ key::Named::ArrowRight => Some(Motion::Right),
+ key::Named::ArrowUp => Some(Motion::Up),
+ key::Named::ArrowDown => Some(Motion::Down),
+ key::Named::Home => Some(Motion::Home),
+ key::Named::End => Some(Motion::End),
+ key::Named::PageUp => Some(Motion::PageUp),
+ key::Named::PageDown => Some(Motion::PageDown),
_ => None,
}
}
diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs
index 8d28e8ee..c3dce8be 100644
--- a/widget/src/text_input.rs
+++ b/widget/src/text_input.rs
@@ -14,6 +14,7 @@ use editor::Editor;
use crate::core::alignment;
use crate::core::event::{self, Event};
use crate::core::keyboard;
+use crate::core::keyboard::key;
use crate::core::layout;
use crate::core::mouse::{self, click};
use crate::core::renderer;
@@ -748,9 +749,7 @@ where
return event::Status::Captured;
}
}
- Event::Keyboard(keyboard::Event::KeyPressed {
- key_code, text, ..
- }) => {
+ Event::Keyboard(keyboard::Event::KeyPressed { key, text, .. }) => {
let state = state();
if let Some(focus) = &mut state.is_focused {
@@ -761,14 +760,13 @@ where
let modifiers = state.keyboard_modifiers;
focus.updated_at = Instant::now();
- match key_code {
- keyboard::KeyCode::Enter
- | keyboard::KeyCode::NumpadEnter => {
+ match key.as_ref() {
+ keyboard::Key::Named(key::Named::Enter) => {
if let Some(on_submit) = on_submit.clone() {
shell.publish(on_submit);
}
}
- keyboard::KeyCode::Backspace => {
+ keyboard::Key::Named(key::Named::Backspace) => {
if platform::is_jump_modifier_pressed(modifiers)
&& state.cursor.selection(value).is_none()
{
@@ -788,7 +786,7 @@ where
update_cache(state, value);
}
- keyboard::KeyCode::Delete => {
+ keyboard::Key::Named(key::Named::Delete) => {
if platform::is_jump_modifier_pressed(modifiers)
&& state.cursor.selection(value).is_none()
{
@@ -810,7 +808,7 @@ where
update_cache(state, value);
}
- keyboard::KeyCode::Left => {
+ keyboard::Key::Named(key::Named::ArrowLeft) => {
if platform::is_jump_modifier_pressed(modifiers)
&& !is_secure
{
@@ -825,7 +823,7 @@ where
state.cursor.move_left(value);
}
}
- keyboard::KeyCode::Right => {
+ keyboard::Key::Named(key::Named::ArrowRight) => {
if platform::is_jump_modifier_pressed(modifiers)
&& !is_secure
{
@@ -840,7 +838,7 @@ where
state.cursor.move_right(value);
}
}
- keyboard::KeyCode::Home => {
+ keyboard::Key::Named(key::Named::Home) => {
if modifiers.shift() {
state
.cursor
@@ -849,7 +847,7 @@ where
state.cursor.move_to(0);
}
}
- keyboard::KeyCode::End => {
+ keyboard::Key::Named(key::Named::End) => {
if modifiers.shift() {
state.cursor.select_range(
state.cursor.start(value),
@@ -859,7 +857,7 @@ where
state.cursor.move_to(value.len());
}
}
- keyboard::KeyCode::C
+ keyboard::Key::Character("c")
if state.keyboard_modifiers.command() =>
{
if let Some((start, end)) =
@@ -869,7 +867,7 @@ where
.write(value.select(start, end).to_string());
}
}
- keyboard::KeyCode::X
+ keyboard::Key::Character("x")
if state.keyboard_modifiers.command() =>
{
if let Some((start, end)) =
@@ -887,7 +885,7 @@ where
update_cache(state, value);
}
- keyboard::KeyCode::V => {
+ keyboard::Key::Character("v") => {
if state.keyboard_modifiers.command()
&& !state.keyboard_modifiers.alt()
{
@@ -924,12 +922,12 @@ where
state.is_pasting = None;
}
}
- keyboard::KeyCode::A
+ keyboard::Key::Character("a")
if state.keyboard_modifiers.command() =>
{
state.cursor.select_all(value);
}
- keyboard::KeyCode::Escape => {
+ keyboard::Key::Named(key::Named::Escape) => {
state.is_focused = None;
state.is_dragging = false;
state.is_pasting = None;
@@ -937,9 +935,11 @@ where
state.keyboard_modifiers =
keyboard::Modifiers::default();
}
- keyboard::KeyCode::Tab
- | keyboard::KeyCode::Up
- | keyboard::KeyCode::Down => {
+ keyboard::Key::Named(
+ key::Named::Tab
+ | key::Named::ArrowUp
+ | key::Named::ArrowDown,
+ ) => {
return event::Status::Ignored;
}
_ => {
@@ -971,17 +971,19 @@ where
return event::Status::Captured;
}
}
- Event::Keyboard(keyboard::Event::KeyReleased { key_code, .. }) => {
+ Event::Keyboard(keyboard::Event::KeyReleased { key, .. }) => {
let state = state();
if state.is_focused.is_some() {
- match key_code {
- keyboard::KeyCode::V => {
+ match key.as_ref() {
+ keyboard::Key::Character("v") => {
state.is_pasting = None;
}
- keyboard::KeyCode::Tab
- | keyboard::KeyCode::Up
- | keyboard::KeyCode::Down => {
+ keyboard::Key::Named(
+ key::Named::Tab
+ | key::Named::ArrowUp
+ | key::Named::ArrowDown,
+ ) => {
return event::Status::Ignored;
}
_ => {}