Skip to content

Commit

Permalink
This PR fixes a bug with select all (CMD + A on MacOS) when using a t…
Browse files Browse the repository at this point in the history
…ext_input.

Previous behaviour: when selecting all (CMD + A) would delete the current text inside the input and replace the content with just the letter 'a'.

Now we check if the logo key (modifier key) has been pressed before checking any other key and save it to the state level. This way we can prevent any text being deleted when using the select all shortcut or text being entered at all when a modifier key is pressed (this behaviour matches other text input behaviour i.e text inputs in the browser etc...).
  • Loading branch information
ZakisM authored and hecrj committed Nov 25, 2020
1 parent 8f081ba commit d275a4e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 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.is_logo_pressed
&& !c.is_control() =>
{
let mut editor =
Expand All @@ -398,6 +399,10 @@ where
key_code,
modifiers,
}) if self.state.is_focused => {
if platform::is_copy_paste_modifier_pressed(modifiers) {
self.state.is_logo_pressed = true;
}

match key_code {
keyboard::KeyCode::Enter => {
if let Some(on_submit) = self.on_submit.clone() {
Expand Down Expand Up @@ -523,7 +528,7 @@ where
}
}
keyboard::KeyCode::V => {
if platform::is_copy_paste_modifier_pressed(modifiers) {
if self.state.is_logo_pressed {
if let Some(clipboard) = clipboard {
let content = match self.state.is_pasting.take()
{
Expand Down Expand Up @@ -558,13 +563,14 @@ where
}
}
keyboard::KeyCode::A => {
if platform::is_copy_paste_modifier_pressed(modifiers) {
if self.state.is_logo_pressed {
self.state.cursor.select_all(&self.value);
}
}
keyboard::KeyCode::Escape => {
self.state.is_focused = false;
self.state.is_dragging = false;
self.state.is_logo_pressed = false;
self.state.is_pasting = None;
}
_ => {}
Expand All @@ -579,7 +585,9 @@ where
keyboard::KeyCode::V => {
self.state.is_pasting = None;
}
_ => {}
_ => {
self.state.is_logo_pressed = false;
}
}

return event::Status::Captured;
Expand Down Expand Up @@ -721,6 +729,7 @@ where
pub struct State {
is_focused: bool,
is_dragging: bool,
is_logo_pressed: bool,
is_pasting: Option<Value>,
last_click: Option<mouse::Click>,
cursor: Cursor,
Expand All @@ -742,6 +751,7 @@ impl State {
Self {
is_focused: true,
is_dragging: false,
is_logo_pressed: false,
is_pasting: None,
last_click: None,
cursor: Cursor::default(),
Expand Down

0 comments on commit d275a4e

Please sign in to comment.