Skip to content

Commit 065d1a0

Browse files
committed
fix: macOS Korean IME trigger key handling
Port of rust-windowing#4478 to v0.30.13. Bug 1 - Space double input: Clear marked_text immediately after commit to prevent subsequent insertText calls from being treated as IME commits. Bug 2 - Trigger key drop (comma, period, etc.): Remove the early return guard in doCommandBySelector when ImeState::Committed, which was swallowing ASCII key commands after IME commit. When upstream PR rust-windowing#4478 is merged and released, this fork should be replaced with the official winit release. Reference: rust-windowing#4478
1 parent e9809ef commit 065d1a0

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

src/platform_impl/macos/view.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,20 @@ declare_class!(
409409

410410
// Commit only if we have marked text.
411411
if unsafe { self.hasMarkedText() } && self.is_ime_enabled() && !is_control {
412+
// Clear marked text as required by the NSTextInputClient protocol.
413+
// This prevents subsequent insertText calls (e.g., for a trailing
414+
// space) from incorrectly seeing stale marked text.
415+
*self.ivars().marked_text.borrow_mut() = NSMutableAttributedString::new();
416+
412417
self.queue_event(WindowEvent::Ime(Ime::Preedit(String::new(), None)));
413418
self.queue_event(WindowEvent::Ime(Ime::Commit(string)));
414419
self.ivars().ime_state.set(ImeState::Committed);
420+
} else if self.ivars().ime_state.get() == ImeState::Committed && !is_control {
421+
// After committing composed text (e.g., Korean "한"), the IME may
422+
// send a second insertText for the triggering character (e.g.,
423+
// space). Forward it to the app as a regular key event instead of
424+
// committing it again through IME, which would cause double input.
425+
self.ivars().forward_key_to_app.set(true);
415426
}
416427
}
417428

@@ -420,12 +431,6 @@ declare_class!(
420431
#[method(doCommandBySelector:)]
421432
fn do_command_by_selector(&self, _command: Sel) {
422433
trace_scope!("doCommandBySelector:");
423-
// We shouldn't forward any character from just committed text, since we'll end up sending
424-
// it twice with some IMEs like Korean one. We'll also always send `Enter` in that case,
425-
// which is not desired given it was used to confirm IME input.
426-
if self.ivars().ime_state.get() == ImeState::Committed {
427-
return;
428-
}
429434

430435
self.ivars().forward_key_to_app.set(true);
431436

0 commit comments

Comments
 (0)