From 4340bd9a65dfd6f3e4315144541e154021d7925d Mon Sep 17 00:00:00 2001 From: Muhammad Ragib Hasin Date: Sat, 10 Aug 2024 12:48:56 +0600 Subject: [PATCH] Fix non-standard text input behavior in Windows --- src/views/editor/view.rs | 5 +++++ src/views/text_input.rs | 43 +++++++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/views/editor/view.rs b/src/views/editor/view.rs index 647910fe..834536b2 100644 --- a/src/views/editor/view.rs +++ b/src/views/editor/view.rs @@ -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; }; @@ -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); + } } } }) diff --git a/src/views/text_input.rs b/src/views/text_input.rs index 7d46277a..0120a059 100644 --- a/src/views/text_input.rs +++ b/src/views/text_input.rs @@ -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 @@ -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,