import "./styles.css"; import type { LifecycleInfo } from "../shared/ipc"; type TabId = "lifecycle" | "windows" | "single"; declare global { interface Window { api: { info(): Promise; open(): Promise<{ ok: boolean }>; minimize(): Promise<{ ok: boolean }>; maximize(): Promise<{ ok: boolean }>; close(): Promise<{ ok: boolean }>; }; __setTab?: (tab: TabId) => void; __snapshot?: () => unknown; } } const state: { tab: TabId; info: LifecycleInfo | null; busy: boolean } = { tab: "lifecycle", info: null, busy: false, }; const tabs: Array<{ id: TabId; label: string }> = [ { id: "lifecycle", label: "生命周期" }, { id: "windows", label: "窗口" }, { id: "single", 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 lifecycle(info: LifecycleInfo): string { return `

app 事件

最近 ${info.events.length} 条事件;主进程按发生顺序记录。

${info.events .map( (event) => ``, ) .join("")}
时间事件详情
${time(event.at)}${escapeHtml(event.name)}${escapeHtml(event.detail ?? "")}

macOS 关窗不退出;Windows/Linux 在 window-all-closed 时退出。before-quit/will-quit 只在真正退出时出现。

`; } function windows(info: LifecycleInfo): string { return `

窗口快照

${info.windows .map( (window) => ``, ) .join("")}
ID位置大小焦点最小化最大化
${window.id}${window.bounds.x}, ${window.bounds.y}${window.bounds.width}×${window.bounds.height}${window.focused}${window.minimized}${window.maximized}

多窗口之间不能直接共享对象;状态同步走主进程广播。位置与尺寸的持久化在第 30 章。

`; } function single(info: LifecycleInfo): string { return `

单实例锁

requestSingleInstanceLock${info.singleInstance}
当前窗口数${info.windows.length}
app.isReady${info.ready}

第二次启动不会创建新实例,而是在已有实例触发 second-instance,参数可以转发给主窗口。

验证:另开一个终端再次执行 start,第一个窗口会获得焦点,事件表出现 second-instance。

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

正在读取生命周期信息…

` : state.tab === "lifecycle" ? lifecycle(info) : state.tab === "windows" ? windows(info) : single(info); root.innerHTML = `

生命周期与窗口

app 事件、窗口快照、单实例锁

${body}
`; for (const button of root.querySelectorAll("[data-tab]")) { button.addEventListener("click", () => { const tab = button.dataset.tab; if (tab === "lifecycle" || tab === "windows" || tab === "single") { state.tab = tab; render(); } }); } const refresh = async (action: () => Promise) => { if (state.busy) return; state.busy = true; try { await action(); state.info = await window.api.info(); } finally { state.busy = false; render(); } }; root.querySelector("#open-button")?.addEventListener("click", () => { void refresh(() => window.api.open()); }); root.querySelector("#minimize-button")?.addEventListener("click", () => { void refresh(() => window.api.minimize()); }); root.querySelector("#maximize-button")?.addEventListener("click", () => { void refresh(() => window.api.maximize()); }); root.querySelector("#close-button")?.addEventListener("click", () => { void window.api.close(); }); } async function boot(): Promise { state.info = await window.api.info(); render(); window.__setTab = (tab) => { state.tab = tab; render(); }; window.__snapshot = () => ({ tab: state.tab, windows: state.info?.windows.length, singleInstance: state.info?.singleInstance, }); } void boot();