From fcd02bd7d1dde123a1b2dce11ae86cea17029ace Mon Sep 17 00:00:00 2001 From: rustbasic <127506429+rustbasic@users.noreply.github.com> Date: Mon, 8 Jul 2024 05:03:13 +0900 Subject: [PATCH] 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. --- crates/egui/src/widgets/text_edit/builder.rs | 30 +++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index 8205da63d0e..ed29899b256 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -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) { + // 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,