Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix word-based actions in text input on macOS #227

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/views/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ impl From<(&KeyEvent, &SmolStr)> for TextCommand {
}
}

fn is_word_based_action(event: &KeyEvent) -> bool {
#[cfg(not(target_os = "macos"))]
return event.modifiers.contains(ModifiersState::CONTROL);

#[cfg(target_os = "macos")]
return event.modifiers.contains(ModifiersState::SUPER);
}

const DEFAULT_FONT_SIZE: f32 = 14.0;
const CURSOR_BLINK_INTERVAL_MS: u64 = 500;
/// Specifies approximately how many characters wide the input field should be
Expand Down Expand Up @@ -622,7 +630,7 @@ impl TextInput {
} else {
let prev_cursor_idx = self.cursor_glyph_idx;

if event.modifiers.contains(ModifiersState::CONTROL) {
if is_word_based_action(event) {
self.move_cursor(Movement::Word, Direction::Left);
} else {
self.move_cursor(Movement::Glyph, Direction::Left);
Expand All @@ -649,7 +657,7 @@ impl TextInput {

let prev_cursor_idx = self.cursor_glyph_idx;

if event.modifiers.contains(ModifiersState::CONTROL) {
if is_word_based_action(event) {
self.move_cursor(Movement::Word, Direction::Right);
} else {
self.move_cursor(Movement::Glyph, Direction::Right);
Expand All @@ -675,7 +683,7 @@ impl TextInput {
Key::Named(NamedKey::ArrowLeft) => {
let old_glyph_idx = self.cursor_glyph_idx;

let cursor_moved = if event.modifiers.contains(ModifiersState::CONTROL) {
let cursor_moved = if is_word_based_action(event) {
self.move_cursor(Movement::Word, Direction::Left)
} else {
self.move_cursor(Movement::Glyph, Direction::Left)
Expand All @@ -699,7 +707,7 @@ impl TextInput {
Key::Named(NamedKey::ArrowRight) => {
let old_glyph_idx = self.cursor_glyph_idx;

let cursor_moved = if event.modifiers.contains(ModifiersState::CONTROL) {
let cursor_moved = if is_word_based_action(event) {
self.move_cursor(Movement::Word, Direction::Right)
} else {
self.move_cursor(Movement::Glyph, Direction::Right)
Expand Down