2026-04-22 00:24:21 +08:00
|
|
|
import { readFileSync, writeFileSync } from "node:fs";
|
2026-04-19 14:18:20 +08:00
|
|
|
import { join } from "node:path";
|
|
|
|
|
|
2026-04-26 19:47:35 +08:00
|
|
|
import { shell } from "electron";
|
2026-04-24 09:39:17 +08:00
|
|
|
import { BrowserWindow, app, ipcMain, nativeTheme } from "electron/main";
|
2026-04-19 14:18:20 +08:00
|
|
|
import type {
|
|
|
|
|
BrowserWindow as ElectronBrowserWindow,
|
2026-04-20 17:16:15 +08:00
|
|
|
WebContents as ElectronWebContents,
|
2026-04-19 14:18:20 +08:00
|
|
|
} from "electron/main";
|
|
|
|
|
import type { DesktopAccountInfo, DesktopRuntimeSessionSyncRequest } from "@geo/shared-types";
|
|
|
|
|
|
2026-04-20 17:16:15 +08:00
|
|
|
import { initAccountHealth } from "./account-health";
|
2026-04-27 12:50:45 +08:00
|
|
|
import {
|
|
|
|
|
applyDesktopHealthIndicatorFromSnapshot,
|
|
|
|
|
createDesktopHealthIcon,
|
|
|
|
|
startDesktopHealthIndicator,
|
|
|
|
|
syncDesktopHealthIndicator,
|
|
|
|
|
} from "./app-issue-indicator";
|
2026-04-19 14:18:20 +08:00
|
|
|
import { bindPublishAccount, openPublishAccountConsole } from "./account-binder";
|
2026-04-22 00:24:21 +08:00
|
|
|
import { installObservedGlobalFetch, registerObservedRequestRendererTarget } from "./network-observer";
|
2026-04-20 11:07:58 +08:00
|
|
|
import { initProcessMetricsSampler } from "./process-metrics";
|
2026-04-20 17:16:15 +08:00
|
|
|
import { getPlaywrightCDPPort, startHiddenPlaywrightReaper } from "./playwright-cdp";
|
2026-04-20 09:52:48 +08:00
|
|
|
import { registerRendererDevtoolsProxyTarget } from "./renderer-devtools-proxy";
|
2026-04-20 17:16:15 +08:00
|
|
|
import { onRuntimeInvalidated } from "./runtime-events";
|
2026-04-20 09:52:48 +08:00
|
|
|
import {
|
2026-04-26 21:57:26 +08:00
|
|
|
noteRuntimeAccountBound,
|
2026-04-27 21:33:55 +08:00
|
|
|
requestRuntimeAccountProbe,
|
2026-04-20 09:52:48 +08:00
|
|
|
refreshRuntimeAccounts,
|
|
|
|
|
releaseRuntimeSession,
|
|
|
|
|
syncRuntimeSession,
|
|
|
|
|
unbindRuntimeAccount,
|
|
|
|
|
} from "./runtime-controller";
|
2026-04-27 21:33:55 +08:00
|
|
|
import { createRuntimeAccountSnapshot, createRuntimeSnapshot } from "./runtime-snapshot";
|
2026-04-27 20:06:37 +08:00
|
|
|
import { resolveDesktopClientID, resolveDesktopDeviceInfo } from "./device-id";
|
2026-04-19 14:18:20 +08:00
|
|
|
import { initScheduler } from "./scheduler";
|
|
|
|
|
import { initSessionRegistry } from "./session-registry";
|
|
|
|
|
import { initSingleInstance } from "./single-instance";
|
2026-04-20 09:52:48 +08:00
|
|
|
import { initTransport, listDesktopPublishTasks, retryDesktopPublishTask } from "./transport/api-client";
|
2026-04-19 14:18:20 +08:00
|
|
|
import { initTray } from "./tray";
|
|
|
|
|
import { STANDARD_USER_AGENT } from "./user-agent";
|
2026-04-20 11:07:58 +08:00
|
|
|
import { startHotViewReaper } from "./view-pool";
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
|
|
|
app.commandLine.appendSwitch(
|
|
|
|
|
"disable-features",
|
|
|
|
|
"PostQuantumKyber,EncryptedClientHello,UseDnsHttpsSvcb,UseDnsHttpsSvcbAlpn",
|
|
|
|
|
);
|
|
|
|
|
app.commandLine.appendSwitch("disable-quic");
|
|
|
|
|
app.commandLine.appendSwitch("disable-background-networking");
|
2026-04-20 17:16:15 +08:00
|
|
|
app.commandLine.appendSwitch("remote-debugging-port", String(getPlaywrightCDPPort()));
|
2026-04-19 14:18:20 +08:00
|
|
|
app.userAgentFallback = STANDARD_USER_AGENT;
|
2026-04-22 00:24:21 +08:00
|
|
|
installObservedGlobalFetch();
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
|
|
|
console.info("[desktop-main] boot", {
|
|
|
|
|
electron: process.versions.electron,
|
|
|
|
|
chrome: process.versions.chrome,
|
|
|
|
|
node: process.versions.node,
|
|
|
|
|
ua: STANDARD_USER_AGENT,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
process.on("unhandledRejection", (reason) => {
|
|
|
|
|
console.error("[desktop-main] unhandled rejection", reason);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
process.on("uncaughtException", (error) => {
|
|
|
|
|
console.error("[desktop-main] uncaught exception", error);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let mainWindow: ElectronBrowserWindow | null = null;
|
2026-04-20 17:16:15 +08:00
|
|
|
let mainRendererContents: ElectronWebContents | null = null;
|
2026-04-20 09:52:48 +08:00
|
|
|
let quitReleaseInFlight = false;
|
2026-04-19 14:18:20 +08:00
|
|
|
|
2026-04-22 00:24:21 +08:00
|
|
|
type WindowMode = "login" | "main";
|
|
|
|
|
|
|
|
|
|
const WINDOW_SIZES: Record<WindowMode, { width: number; height: number; resizable: boolean }> = {
|
|
|
|
|
login: { width: 340, height: 540, resizable: false },
|
|
|
|
|
main: { width: 1320, height: 860, resizable: true },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function windowModePath(): string {
|
|
|
|
|
return join(app.getPath("userData"), "window-mode.json");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function readPersistedWindowMode(): WindowMode {
|
|
|
|
|
try {
|
|
|
|
|
const raw = readFileSync(windowModePath(), "utf8");
|
|
|
|
|
const data = JSON.parse(raw) as { mode?: unknown };
|
|
|
|
|
if (data?.mode === "main" || data?.mode === "login") {
|
|
|
|
|
return data.mode;
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// First run, missing file, or corrupt — fall through to default.
|
|
|
|
|
}
|
|
|
|
|
return "login";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function persistWindowMode(mode: WindowMode): void {
|
|
|
|
|
try {
|
|
|
|
|
writeFileSync(windowModePath(), JSON.stringify({ mode }), "utf8");
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.warn("[desktop-main] persist window mode failed", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let currentWindowMode: WindowMode = "login";
|
|
|
|
|
let windowModeApplied = false;
|
|
|
|
|
|
|
|
|
|
function applyWindowMode(window: ElectronBrowserWindow, mode: WindowMode): void {
|
|
|
|
|
const isFirstApply = !windowModeApplied;
|
|
|
|
|
const sameMode = windowModeApplied && currentWindowMode === mode;
|
|
|
|
|
|
|
|
|
|
if (!sameMode) {
|
|
|
|
|
currentWindowMode = mode;
|
|
|
|
|
persistWindowMode(mode);
|
|
|
|
|
|
|
|
|
|
const target = WINDOW_SIZES[mode];
|
|
|
|
|
window.setResizable(true);
|
|
|
|
|
window.setMinimumSize(1, 1);
|
|
|
|
|
window.setMaximumSize(0, 0);
|
|
|
|
|
window.setSize(target.width, target.height, false);
|
|
|
|
|
window.center();
|
|
|
|
|
|
|
|
|
|
if (mode === "login") {
|
|
|
|
|
window.setMinimumSize(target.width, target.height);
|
|
|
|
|
window.setMaximumSize(target.width, target.height);
|
|
|
|
|
window.setResizable(false);
|
|
|
|
|
} else {
|
|
|
|
|
window.setMinimumSize(800, 600);
|
|
|
|
|
window.setResizable(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
windowModeApplied = true;
|
|
|
|
|
|
|
|
|
|
if (isFirstApply || !window.isVisible()) {
|
|
|
|
|
window.show();
|
|
|
|
|
window.focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function currentMainWindow(): ElectronBrowserWindow | null {
|
|
|
|
|
if (!mainWindow) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (mainWindow.isDestroyed()) {
|
|
|
|
|
mainWindow = null;
|
|
|
|
|
windowModeApplied = false;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return mainWindow;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 12:50:45 +08:00
|
|
|
function captureRuntimeSnapshot() {
|
|
|
|
|
const snapshot = createRuntimeSnapshot();
|
|
|
|
|
applyDesktopHealthIndicatorFromSnapshot(snapshot, currentMainWindow());
|
|
|
|
|
return snapshot;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 21:33:55 +08:00
|
|
|
function captureRuntimeAccountSnapshot(accountId: string) {
|
|
|
|
|
return createRuntimeAccountSnapshot(accountId);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 00:24:21 +08:00
|
|
|
async function ensureMainWindow(): Promise<ElectronBrowserWindow> {
|
|
|
|
|
const existing = currentMainWindow();
|
|
|
|
|
if (existing) {
|
|
|
|
|
return existing;
|
|
|
|
|
}
|
|
|
|
|
const window = await createMainWindow();
|
|
|
|
|
mainWindow = window;
|
|
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function revealMainWindow(): Promise<void> {
|
|
|
|
|
const window = await ensureMainWindow();
|
2026-04-27 20:06:37 +08:00
|
|
|
if (window.isMinimized()) {
|
|
|
|
|
window.restore();
|
|
|
|
|
}
|
2026-04-22 00:24:21 +08:00
|
|
|
applyWindowMode(window, currentWindowMode);
|
2026-04-27 20:06:37 +08:00
|
|
|
if (!window.isVisible()) {
|
|
|
|
|
window.show();
|
|
|
|
|
}
|
|
|
|
|
window.focus();
|
|
|
|
|
app.focus({ steal: true });
|
2026-04-22 00:24:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function revealMainWindowSafely(source: string): void {
|
|
|
|
|
void revealMainWindow().catch((error) => {
|
|
|
|
|
console.error("[desktop-main] reveal main window failed", { source, error });
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 14:18:20 +08:00
|
|
|
function rendererURL(): string | null {
|
|
|
|
|
return process.env.ELECTRON_RENDERER_URL ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function preloadPath(): string {
|
|
|
|
|
return join(__dirname, "../preload/bridge.cjs");
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 09:39:17 +08:00
|
|
|
async function mountRendererWebContents(window: ElectronBrowserWindow): Promise<void> {
|
|
|
|
|
const contents = window.webContents;
|
2026-04-19 14:18:20 +08:00
|
|
|
|
2026-04-24 09:39:17 +08:00
|
|
|
contents.setWindowOpenHandler(() => ({ action: "deny" }));
|
|
|
|
|
registerRendererDevtoolsProxyTarget(contents);
|
|
|
|
|
registerObservedRequestRendererTarget(contents);
|
|
|
|
|
mainRendererContents = contents;
|
2026-04-20 17:16:15 +08:00
|
|
|
window.on("closed", () => {
|
2026-04-24 09:39:17 +08:00
|
|
|
if (mainRendererContents === contents) {
|
2026-04-20 17:16:15 +08:00
|
|
|
mainRendererContents = null;
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
|
|
|
const devServerURL = rendererURL();
|
|
|
|
|
if (devServerURL) {
|
2026-04-24 09:39:17 +08:00
|
|
|
await contents.loadURL(devServerURL);
|
|
|
|
|
contents.openDevTools({ mode: "detach" });
|
2026-04-19 14:18:20 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 09:39:17 +08:00
|
|
|
await contents.loadFile(join(__dirname, "../renderer/index.html"));
|
2026-04-19 14:18:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function createMainWindow(): Promise<ElectronBrowserWindow> {
|
2026-04-22 00:24:21 +08:00
|
|
|
const initial = WINDOW_SIZES[currentWindowMode];
|
2026-04-19 14:18:20 +08:00
|
|
|
const window = new BrowserWindow({
|
2026-04-22 00:24:21 +08:00
|
|
|
width: initial.width,
|
|
|
|
|
height: initial.height,
|
|
|
|
|
show: false,
|
2026-04-19 14:18:20 +08:00
|
|
|
title: "GEO Rankly Desktop",
|
2026-04-27 12:50:45 +08:00
|
|
|
icon: createDesktopHealthIcon("normal"),
|
2026-04-22 00:24:21 +08:00
|
|
|
titleBarStyle: "hiddenInset",
|
|
|
|
|
trafficLightPosition: { x: 12, y: 14 },
|
|
|
|
|
backgroundColor: nativeTheme.shouldUseDarkColors ? "#15191a" : "#eaf2ff",
|
2026-04-24 09:39:17 +08:00
|
|
|
webPreferences: {
|
|
|
|
|
preload: preloadPath(),
|
|
|
|
|
sandbox: true,
|
|
|
|
|
contextIsolation: true,
|
|
|
|
|
nodeIntegration: false,
|
|
|
|
|
},
|
2026-04-19 14:18:20 +08:00
|
|
|
});
|
2026-04-22 00:24:21 +08:00
|
|
|
|
|
|
|
|
const fallbackReveal = setTimeout(() => {
|
|
|
|
|
if (!windowModeApplied && !window.isDestroyed()) {
|
|
|
|
|
applyWindowMode(window, currentWindowMode);
|
|
|
|
|
}
|
|
|
|
|
}, 1500);
|
|
|
|
|
window.once("closed", () => {
|
|
|
|
|
clearTimeout(fallbackReveal);
|
|
|
|
|
if (mainWindow === window) {
|
|
|
|
|
mainWindow = null;
|
|
|
|
|
windowModeApplied = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-23 09:09:20 +08:00
|
|
|
window.on("minimize", () => {
|
|
|
|
|
console.warn("[desktop-main] main window minimize event", {
|
|
|
|
|
at: new Date().toISOString(),
|
|
|
|
|
stack: new Error("trace").stack,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
window.on("hide", () => {
|
|
|
|
|
console.warn("[desktop-main] main window hide event", {
|
|
|
|
|
at: new Date().toISOString(),
|
|
|
|
|
stack: new Error("trace").stack,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
window.on("blur", () => {
|
|
|
|
|
console.warn("[desktop-main] main window blur event", {
|
|
|
|
|
at: new Date().toISOString(),
|
|
|
|
|
visible: window.isVisible(),
|
|
|
|
|
minimized: window.isMinimized(),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-24 09:39:17 +08:00
|
|
|
await mountRendererWebContents(window);
|
2026-04-19 14:18:20 +08:00
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function flattenError(error: unknown): Error {
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
const copy = new Error(error.message || "ipc_handler_failed");
|
|
|
|
|
copy.name = error.name;
|
|
|
|
|
return copy;
|
|
|
|
|
}
|
|
|
|
|
return new Error(typeof error === "string" ? error : "ipc_handler_failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function safeHandle<Args extends unknown[], Result>(
|
|
|
|
|
channel: string,
|
|
|
|
|
handler: (...args: Args) => Promise<Result> | Result,
|
|
|
|
|
): void {
|
|
|
|
|
ipcMain.handle(channel, async (...args) => {
|
|
|
|
|
try {
|
|
|
|
|
return await handler(...(args as unknown as Args));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw flattenError(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 19:47:35 +08:00
|
|
|
function isSafeExternalUrl(url: string): boolean {
|
|
|
|
|
try {
|
|
|
|
|
const parsed = new URL(url);
|
|
|
|
|
return parsed.protocol === "http:" || parsed.protocol === "https:";
|
|
|
|
|
} catch {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 14:18:20 +08:00
|
|
|
function registerBridgeHandlers(): void {
|
|
|
|
|
ipcMain.handle("desktop:ping", () => "pong");
|
2026-04-27 20:06:37 +08:00
|
|
|
ipcMain.handle("desktop:device-info", () => resolveDesktopDeviceInfo());
|
|
|
|
|
safeHandle(
|
|
|
|
|
"desktop:client-id",
|
|
|
|
|
(_event, scope: { tenant_id?: unknown; workspace_id?: unknown; user_id?: unknown }) =>
|
|
|
|
|
resolveDesktopClientID({
|
|
|
|
|
tenant_id: typeof scope?.tenant_id === "number" ? scope.tenant_id : 0,
|
|
|
|
|
workspace_id: typeof scope?.workspace_id === "number" ? scope.workspace_id : 0,
|
|
|
|
|
user_id: typeof scope?.user_id === "number" ? scope.user_id : 0,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2026-04-27 12:50:45 +08:00
|
|
|
ipcMain.handle("desktop:runtime-snapshot", () => captureRuntimeSnapshot());
|
2026-04-27 21:33:55 +08:00
|
|
|
ipcMain.handle("desktop:runtime-account-snapshot", (_event, accountId: string) =>
|
|
|
|
|
captureRuntimeAccountSnapshot(accountId),
|
|
|
|
|
);
|
2026-04-19 14:18:20 +08:00
|
|
|
safeHandle("desktop:bind-publish-account", async (_event, platformId: string) => {
|
|
|
|
|
const account = (await bindPublishAccount(platformId)) as DesktopAccountInfo;
|
2026-04-26 21:57:26 +08:00
|
|
|
noteRuntimeAccountBound(account);
|
|
|
|
|
void refreshRuntimeAccounts().catch((error) => {
|
|
|
|
|
console.warn("[desktop-bind] background account refresh failed", {
|
|
|
|
|
message: error instanceof Error ? error.message : String(error),
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-04-19 14:18:20 +08:00
|
|
|
return account;
|
|
|
|
|
});
|
2026-04-20 09:52:48 +08:00
|
|
|
safeHandle("desktop:refresh-runtime-accounts", async () => {
|
|
|
|
|
await refreshRuntimeAccounts();
|
|
|
|
|
return null;
|
|
|
|
|
});
|
2026-04-27 21:33:55 +08:00
|
|
|
safeHandle("desktop:probe-runtime-account", async (_event, accountId: string) => {
|
|
|
|
|
requestRuntimeAccountProbe(accountId);
|
|
|
|
|
return captureRuntimeAccountSnapshot(accountId);
|
|
|
|
|
});
|
2026-04-19 14:18:20 +08:00
|
|
|
safeHandle(
|
|
|
|
|
"desktop:open-publish-account-console",
|
|
|
|
|
async (
|
|
|
|
|
_event,
|
|
|
|
|
account: { id: string; platform: string; platformUid: string; displayName: string },
|
|
|
|
|
) => {
|
|
|
|
|
await openPublishAccountConsole(account);
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
safeHandle(
|
2026-04-20 09:52:48 +08:00
|
|
|
"desktop:unbind-publish-account",
|
|
|
|
|
async (_event, accountId: string, syncVersion: number) => {
|
|
|
|
|
await unbindRuntimeAccount(accountId, syncVersion);
|
2026-04-19 14:18:20 +08:00
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
);
|
2026-04-20 09:52:48 +08:00
|
|
|
safeHandle("desktop:list-publish-tasks", async (_event, params?: { page?: number; page_size?: number; title?: string }) => {
|
|
|
|
|
return listDesktopPublishTasks(params);
|
|
|
|
|
});
|
|
|
|
|
safeHandle("desktop:retry-publish-task", async (_event, taskId: string) => {
|
|
|
|
|
return retryDesktopPublishTask(taskId);
|
|
|
|
|
});
|
2026-04-26 19:47:35 +08:00
|
|
|
safeHandle("desktop:open-external-url", async (_event, url: string) => {
|
|
|
|
|
if (!isSafeExternalUrl(url)) {
|
|
|
|
|
throw new Error("unsupported_external_url");
|
|
|
|
|
}
|
|
|
|
|
await shell.openExternal(url);
|
|
|
|
|
return null;
|
|
|
|
|
});
|
2026-04-19 14:18:20 +08:00
|
|
|
ipcMain.handle(
|
|
|
|
|
"desktop:runtime-session-sync",
|
|
|
|
|
(_event, session: DesktopRuntimeSessionSyncRequest | null) => {
|
|
|
|
|
syncRuntimeSession(session);
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
);
|
2026-04-20 09:52:48 +08:00
|
|
|
safeHandle("desktop:runtime-session-release", async (_event, revoke?: boolean) => {
|
|
|
|
|
await releaseRuntimeSession({ revoke: Boolean(revoke) });
|
|
|
|
|
return null;
|
|
|
|
|
});
|
2026-04-22 00:24:21 +08:00
|
|
|
ipcMain.handle("desktop:set-window-mode", (_event, mode: WindowMode) => {
|
|
|
|
|
const window = currentMainWindow();
|
|
|
|
|
if (window && (mode === "login" || mode === "main")) {
|
|
|
|
|
applyWindowMode(window, mode);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
});
|
2026-04-19 14:18:20 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
const hasSingleInstanceLock = initSingleInstance(() => {
|
2026-04-22 00:24:21 +08:00
|
|
|
revealMainWindowSafely("single-instance");
|
2026-04-27 20:06:37 +08:00
|
|
|
});
|
2026-04-19 14:18:20 +08:00
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
if (!hasSingleInstanceLock) {
|
|
|
|
|
console.info("[desktop-main] another instance is already running; exiting");
|
|
|
|
|
app.exit(0);
|
|
|
|
|
} else {
|
|
|
|
|
app.whenReady().then(async () => {
|
|
|
|
|
currentWindowMode = readPersistedWindowMode();
|
|
|
|
|
registerBridgeHandlers();
|
|
|
|
|
await initSessionRegistry();
|
|
|
|
|
initAccountHealth();
|
|
|
|
|
initTransport();
|
|
|
|
|
initScheduler();
|
|
|
|
|
initProcessMetricsSampler();
|
|
|
|
|
startHotViewReaper();
|
|
|
|
|
startHiddenPlaywrightReaper();
|
|
|
|
|
onRuntimeInvalidated((event) => {
|
|
|
|
|
syncDesktopHealthIndicator(currentMainWindow());
|
|
|
|
|
if (!mainRendererContents || mainRendererContents.isDestroyed()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mainRendererContents.send("desktop:runtime-invalidated", event);
|
|
|
|
|
});
|
|
|
|
|
await ensureMainWindow();
|
|
|
|
|
initTray(() => {
|
|
|
|
|
revealMainWindowSafely("tray");
|
|
|
|
|
});
|
|
|
|
|
startDesktopHealthIndicator(() => currentMainWindow());
|
|
|
|
|
}).catch((error) => {
|
|
|
|
|
console.error("[desktop-main] app.whenReady failed", error);
|
2026-04-19 14:18:20 +08:00
|
|
|
});
|
|
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
app.on("activate", () => {
|
|
|
|
|
revealMainWindowSafely("activate");
|
|
|
|
|
});
|
2026-04-19 14:18:20 +08:00
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
app.on("window-all-closed", () => {
|
|
|
|
|
if (process.platform !== "darwin") {
|
|
|
|
|
app.quit();
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-04-20 09:52:48 +08:00
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
app.on("before-quit", (event) => {
|
|
|
|
|
if (quitReleaseInFlight) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-20 09:52:48 +08:00
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
quitReleaseInFlight = true;
|
|
|
|
|
event.preventDefault();
|
2026-04-20 09:52:48 +08:00
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
void Promise.race([
|
|
|
|
|
releaseRuntimeSession(),
|
|
|
|
|
new Promise<void>((resolve) => setTimeout(resolve, 1500)),
|
|
|
|
|
]).finally(() => {
|
|
|
|
|
app.quit();
|
|
|
|
|
});
|
2026-04-20 09:52:48 +08:00
|
|
|
});
|
2026-04-27 20:06:37 +08:00
|
|
|
}
|