Skip to content

Commit

Permalink
Merge pull request #605 from ZakisM/text_input_select_all_fix
Browse files Browse the repository at this point in the history
This PR fixes a bug with select all (CMD + A on MacOS) when using a text_input.
  • Loading branch information
hecrj authored Nov 25, 2020
2 parents 87c9df2 + 1d23db1 commit 18aa14c
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions native/src/widget/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ where
Event::Keyboard(keyboard::Event::CharacterReceived(c))
if self.state.is_focused
&& self.state.is_pasting.is_none()
&& !self.state.keyboard_modifiers.is_command_pressed()
&& !c.is_control() =>
{
let mut editor =
Expand All @@ -395,9 +396,10 @@ where
return event::Status::Captured;
}
Event::Keyboard(keyboard::Event::KeyPressed {
key_code,
modifiers,
key_code, ..
}) if self.state.is_focused => {
let modifiers = self.state.keyboard_modifiers;

match key_code {
keyboard::KeyCode::Enter => {
if let Some(on_submit) = self.on_submit.clone() {
Expand Down Expand Up @@ -523,7 +525,7 @@ where
}
}
keyboard::KeyCode::V => {
if platform::is_copy_paste_modifier_pressed(modifiers) {
if self.state.keyboard_modifiers.is_command_pressed() {
if let Some(clipboard) = clipboard {
let content = match self.state.is_pasting.take()
{
Expand Down Expand Up @@ -558,14 +560,17 @@ where
}
}
keyboard::KeyCode::A => {
if platform::is_copy_paste_modifier_pressed(modifiers) {
if self.state.keyboard_modifiers.is_command_pressed() {
self.state.cursor.select_all(&self.value);
}
}
keyboard::KeyCode::Escape => {
self.state.is_focused = false;
self.state.is_dragging = false;
self.state.is_pasting = None;

self.state.keyboard_modifiers =
keyboard::ModifiersState::default();
}
_ => {}
}
Expand All @@ -584,6 +589,11 @@ where

return event::Status::Captured;
}
Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers))
if self.state.is_focused =>
{
self.state.keyboard_modifiers = modifiers;
}
_ => {}
}

Expand Down Expand Up @@ -724,6 +734,7 @@ pub struct State {
is_pasting: Option<Value>,
last_click: Option<mouse::Click>,
cursor: Cursor,
keyboard_modifiers: keyboard::ModifiersState,
// TODO: Add stateful horizontal scrolling offset
}

Expand All @@ -745,6 +756,7 @@ impl State {
is_pasting: None,
last_click: None,
cursor: Cursor::default(),
keyboard_modifiers: keyboard::ModifiersState::default(),
}
}

Expand Down Expand Up @@ -870,14 +882,4 @@ mod platform {
modifiers.control
}
}

pub fn is_copy_paste_modifier_pressed(
modifiers: keyboard::ModifiersState,
) -> bool {
if cfg!(target_os = "macos") {
modifiers.logo
} else {
modifiers.control
}
}
}

0 comments on commit 18aa14c

Please sign in to comment.