feat(desktop-client): tighten window mode switching and add win/linux package scripts
Frontend CI / Frontend (push) Successful in 2m18s

- Track each window's renderer mode in a WeakMap (and fall back to
  parsing ?window= from the URL) so retireInactiveAppWindows can
  destroy stale login/main/settings windows when we switch modes,
  not just the previous one of the canonical pair.
- Replace closeWindowAfterIpcReturn with retireWindowAfterIpcReturn
  (hides immediately, then destroys instead of close()) so the IPC
  reply still reaches the caller before teardown.
- Add waitForWindowReady() with a 1.2s ceiling and call it from
  revealWindow so we don't show a half-loaded shell while
  switchWindowMode is running. Pass the source window from the IPC
  event into switchWindowMode so the closing target is the actual
  caller, not a guess based on mode.
- Silence console.* and skip the observed-fetch install in packaged
  runtime, and only auto-openDevTools in dev.
- Eagerly import LoginView in App.vue instead of defineAsyncComponent
  to remove the boot flash on the login window.
- package.json: prefix package:* with `pnpm run build` and add
  package:win and package:linux for parity.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-01 17:20:18 +08:00
parent 8b4697d605
commit 54fc313e10
3 changed files with 134 additions and 14 deletions
+3 -1
View File
@@ -13,7 +13,9 @@
"scripts": {
"dev": "unset ELECTRON_RUN_AS_NODE && electron-vite dev --inspect=9229",
"build": "electron-vite build",
"package:mac": "electron-builder --mac --arm64",
"package:mac": "pnpm run build && electron-builder --mac --arm64,universal",
"package:win": "pnpm run build && electron-builder --win --x64",
"package:linux": "pnpm run build && electron-builder --linux --x64,arm64",
"sign:setup": "bash scripts/sign-setup.sh",
"sign:mac": "bash scripts/sign-mac.sh",
"test": "vitest run",
+130 -12
View File
@@ -5,6 +5,7 @@ import { shell } from "electron";
import { BrowserWindow, app, ipcMain, nativeTheme } from "electron/main";
import type {
BrowserWindow as ElectronBrowserWindow,
IpcMainInvokeEvent,
WebContents as ElectronWebContents,
} from "electron/main";
import type { DesktopAccountInfo, DesktopRuntimeSessionSyncRequest } from "@geo/shared-types";
@@ -52,9 +53,29 @@ app.commandLine.appendSwitch(
);
app.commandLine.appendSwitch("disable-quic");
app.commandLine.appendSwitch("disable-background-networking");
// CDP is required by the hidden Playwright execution pool in packaged builds.
app.commandLine.appendSwitch("remote-debugging-port", String(getPlaywrightCDPPort()));
app.userAgentFallback = STANDARD_USER_AGENT;
installObservedGlobalFetch();
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();
}
console.info("[desktop-main] boot", {
electron: process.versions.electron,
@@ -77,6 +98,7 @@ let settingsWindow: ElectronBrowserWindow | null = null;
let mainRendererContents: ElectronWebContents | null = null;
let quitReleaseInFlight = false;
const forceCloseWindows = new WeakSet<ElectronBrowserWindow>();
const appWindowModes = new WeakMap<ElectronBrowserWindow, RendererWindowMode>();
type WindowMode = "login" | "main";
type RendererWindowMode = WindowMode | "settings";
@@ -95,6 +117,8 @@ interface WindowModeRequest {
animate?: boolean;
}
const WINDOW_READY_TIMEOUT_MS = 1200;
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 },
@@ -187,15 +211,87 @@ function currentActiveWindow(): ElectronBrowserWindow | null {
return currentMainWindow() ?? currentLoginWindow();
}
function closeWindowAfterIpcReturn(window: ElectronBrowserWindow): void {
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;
}
forceCloseWindows.add(window);
if (window.isVisible()) {
window.hide();
}
setTimeout(() => {
if (!window.isDestroyed()) {
window.close();
window.destroy();
}
}, 50);
}
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"))
|| (mode === "login" && (candidateMode === "main" || candidateMode === "settings"))
) {
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);
});
}
function captureRuntimeSnapshot() {
const snapshot = createRuntimeSnapshot();
applyDesktopHealthIndicatorFromSnapshot(snapshot, currentMainWindow());
@@ -241,6 +337,7 @@ async function ensureWindow(mode: WindowMode): Promise<ElectronBrowserWindow> {
}
async function revealWindow(window: ElectronBrowserWindow): Promise<void> {
await waitForWindowReady(window);
if (window.isMinimized()) {
window.restore();
}
@@ -262,12 +359,28 @@ function revealActiveWindowSafely(source: string): void {
});
}
async function switchWindowMode(mode: WindowMode, _request: WindowModeRequest = {}): Promise<void> {
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> {
currentWindowMode = mode;
persistWindowMode(mode);
const target = await ensureWindow(mode);
const previous = mode === "main" ? currentLoginWindow() : currentMainWindow();
const previous = sourceWindow && sourceWindow !== target
? sourceWindow
: mode === "main"
? currentLoginWindow()
: currentMainWindow();
if (!target.isVisible()) {
target.center();
@@ -275,13 +388,14 @@ async function switchWindowMode(mode: WindowMode, _request: WindowModeRequest =
await revealWindow(target);
if (previous && previous !== target && !previous.isDestroyed()) {
closeWindowAfterIpcReturn(previous);
retireWindowAfterIpcReturn(previous);
}
const settings = currentSettingsWindow();
if (mode === "login" && settings) {
closeWindowAfterIpcReturn(settings);
retireWindowAfterIpcReturn(settings);
}
retireInactiveAppWindows(mode, target);
}
async function openSettingsWindow(): Promise<void> {
@@ -303,7 +417,7 @@ async function loadRenderer(window: ElectronBrowserWindow, mode: RendererWindowM
const url = new URL(devServerURL);
url.searchParams.set("window", mode);
await window.webContents.loadURL(url.toString());
if (mode === "main") {
if (mode === "main" && isDevelopmentRuntime) {
window.webContents.openDevTools({ mode: "detach" });
}
return;
@@ -319,8 +433,10 @@ async function mountRendererWebContents(window: ElectronBrowserWindow, mode: Ren
contents.setWindowOpenHandler(() => ({ action: "deny" }));
if (mode === "main") {
registerRendererDevtoolsProxyTarget(contents);
registerObservedRequestRendererTarget(contents);
if (isDevelopmentRuntime) {
registerRendererDevtoolsProxyTarget(contents);
registerObservedRequestRendererTarget(contents);
}
mainRendererContents = contents;
window.on("closed", () => {
if (mainRendererContents === contents) {
@@ -355,6 +471,7 @@ async function createAppWindow(mode: WindowMode): Promise<ElectronBrowserWindow>
nodeIntegration: false,
},
});
appWindowModes.set(window, mode);
if (!initial.resizable) {
window.setMinimumSize(initial.minWidth, initial.minHeight);
@@ -430,6 +547,7 @@ async function createSettingsWindow(): Promise<ElectronBrowserWindow> {
nodeIntegration: false,
},
});
appWindowModes.set(window, "settings");
window.once("closed", () => {
if (settingsWindow === window) {
@@ -568,9 +686,9 @@ function registerBridgeHandlers(): void {
await releaseRuntimeSession({ revoke: Boolean(revoke) });
return null;
});
ipcMain.handle("desktop:set-window-mode", async (_event, mode: WindowMode, request?: unknown) => {
ipcMain.handle("desktop:set-window-mode", async (event, mode: WindowMode, request?: unknown) => {
if (mode === "login" || mode === "main") {
await switchWindowMode(mode, normalizeWindowModeRequest(request));
await switchWindowMode(mode, normalizeWindowModeRequest(request), windowFromIpcEvent(event));
}
return null;
});
+1 -1
View File
@@ -1,9 +1,9 @@
<script setup lang="ts">
import { computed, defineAsyncComponent, onMounted, watch } from "vue";
import { useDesktopSession } from "./composables/useDesktopSession";
import LoginView from "./views/LoginView.vue";
const DesktopShell = defineAsyncComponent(() => import("./components/DesktopShell.vue"));
const LoginView = defineAsyncComponent(() => import("./views/LoginView.vue"));
const SettingsWindow = defineAsyncComponent(() => import("./views/SettingsWindow.vue"));
const { isAuthenticated, syncRuntimeSession } = useDesktopSession();