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 non-standard text input behavior in Windows #544

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
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
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