fix(desktop): keep terminal scroll position across bot switches - #7
Merged
Merged
Conversation
A cached terminal is re-parented when the user switches bots, and the browser resets a scrollable element's offset the moment it leaves the document. The pane painted the screen it had kept while its viewport reported being at the top, so the first wheel tick after a switch snapped the buffer back to the start of the scrollback. Record the viewport offset while the element is still connected and restore it after re-attaching, ignoring the scroll event that detaching itself produces.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Switching bots no longer resets the terminal's scroll position. A pane that was parked mid-scrollback comes back exactly where the user left it, and the first wheel tick after a switch no longer snaps the buffer to the top of the scrollback.
Why
TerminalPanecaches terminals and re-parents the same DOM element when the user switches bots. The browser resets a scrollable element'sscrollTopthe moment it leaves the document, so the restored pane painted the screen it had kept while.xterm-viewportreported being at offset 0. xterm then treated the next scroll input as starting from the top of the buffer.The fix stores the viewport offset on the cache entry and restores it after re-attaching. The offset is only recorded while the viewport is still connected — detaching itself fires a scroll event reporting a position nobody scrolled to, and recording that would park the terminal at the top, which is the bug the change is meant to remove.
How tested
pnpm typecheck,pnpm lint,pnpm format:check,pnpm knipinapps/desktop— clean.pnpm testinapps/desktop— 441 tests across 54 files pass.apps/desktop/src/components/TerminalPane.scroll.test.tsxcovers both halves: the offset survives a detach/re-attach cycle, and the scroll-to-top event that accompanies detaching does not overwrite the stored offset. The test double for xterm now creates a real.xterm-viewportelement, matching what xterm builds onopen().Notes
Branched from
mainof this repository rather than the archivedgravity-private-archiveremote the worktree still points at; the two share no history. A second local commit that bumpedprotocol_versionto 2 in.claude/skills/terminal-perf-test/scripts/drive.mjswas dropped — that fix already exists here.