cc-haha/desktop/src/lib/terminalRuntime.ts
程序员阿江(Relakkes) 76a4ca5d30 fix: preserve desktop terminal sessions across panel moves
Users can be typing in the docked terminal when they hide the panel or promote it into a tab. The old component-owned lifecycle treated those UI moves as terminal teardown and spawned a fresh shell afterward.

This moves terminal ownership into a small runtime registry keyed by panel or tab identity, keeps docked terminals mounted while hidden, and transfers the runtime id to the terminal tab when promoted. Closing the owning tab or session still releases the PTY.

Constraint: Keep native PTY behavior unchanged and fix this in the desktop React lifecycle layer.
Rejected: Persist terminal tabs through localStorage | runtime PTYs are process-local and should not be restored after app restart.
Confidence: high
Scope-risk: moderate
Directive: Do not tie terminal process lifetime to panel visibility; only explicit owning-surface close/restart should destroy it.
Tested: cd desktop && bun run test -- src/pages/TerminalSettings.test.tsx src/pages/ActiveSession.test.tsx src/components/layout/ContentRouter.test.tsx src/stores/tabStore.test.ts
Tested: bun run check:desktop
Not-tested: Manual Tauri window PTY smoke.
2026-05-25 22:33:22 +08:00

108 lines
2.8 KiB
TypeScript

import type { Terminal as XTermTerminal, IDisposable } from '@xterm/xterm'
import type { FitAddon as XTermFitAddon } from '@xterm/addon-fit'
import { terminalApi } from '../api/terminal'
export type TerminalStatus = 'idle' | 'starting' | 'running' | 'exited' | 'error' | 'unavailable'
export type TerminalShellInfo = {
shell: string
cwd: string
}
export type TerminalRuntime = {
id: string
terminal: XTermTerminal | null
fit: XTermFitAddon | null
nativeSessionId: number | null
unlisteners: Array<() => void>
dataDisposable: IDisposable | null
status: TerminalStatus
error: string | null
shellInfo: TerminalShellInfo | null
listeners: Set<() => void>
}
const runtimes = new Map<string, TerminalRuntime>()
let localRuntimeCounter = 0
export function createLocalTerminalRuntimeId() {
localRuntimeCounter += 1
return `local-terminal-${localRuntimeCounter}`
}
export function getTerminalRuntime(id: string, initialStatus: TerminalStatus): TerminalRuntime {
const existing = runtimes.get(id)
if (existing) return existing
const runtime: TerminalRuntime = {
id,
terminal: null,
fit: null,
nativeSessionId: null,
unlisteners: [],
dataDisposable: null,
status: initialStatus,
error: null,
shellInfo: null,
listeners: new Set(),
}
runtimes.set(id, runtime)
return runtime
}
export function updateTerminalRuntime(
runtime: TerminalRuntime,
patch: Partial<Pick<TerminalRuntime, 'terminal' | 'fit' | 'nativeSessionId' | 'status' | 'error' | 'shellInfo'>>,
) {
Object.assign(runtime, patch)
notifyTerminalRuntime(runtime)
}
export function notifyTerminalRuntime(runtime: TerminalRuntime) {
runtime.listeners.forEach((listener) => listener())
}
export function subscribeTerminalRuntime(runtime: TerminalRuntime, listener: () => void) {
runtime.listeners.add(listener)
return () => {
runtime.listeners.delete(listener)
}
}
export function attachTerminalRuntime(runtime: TerminalRuntime, host: HTMLElement) {
const terminal = runtime.terminal
if (!terminal) return
const element = terminal.element
if (element) {
if (element.parentElement !== host) {
host.replaceChildren(element)
}
} else {
host.innerHTML = ''
terminal.open(host)
}
runtime.fit?.fit()
}
export function destroyTerminalRuntime(id: string) {
const runtime = runtimes.get(id)
if (!runtime) return
runtimes.delete(id)
const sessionId = runtime.nativeSessionId
runtime.nativeSessionId = null
if (sessionId) {
void terminalApi.kill(sessionId).catch(() => {})
}
runtime.dataDisposable?.dispose()
runtime.dataDisposable = null
runtime.unlisteners.forEach((unlisten) => unlisten())
runtime.unlisteners = []
runtime.terminal?.dispose()
runtime.terminal = null
runtime.fit = null
runtime.listeners.clear()
}