Skip to content

Commit

Permalink
Fix non-standard text input behavior in Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
RagibHasin committed Aug 10, 2024
1 parent a207ef4 commit 4340bd9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/views/editor/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,7 @@ fn editor_content(
return;
};

let key_text = key_event.key.text.clone();
let Ok(keypress) = KeyPress::try_from(key_event) else {
return;
};
Expand All @@ -1181,6 +1182,10 @@ fn editor_content(
} else if let KeyInput::Keyboard(Key::Named(NamedKey::Space), _) = keypress.key
{
editor.get_untracked().receive_char(" ");
} else if let KeyInput::Keyboard(Key::Unidentified(_), _) = keypress.key {
if let Some(text) = key_text {
editor.get_untracked().receive_char(&text);
}
}
}
})
Expand Down
43 changes: 25 additions & 18 deletions src/views/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,24 +620,12 @@ impl TextInput {

fn handle_key_down(&mut self, cx: &mut EventCx, event: &KeyEvent) -> bool {
match event.key.logical_key {
Key::Character(ref ch) => {
let handled_modifier_cmd = self.handle_modifier_cmd(event, ch);
if handled_modifier_cmd {
return true;
}

let selection = self.selection.clone();
if let Some(selection) = selection {
self.buffer
.update(|buf| replace_range(buf, selection.clone(), None));
self.cursor_glyph_idx = selection.start;
self.selection = None;
}

self.buffer
.update(|buf| buf.insert_str(self.cursor_glyph_idx, &ch.clone()));
self.move_cursor(Movement::Glyph, Direction::Right)
}
Key::Character(ref ch) => self.insert_text(event, ch),
Key::Unidentified(_) => event
.key
.text
.as_ref()
.map_or(false, |ch| self.insert_text(event, ch)),
Key::Named(NamedKey::Space) => {
if let Some(selection) = &self.selection {
self.buffer
Expand Down Expand Up @@ -782,6 +770,25 @@ impl TextInput {
}
}

fn insert_text(&mut self, event: &KeyEvent, ch: &SmolStr) -> bool {
let handled_modifier_cmd = self.handle_modifier_cmd(event, ch);
if handled_modifier_cmd {
return true;
}

let selection = self.selection.clone();
if let Some(selection) = selection {
self.buffer
.update(|buf| replace_range(buf, selection.clone(), None));
self.cursor_glyph_idx = selection.start;
self.selection = None;
}

self.buffer
.update(|buf| buf.insert_str(self.cursor_glyph_idx, &ch.clone()));
self.move_cursor(Movement::Glyph, Direction::Right)
}

fn move_selection(
&mut self,
old_glyph_idx: usize,
Expand Down

0 comments on commit 4340bd9

Please sign in to comment.