import "./styles.css"; import type { ExplorerInfo, WindowRow } from "../shared/ipc"; type TabId = "windows" | "processes" | "events"; declare global { interface Window { api: { list(): Promise; kill(id: number): Promise<{ ok: boolean; code?: string }>; focus(id: number): Promise<{ ok: boolean; code?: string }>; create(): Promise<{ ok: boolean; id?: number }>; }; __setTab?: (tab: TabId) => void; __snapshot?: () => unknown; } } const state: { tab: TabId; info: ExplorerInfo | null; busy: boolean; error: string; } = { tab: "windows", info: null, busy: false, error: "", }; const tabs: Array<{ id: TabId; label: string }> = [ { id: "windows", label: "窗口" }, { id: "processes", label: "进程" }, { id: "events", label: "恢复日志" }, ]; function escapeHtml(value: string): string { return value .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """); } function time(at: number): string { return new Date(at).toLocaleTimeString("zh-CN", { hour12: false }); } function mb(kb: number): string { return `${(kb / 1024).toFixed(1)} MB`; } function uptime(ms: number): string { if (ms < 1000) return `${ms} ms`; if (ms < 60_000) return `${(ms / 1000).toFixed(1)} s`; const minutes = Math.floor(ms / 60_000); const seconds = Math.floor((ms % 60_000) / 1000); return `${minutes}m ${String(seconds).padStart(2, "0")}s`; } function summary(info: ExplorerInfo): string { const recoveries = Object.values(info.recoveryCounts).reduce( (sum, value) => sum + (value ?? 0), 0, ); return `
窗口
${info.windows.length}
渲染进程
${info.windows.length}
系统进程
${info.processes.length}
恢复次数
${recoveries}
`; } function windowTable(rows: WindowRow[]): string { return `

窗口与渲染进程

${rows .map( (row) => ``, ) .join("")}
ID角色PID运行时长内存可见操作
${row.id} ${escapeHtml(row.role)} ${row.pid} ${uptime(row.uptimeMs)} ${mb(row.memoryKB)} ${row.visible ? "是" : "否"} ${ row.role === "control" ? `` : `` }

结束渲染进程会触发 render-process-gone,主进程在 300ms 后重建同角色窗口。

`; } function processTable(info: ExplorerInfo): string { return `

app.getAppMetrics()

${info.processes .map( (row) => ``, ) .join("")}
类型PID内存CPU
${escapeHtml(row.type)} ${row.pid} ${mb(row.memoryKB)} ${row.cpu.toFixed(1)}%

Browser 是主进程;Tab 是渲染进程;GPU / Utility 由 Chromium 管理。内存单位是 workingSetSize。

`; } function eventTable(info: ExplorerInfo): string { return `

恢复日志

${info.events .slice() .reverse() .map( (row) => ``, ) .join("")}
时间事件消息
${time(row.at)} ${escapeHtml(row.evt)} ${escapeHtml(row.msg ?? "")}

每个窗口最多自动恢复 3 次;超过后停止,避免崩溃循环把 CPU 打满。

`; } function render(): void { const root = document.querySelector("#app"); if (!root) return; const info = state.info; let body = `

正在读取进程信息…

`; if (info) { if (state.tab === "windows") body = summary(info) + windowTable(info.windows); else if (state.tab === "processes") body = processTable(info); else body = eventTable(info); } root.innerHTML = `

进程浏览器

窗口 / 渲染进程 PID / 内存 / 崩溃恢复${ info?.isPackaged ? "(打包版)" : "" }

日志:${escapeHtml(info?.logFileLabel ?? "")}
${state.error ? `

${escapeHtml(state.error)}

` : ""}
${body}
`; for (const button of root.querySelectorAll("[data-tab]")) { button.addEventListener("click", () => { const tab = button.dataset.tab; if (tab === "windows" || tab === "processes" || tab === "events") { state.tab = tab; render(); } }); } for (const button of root.querySelectorAll( "[data-focus]", )) { button.addEventListener("click", () => { const id = Number(button.dataset.focus); void action(() => window.api.focus(id)); }); } for (const button of root.querySelectorAll( "[data-kill]", )) { button.addEventListener("click", () => { const id = Number(button.dataset.kill); void action(() => window.api.kill(id)); }); } root.querySelector("#create-button")?.addEventListener("click", () => { void action(() => window.api.create()); }); root.querySelector("#refresh-button")?.addEventListener("click", () => { void refresh(); }); } async function action(run: () => Promise): Promise { if (state.busy) return; state.busy = true; try { await run(); state.error = ""; await new Promise((resolve) => setTimeout(resolve, 120)); state.info = await window.api.list(); } catch (error) { state.error = String(error); } finally { state.busy = false; render(); } } async function refresh(): Promise { if (state.busy) return; state.busy = true; try { state.info = await window.api.list(); state.error = ""; } catch (error) { state.error = String(error); } finally { state.busy = false; render(); } } async function boot(): Promise { state.info = await window.api.list(); render(); window.__setTab = (tab) => { state.tab = tab; render(); }; window.__snapshot = () => ({ tab: state.tab, windows: state.info?.windows.length ?? 0, processes: state.info?.processes.length ?? 0, events: state.info?.events.length ?? 0, }); setInterval(() => { if (!state.busy) void refresh(); }, 1000); } void boot();