Skip to content

Commit fcd02bd

Browse files
authored
Handle the IME event first in TextEdit to fix some bugs (emilk#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.
1 parent cd1e4c5 commit fcd02bd

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

crates/egui/src/widgets/text_edit/builder.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,14 @@ fn events(
855855

856856
let mut any_change = false;
857857

858-
let events = ui.input(|i| i.filtered_events(&event_filter));
858+
let mut events = ui.input(|i| i.filtered_events(&event_filter));
859+
860+
if state.ime_enabled {
861+
remove_ime_incompatible_events(&mut events);
862+
// Process IME events first:
863+
events.sort_by_key(|e| !matches!(e, Event::Ime(_)));
864+
}
865+
859866
for event in &events {
860867
let did_mutate_text = match event {
861868
// First handle events that only changes the selection cursor, not the text:
@@ -1055,6 +1062,27 @@ fn events(
10551062

10561063
// ----------------------------------------------------------------------------
10571064

1065+
fn remove_ime_incompatible_events(events: &mut Vec<Event>) {
1066+
// Remove key events which cause problems while 'IME' is being used.
1067+
// See https://github.com/emilk/egui/pull/4509
1068+
events.retain(|event| {
1069+
!matches!(
1070+
event,
1071+
Event::Key { repeat: true, .. }
1072+
| Event::Key {
1073+
key: Key::Backspace
1074+
| Key::ArrowUp
1075+
| Key::ArrowDown
1076+
| Key::ArrowLeft
1077+
| Key::ArrowRight,
1078+
..
1079+
}
1080+
)
1081+
});
1082+
}
1083+
1084+
// ----------------------------------------------------------------------------
1085+
10581086
/// Returns `Some(new_cursor)` if we did mutate `text`.
10591087
fn check_for_mutating_key_press(
10601088
os: OperatingSystem,

0 commit comments

Comments
 (0)