diff --git a/crates/ui-egui/src/shortcuts/tests.rs b/crates/ui-egui/src/shortcuts/tests.rs index d0c2ed3e61..4fad102f73 100644 --- a/crates/ui-egui/src/shortcuts/tests.rs +++ b/crates/ui-egui/src/shortcuts/tests.rs @@ -324,6 +324,14 @@ fn alt_chord_text_from_the_layout_is_typed_once() { let mut ime_commit_after_release = alt_press(Key::R, false, None); ime_commit_after_release.push(egui::Event::ModifiersChanged(Modifiers::NONE)); ime_commit_after_release.push(egui::Event::Ime(egui::ImeEvent::Commit("\u{0159}".into()))); + // Windows sends an empty pre-edit before each commit, and an input method can delete the text + // around the caret between the press and its text. + let mut ime_commit_after_empty_preedit = alt_press(Key::G, false, None); + ime_commit_after_empty_preedit.extend([ + egui::Event::Ime(egui::ImeEvent::DeleteSurrounding { before_chars: 0, after_chars: 0 }), + egui::Event::Ime(egui::ImeEvent::Preedit { text: String::new(), active_range_chars: None }), + egui::Event::Ime(egui::ImeEvent::Commit("\u{011F}".into())), + ]); let commit_then_chord = vec![ egui::Event::Ime(egui::ImeEvent::Commit("x".into())), egui::Event::Key { key: Key::Num8, physical_key: Some(Key::Num8), pressed: true, repeat: false, modifiers: Modifiers::ALT }, @@ -331,6 +339,7 @@ fn alt_chord_text_from_the_layout_is_typed_once() { for (events, expected) in [ (ime_commit, "Hi\u{011F}"), (ime_commit_after_release, "Hi\u{0159}"), + (ime_commit_after_empty_preedit, "Hi\u{011F}"), (commit_then_chord, format!("Hix{bullet}").as_str()), (alt_press(Key::G, false, Some("\u{00A9}")), "Hi\u{00A9}"), (alt_press(Key::G, false, Some("\u{011F}")), "Hi\u{011F}"), diff --git a/crates/ui-egui/src/shortcuts/typing.rs b/crates/ui-egui/src/shortcuts/typing.rs index 8471f69c36..94a5817d9e 100644 --- a/crates/ui-egui/src/shortcuts/typing.rs +++ b/crates/ui-egui/src/shortcuts/typing.rs @@ -70,8 +70,7 @@ fn alt_chord_character(key: Key, m: egui::Modifiers) -> Option<(char, &'static s /// as it came. A press that typed no text gets its character added after it. A text's press is the /// one [`typing_presses`] pairs it with: the press before it, or in the web build the press after it. /// An input method's committed or marked text belongs to the last press before it, past modifier -/// changes and key releases; that press is left as it came, since the input method's text is what -/// the key typed. +/// changes, key releases, empty pre-edits and deletions around the caret; that press is left as it came, since the input method's text is what the key typed. fn type_alt_chords(events: &mut Vec) { let chord = |e: &egui::Event| match e { egui::Event::Key { key, pressed: true, modifiers, .. } => alt_chord_character(*key, *modifiers), @@ -84,6 +83,14 @@ fn type_alt_chords(events: &mut Vec) { egui::Event::Ime(egui::ImeEvent::Commit(t) | egui::ImeEvent::Preedit { text: t, .. }) => !t.is_empty(), _ => false, }; + // Events between a press and the input method's text it typed. Windows sends an empty pre-edit + // before each commit, and an input method can delete the text around the caret first. + let between = |e: &egui::Event| match e { + egui::Event::ModifiersChanged(_) | egui::Event::Key { pressed: false, .. } => true, + egui::Event::Ime(egui::ImeEvent::Preedit { text, .. }) => text.is_empty(), + egui::Event::Ime(egui::ImeEvent::DeleteSurrounding { .. } | egui::ImeEvent::DeleteSurroundingBytes { .. }) => true, + _ => false, + }; // The press an input method's text at `j` belongs to. let ime_press = |j: usize| { events @@ -91,7 +98,7 @@ fn type_alt_chords(events: &mut Vec) { .enumerate() .take(j) .rev() - .find(|(_, e)| !matches!(e, egui::Event::ModifiersChanged(_) | egui::Event::Key { pressed: false, .. })) + .find(|(_, e)| !between(e)) .filter(|(_, e)| matches!(e, egui::Event::Key { pressed: true, .. })) .map(|(k, _)| k) };