terminal fixes

This commit is contained in:
Neil
2026-03-21 12:11:17 -07:00
parent a3ef3a59d5
commit 173965b57f
2 changed files with 118 additions and 30 deletions
+64 -15
View File
@@ -389,6 +389,11 @@ export default function TerminalPane({
)
// Track transports per pane for PTY communication
const paneTransportsRef = useRef<Map<number, PtyTransport>>(new Map())
// Buffer PTY data for background (non-visible) terminals to avoid
// unnecessary parser/render work. Flushed when the tab becomes active.
const pendingWritesRef = useRef<Map<number, string>>(new Map())
const isActiveRef = useRef(isActive)
isActiveRef.current = isActive
const [terminalMenuOpen, setTerminalMenuOpen] = useState(false)
const [terminalMenuPoint, setTerminalMenuPoint] = useState({ x: 0, y: 0 })
const [expandedPaneId, setExpandedPaneId] = useState<number | null>(null)
@@ -637,18 +642,39 @@ export default function TerminalPane({
transport.resize(cols, rows)
})
// Connect PTY and wire PTY → terminal
const cols = pane.terminal.cols
const rows = pane.terminal.rows
transport.connect({
url: '',
cols,
rows,
callbacks: {
onData: (data) => {
pane.terminal.write(data)
}
// Defer PTY spawn to next frame so FitAddon has time to calculate
// the correct terminal dimensions from the laid-out container. Without
// this, the PTY is spawned with the default 80×24 and never resized
// to fill the actual container.
pendingWritesRef.current.set(pane.id, '')
requestAnimationFrame(() => {
// Fit first so cols/rows reflect the real container size
try {
pane.fitAddon.fit()
} catch {
/* ignore */
}
const cols = pane.terminal.cols
const rows = pane.terminal.rows
transport.connect({
url: '',
cols,
rows,
callbacks: {
onData: (data) => {
if (isActiveRef.current) {
// Visible — write immediately for responsive output
pane.terminal.write(data)
} else {
// Hidden — buffer data to avoid unnecessary render work.
// The buffer is flushed in one write() call when the tab
// becomes visible, which is much cheaper than N small writes.
const pending = pendingWritesRef.current
pending.set(pane.id, (pending.get(pane.id) ?? '') + data)
}
}
}
})
})
}
@@ -697,6 +723,7 @@ export default function TerminalPane({
paneTransportsRef.current.delete(paneId)
}
paneFontSizesRef.current.delete(paneId)
pendingWritesRef.current.delete(paneId)
},
onActivePaneChange: () => {
if (shouldPersistLayout) persistLayoutSnapshot()
@@ -712,9 +739,15 @@ export default function TerminalPane({
return {
fontSize: currentSettings?.terminalFontSize ?? 14,
fontFamily: buildFontFamily(currentSettings?.terminalFontFamily ?? 'SF Mono'),
scrollback: Math.max(
1000,
Math.round((currentSettings?.terminalScrollbackBytes ?? 10_000_000) / 100)
// Convert byte budget to line count. ~200 bytes/line is a reasonable
// average for typical terminal output (columns × ~2 bytes + overhead).
// Default 10MB → ~50K lines; cap at 50K to keep memory reasonable.
scrollback: Math.min(
50_000,
Math.max(
1000,
Math.round((currentSettings?.terminalScrollbackBytes ?? 10_000_000) / 200)
)
),
cursorStyle: currentSettings?.terminalCursorStyle ?? 'bar',
cursorBlink: currentSettings?.terminalCursorBlink ?? true
@@ -760,6 +793,7 @@ export default function TerminalPane({
transport.destroy?.()
}
paneTransportsRef.current.clear()
pendingWritesRef.current.clear()
manager.destroy()
managerRef.current = null
setTabPaneExpanded(tabId, false)
@@ -821,12 +855,24 @@ export default function TerminalPane({
})
}, [isActive])
// Handle focus and resize when tab becomes active
// Handle focus, resize, and WebGL suspend/resume when tab becomes active/inactive
useEffect(() => {
const manager = managerRef.current
if (!manager) return
if (isActive) {
// Resume GPU rendering — recreate WebGL addons that were disposed
manager.resumeRendering()
// Flush any buffered PTY data that arrived while hidden
for (const [paneId, buf] of pendingWritesRef.current.entries()) {
if (buf.length > 0) {
const pane = manager.getPanes().find((p) => p.id === paneId)
if (pane) pane.terminal.write(buf)
pendingWritesRef.current.set(paneId, '')
}
}
// Ensure size/focus is correct both on initial mount and tab activation.
requestAnimationFrame(() => {
const panes = manager.getPanes()
@@ -842,6 +888,9 @@ export default function TerminalPane({
active.terminal.focus()
}
})
} else if (wasActiveRef.current) {
// Went from active → inactive: free GPU contexts
manager.suspendRendering()
}
wasActiveRef.current = isActive
}, [isActive])
+54 -15
View File
@@ -72,6 +72,13 @@ export class PaneManager {
createInitialPane(opts?: { focus?: boolean }): ManagedPane {
const pane = this.createPaneInternal()
// When the pane is the sole child of root (no splits), it must
// fill the root container so FitAddon calculates correct dimensions.
pane.container.style.width = '100%'
pane.container.style.height = '100%'
pane.container.style.position = 'relative'
pane.container.style.overflow = 'hidden'
// Place directly into root
this.root.appendChild(pane.container)
@@ -198,16 +205,14 @@ export class PaneManager {
const grandparent = parent.parentElement
if (grandparent) {
if (grandparent === this.root) {
// Going back to root level — remove flex styles so it fills naturally
// Going back to root level — fill the root container
sibling.style.flex = ''
sibling.style.minWidth = ''
sibling.style.minHeight = ''
sibling.style.position = ''
sibling.style.overflow = ''
if (sibling.classList.contains('pane-split')) {
sibling.style.width = '100%'
sibling.style.height = '100%'
}
sibling.style.width = '100%'
sibling.style.height = '100%'
sibling.style.position = 'relative'
sibling.style.overflow = 'hidden'
} else if (grandparent.classList.contains('pane-split')) {
// Going into another split — inherit the flex slot from the
// split container we're removing
@@ -283,6 +288,36 @@ export class PaneManager {
this.applyRootBackground()
}
/**
* Suspend GPU rendering for all panes. Disposes WebGL addons to free
* GPU contexts while keeping Terminal instances alive (scrollback, cursor,
* screen buffer all preserved). Call when this tab/worktree becomes hidden.
*/
suspendRendering(): void {
for (const pane of this.panes.values()) {
if (pane.webglAddon) {
try {
pane.webglAddon.dispose()
} catch {
/* ignore */
}
pane.webglAddon = null
}
}
}
/**
* Resume GPU rendering for all panes. Recreates WebGL addons. Call when
* this tab/worktree becomes visible again. Must be followed by a fit() pass.
*/
resumeRendering(): void {
for (const pane of this.panes.values()) {
if (!pane.webglAddon) {
this.attachWebgl(pane)
}
}
}
destroy(): void {
this.destroyed = true
for (const pane of this.panes.values()) {
@@ -374,24 +409,28 @@ export class PaneManager {
// Activate unicode 11
terminal.unicode.activeVersion = '11'
// Try WebGL, fall back to canvas
// Attach GPU renderer
this.attachWebgl(pane)
// Initial fit (deferred to ensure layout has settled)
requestAnimationFrame(() => {
this.safeFit(pane)
})
}
private attachWebgl(pane: ManagedPaneInternal): void {
try {
const webglAddon = new WebglAddon()
webglAddon.onContextLoss(() => {
webglAddon.dispose()
pane.webglAddon = null
})
terminal.loadAddon(webglAddon)
pane.terminal.loadAddon(webglAddon)
pane.webglAddon = webglAddon
} catch {
// WebGL not available — default canvas renderer is fine
// WebGL not available — default DOM renderer is fine
pane.webglAddon = null
}
// Initial fit (deferred to ensure layout has settled)
requestAnimationFrame(() => {
this.safeFit(pane)
})
}
private safeFit(pane: ManagedPaneInternal): void {