feat(desktop-client): give login and main views their own BrowserWindow

Spawn a separate window per mode keyed by a ?window= query param so each surface keeps its size, devtools, and renderer state independently. Logout now persists the cleared session and switches windows before awaiting the runtime release, avoiding a stale main-window flash on the way out.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-30 20:56:03 +08:00
parent cdd63db7f1
commit ed38721feb
7 changed files with 265 additions and 95 deletions
+40 -10
View File
@@ -1,14 +1,23 @@
<script setup lang="ts">
import { defineAsyncComponent, onMounted, watch } from "vue";
import { computed, defineAsyncComponent, onMounted, watch } from "vue";
import { useDesktopSession } from "./composables/useDesktopSession";
const DesktopShell = defineAsyncComponent(() => import("./components/DesktopShell.vue"));
const LoginView = defineAsyncComponent(() => import("./views/LoginView.vue"));
const { isAuthenticated, syncRuntimeSession } = useDesktopSession();
type RendererWindowMode = "auto" | "login" | "main";
interface WindowModeBridge {
setWindowMode?: (mode: "login" | "main") => Promise<unknown>;
setWindowMode?: (
mode: "login" | "main",
request?: { source?: "auth-state" | "login" | "logout"; animate?: boolean },
) => Promise<unknown>;
}
function resolveRendererWindowMode(): RendererWindowMode {
const mode = new URLSearchParams(window.location.search).get("window");
return mode === "login" || mode === "main" ? mode : "auto";
}
function getModeBridge(): WindowModeBridge | undefined {
@@ -18,21 +27,42 @@ function getModeBridge(): WindowModeBridge | undefined {
}
function syncWindowMode(authenticated: boolean): void {
void getModeBridge()?.setWindowMode?.(authenticated ? "main" : "login");
void getModeBridge()?.setWindowMode?.(authenticated ? "main" : "login", { source: "auth-state" });
}
// Fire immediately during setup so the main process can size + reveal the
// window before the user sees any "wrong-sized" frame.
syncWindowMode(isAuthenticated.value);
watch(isAuthenticated, (next) => syncWindowMode(next));
const rendererWindowMode = resolveRendererWindowMode();
const usesAutomaticWindowMode = rendererWindowMode === "auto";
const showsMainSurface = computed(() =>
rendererWindowMode === "main" || (rendererWindowMode === "auto" && isAuthenticated.value),
);
if (usesAutomaticWindowMode) {
syncWindowMode(isAuthenticated.value);
watch(isAuthenticated, (next) => syncWindowMode(next));
}
if (rendererWindowMode === "main") {
if (!isAuthenticated.value) {
syncWindowMode(false);
}
watch(isAuthenticated, (next) => {
if (!next) {
syncWindowMode(false);
}
});
}
onMounted(() => {
void syncRuntimeSession();
syncWindowMode(isAuthenticated.value);
if (rendererWindowMode !== "login") {
void syncRuntimeSession();
}
if (usesAutomaticWindowMode) {
syncWindowMode(isAuthenticated.value);
}
});
</script>
<template>
<DesktopShell v-if="isAuthenticated" />
<DesktopShell v-if="showsMainSurface" />
<LoginView v-else />
</template>
@@ -477,6 +477,7 @@ h1 {
.content {
flex: 1;
min-width: 0;
height: 100vh;
padding: 24px 32px;
overflow-y: auto;
@@ -39,6 +39,7 @@ const apiBaseURL = shallowRef(readStoredApiBaseURL());
const pending = shallowRef(false);
const error = shallowRef<string | null>(null);
let loginPromise: Promise<void> | null = null;
let logoutPromise: Promise<void> | null = null;
function readStoredSession(): DesktopSession | null {
if (typeof window === "undefined") {
@@ -132,6 +133,39 @@ function createAuthenticatedClient() {
});
}
function createSessionBoundAuthenticatedClient(source: DesktopSession) {
let accessToken = source.accessToken;
let refreshToken = source.refreshToken;
return createApiClient({
baseURL: apiBaseURL.value,
timeoutMs: 15000,
auth: {
getAccessToken: () => accessToken,
getRefreshToken: () => refreshToken,
setTokens: (tokens: AuthTokens) => {
accessToken = tokens.accessToken;
refreshToken = tokens.refreshToken;
},
clearTokens: () => {
accessToken = null;
refreshToken = null;
},
async refresh(refreshTokenValue: string): Promise<AuthTokens> {
const next = await createPublicClient().post<RefreshResponse, { refresh_token: string }>(
"/api/auth/refresh",
{ refresh_token: refreshTokenValue },
);
return {
accessToken: next.access_token,
refreshToken: next.refresh_token,
};
},
},
});
}
function resolveFallbackPlatform(): string {
const currentNavigator = globalThis.navigator as Navigator & {
userAgentData?: { platform?: string };
@@ -416,10 +450,7 @@ async function enterPreview(): Promise<void> {
});
}
async function logout(): Promise<void> {
error.value = null;
const current = session.value;
async function releaseSessionForLogout(current: DesktopSession | null): Promise<void> {
if (current?.mode === "authenticated") {
try {
await window.desktopBridge?.app?.releaseRuntimeSession?.(true);
@@ -428,13 +459,43 @@ async function logout(): Promise<void> {
}
try {
await createAuthenticatedClient().post<null, Record<string, never>>("/api/auth/logout", {});
await createSessionBoundAuthenticatedClient(current).post<null, Record<string, never>>("/api/auth/logout", {});
} catch (err) {
console.warn("[desktop-session] auth logout failed", err);
}
}
}
async function transitionWindowForLogout(): Promise<void> {
try {
await window.desktopBridge?.app?.setWindowMode?.("login", { source: "logout", animate: true });
} catch (err) {
console.warn("[desktop-session] logout window transition failed", err);
}
}
async function performLogout(): Promise<void> {
error.value = null;
const current = session.value;
const releasePromise = releaseSessionForLogout(current);
persistSession(null);
await transitionWindowForLogout();
void releasePromise.catch((err) => {
console.warn("[desktop-session] background logout release failed", err);
});
}
async function logout(): Promise<void> {
if (logoutPromise) {
return logoutPromise;
}
logoutPromise = performLogout().finally(() => {
logoutPromise = null;
});
return logoutPromise;
}
if (typeof window !== "undefined") {
+4 -1
View File
@@ -42,7 +42,10 @@ declare global {
openExternalUrl(url: string): Promise<null>;
syncRuntimeSession(session: DesktopRuntimeSessionSyncRequest | null): Promise<null>;
releaseRuntimeSession(revoke?: boolean): Promise<null>;
setWindowMode(mode: "login" | "main"): Promise<null>;
setWindowMode(mode: "login" | "main", request?: {
source?: "auth-state" | "login" | "logout";
animate?: boolean;
}): Promise<null>;
onRuntimeInvalidated(
listener: (event: { reason: "account-health"; at: number; accountId?: string }) => void,
): () => void;
@@ -61,6 +61,7 @@ async function submitLogin() {
email: form.email,
password: form.password,
});
await window.desktopBridge?.app?.setWindowMode?.("main", { source: "login" });
}
onMounted(() => {