diff options
| author | 2024-03-08 09:35:43 -0500 | |
|---|---|---|
| committer | 2024-05-31 16:11:33 +0200 | |
| commit | bd48946b02268cb5a7ae932e388abfa75fe9e375 (patch) | |
| tree | 7e7595cfb333b732ec26ff6934999eb977adf8e4 /widget/src/text_editor.rs | |
| parent | 12f4b875cfb0eeb0bac5416921c6cec40edd420c (diff) | |
| download | iced-bd48946b02268cb5a7ae932e388abfa75fe9e375.tar.gz iced-bd48946b02268cb5a7ae932e388abfa75fe9e375.tar.bz2 iced-bd48946b02268cb5a7ae932e388abfa75fe9e375.zip | |
Add Command + ArrowLeft/Right input behavior for macos
Diffstat (limited to 'widget/src/text_editor.rs')
| -rw-r--r-- | widget/src/text_editor.rs | 37 |
1 files changed, 31 insertions, 6 deletions
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<Motion> { +fn motion(key: key::Named, modifiers: keyboard::Modifiers) -> 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::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`]. |
