style(terminal): keep IME ownership rationale concise

This commit is contained in:
OrcaWin
2026-08-03 01:30:58 -07:00
committed by OrcaWin
parent bacdf3a31d
commit a5655321b5
2 changed files with 8 additions and 46 deletions
@@ -1,11 +1,4 @@
// @vitest-environment happy-dom
// Windows IME Enter-keyup synthesis must require press-time evidence.
//
// The keyup branch exists for presses whose keydown the IME swallowed entirely.
// When the keydown WAS observed, release-time modifier state is rollover noise:
// a plain committing Enter followed by a rolled-over Shift for the next doubled
// consonant (했다 → commit-Enter, then Shift for ㄸ) must not become Shift+Enter,
// and a directly-sent Shift+Enter must not send a second newline from its keyup.
import { cleanup, renderHook } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { PaneManager } from '@/lib/pane-manager/pane-manager'
@@ -136,7 +129,6 @@ describe('Windows IME Enter-keyup press-time evidence', () => {
const hook = renderHook(() => useTerminalKeyboardShortcuts(harness.deps))
harness.startComposition()
// Plain committing Enter, consumed by the IME: Process/229, no modifiers.
harness.terminalInput.dispatchEvent(
keyboardEvent('keydown', {
key: 'Process',
@@ -146,7 +138,6 @@ describe('Windows IME Enter-keyup press-time evidence', () => {
isComposing: true
})
)
// Rollover: Shift pressed for the next doubled consonant before the Enter keyup.
harness.terminalInput.dispatchEvent(
keyboardEvent('keydown', {
key: 'Shift',
@@ -156,8 +147,7 @@ describe('Windows IME Enter-keyup press-time evidence', () => {
shiftKey: true
})
)
// The Enter keyup reports release-time shiftKey=true while the session is
// still pending (the session-end finalizer is delayed under renderer lag).
// Release-time Shift belongs to the next doubled consonant.
harness.terminalInput.dispatchEvent(
keyboardEvent('keyup', {
key: 'Enter',
@@ -188,7 +178,6 @@ describe('Windows IME Enter-keyup press-time evidence', () => {
isComposing: true
})
)
// Balancing keyup copied from the same native event (same timeStamp).
harness.terminalInput.dispatchEvent(
keyboardEvent('keyup', {
key: 'Enter',
@@ -206,7 +195,6 @@ describe('Windows IME Enter-keyup press-time evidence', () => {
shiftKey: true
})
)
// The physical release arrives later with the rolled-over Shift held.
harness.terminalInput.dispatchEvent(
keyboardEvent('keyup', {
key: 'Enter',
@@ -228,7 +216,6 @@ describe('Windows IME Enter-keyup press-time evidence', () => {
const hook = renderHook(() => useTerminalKeyboardShortcuts(harness.deps))
harness.startComposition()
// Two committing Enter presses land before either release is processed.
harness.terminalInput.dispatchEvent(
keyboardEvent('keydown', {
key: 'Process',
@@ -279,8 +266,6 @@ describe('Windows IME Enter-keyup press-time evidence', () => {
const hook = renderHook(() => useTerminalKeyboardShortcuts(harness.deps))
harness.startComposition()
// Auto-repeat re-fires keydown with no release in between; the whole run
// must cost exactly one release to drain.
harness.terminalInput.dispatchEvent(
keyboardEvent('keydown', {
key: 'Process',
@@ -312,7 +297,6 @@ describe('Windows IME Enter-keyup press-time evidence', () => {
vi.runAllTimers()
expect(harness.sendInput).not.toHaveBeenCalled()
// Evidence is now drained: a genuinely swallowed keydown still synthesizes.
harness.terminalInput.dispatchEvent(
keyboardEvent('keyup', {
key: 'Enter',
@@ -335,8 +319,7 @@ describe('Windows IME Enter-keyup press-time evidence', () => {
const hook = renderHook(() => useTerminalKeyboardShortcuts(harness.deps))
harness.startComposition()
// No Enter keydown was ever observed: only the keyup arrives, with the
// chord modifier still held. This is the case the keyup path exists for.
// No keydown evidence: preserve the swallowed-keydown fallback.
harness.terminalInput.dispatchEvent(
keyboardEvent('keyup', {
key: 'Enter',
@@ -358,7 +341,6 @@ describe('Windows IME Enter-keyup press-time evidence', () => {
const harness = createHarness()
const hook = renderHook(() => useTerminalKeyboardShortcuts(harness.deps))
// No composition yet: Shift+Enter keydown is sent directly.
harness.terminalInput.dispatchEvent(
keyboardEvent('keydown', {
key: 'Enter',
@@ -370,7 +352,6 @@ describe('Windows IME Enter-keyup press-time evidence', () => {
)
expect(harness.sendInput).toHaveBeenCalledTimes(1)
// The user starts the next Hangul syllable before releasing Enter/Shift.
harness.startComposition()
harness.terminalInput.dispatchEvent(
keyboardEvent('keyup', {
@@ -95,9 +95,7 @@ export function recordKeyboardCreatedTerminalPaneSplit(
return recordCreatedTerminalPaneSplit(createdPane, args)
}
// Why: bounds the per-code press evidence below so a keydown whose keyup never
// arrives cannot grow the list without end. Real typing never stacks this many
// Enter presses before a release.
// Bounds evidence when Chromium drops a matching keyup.
const MAX_OBSERVED_ENTER_KEYDOWNS_PER_CODE = 8
function isEditableTarget(target: EventTarget | null): boolean {
@@ -305,15 +303,7 @@ export function useTerminalKeyboardShortcuts({
}
}
}
// Why: the Enter-keyup synthesis below must only service presses whose
// keydown the IME swallowed entirely. When a keydown for the same physical
// key was observed, release-time modifier state is rollover noise, not a
// chord: a plain committing Enter followed by a rolled-over Shift for the
// next doubled consonant must not become Shift+Enter. One entry is kept per
// press so a rapid second press cannot be left unguarded by the first
// release, and each entry keeps its keydown timeStamp so a balancing keyup
// (copied from the same native event) does not consume it ahead of the
// physical release.
// Press evidence distinguishes swallowed keydowns from modifier rollover on keyup.
const observedEnterKeydownTimeStamps = new Map<string, number[]>()
const getHeldImeEnterModifier = () =>
heldImeEnterModifiers.size === 1
@@ -469,9 +459,7 @@ export function useTerminalKeyboardShortcuts({
// Why: replace stale state only for this physical key so rollover cannot
// disarm a still-held native-only chord before its Kitty keyup arrives.
nativeOnlyShortcutTracker.prepareKeyDown(e)
// Why: recorded ahead of every early return so any observed Enter
// keydown (editable targets and re-dispatches included) disqualifies
// its keyup from the swallowed-keydown synthesis below.
// Record before early returns so every observed Enter disqualifies keyup synthesis.
if (
isWindows &&
((e.key === 'Enter' && e.keyCode === 13) ||
@@ -481,9 +469,7 @@ export function useTerminalKeyboardShortcuts({
if (!observed) {
observedEnterKeydownTimeStamps.set(e.code, [e.timeStamp])
} else if (!e.repeat && observed.length < MAX_OBSERVED_ENTER_KEYDOWNS_PER_CODE) {
// Why: auto-repeat re-fires keydown with no keyup in between and the
// whole run ends in a single release, so a repeat must not stack an
// entry that release cannot drain.
// Auto-repeat shares one physical release.
observed.push(e.timeStamp)
}
}
@@ -822,9 +808,7 @@ export function useTerminalKeyboardShortcuts({
const observedEnterKeydowns = observedEnterKeydownTimeStamps.get(e.code)
const enterKeydownWasObserved = observedEnterKeydowns !== undefined
if (enterKeydownWasObserved && !observedEnterKeydowns.includes(e.timeStamp)) {
// Why: a balancing keyup copies the keydown's native timeStamp; only a
// physical release (a different timeStamp) consumes one press worth of
// evidence, so a second press stays guarded until its own release.
// A balancing keyup copies its keydown timestamp; only the physical release drains it.
observedEnterKeydowns.shift()
if (observedEnterKeydowns.length === 0) {
observedEnterKeydownTimeStamps.delete(e.code)
@@ -844,10 +828,7 @@ export function useTerminalKeyboardShortcuts({
const manager = managerRef.current
const keyboardScope = keyboardScopeRef.current
if (
// Why: synthesize only when the IME swallowed the keydown entirely.
// An observed keydown already chose its own path (plain commit,
// chord defer, or direct send); a modifier flag on this keyup is
// then rollover state from the next keystroke, not a chord.
// An observed keydown makes this modifier state rollover, not a swallowed chord.
!enterKeydownWasObserved &&
manager &&
!isEditableTarget(e.target) &&