Skip to content

Commit

Permalink
Add Command + ArrowLeft/Right input behavior for macos
Browse files Browse the repository at this point in the history
  • Loading branch information
BradySimon committed May 3, 2024
1 parent 5ef593c commit 5602459
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 26 deletions.
37 changes: 31 additions & 6 deletions widget/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,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,
) {
Expand All @@ -789,14 +789,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,
Expand All @@ -813,6 +825,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`].
Expand Down
81 changes: 61 additions & 20 deletions widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<P: text::Paragraph>(
Expand Down

0 comments on commit 5602459

Please sign in to comment.