Handle the IME event first in `TextEdit` to fix some bugs (#4794)

Handle the `IME` event first 
There is a need to handle the Ime event first.

Fix Issues : When you press `BackSpace`, the entire text is erased, and
when you press another letter, it appears again.
Fix Issues : When you press `Left`, the character being entered will be
copied once more.
Fix Issues : When you press `Enter`, `Enter` with `repeat` set is
entered and `Enter` is entered twice.
Fix Issues : When you press a key in `IME` mode, `repeat` is often set.

Fix Issues : Since you may be selecting something in the IME, this also
disables the `Arrow` keys.
This commit is contained in:
rustbasic 2024-07-08 05:03:13 +09:00 committed by GitHub
parent cd1e4c573a
commit fcd02bd7d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 29 additions and 1 deletions

View File

@ -855,7 +855,14 @@ fn events(
let mut any_change = false;
let events = ui.input(|i| i.filtered_events(&event_filter));
let mut events = ui.input(|i| i.filtered_events(&event_filter));
if state.ime_enabled {
remove_ime_incompatible_events(&mut events);
// Process IME events first:
events.sort_by_key(|e| !matches!(e, Event::Ime(_)));
}
for event in &events {
let did_mutate_text = match event {
// First handle events that only changes the selection cursor, not the text:
@ -1055,6 +1062,27 @@ fn events(
// ----------------------------------------------------------------------------
fn remove_ime_incompatible_events(events: &mut Vec<Event>) {
// Remove key events which cause problems while 'IME' is being used.
// See https://github.com/emilk/egui/pull/4509
events.retain(|event| {
!matches!(
event,
Event::Key { repeat: true, .. }
| Event::Key {
key: Key::Backspace
| Key::ArrowUp
| Key::ArrowDown
| Key::ArrowLeft
| Key::ArrowRight,
..
}
)
});
}
// ----------------------------------------------------------------------------
/// Returns `Some(new_cursor)` if we did mutate `text`.
fn check_for_mutating_key_press(
os: OperatingSystem,