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-05-01 18:02:00 +08:00
|
|
|
import { BrowserWindow, Menu, app, ipcMain, nativeTheme } from "electron/main";
|
2026-04-19 14:18:20 +08:00
|
|
|
import type {
|
|
|
|
|
BrowserWindow as ElectronBrowserWindow,
|
2026-05-01 17:20:18 +08:00
|
|
|
IpcMainInvokeEvent,
|
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-30 22:00:01 +08:00
|
|
|
import {
|
|
|
|
|
getDesktopAppSettings,
|
|
|
|
|
initDesktopAppSettings,
|
|
|
|
|
setDesktopAppSetting,
|
|
|
|
|
type DesktopAppSettingKey,
|
|
|
|
|
} from "./app-settings";
|
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-05-01 17:20:18 +08:00
|
|
|
// CDP is required by the hidden Playwright execution pool in packaged builds.
|
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-05-01 17:20:18 +08:00
|
|
|
|
|
|
|
|
const isDevelopmentRuntime = !app.isPackaged;
|
|
|
|
|
|
|
|
|
|
function silencePackagedConsole(): void {
|
|
|
|
|
if (isDevelopmentRuntime) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const noop = () => undefined;
|
|
|
|
|
console.log = noop;
|
|
|
|
|
console.info = noop;
|
|
|
|
|
console.warn = noop;
|
|
|
|
|
console.error = noop;
|
|
|
|
|
console.debug = noop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
silencePackagedConsole();
|
|
|
|
|
if (isDevelopmentRuntime) {
|
|
|
|
|
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-30 20:56:03 +08:00
|
|
|
let loginWindow: ElectronBrowserWindow | null = null;
|
2026-04-30 22:00:01 +08:00
|
|
|
let settingsWindow: 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-05-01 18:05:12 +08:00
|
|
|
let mainWindowCreatePromise: Promise<ElectronBrowserWindow> | null = null;
|
|
|
|
|
let loginWindowCreatePromise: Promise<ElectronBrowserWindow> | null = null;
|
|
|
|
|
let settingsWindowCreatePromise: Promise<ElectronBrowserWindow> | null = null;
|
|
|
|
|
let windowModeSwitchQueue: Promise<void> = Promise.resolve();
|
2026-04-30 22:00:01 +08:00
|
|
|
const forceCloseWindows = new WeakSet<ElectronBrowserWindow>();
|
2026-05-01 17:20:18 +08:00
|
|
|
const appWindowModes = new WeakMap<ElectronBrowserWindow, RendererWindowMode>();
|
2026-04-19 14:18:20 +08:00
|
|
|
|
2026-04-22 00:24:21 +08:00
|
|
|
type WindowMode = "login" | "main";
|
2026-04-30 22:00:01 +08:00
|
|
|
type RendererWindowMode = WindowMode | "settings";
|
2026-04-30 20:56:03 +08:00
|
|
|
type WindowModeSource = "auth-state" | "login" | "logout";
|
|
|
|
|
|
|
|
|
|
interface WindowSizePreset {
|
|
|
|
|
width: number;
|
|
|
|
|
height: number;
|
|
|
|
|
minWidth: number;
|
|
|
|
|
minHeight: number;
|
|
|
|
|
resizable: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface WindowModeRequest {
|
|
|
|
|
source?: WindowModeSource;
|
|
|
|
|
animate?: boolean;
|
|
|
|
|
}
|
2026-04-22 00:24:21 +08:00
|
|
|
|
2026-05-01 17:20:18 +08:00
|
|
|
const WINDOW_READY_TIMEOUT_MS = 1200;
|
|
|
|
|
|
2026-04-30 20:56:03 +08:00
|
|
|
const WINDOW_SIZES: Record<WindowMode, WindowSizePreset> = {
|
|
|
|
|
login: { width: 340, height: 540, minWidth: 340, minHeight: 540, resizable: false },
|
|
|
|
|
main: { width: 1320, height: 860, minWidth: 900, minHeight: 600, resizable: true },
|
2026-04-22 00:24:21 +08:00
|
|
|
};
|
|
|
|
|
|
2026-04-30 22:00:01 +08:00
|
|
|
const SETTINGS_WINDOW_SIZE: WindowSizePreset = {
|
|
|
|
|
width: 920,
|
|
|
|
|
height: 640,
|
|
|
|
|
minWidth: 760,
|
|
|
|
|
minHeight: 520,
|
|
|
|
|
resizable: true,
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-01 18:02:52 +08:00
|
|
|
const TITLE_BAR_OVERLAY_HEIGHT = 24;
|
|
|
|
|
|
|
|
|
|
function appTitleBarStyle(): "hidden" | "hiddenInset" {
|
|
|
|
|
return process.platform === "darwin" ? "hiddenInset" : "hidden";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function appTitleBarOverlay(color: string): { color: string; symbolColor: string; height: number } | undefined {
|
|
|
|
|
if (process.platform === "darwin") {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
color,
|
|
|
|
|
symbolColor: nativeTheme.shouldUseDarkColors ? "#f8fafc" : "#111827",
|
|
|
|
|
height: TITLE_BAR_OVERLAY_HEIGHT,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 18:02:00 +08:00
|
|
|
function suppressNativeWindowMenu(window?: ElectronBrowserWindow): void {
|
|
|
|
|
if (process.platform === "darwin") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Menu.setApplicationMenu(null);
|
|
|
|
|
window?.setMenu(null);
|
|
|
|
|
window?.setMenuBarVisibility(false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 00:24:21 +08:00
|
|
|
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";
|
|
|
|
|
|
2026-04-30 20:56:03 +08:00
|
|
|
function normalizeWindowModeRequest(input: unknown): WindowModeRequest {
|
|
|
|
|
if (!input || typeof input !== "object") {
|
|
|
|
|
return {};
|
2026-04-22 00:24:21 +08:00
|
|
|
}
|
2026-04-30 20:56:03 +08:00
|
|
|
|
|
|
|
|
const candidate = input as { source?: unknown; animate?: unknown };
|
|
|
|
|
return {
|
|
|
|
|
source:
|
|
|
|
|
candidate.source === "login" || candidate.source === "logout" || candidate.source === "auth-state"
|
|
|
|
|
? candidate.source
|
|
|
|
|
: undefined,
|
|
|
|
|
animate: candidate.animate === true,
|
|
|
|
|
};
|
2026-04-22 00:24:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function currentMainWindow(): ElectronBrowserWindow | null {
|
|
|
|
|
if (!mainWindow) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (mainWindow.isDestroyed()) {
|
|
|
|
|
mainWindow = null;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return mainWindow;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 20:56:03 +08:00
|
|
|
function currentLoginWindow(): ElectronBrowserWindow | null {
|
|
|
|
|
if (!loginWindow) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (loginWindow.isDestroyed()) {
|
|
|
|
|
loginWindow = null;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return loginWindow;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 22:00:01 +08:00
|
|
|
function currentSettingsWindow(): ElectronBrowserWindow | null {
|
|
|
|
|
if (!settingsWindow) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (settingsWindow.isDestroyed()) {
|
|
|
|
|
settingsWindow = null;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return settingsWindow;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 20:56:03 +08:00
|
|
|
function currentActiveWindow(): ElectronBrowserWindow | null {
|
|
|
|
|
return currentMainWindow() ?? currentLoginWindow();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 17:20:18 +08:00
|
|
|
function rendererModeFromWindow(window: ElectronBrowserWindow): RendererWindowMode | null {
|
|
|
|
|
if (window.isDestroyed()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const knownMode = appWindowModes.get(window);
|
|
|
|
|
if (knownMode) {
|
|
|
|
|
return knownMode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const url = new URL(window.webContents.getURL());
|
|
|
|
|
const mode = url.searchParams.get("window");
|
|
|
|
|
return mode === "login" || mode === "main" || mode === "settings" ? mode : null;
|
|
|
|
|
} catch {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function retireWindowAfterIpcReturn(window: ElectronBrowserWindow): void {
|
|
|
|
|
if (window.isDestroyed()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 22:00:01 +08:00
|
|
|
forceCloseWindows.add(window);
|
2026-05-01 17:20:18 +08:00
|
|
|
if (window.isVisible()) {
|
|
|
|
|
window.hide();
|
|
|
|
|
}
|
2026-04-30 20:56:03 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
if (!window.isDestroyed()) {
|
2026-05-01 17:20:18 +08:00
|
|
|
window.destroy();
|
2026-04-30 20:56:03 +08:00
|
|
|
}
|
|
|
|
|
}, 50);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 17:20:18 +08:00
|
|
|
function retireInactiveAppWindows(mode: WindowMode, target: ElectronBrowserWindow): void {
|
|
|
|
|
for (const candidate of BrowserWindow.getAllWindows()) {
|
|
|
|
|
if (candidate === target || candidate.isDestroyed()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const candidateMode = rendererModeFromWindow(candidate);
|
|
|
|
|
if (
|
|
|
|
|
(mode === "main" && (candidateMode === "login" || candidateMode === "main"))
|
2026-05-01 18:05:12 +08:00
|
|
|
|| (mode === "login" && (candidateMode === "login" || candidateMode === "main" || candidateMode === "settings"))
|
2026-05-01 17:20:18 +08:00
|
|
|
) {
|
|
|
|
|
retireWindowAfterIpcReturn(candidate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function waitForWindowReady(window: ElectronBrowserWindow): Promise<void> {
|
|
|
|
|
if (window.isDestroyed() || !window.webContents.isLoading()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
|
let settled = false;
|
|
|
|
|
let timeout: ReturnType<typeof setTimeout> | null = null;
|
|
|
|
|
|
|
|
|
|
const finish = () => {
|
|
|
|
|
if (settled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
settled = true;
|
|
|
|
|
if (timeout) {
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
}
|
|
|
|
|
window.off("ready-to-show", finish);
|
|
|
|
|
window.webContents.off("did-finish-load", finish);
|
|
|
|
|
window.webContents.off("did-fail-load", finish);
|
|
|
|
|
resolve();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
timeout = setTimeout(finish, WINDOW_READY_TIMEOUT_MS);
|
|
|
|
|
window.once("ready-to-show", finish);
|
|
|
|
|
window.webContents.once("did-finish-load", finish);
|
|
|
|
|
window.webContents.once("did-fail-load", finish);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2026-05-01 18:05:12 +08:00
|
|
|
if (!mainWindowCreatePromise) {
|
|
|
|
|
mainWindowCreatePromise = createAppWindow("main")
|
|
|
|
|
.then((window) => {
|
|
|
|
|
mainWindow = window;
|
|
|
|
|
return window;
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
mainWindowCreatePromise = null;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return mainWindowCreatePromise;
|
2026-04-22 00:24:21 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 20:56:03 +08:00
|
|
|
async function ensureLoginWindow(): Promise<ElectronBrowserWindow> {
|
|
|
|
|
const existing = currentLoginWindow();
|
|
|
|
|
if (existing) {
|
|
|
|
|
return existing;
|
|
|
|
|
}
|
2026-05-01 18:05:12 +08:00
|
|
|
if (!loginWindowCreatePromise) {
|
|
|
|
|
loginWindowCreatePromise = createAppWindow("login")
|
|
|
|
|
.then((window) => {
|
|
|
|
|
loginWindow = window;
|
|
|
|
|
return window;
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
loginWindowCreatePromise = null;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return loginWindowCreatePromise;
|
2026-04-30 20:56:03 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 22:00:01 +08:00
|
|
|
async function ensureSettingsWindow(): Promise<ElectronBrowserWindow> {
|
|
|
|
|
const existing = currentSettingsWindow();
|
|
|
|
|
if (existing) {
|
|
|
|
|
return existing;
|
|
|
|
|
}
|
2026-05-01 18:05:12 +08:00
|
|
|
if (!settingsWindowCreatePromise) {
|
|
|
|
|
settingsWindowCreatePromise = createSettingsWindow()
|
|
|
|
|
.then((window) => {
|
|
|
|
|
settingsWindow = window;
|
|
|
|
|
return window;
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
settingsWindowCreatePromise = null;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return settingsWindowCreatePromise;
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 20:56:03 +08:00
|
|
|
async function ensureWindow(mode: WindowMode): Promise<ElectronBrowserWindow> {
|
|
|
|
|
return mode === "main" ? ensureMainWindow() : ensureLoginWindow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function revealWindow(window: ElectronBrowserWindow): Promise<void> {
|
2026-05-01 17:20:18 +08:00
|
|
|
await waitForWindowReady(window);
|
2026-04-27 20:06:37 +08:00
|
|
|
if (window.isMinimized()) {
|
|
|
|
|
window.restore();
|
|
|
|
|
}
|
|
|
|
|
if (!window.isVisible()) {
|
|
|
|
|
window.show();
|
|
|
|
|
}
|
|
|
|
|
window.focus();
|
|
|
|
|
app.focus({ steal: true });
|
2026-04-22 00:24:21 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 20:56:03 +08:00
|
|
|
async function revealActiveWindow(): Promise<void> {
|
|
|
|
|
const window = currentActiveWindow() ?? await ensureWindow(currentWindowMode);
|
|
|
|
|
await revealWindow(window);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function revealActiveWindowSafely(source: string): void {
|
|
|
|
|
void revealActiveWindow().catch((error) => {
|
|
|
|
|
console.error("[desktop-main] reveal active window failed", { source, error });
|
2026-04-22 00:24:21 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 17:20:18 +08:00
|
|
|
function windowFromIpcEvent(event: IpcMainInvokeEvent): ElectronBrowserWindow | null {
|
|
|
|
|
const window = BrowserWindow.fromWebContents(event.sender);
|
|
|
|
|
if (!window || window.isDestroyed()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function switchWindowMode(
|
|
|
|
|
mode: WindowMode,
|
|
|
|
|
_request: WindowModeRequest = {},
|
|
|
|
|
sourceWindow: ElectronBrowserWindow | null = null,
|
|
|
|
|
): Promise<void> {
|
2026-04-30 20:56:03 +08:00
|
|
|
currentWindowMode = mode;
|
|
|
|
|
persistWindowMode(mode);
|
|
|
|
|
|
|
|
|
|
const target = await ensureWindow(mode);
|
2026-05-01 17:20:18 +08:00
|
|
|
const previous = sourceWindow && sourceWindow !== target
|
|
|
|
|
? sourceWindow
|
|
|
|
|
: mode === "main"
|
|
|
|
|
? currentLoginWindow()
|
|
|
|
|
: currentMainWindow();
|
2026-04-30 20:56:03 +08:00
|
|
|
|
|
|
|
|
if (!target.isVisible()) {
|
|
|
|
|
target.center();
|
|
|
|
|
}
|
|
|
|
|
await revealWindow(target);
|
|
|
|
|
|
|
|
|
|
if (previous && previous !== target && !previous.isDestroyed()) {
|
2026-05-01 17:20:18 +08:00
|
|
|
retireWindowAfterIpcReturn(previous);
|
2026-04-30 20:56:03 +08:00
|
|
|
}
|
2026-04-30 22:00:01 +08:00
|
|
|
|
|
|
|
|
const settings = currentSettingsWindow();
|
|
|
|
|
if (mode === "login" && settings) {
|
2026-05-01 17:20:18 +08:00
|
|
|
retireWindowAfterIpcReturn(settings);
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
2026-05-01 17:20:18 +08:00
|
|
|
retireInactiveAppWindows(mode, target);
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-01 18:05:12 +08:00
|
|
|
function queueWindowModeSwitch(
|
|
|
|
|
mode: WindowMode,
|
|
|
|
|
request: WindowModeRequest = {},
|
|
|
|
|
sourceWindow: ElectronBrowserWindow | null = null,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const nextSwitch = windowModeSwitchQueue
|
|
|
|
|
.catch(() => undefined)
|
|
|
|
|
.then(() => switchWindowMode(mode, request, sourceWindow));
|
|
|
|
|
windowModeSwitchQueue = nextSwitch.catch(() => undefined);
|
|
|
|
|
return nextSwitch;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 22:00:01 +08:00
|
|
|
async function openSettingsWindow(): Promise<void> {
|
|
|
|
|
const window = await ensureSettingsWindow();
|
|
|
|
|
await revealWindow(window);
|
2026-04-30 20:56:03 +08:00
|
|
|
}
|
|
|
|
|
|
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-30 22:00:01 +08:00
|
|
|
async function loadRenderer(window: ElectronBrowserWindow, mode: RendererWindowMode): Promise<void> {
|
2026-04-19 14:18:20 +08:00
|
|
|
const devServerURL = rendererURL();
|
|
|
|
|
if (devServerURL) {
|
2026-04-30 20:56:03 +08:00
|
|
|
const url = new URL(devServerURL);
|
|
|
|
|
url.searchParams.set("window", mode);
|
|
|
|
|
await window.webContents.loadURL(url.toString());
|
2026-05-01 17:20:18 +08:00
|
|
|
if (mode === "main" && isDevelopmentRuntime) {
|
2026-04-30 20:56:03 +08:00
|
|
|
window.webContents.openDevTools({ mode: "detach" });
|
|
|
|
|
}
|
2026-04-19 14:18:20 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 20:56:03 +08:00
|
|
|
await window.webContents.loadFile(join(__dirname, "../renderer/index.html"), {
|
|
|
|
|
query: { window: mode },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 22:00:01 +08:00
|
|
|
async function mountRendererWebContents(window: ElectronBrowserWindow, mode: RendererWindowMode): Promise<void> {
|
2026-04-30 20:56:03 +08:00
|
|
|
const contents = window.webContents;
|
|
|
|
|
|
|
|
|
|
contents.setWindowOpenHandler(() => ({ action: "deny" }));
|
|
|
|
|
if (mode === "main") {
|
2026-05-01 17:20:18 +08:00
|
|
|
if (isDevelopmentRuntime) {
|
|
|
|
|
registerRendererDevtoolsProxyTarget(contents);
|
|
|
|
|
registerObservedRequestRendererTarget(contents);
|
|
|
|
|
}
|
2026-04-30 20:56:03 +08:00
|
|
|
mainRendererContents = contents;
|
|
|
|
|
window.on("closed", () => {
|
|
|
|
|
if (mainRendererContents === contents) {
|
|
|
|
|
mainRendererContents = null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await loadRenderer(window, mode);
|
2026-04-19 14:18:20 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 20:56:03 +08:00
|
|
|
async function createAppWindow(mode: WindowMode): Promise<ElectronBrowserWindow> {
|
|
|
|
|
const initial = WINDOW_SIZES[mode];
|
2026-05-01 18:02:52 +08:00
|
|
|
const backgroundColor = mode === "login" ? "#eaf2ff" : nativeTheme.shouldUseDarkColors ? "#15191a" : "#f0f2f5";
|
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,
|
2026-04-30 20:56:03 +08:00
|
|
|
minWidth: initial.minWidth,
|
|
|
|
|
minHeight: initial.minHeight,
|
2026-04-22 00:24:21 +08:00
|
|
|
show: false,
|
2026-05-01 11:03:55 +08:00
|
|
|
title: "省心推",
|
2026-04-27 12:50:45 +08:00
|
|
|
icon: createDesktopHealthIcon("normal"),
|
2026-05-01 18:02:52 +08:00
|
|
|
titleBarStyle: appTitleBarStyle(),
|
|
|
|
|
titleBarOverlay: appTitleBarOverlay(mode === "login" ? "#00000000" : backgroundColor),
|
2026-04-22 00:24:21 +08:00
|
|
|
trafficLightPosition: { x: 12, y: 14 },
|
2026-04-30 20:56:03 +08:00
|
|
|
resizable: initial.resizable,
|
2026-05-01 18:02:52 +08:00
|
|
|
minimizable: mode !== "login",
|
2026-04-30 20:56:03 +08:00
|
|
|
maximizable: initial.resizable,
|
|
|
|
|
fullscreenable: initial.resizable,
|
2026-05-01 18:02:00 +08:00
|
|
|
autoHideMenuBar: true,
|
2026-05-01 18:02:52 +08:00
|
|
|
backgroundColor,
|
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-05-01 17:20:18 +08:00
|
|
|
appWindowModes.set(window, mode);
|
2026-05-01 18:02:00 +08:00
|
|
|
suppressNativeWindowMenu(window);
|
2026-04-22 00:24:21 +08:00
|
|
|
|
2026-04-30 20:56:03 +08:00
|
|
|
if (!initial.resizable) {
|
|
|
|
|
window.setMinimumSize(initial.minWidth, initial.minHeight);
|
|
|
|
|
window.setMaximumSize(initial.width, initial.height);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 00:24:21 +08:00
|
|
|
window.once("closed", () => {
|
2026-04-30 20:56:03 +08:00
|
|
|
if (mode === "main" && mainWindow === window) {
|
2026-04-22 00:24:21 +08:00
|
|
|
mainWindow = null;
|
2026-04-30 20:56:03 +08:00
|
|
|
}
|
|
|
|
|
if (mode === "login" && loginWindow === window) {
|
|
|
|
|
loginWindow = null;
|
2026-04-22 00:24:21 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-30 22:00:01 +08:00
|
|
|
window.on("close", (event) => {
|
|
|
|
|
if (
|
|
|
|
|
mode === "main"
|
|
|
|
|
&& !quitReleaseInFlight
|
|
|
|
|
&& !forceCloseWindows.has(window)
|
|
|
|
|
&& getDesktopAppSettings().keepRunningInBackground
|
|
|
|
|
) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
window.hide();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
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-30 20:56:03 +08:00
|
|
|
await mountRendererWebContents(window, mode);
|
2026-04-19 14:18:20 +08:00
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 22:00:01 +08:00
|
|
|
async function createSettingsWindow(): Promise<ElectronBrowserWindow> {
|
|
|
|
|
const initial = SETTINGS_WINDOW_SIZE;
|
2026-05-01 18:02:52 +08:00
|
|
|
const backgroundColor = nativeTheme.shouldUseDarkColors ? "#15191a" : "#f2f4f7";
|
2026-04-30 22:00:01 +08:00
|
|
|
const window = new BrowserWindow({
|
|
|
|
|
width: initial.width,
|
|
|
|
|
height: initial.height,
|
|
|
|
|
minWidth: initial.minWidth,
|
|
|
|
|
minHeight: initial.minHeight,
|
|
|
|
|
show: false,
|
2026-05-01 11:03:55 +08:00
|
|
|
title: "省心推设置",
|
2026-04-30 22:00:01 +08:00
|
|
|
icon: createDesktopHealthIcon("normal"),
|
2026-05-01 18:02:52 +08:00
|
|
|
titleBarStyle: appTitleBarStyle(),
|
|
|
|
|
titleBarOverlay: appTitleBarOverlay(backgroundColor),
|
2026-04-30 22:00:01 +08:00
|
|
|
trafficLightPosition: { x: 14, y: 15 },
|
|
|
|
|
resizable: initial.resizable,
|
|
|
|
|
maximizable: initial.resizable,
|
|
|
|
|
fullscreenable: false,
|
|
|
|
|
autoHideMenuBar: true,
|
2026-05-01 18:02:52 +08:00
|
|
|
backgroundColor,
|
2026-04-30 22:00:01 +08:00
|
|
|
webPreferences: {
|
|
|
|
|
preload: preloadPath(),
|
|
|
|
|
sandbox: true,
|
|
|
|
|
contextIsolation: true,
|
|
|
|
|
nodeIntegration: false,
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-05-01 17:20:18 +08:00
|
|
|
appWindowModes.set(window, "settings");
|
2026-05-01 18:02:00 +08:00
|
|
|
suppressNativeWindowMenu(window);
|
2026-04-30 22:00:01 +08:00
|
|
|
|
|
|
|
|
window.once("closed", () => {
|
|
|
|
|
if (settingsWindow === window) {
|
|
|
|
|
settingsWindow = null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await mountRendererWebContents(window, "settings");
|
|
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 14:18:20 +08:00
|
|
|
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) => {
|
2026-04-29 15:58:25 +08:00
|
|
|
await requestRuntimeAccountProbe(accountId);
|
2026-04-27 21:33:55 +08:00
|
|
|
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);
|
2026-04-30 19:32:20 +08:00
|
|
|
void requestRuntimeAccountProbe(account.id, {
|
|
|
|
|
account,
|
|
|
|
|
force: true,
|
|
|
|
|
}).catch((error) => {
|
|
|
|
|
console.warn("[desktop-console] background account probe failed", {
|
|
|
|
|
accountId: account.id,
|
|
|
|
|
platform: account.platform,
|
|
|
|
|
message: error instanceof Error ? error.message : String(error),
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-04-19 14:18:20 +08:00
|
|
|
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-30 22:00:01 +08:00
|
|
|
safeHandle("desktop:open-settings-window", async () => {
|
|
|
|
|
await openSettingsWindow();
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
ipcMain.handle("desktop:get-app-settings", () => getDesktopAppSettings());
|
|
|
|
|
safeHandle("desktop:set-app-setting", async (_event, key: DesktopAppSettingKey, value: boolean) => {
|
|
|
|
|
if (key !== "openAtLogin" && key !== "keepRunningInBackground") {
|
|
|
|
|
throw new Error("unsupported_app_setting");
|
|
|
|
|
}
|
|
|
|
|
return setDesktopAppSetting(key, value);
|
|
|
|
|
});
|
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-05-01 17:20:18 +08:00
|
|
|
ipcMain.handle("desktop:set-window-mode", async (event, mode: WindowMode, request?: unknown) => {
|
2026-04-30 20:56:03 +08:00
|
|
|
if (mode === "login" || mode === "main") {
|
2026-05-01 18:05:12 +08:00
|
|
|
await queueWindowModeSwitch(mode, normalizeWindowModeRequest(request), windowFromIpcEvent(event));
|
2026-04-22 00:24:21 +08:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
});
|
2026-04-19 14:18:20 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
const hasSingleInstanceLock = initSingleInstance(() => {
|
2026-04-30 20:56:03 +08:00
|
|
|
revealActiveWindowSafely("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 () => {
|
2026-05-01 18:02:00 +08:00
|
|
|
suppressNativeWindowMenu();
|
2026-04-27 20:06:37 +08:00
|
|
|
currentWindowMode = readPersistedWindowMode();
|
2026-04-30 22:00:01 +08:00
|
|
|
initDesktopAppSettings();
|
2026-04-27 20:06:37 +08:00
|
|
|
registerBridgeHandlers();
|
|
|
|
|
await initSessionRegistry();
|
|
|
|
|
initAccountHealth();
|
|
|
|
|
initTransport();
|
|
|
|
|
initScheduler();
|
|
|
|
|
initProcessMetricsSampler();
|
|
|
|
|
startHotViewReaper();
|
|
|
|
|
startHiddenPlaywrightReaper();
|
|
|
|
|
onRuntimeInvalidated((event) => {
|
|
|
|
|
syncDesktopHealthIndicator(currentMainWindow());
|
|
|
|
|
if (!mainRendererContents || mainRendererContents.isDestroyed()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mainRendererContents.send("desktop:runtime-invalidated", event);
|
|
|
|
|
});
|
2026-05-01 18:05:12 +08:00
|
|
|
await queueWindowModeSwitch(currentWindowMode);
|
2026-04-27 20:06:37 +08:00
|
|
|
initTray(() => {
|
2026-04-30 20:56:03 +08:00
|
|
|
revealActiveWindowSafely("tray");
|
2026-04-27 20:06:37 +08:00
|
|
|
});
|
|
|
|
|
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", () => {
|
2026-04-30 20:56:03 +08:00
|
|
|
revealActiveWindowSafely("activate");
|
2026-04-27 20:06:37 +08:00
|
|
|
});
|
2026-04-19 14:18:20 +08:00
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
app.on("window-all-closed", () => {
|
2026-04-30 22:00:01 +08:00
|
|
|
if (getDesktopAppSettings().keepRunningInBackground) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-27 20:06:37 +08:00
|
|
|
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
|
|
|
}
|