// The Terminal feature. `loader.ts` imports this module at the first use of a terminal panel: // the Terminal button, a terminal shortcut, or a terminal window. Settings → Terminal does not load it. // It starts shells through the `pty_*` commands of src-tauri/src/pty.rs (`transport.ts`). import { flushSync, mount, unmount } from "svelte"; import { tr } from "../../i18n/index.ts"; import type { TerminalInstance, TerminalMount } from "../terminalHost.ts"; import type { FeatureContext } from "../types.ts"; import { TerminalSessions } from "./sessions.ts"; import { desktopTransport } from "./transport.ts"; import TerminalBridge from "./TerminalBridge.svelte"; // The xterm styles load with the feature chunk. import "@xterm/xterm/css/xterm.css"; export function start(_context: FeatureContext): TerminalInstance { const sessions = new TerminalSessions(desktopTransport); const docks = new Set<() => Promise>(); return { mount() { throw new Error(tr("This view is unavailable.")); }, mountDock(element, props): TerminalMount { if (sessions.stopped) throw new Error(tr("This feature is off.")); const instance = mount(TerminalBridge, { target: element, props: { initial: props, sessions } }); flushSync(); let stopping: Promise | undefined; const dispose = () => stopping ??= unmount(instance).then(() => { docks.delete(dispose); }); docks.add(dispose); return { update: value => instance.update(value), command: action => instance.command(action), dispose }; }, /** Remove the panels, then stop the shells that are left. Panels close their own sessions when they unmount. */ async dispose() { const results = await Promise.allSettled([...docks].map(dispose => dispose())); await sessions.closeAll(); const errors = results.filter(result => result.status === "rejected"); if (errors.length) throw new Error(errors.map(result => String(result.reason)).join("\n")); }, }; }