Skip to content

Fix NaN coordinates in mouse wheel reports from touch fling inertia - #6059

Open
tajo wants to merge 1 commit into
xtermjs:masterfrom
tajo:fix/touch-inertia-nan-mouse-reports
Open

Fix NaN coordinates in mouse wheel reports from touch fling inertia#6059
tajo wants to merge 1 commit into
xtermjs:masterfrom
tajo:fix/touch-inertia-nan-mouse-reports

Conversation

@tajo

@tajo tajo commented Jul 22, 2026

Copy link
Copy Markdown

Found this problem when using 6.1.0-beta.291 on iPhone running claude code inside. Scrolling (touch fling) was causing garbage chars like NaN;NaNM being inputed into the prompt. This fixes it.

The rest is generated by Claude Fable.

Problem

Inertia (fling momentum) CHANGE events dispatched by Gesture._inertia (src/browser/scrollable/touch.ts) carried only translationX/Y, while real touchmove CHANGE events also carry page/client coordinates. The mouse-report wheel path (MouseService._handleTouchScrollAsWheel) reads event.clientX/clientY to compute the reported cell position, which produced NaN coordinates that passed through getMouseReportCoords' clamping untouched and were serialized into wheel reports (e.g. CSI < 64 ; NaN ; NaN M) — applications with mouse tracking enabled (vim with mouse=a, tmux, agent TUIs) received them as garbage input on every fling release.

Fix

Two additive changes (+18 lines):

  • src/browser/scrollable/touch.ts — fix at the source by stamping the position the inertia animation already tracks onto the momentum events (page coordinates; client derived via the window's scroll offsets), matching what _handleTouchMove stamps on real frames.
  • src/browser/services/MouseService.ts — defensively skip wheel reports for coordinate-less gesture events (!isFinite(e.clientX) || !isFinite(e.clientY)) so malformed sequences can't reach the PTY from this path again.

To reproduce

On a touch device (or DevTools touch emulation), run any app that enables mouse tracking (e.g. vim with set mouse=a) in the demo, flick-scroll and lift — the app receives ESC[<64;NaN;NaNM per momentum tick, visible as literal NaN;NaNM input. Discovered embedding xterm.js in a web dashboard terminal running an agent TUI with mouse reporting.

Validation

  • npm run build (tsgo) clean
  • npm run esbuild clean
  • npm run test-unit — 2403 passing, 0 failing

No new test added — the touch handlers are private/DOM-wired and the unit harness has no gesture mocks; happy to add coverage wherever you'd prefer it.

🤖 Generated with Claude Code

Inertia (fling momentum) CHANGE events carried only translationX/Y,
while real touchmove CHANGE events also carry page/client coordinates.
The mouse-report wheel path (_handleTouchScrollAsWheel) reads
event.clientX/clientY to compute the reported cell position, which
produced NaN coordinates that passed through getMouseReportCoords'
clamping untouched and were serialized into wheel reports (e.g.
`CSI < 64 ; NaN ; NaN M`) — applications with mouse tracking enabled
(vim with mouse=a, tmux, agent TUIs) received them as garbage input
on every fling release.

Fix at the source by stamping the position the inertia animation
already tracks onto the events (page coordinates; client derived via
the window's scroll offsets), and defensively skip wheel reports for
coordinate-less gesture events so malformed sequences can't reach the
PTY from this path again.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@tajo
tajo marked this pull request as ready for review July 22, 2026 03:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant