Skip to content

Commit 16b81b4

Browse files
committed
perf(web): stop rendering hidden terminals
Pause terminal snapshots, canvas paint, queued frames, and cursor timers while the drawer or right-panel terminal is hidden. Keep parsing output and answering VT queries. Refit and repaint current output on reveal. Preserve selection state and read the latest visibility after asynchronous WASM setup. Verify with 73 focused headless tests using the real Ghostty core, plus the web typecheck and targeted lint. Created with GPT-6 Astra (preview) in Codex.
1 parent 7c449d6 commit 16b81b4

4 files changed

Lines changed: 394 additions & 16 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,7 @@ const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerminalDra
11541154
});
11551155

11561156
interface PersistentThreadTerminalPanelProps {
1157+
visible: boolean;
11571158
threadRef: ScopedThreadRef;
11581159
surface: Extract<RightPanelSurface, { kind: "terminal" }>;
11591160
launchContext: PersistentTerminalLaunchContext | null;
@@ -1172,6 +1173,7 @@ interface PersistentThreadTerminalPanelProps {
11721173
}
11731174

11741175
const PersistentThreadTerminalPanel = memo(function PersistentThreadTerminalPanel({
1176+
visible,
11751177
threadRef,
11761178
surface,
11771179
launchContext,
@@ -1287,6 +1289,7 @@ const PersistentThreadTerminalPanel = memo(function PersistentThreadTerminalPane
12871289
return (
12881290
<ThreadTerminalDrawer
12891291
mode="panel"
1292+
visible={visible}
12901293
threadRef={threadRef}
12911294
threadId={threadRef.threadId}
12921295
cwd={cwd}
@@ -7549,6 +7552,7 @@ export default function ChatView(props: ChatViewProps) {
75497552
</Suspense>
75507553
) : renderedRightPanelSurface?.kind === "terminal" ? (
75517554
<PersistentThreadTerminalPanel
7555+
visible={rightPanelOpen}
75527556
threadRef={activeThreadRef}
75537557
surface={renderedRightPanelSurface}
75547558
launchContext={activeTerminalLaunchContext ?? null}

‎apps/web/src/components/ThreadTerminalDrawer.tsx‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
useCallback,
3434
useEffect,
3535
useEffectEvent,
36+
useLayoutEffect,
3637
useMemo,
3738
useRef,
3839
useState,
@@ -295,6 +296,7 @@ interface TerminalViewportProps {
295296
onAddTerminalContext: (selection: TerminalContextSelection) => void;
296297
focusRequestId: number;
297298
autoFocus: boolean;
299+
visible: boolean;
298300
resizeEpoch: number;
299301
drawerHeight: number;
300302
keybindings: ResolvedKeybindingsConfig;
@@ -319,12 +321,14 @@ export function TerminalViewport({
319321
onAddTerminalContext,
320322
focusRequestId,
321323
autoFocus,
324+
visible,
322325
resizeEpoch,
323326
drawerHeight,
324327
keybindings,
325328
}: TerminalViewportProps) {
326329
const containerRef = useRef<HTMLDivElement>(null);
327330
const terminalRef = useRef<GhosttyTerminalSurface | null>(null);
331+
const visibleRef = useRef(visible);
328332
const environmentId = threadRef.environmentId;
329333
const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
330334
const openInPreferredEditor = useOpenInPreferredEditor(
@@ -434,6 +438,11 @@ export function TerminalViewport({
434438
keybindingsRef.current = keybindings;
435439
}, [keybindings]);
436440

441+
useLayoutEffect(() => {
442+
visibleRef.current = visible;
443+
terminalRef.current?.setVisible(visible);
444+
}, [visible]);
445+
437446
useEffect(() => {
438447
const current = terminalFontRef.current;
439448
if (current.family === terminalFontFamily && current.size === terminalFontSize) return;
@@ -457,6 +466,9 @@ export function TerminalViewport({
457466
const terminalOptions: GhosttyTerminalSurfaceOptions = {
458467
theme: terminalThemeFromApp(mount),
459468
font: terminalFontOptions(setupFont.family, setupFont.size),
469+
get visible() {
470+
return visibleRef.current;
471+
},
460472
onData: (data) => handleData(data),
461473
onResize: (cols, rows) => void resizeTerminal(cols, rows),
462474
onSelectionChange: () => handleSelectionChange(),
@@ -474,6 +486,7 @@ export function TerminalViewport({
474486
terminal.dispose();
475487
return null;
476488
}
489+
terminal.setVisible(visibleRef.current);
477490
// The theme observer is not installed yet, so re-read the theme in case
478491
// the app toggled light/dark while the WASM surface was loading.
479492
terminal.setTheme(terminalThemeFromApp(mount));
@@ -503,7 +516,7 @@ export function TerminalViewport({
503516
// never started, so only "exited" triggers the message — as with xterm.)
504517
synchronizedStatusRef.current = "closed";
505518
synchronizeTerminalStatus(terminal, latestSession.status);
506-
if (autoFocus) window.requestAnimationFrame(() => terminal.focus());
519+
if (autoFocus && visibleRef.current) window.requestAnimationFrame(() => terminal.focus());
507520

508521
const dismissSelectionAction = (supersede = false) => {
509522
const ownsMenu =
@@ -891,7 +904,7 @@ export function TerminalViewport({
891904
writeSystemMessage(terminal, current.error);
892905
}
893906

894-
if (previous.version === 0 && autoFocus) {
907+
if (previous.version === 0 && autoFocus && visibleRef.current) {
895908
window.requestAnimationFrame(() => {
896909
terminal.focus();
897910
});
@@ -900,7 +913,7 @@ export function TerminalViewport({
900913
}, [autoFocus, terminalOutput, terminalError, terminalStatus, terminalVersion]);
901914

902915
useEffect(() => {
903-
if (!autoFocus) return;
916+
if (!autoFocus || !visible) return;
904917
const terminal = terminalRef.current;
905918
if (!terminal) return;
906919
const frame = window.requestAnimationFrame(() => {
@@ -909,15 +922,16 @@ export function TerminalViewport({
909922
return () => {
910923
window.cancelAnimationFrame(frame);
911924
};
912-
}, [autoFocus, focusRequestId]);
925+
}, [autoFocus, focusRequestId, visible]);
913926

914927
useEffect(() => {
915928
const terminal = terminalRef.current;
916-
if (!terminal) return;
929+
if (!terminal || !visibleRef.current) return;
917930
const wasAtBottom = terminal.isAtBottom();
918931
// The surface reports grid changes through onResize, which is the single
919932
// channel for PTY resize RPCs; fitting here only refreshes the layout.
920933
const frame = window.requestAnimationFrame(() => {
934+
if (!visibleRef.current) return;
921935
terminal.fit();
922936
if (wasAtBottom) {
923937
terminal.scrollToBottom();
@@ -1493,6 +1507,7 @@ export default function ThreadTerminalDrawer({
14931507
onAddTerminalContext={onAddTerminalContext}
14941508
focusRequestId={focusRequestId}
14951509
autoFocus={terminalId === resolvedActiveTerminalId}
1510+
visible={visible}
14961511
resizeEpoch={resizeEpoch}
14971512
drawerHeight={drawerHeight}
14981513
keybindings={keybindings}
@@ -1522,6 +1537,7 @@ export default function ThreadTerminalDrawer({
15221537
onAddTerminalContext={onAddTerminalContext}
15231538
focusRequestId={focusRequestId}
15241539
autoFocus
1540+
visible={visible}
15251541
resizeEpoch={resizeEpoch}
15261542
drawerHeight={drawerHeight}
15271543
keybindings={keybindings}

0 commit comments

Comments
 (0)