fix(desktop): send the terminal Shift+Enter newline after IME text

Closed
HU
2d ago
+106-2

What & why

Fixes #10177

With a macOS IME (Korean 2-Set), when you type in a terminal TUI and press Shift+Enter before the composition commits, the newline goes before the last composed character, and it goes twice. In Oh My Pi, 한 글 + Shift+Enter gave 한, an empty line, and then 글 on the third line.

I recorded this event order in the dev app (Electron 44, macOS, Korean 2-Set). I used a capture listener on the xterm textarea and logged the PTY input frames:

keydown Enter keyCode=229 isComposing=true    -> PTY "\x1b\r"
compositionend "글"
keydown Enter keyCode=13  isComposing=false   -> PTY "\x1b\r"
                                              -> PTY "글"  (xterm's compositionend timer)

There are two causes:

  • The Shift+Enter to ESC+CR translation runs in the custom key handler, before xterm's CompositionHelper. It did not skip the IME keydown (229). The browser sends the key again after the commit, so the newline went out twice.
  • At the second keydown, xterm still holds the committed text in the setTimeout(0) that it starts on compositionend. A synchronous ESC+CR goes before that text.

The fix:

  • Do not send a translated chord on the keydown the IME takes to commit (keyCode === 229). The browser sends the key again after the commit. A chord that keeps its real keyCode still sends.
  • Track compositionend on the textarea. While xterm's timer is pending, send the chord from a later timer. The listener is added after terminal.open(), so its timers run after xterm's. Plain Shift+Enter with no composition still sends at once.

How I tested it

I sent real macOS key events (HID level, Korean 2-Set input source active) to the dev app. Oh My Pi ran in a workspace terminal. I typed 한 글 and then pressed Shift+Enter.

Before: the PTY got ["한 ", "\x1b\r", "\x1b\r", "글"]

before

After: the PTY got ["한 ", "글", "\x1b\r"]

after

  • English input: abc + Shift+Enter still sends ["a", "b", "c", "\x1b\r"], with the newline sent at once.
  • New test terminal-key-event-handler.test.ts replays the recorded event order against real xterm. On main it fails with ["\x1b\r", "\x1b\r", "글"]. With the fix it passes.
  • bun run lint, bun run typecheck in apps/desktop, and bun test src/renderer/lib/terminal src/xterm-ime-patch.test.ts (4083 pass).

Checklist

  • PR title follows conventional commits (type(scope): subject)
  • bun run lint and bun run typecheck pass (CI fails on lint warnings too)
  • "Allow edits from maintainers" is checked on fork PRs
  • I have signed the CLA (the bot asks on your first PR)

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a macOS terminal issue where pressing Shift+Enter during Korean text composition could interfere with the composed text. The text is now preserved, and the key combination is handled correctly after composition is committed.