Skip to content

Commit

Permalink
Merge pull request #2315 from Brady-Simon/macos-command-input-behavior
Browse files Browse the repository at this point in the history
Add Command + ArrowLeft/Right input behavior for macOS
  • Loading branch information
hecrj authored May 31, 2024
2 parents 12f4b87 + 3312dc8 commit 06ff17f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 55 deletions.
24 changes: 24 additions & 0 deletions core/src/keyboard/modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,28 @@ impl Modifiers {

is_pressed
}

/// Returns true if the "jump key" is pressed in the [`Modifiers`].
///
/// The "jump key" is the modifier key used to widen text motions. It is the `Alt`
/// key in macOS and the `Ctrl` key in other platforms.
pub fn jump(self) -> bool {
if cfg!(target_os = "macos") {
self.alt()
} else {
self.control()
}
}

/// Returns true if the "command key" is pressed on a macOS device.
///
/// This is relevant for macOS-specific actions (e.g. `⌘ + ArrowLeft` moves the cursor
/// to the beginning of the line).
pub fn macos_command(self) -> bool {
if cfg!(target_os = "macos") {
self.logo()
} else {
false
}
}
}
26 changes: 11 additions & 15 deletions widget/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,9 +768,17 @@ impl Update {

if let keyboard::Key::Named(named_key) = key.as_ref() {
if let Some(motion) = motion(named_key) {
let motion = if platform::is_jump_modifier_pressed(
modifiers,
) {
let motion = if modifiers.macos_command() {
match motion {
Motion::Left => Motion::Home,
Motion::Right => Motion::End,
_ => motion,
}
} else {
motion
};

let motion = if modifiers.jump() {
motion.widen()
} else {
motion
Expand Down Expand Up @@ -807,18 +815,6 @@ fn motion(key: key::Named) -> Option<Motion> {
}
}

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()
}
}
}

/// The possible status of a [`TextEditor`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Status {
Expand Down
88 changes: 48 additions & 40 deletions widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -876,10 +876,52 @@ 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 modifiers.macos_command() =>
{
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 modifiers.macos_command() =>
{
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
{
if modifiers.jump() && !self.is_secure {
if modifiers.shift() {
state
.cursor
Expand All @@ -896,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
Expand All @@ -914,26 +954,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 @@ -1281,18 +1301,6 @@ impl<P: text::Paragraph> operation::TextInput for State<P> {
}
}

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()
}
}
}

fn offset<P: text::Paragraph>(
text_bounds: Rectangle,
value: &Value,
Expand Down

0 comments on commit 06ff17f

Please sign in to comment.