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_editor.rs | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'widget/src/text_editor.rs') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 7c0b98ea..3b6647e4 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -767,7 +767,7 @@ impl Update { } if let keyboard::Key::Named(named_key) = key.as_ref() { - if let Some(motion) = motion(named_key) { + if let Some(motion) = motion(named_key, modifiers) { let motion = if platform::is_jump_modifier_pressed( modifiers, ) { @@ -793,14 +793,26 @@ impl Update { } } -fn motion(key: key::Named) -> Option { +fn motion(key: key::Named, modifiers: keyboard::Modifiers) -> Option { 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::ArrowLeft => { + if platform::is_macos_command_pressed(modifiers) { + Some(Motion::Home) + } else { + Some(Motion::Left) + } + } + key::Named::ArrowRight => { + if platform::is_macos_command_pressed(modifiers) { + Some(Motion::End) + } else { + Some(Motion::Right) + } + } + key::Named::ArrowUp => Some(Motion::Up), + key::Named::ArrowDown => Some(Motion::Down), key::Named::PageUp => Some(Motion::PageUp), key::Named::PageDown => Some(Motion::PageDown), _ => None, @@ -817,6 +829,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 + } + } } /// The possible status of a [`TextEditor`]. -- cgit From 8cfa8149f56a0e58eb0257ebb181c69bec5c095f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 31 May 2024 16:10:59 +0200 Subject: Keep unary `motion` function in `text_editor` --- widget/src/text_editor.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'widget/src/text_editor.rs') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 3b6647e4..9e494394 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -767,7 +767,19 @@ impl Update { } if let keyboard::Key::Named(named_key) = key.as_ref() { - if let Some(motion) = motion(named_key, modifiers) { + if let Some(motion) = motion(named_key) { + let motion = if platform::is_macos_command_pressed( + modifiers, + ) { + match motion { + Motion::Left => Motion::Home, + Motion::Right => Motion::End, + _ => motion, + } + } else { + motion + }; + let motion = if platform::is_jump_modifier_pressed( modifiers, ) { @@ -793,26 +805,14 @@ impl Update { } } -fn motion(key: key::Named, modifiers: keyboard::Modifiers) -> Option { +fn motion(key: key::Named) -> Option { match key { - key::Named::Home => Some(Motion::Home), - key::Named::End => Some(Motion::End), - key::Named::ArrowLeft => { - if platform::is_macos_command_pressed(modifiers) { - Some(Motion::Home) - } else { - Some(Motion::Left) - } - } - key::Named::ArrowRight => { - if platform::is_macos_command_pressed(modifiers) { - Some(Motion::End) - } else { - Some(Motion::Right) - } - } + 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, -- 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_editor.rs | 33 ++------------------------------- 1 file changed, 2 insertions(+), 31 deletions(-) (limited to 'widget/src/text_editor.rs') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 9e494394..41b058af 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -768,9 +768,7 @@ impl Update { if let keyboard::Key::Named(named_key) = key.as_ref() { if let Some(motion) = motion(named_key) { - let motion = if platform::is_macos_command_pressed( - modifiers, - ) { + let motion = if modifiers.macos_command() { match motion { Motion::Left => Motion::Home, Motion::Right => Motion::End, @@ -780,9 +778,7 @@ impl Update { motion }; - let motion = if platform::is_jump_modifier_pressed( - modifiers, - ) { + let motion = if modifiers.jump() { motion.widen() } else { motion @@ -819,31 +815,6 @@ fn motion(key: key::Named) -> Option { } } -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 - } - } -} - /// The possible status of a [`TextEditor`]. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Status { -- cgit