diff --git a/src/platform_impl/macos/view.rs b/src/platform_impl/macos/view.rs index 8d0eb1a5..e17a00fb 100644 --- a/src/platform_impl/macos/view.rs +++ b/src/platform_impl/macos/view.rs @@ -481,16 +481,16 @@ declare_class!( if has_marked { self.queue_event(WindowEvent::Ime(Ime::Preedit(String::new(), None))); } - // Press-and-hold and dictation replace text they inserted before. - let mut delete = self.replaced(replacement_range); - // Only commit via IME if there was a real composition session. // Some IMEs send insertText for all typing (e.g. spaces, English chars) // which should go through keyboard input instead of paste. if pending_commit && ime_enabled && !is_control { self.ivars().pending_commit.set(false); + // Press-and-hold and dictation replace text they inserted before. Only the arms + // that deliver the text apply the replacement to `ime_document`. + let delete = self.replaced(replacement_range); self.ivars().ime_document.borrow_mut().insert(&string); - self.queue_commit(string, delete.take()); + self.queue_commit(string, delete); // WordCraft patch: input methods also commit outside a key press (a candidate // clicked with the mouse, dictation), where no key follows to clear the marked text // and reset the state: the next key would be swallowed @@ -515,23 +515,26 @@ declare_class!( let key_characters = self.ivars().key_characters.borrow().clone(); match plain_insert(&string, key_characters.as_deref()) { PlainInsert::KeyText => { + let delete = self.replaced(replacement_range); // The deletion goes before the key's `KeyboardInput`, which `keyDown:` // sends after this returns. if delete.is_some() && self.ivars().ime_allowed.get() { self.notify_ime_enabled(); - self.queue_commit(String::new(), delete.take()); + self.queue_commit(String::new(), delete); } self.ivars().ime_document.borrow_mut().insert(&string) }, PlainInsert::Commit if self.ivars().ime_allowed.get() => { + let delete = self.replaced(replacement_range); self.notify_ime_enabled(); if self.ivars().in_key_down.get() { // `keyDown:` doesn't send the key's own text. self.ivars().committed_in_key_down.set(true); } self.ivars().ime_document.borrow_mut().insert(&string); - self.queue_commit(string, delete.take()); + self.queue_commit(string, delete); }, + // Dropped text leaves `ime_document` as it is. PlainInsert::Commit | PlainInsert::Ignore => {}, } } diff --git a/tests/ime_patches.rs b/tests/ime_patches.rs index e39a3186..6380a832 100644 --- a/tests/ime_patches.rs +++ b/tests/ime_patches.rs @@ -297,6 +297,14 @@ fn macos_input_methods_see_a_caret_and_replace_text() { "{method} ignores its replacement range" ); } + // Text `insertText` drops (emoji picker or dictation text while IME isn't allowed) leaves the + // document alone, or its caret falls behind the app's. + let insert_text = view_method(&view, "fn insert_text("); + let decided = insert_text.find("if pending_commit && ime_enabled").expect("no commit branch"); + assert!( + !insert_text[..decided].contains("self.replaced("), + "insertText replaces text before deciding whether to deliver it" + ); } #[test]