From 996be15f369e125e2da46194eefc149d7d14230e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 28 Feb 2024 09:40:14 +0100 Subject: Prioritize `TextInput` and `TextEditor` commands over text insertion --- widget/src/text_editor.rs | 62 +++++++++--------- widget/src/text_input.rs | 156 ++++++++++++++++++++++++---------------------- 2 files changed, 115 insertions(+), 103 deletions(-) diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 79432fe9..bad3ef4d 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -686,6 +686,37 @@ impl Update { text, .. } if state.is_focused => { + match key.as_ref() { + keyboard::Key::Named(key::Named::Enter) => { + return edit(Edit::Enter); + } + keyboard::Key::Named(key::Named::Backspace) => { + return edit(Edit::Backspace); + } + keyboard::Key::Named(key::Named::Delete) => { + return edit(Edit::Delete); + } + keyboard::Key::Named(key::Named::Escape) => { + return Some(Self::Unfocus); + } + keyboard::Key::Character("c") + if modifiers.command() => + { + return Some(Self::Copy); + } + keyboard::Key::Character("x") + if modifiers.command() => + { + return Some(Self::Cut); + } + keyboard::Key::Character("v") + if modifiers.command() && !modifiers.alt() => + { + return Some(Self::Paste); + } + _ => {} + } + if let Some(text) = text { if let Some(c) = text.chars().find(|c| !c.is_control()) { @@ -711,36 +742,7 @@ impl Update { } } - 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::Key::Character("x") - if modifiers.command() => - { - Some(Self::Cut) - } - keyboard::Key::Character("v") - if modifiers.command() && !modifiers.alt() => - { - Some(Self::Paste) - } - _ => None, - } + None } _ => None, }, diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 73346b3d..efdfb0dc 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -762,12 +762,92 @@ where let modifiers = state.keyboard_modifiers; focus.updated_at = Instant::now(); + match key.as_ref() { + keyboard::Key::Character("c") + if state.keyboard_modifiers.command() => + { + if let Some((start, end)) = + state.cursor.selection(value) + { + clipboard.write( + clipboard::Kind::Standard, + value.select(start, end).to_string(), + ); + } + + return event::Status::Captured; + } + keyboard::Key::Character("x") + if state.keyboard_modifiers.command() => + { + if let Some((start, end)) = + state.cursor.selection(value) + { + clipboard.write( + clipboard::Kind::Standard, + value.select(start, end).to_string(), + ); + } + + let mut editor = Editor::new(value, &mut state.cursor); + editor.delete(); + + let message = (on_input)(editor.contents()); + shell.publish(message); + + update_cache(state, value); + + return event::Status::Captured; + } + keyboard::Key::Character("v") + if state.keyboard_modifiers.command() + && !state.keyboard_modifiers.alt() => + { + let content = match state.is_pasting.take() { + Some(content) => content, + None => { + let content: String = clipboard + .read(clipboard::Kind::Standard) + .unwrap_or_default() + .chars() + .filter(|c| !c.is_control()) + .collect(); + + Value::new(&content) + } + }; + + let mut editor = Editor::new(value, &mut state.cursor); + + editor.paste(content.clone()); + + let message = if let Some(paste) = &on_paste { + (paste)(editor.contents()) + } else { + (on_input)(editor.contents()) + }; + shell.publish(message); + + state.is_pasting = Some(content); + + update_cache(state, value); + + return event::Status::Captured; + } + keyboard::Key::Character("a") + if state.keyboard_modifiers.command() => + { + state.cursor.select_all(value); + } + _ => {} + } + if let Some(text) = text { state.is_pasting = None; - let c = text.chars().next().unwrap_or_default(); - - if !c.is_control() { + if let Some(c) = + text.chars().next().filter(|c| !c.is_control()) + { let mut editor = Editor::new(value, &mut state.cursor); editor.insert(c); @@ -880,76 +960,6 @@ where state.cursor.move_to(value.len()); } } - keyboard::Key::Character("c") - if state.keyboard_modifiers.command() => - { - if let Some((start, end)) = - state.cursor.selection(value) - { - clipboard.write( - clipboard::Kind::Standard, - value.select(start, end).to_string(), - ); - } - } - keyboard::Key::Character("x") - if state.keyboard_modifiers.command() => - { - if let Some((start, end)) = - state.cursor.selection(value) - { - clipboard.write( - clipboard::Kind::Standard, - value.select(start, end).to_string(), - ); - } - - let mut editor = Editor::new(value, &mut state.cursor); - editor.delete(); - - let message = (on_input)(editor.contents()); - shell.publish(message); - - update_cache(state, value); - } - keyboard::Key::Character("v") - if state.keyboard_modifiers.command() - && !state.keyboard_modifiers.alt() => - { - let content = match state.is_pasting.take() { - Some(content) => content, - None => { - let content: String = clipboard - .read(clipboard::Kind::Standard) - .unwrap_or_default() - .chars() - .filter(|c| !c.is_control()) - .collect(); - - Value::new(&content) - } - }; - - let mut editor = Editor::new(value, &mut state.cursor); - - editor.paste(content.clone()); - - let message = if let Some(paste) = &on_paste { - (paste)(editor.contents()) - } else { - (on_input)(editor.contents()) - }; - shell.publish(message); - - state.is_pasting = Some(content); - - update_cache(state, value); - } - keyboard::Key::Character("a") - if state.keyboard_modifiers.command() => - { - state.cursor.select_all(value); - } keyboard::Key::Named(key::Named::Escape) => { state.is_focused = None; state.is_dragging = false; -- cgit