From bd48946b02268cb5a7ae932e388abfa75fe9e375 Mon Sep 17 00:00:00 2001 From: BradySimon Date: Fri, 8 Mar 2024 09:35:43 -0500 Subject: Add Command + ArrowLeft/Right input behavior for macos --- widget/src/text_input.rs | 81 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 20 deletions(-) (limited to 'widget/src/text_input.rs') diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index e9f07838..2c951c12 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -876,6 +876,54 @@ where update_cache(state, &self.value); } + keyboard::Key::Named(key::Named::Home) => { + if modifiers.shift() { + state.cursor.select_range( + state.cursor.start(&self.value), + 0, + ); + } else { + state.cursor.move_to(0); + } + } + keyboard::Key::Named(key::Named::End) => { + if modifiers.shift() { + state.cursor.select_range( + state.cursor.start(&self.value), + self.value.len(), + ); + } else { + state.cursor.move_to(self.value.len()); + } + } + keyboard::Key::Named(key::Named::ArrowLeft) + if platform::is_macos_command_pressed( + modifiers, + ) => + { + if modifiers.shift() { + state.cursor.select_range( + state.cursor.start(&self.value), + 0, + ); + } else { + state.cursor.move_to(0); + } + } + keyboard::Key::Named(key::Named::ArrowRight) + if platform::is_macos_command_pressed( + modifiers, + ) => + { + if modifiers.shift() { + state.cursor.select_range( + state.cursor.start(&self.value), + self.value.len(), + ); + } else { + state.cursor.move_to(self.value.len()); + } + } keyboard::Key::Named(key::Named::ArrowLeft) => { if platform::is_jump_modifier_pressed(modifiers) && !self.is_secure @@ -914,26 +962,6 @@ where state.cursor.move_right(&self.value); } } - keyboard::Key::Named(key::Named::Home) => { - if modifiers.shift() { - state.cursor.select_range( - state.cursor.start(&self.value), - 0, - ); - } else { - state.cursor.move_to(0); - } - } - keyboard::Key::Named(key::Named::End) => { - if modifiers.shift() { - state.cursor.select_range( - state.cursor.start(&self.value), - self.value.len(), - ); - } else { - state.cursor.move_to(self.value.len()); - } - } keyboard::Key::Named(key::Named::Escape) => { state.is_focused = None; state.is_dragging = false; @@ -1291,6 +1319,19 @@ mod platform { modifiers.control() } } + + /// Whether the command key is pressed on a macOS device. + /// + /// This is relevant for actions like ⌘ + ArrowLeft to move to the beginning of the + /// line where the equivalent behavior for `modifiers.command()` is instead a jump on + /// other platforms. + pub fn is_macos_command_pressed(modifiers: keyboard::Modifiers) -> bool { + if cfg!(target_os = "macos") { + modifiers.logo() + } else { + false + } + } } fn offset( -- cgit From 3312dc808012e2c049fb2f178d51bfe0b4e30399 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 31 May 2024 16:23:09 +0200 Subject: Create `jump` and `macos_command` methods in `keyboard::Modifiers` --- widget/src/text_input.rs | 45 ++++++--------------------------------------- 1 file changed, 6 insertions(+), 39 deletions(-) (limited to 'widget/src/text_input.rs') diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 2c951c12..941e9bde 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -826,7 +826,7 @@ where } } keyboard::Key::Named(key::Named::Backspace) => { - if platform::is_jump_modifier_pressed(modifiers) + if modifiers.jump() && state.cursor.selection(&self.value).is_none() { if self.is_secure { @@ -850,7 +850,7 @@ where update_cache(state, &self.value); } keyboard::Key::Named(key::Named::Delete) => { - if platform::is_jump_modifier_pressed(modifiers) + if modifiers.jump() && state.cursor.selection(&self.value).is_none() { if self.is_secure { @@ -897,9 +897,7 @@ where } } keyboard::Key::Named(key::Named::ArrowLeft) - if platform::is_macos_command_pressed( - modifiers, - ) => + if modifiers.macos_command() => { if modifiers.shift() { state.cursor.select_range( @@ -911,9 +909,7 @@ where } } keyboard::Key::Named(key::Named::ArrowRight) - if platform::is_macos_command_pressed( - modifiers, - ) => + if modifiers.macos_command() => { if modifiers.shift() { state.cursor.select_range( @@ -925,9 +921,7 @@ where } } keyboard::Key::Named(key::Named::ArrowLeft) => { - if platform::is_jump_modifier_pressed(modifiers) - && !self.is_secure - { + if modifiers.jump() && !self.is_secure { if modifiers.shift() { state .cursor @@ -944,9 +938,7 @@ where } } keyboard::Key::Named(key::Named::ArrowRight) => { - if platform::is_jump_modifier_pressed(modifiers) - && !self.is_secure - { + if modifiers.jump() && !self.is_secure { if modifiers.shift() { state .cursor @@ -1309,31 +1301,6 @@ impl operation::TextInput for State

{ } } -mod platform { - use crate::core::keyboard; - - pub fn is_jump_modifier_pressed(modifiers: keyboard::Modifiers) -> bool { - if cfg!(target_os = "macos") { - modifiers.alt() - } else { - modifiers.control() - } - } - - /// Whether the command key is pressed on a macOS device. - /// - /// This is relevant for actions like ⌘ + ArrowLeft to move to the beginning of the - /// line where the equivalent behavior for `modifiers.command()` is instead a jump on - /// other platforms. - pub fn is_macos_command_pressed(modifiers: keyboard::Modifiers) -> bool { - if cfg!(target_os = "macos") { - modifiers.logo() - } else { - false - } - } -} - fn offset( text_bounds: Rectangle, value: &Value, -- cgit