Skip to content

fix(terminal): restore tmux scroll and resize after browser pane open/close#48

Merged
amirlehmam merged 1 commit into
amirlehmam:masterfrom
schroldgames:fix/tmux-scroll-resize
Jun 20, 2026
Merged

fix(terminal): restore tmux scroll and resize after browser pane open/close#48
amirlehmam merged 1 commit into
amirlehmam:masterfrom
schroldgames:fix/tmux-scroll-resize

Conversation

@schroldgames

Copy link
Copy Markdown
Contributor

Fixes #47. Complementary to #41 / PR #42 — this addresses the specific case where a browser pane (webview) is open alongside a tmux terminal.

Root causes

Webview compositor stealing wheel events: When a <webview> is present, Chromium routes un-prevented wheel events to it. The existing handler correctly falls through on the alternate buffer so xterm can handle it natively, but with a webview that fall-through sends the event to the webview instead of tmux.

Unreliable buffer type after remount: tmux doesn't re-send \x1b[?1049h on SIGWINCH — only on a fresh client attach. After React remounts the terminal (e.g. on browser pane close), xterm.buffer.active.type reads 'normal' even though tmux is still running, causing the wrong scroll path and a premature terminal.refresh() that paints stale content before tmux's own SIGWINCH redraw arrives.

Silent resize drop: If ResizeObserver fires before pty.create IPC resolves, the resize is dropped and tmux never gets SIGWINCH.

Changes (src/renderer/hooks/useTerminal.ts)

  • surfaceMouseEnabled map (module-level, survives remounts): tracks SGR/button mouse enable (\x1b[?100[0236]h) and disable per surface. Used by the wheel handler to reliably identify tmux/vim sessions independent of buffer.active.type.

  • hasWebviewCached + MutationObserver: caches webview presence to avoid a live querySelector on every wheel event. A single shared observer updates it when the DOM changes.

  • Wheel handler now branches on webview presence and mouse-tracking state:

    Condition Action
    No webview, normal buffer Intercept → scrollback
    No webview, alt buffer Fall through → xterm handles natively
    Webview + plain shell Intercept → scrollback
    Webview + mouse-enabled (tmux/vim) Intercept → write SGR scroll escapes to PTY
  • pendingResizeDims: captures ResizeObserver events that arrive before pty.create IPC resolves, flushed in attachToPty.

  • doInitialResize: retries via RAF (up to 8 attempts) until fitAddon.proposeDimensions() returns non-null, ensuring tmux gets SIGWINCH and redraws into the new xterm instance after remount.

  • Conditional terminal.refresh() in ResizeObserver: plain shells get it to mark rows dirty after a layout change; mouse-enabled apps skip it to avoid painting stale content before their own SIGWINCH redraw arrives.

Relationship to PR #42

PR #42 fixes the general alternate-buffer scroll regression (#41) by taking ownership of wheel events in the alternate screen unconditionally. This PR fixes the orthogonal webview case — the two changes are complementary and can coexist without conflict.

🤖 Generated with Claude Code

…/close

When a browser pane is open, Chromium routes un-prevented wheel events to the
webview compositor, stealing them from the terminal. This caused tmux scroll to
break and SGR escape sequences to leak into plain shells.

- Module-level surfaceMouseEnabled map tracks whether mouse reporting is active
  per surface (survives React remounts). Set on \x1b[?100[0236]h, cleared on
  \x1b[?100[0236]l. Used to distinguish tmux/vim from a plain shell even when
  xterm's buffer.active.type is unreliable after remount (tmux doesn't re-send
  \x1b[?1049h on SIGWINCH, only on a fresh client attach).

- Wheel handler now branches on webview presence (cached via MutationObserver):
    no webview, normal buffer  → scrollback directly
    no webview, alt buffer     → fall through to xterm natively
    webview + plain shell      → scrollback directly
    webview + mouse-enabled    → write SGR scroll escapes to PTY

- pendingResizeDims captures ResizeObserver events that arrive before pty.create
  IPC resolves so they are not silently dropped.

- doInitialResize retries via requestAnimationFrame (up to 8 attempts) until
  fitAddon.proposeDimensions() returns non-null, ensuring tmux gets SIGWINCH and
  redraws into the new xterm instance after remount.

- Deferred 300ms repaint safety-net (scrollToBottom + refresh) ensures the
  renderer paints the initial content after PTY attach.

- ResizeObserver refresh is now conditional: plain shells get terminal.refresh()
  to mark rows dirty after a layout change; mouse-enabled apps (tmux, vim) skip
  it to avoid painting stale buffer content before their own SIGWINCH redraw arrives.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@amirlehmam amirlehmam merged commit ee1ebcb into amirlehmam:master Jun 20, 2026
@amirlehmam

Copy link
Copy Markdown
Owner

Merged and shipped in v0.8.8 🚀 Thanks @schroldgames!

This overlapped with #42 in useTerminal.ts (both rewrote the wheel handler), so I unified the two rather than taking one wholesale. The merged handler:

Net behavior matches your PR for every webview case, plus #42's reliability for the non-webview alt-buffer case. tsc + the full suite are green (the 5 pty-manager failures are pre-existing/environmental — node-pty's native is built for Electron's ABI, not the Node test runner).

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.

bug(terminal): tmux scroll and resize break when a browser pane is open

2 participants