Skip to content

Commit

Permalink
Handle the IME event first in TextEdit to fix some bugs (#4794)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rustbasic authored Jul 7, 2024
1 parent cd1e4c5 commit fcd02bd
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit fcd02bd

Please sign in to comment.