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

Handle the IME event first #4509

Closed
wants to merge 14 commits into from
45 changes: 43 additions & 2 deletions crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ impl<'t> TextEdit<'t> {
)
});
} else if selection_changed {
let cursor_range = cursor_range.unwrap();
let cursor_range = cursor_range.unwrap_or_default();
let char_range =
cursor_range.primary.ccursor.index..=cursor_range.secondary.ccursor.index;
let info = WidgetInfo::text_selection_changed(
Expand Down Expand Up @@ -826,7 +826,14 @@ fn events(

let mut any_change = false;

let events = ui.input(|i| i.filtered_events(&event_filter));
let input_events = ui.input(|i| i.filtered_events(&event_filter));

let filter_events = match state.ime_enabled {
true => ime_enabled_filter_events(input_events),
false => input_events,
};
let events = ime_front_events(filter_events);

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 @@ -1025,6 +1032,40 @@ fn events(

// ----------------------------------------------------------------------------

fn ime_enabled_filter_events(events: Vec<Event>) -> Vec<Event> {
let mut filter_events: Vec<Event> = Vec::new();

for event in events {
match event {
Event::Key { repeat: true, .. }
| Event::Key {
key: Key::Backspace,
..
} => {}
rustbasic marked this conversation as resolved.
Show resolved Hide resolved
_ => filter_events.push(event),
}
}

filter_events
rustbasic marked this conversation as resolved.
Show resolved Hide resolved
}

fn ime_front_events(events: Vec<Event>) -> Vec<Event> {
let mut ime_front_events: Vec<Event> = Vec::new();
let mut other_events: Vec<Event> = Vec::new();

for event in events {
match event {
Event::Ime(_) => ime_front_events.push(event),
_ => other_events.push(event),
}
}

ime_front_events.extend(other_events);
ime_front_events
}
rustbasic marked this conversation as resolved.
Show resolved Hide resolved

// ----------------------------------------------------------------------------

/// Returns `Some(new_cursor)` if we did mutate `text`.
fn check_for_mutating_key_press(
os: OperatingSystem,
Expand Down
Loading