From 9fff952602de29376fa1703bec385f02c4b69d14 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:35:03 +0000 Subject: [PATCH] fix(ime): stop a commit key with no keyup from eating the next Enter compositionend only scheduled the carry's expiry when it armed a new carry. A marked commit key (Process/229 with an unknown code) whose keyup never arrives left its carry armed forever, so the next bare Enter in the chat composer and every IME text field was swallowed while Ctrl/Cmd+Enter still submitted. compositionend now always schedules the expiry. Refs #41072, STA-14827 --- .../ime-composition-keyboard-event.test.ts | 60 +++++++++++++++++++ .../ime/ime-composition-keyboard-event.ts | 7 ++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/lib/text-input/ime/ime-composition-keyboard-event.test.ts b/src/renderer/src/lib/text-input/ime/ime-composition-keyboard-event.test.ts index e8f531df515..3cdd85f53bd 100644 --- a/src/renderer/src/lib/text-input/ime/ime-composition-keyboard-event.test.ts +++ b/src/renderer/src/lib/text-input/ime/ime-composition-keyboard-event.test.ts @@ -7,6 +7,7 @@ import { useImeEnterGestureOwnership } from './ime-composition-keyboard-event' type ImeEnterGestureEventForTest = Pick< ReactKeyboardEvent, | 'altKey' + | 'code' | 'ctrlKey' | 'key' | 'keyCode' @@ -20,6 +21,7 @@ describe('useImeEnterGestureOwnership', () => { function gestureEvent(init: { key: string keyCode: number + code?: string altKey?: boolean ctrlKey?: boolean isComposing?: boolean @@ -30,6 +32,7 @@ describe('useImeEnterGestureOwnership', () => { return { key: init.key, keyCode: init.keyCode, + code: init.code ?? '', altKey: init.altKey ?? false, ctrlKey: init.ctrlKey ?? false, metaKey: init.metaKey ?? false, @@ -125,4 +128,61 @@ describe('useImeEnterGestureOwnership', () => { expect(result.current.ownsKeyDown(redispatch)).toBe(true) expect(redispatch.prevented).toBe(true) }) + + // Each commit ends with compositionend and no keyup; a confirm Enter's echo comes before the next frame. + it.each([ + { + name: 'expires a Pinyin commit key that reports no code and no keyup', + commitKey: { key: 'Process', keyCode: 229, code: '' }, + committed: '你好', + frameRuns: true, + swallowed: false + }, + { + name: 'expires a Hankaku/Zenkaku commit that never sends its keyup (#39289)', + commitKey: { key: 'Process', keyCode: 229, code: 'Backquote' }, + committed: 'にほんご', + frameRuns: true, + swallowed: false + }, + { + name: 'swallows the bare Enter that echoes a Japanese confirm Enter', + commitKey: { key: 'Enter', keyCode: 229, code: 'Enter' }, + committed: '日本語', + frameRuns: false, + swallowed: true + }, + { + name: 'swallows the IBus confirm Enter that follows compositionend', + commitKey: null, + committed: '日本語', + frameRuns: false, + swallowed: true + } + ])('$name', ({ commitKey, committed, frameRuns, swallowed }) => { + const frames: FrameRequestCallback[] = [] + const raf = vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback) => { + frames.push(callback) + return frames.length + }) + try { + const { result } = renderHook(() => useImeEnterGestureOwnership()) + result.current.setComposing(true) + if (commitKey) { + result.current.ownsKeyDown(gestureEvent({ ...commitKey, isComposing: true })) + } + result.current.onCompositionEnd(committed) + if (frameRuns) { + for (const frame of frames.splice(0)) { + frame(0) + } + } + + const enter = gestureEvent({ key: 'Enter', keyCode: 13, code: 'Enter' }) + expect(result.current.ownsKeyDown(enter)).toBe(swallowed) + expect(enter.prevented).toBe(swallowed) + } finally { + raf.mockRestore() + } + }) }) diff --git a/src/renderer/src/lib/text-input/ime/ime-composition-keyboard-event.ts b/src/renderer/src/lib/text-input/ime/ime-composition-keyboard-event.ts index 1d5e8f76bd6..f29e78b6ba9 100644 --- a/src/renderer/src/lib/text-input/ime/ime-composition-keyboard-event.ts +++ b/src/renderer/src/lib/text-input/ime/ime-composition-keyboard-event.ts @@ -113,9 +113,10 @@ export function useImeEnterGestureOwnership(): { stateRef.current.composing = false if (HANGUL_ONLY.test(committedText)) { stateRef.current.pendingEnter = null - } else if (compositionWasActive && !stateRef.current.pendingEnter) { - // IBus can finish composition before its only unmarked confirming Enter. - stateRef.current.pendingEnter = {} + } else if (compositionWasActive) { + // IBus can finish composition before its only unmarked confirming Enter, and a marked + // commit key may never send the keyup that would otherwise expire its carry. + stateRef.current.pendingEnter ??= {} expirePendingEnter() } },