Add window persistence across app restarts

This commit is contained in:
James Babcock committed 2026-08-01 15:27:44 -07:00
1 parent 02c09ef889
commit 37e95627b1
4 files changed
+127

No files matched your search

+52
View File
@@ -79,6 +79,7 @@ import {
TRANSCRIBE_WINDOW_ID,
} from "../ui/shell/transcribe-app";
import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-process-window";
import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-apps-persistence";
import { appViewportRect, type WindowHeightMode } from "../ui/shell/geometry";
import { type LayerActions } from "../ui/layers";
import {
@@ -252,6 +253,10 @@ class DashboardController {
private sharedActions!: Omit<LayerActions, "requestRender">;
private lastForegroundNotificationUpdateAtMs = 0;
private lastConnectedPreviewUpdateAtMs = 0;
// Saving the open-app list is gated until the one-time restore has run, so
// the boot-time registry (just the launcher) never clobbers the saved list.
private openAppsRestored = false;
private suppressOpenAppsPersist = false;
constructor() {
const sharedActions = {
@@ -285,6 +290,7 @@ class DashboardController {
},
getScreenTimeoutMs: () => screenTimeoutSettingToMs(screenTimeoutSetting.get()),
requestShellRender: () => this.requestShellRender(),
onWindowsChanged: () => this.persistOpenApps(),
onScreenStateChanged: (on) => {
this.handleScreenStateChanged(on);
if (on) this.requestShellRender();
@@ -1342,6 +1348,52 @@ class DashboardController {
return host;
}
/** Save which apps are open (and which is foreground) for restoreOpenApps. */
private persistOpenApps(): void {
if (!this.openAppsRestored || this.suppressOpenAppsPersist) return;
const open: string[] = [];
for (const window of shell.getWindows()) {
if (window.appId === "launcher") continue;
if (!open.includes(window.appId)) open.push(window.appId);
}
savePersistedOpenApps({ open, foreground: shell.foregroundWindow()?.appId ?? null });
}
/**
* Reopen the apps that were open when the app last ran — a development
* convenience so installing a new build restores the working set. Shallow by
* design: apps relaunch fresh (no within-app state), and multi-window apps
* reopen only their primary window. Runs once, on reaching the main page;
* windows opened here get compositor surfaces at connect like any launch.
*/
async restoreOpenApps(): Promise<void> {
if (this.openAppsRestored) return;
this.openAppsRestored = true;
const saved = loadPersistedOpenApps();
if (!saved.open.length) return;
const known = new Set(LAUNCHER_APPS.map((app) => app.appId));
this.suppressOpenAppsPersist = true;
try {
for (const appId of saved.open) {
if (!known.has(appId)) continue;
try {
await this.launchApp(appId);
} catch (error) {
this.appendLog(`restore of ${appId} failed: ${this.formatError(error)}`);
}
}
const foreground = saved.foreground
? shell.getWindows().find((window) => window.appId === saved.foreground)
: undefined;
if (foreground) shell.focusWindow(foreground.windowId);
} finally {
this.suppressOpenAppsPersist = false;
}
// Re-save the canonical post-restore state (drops apps that failed to open).
this.persistOpenApps();
this.requestShellRender();
}
/**
* Launch an app from the launcher: open a window in the app's shared
* worker (spawned on first launch) and foreground it. Launching an app
+3
View File
@@ -98,6 +98,9 @@ export function loaded(args: EventData) {
// Automatically connect on reaching the main page (no-op if already
// connected or if no glasses are configured).
void model?.autoConnect()
// Reopen the apps that were open before the last restart (no-op after the
// first load).
void dashboardController.restoreOpenApps()
const scrollViews = [
page.getViewById<ScrollView>('logScrollView'),
page.getViewById<ScrollView>('logScrollViewLandscape'),
+67
View File
@@ -0,0 +1,67 @@
import { File, knownFolders } from "@nativescript/core";
/**
* Persistence for the set of open apps, so a restart (usually installing a new
* development build) reopens the same windows. Deliberately shallow: only
* which apps were open and which was foreground, not within-app state.
*/
export type PersistedOpenApps = {
/** App ids in sidebar (registration) order, launcher excluded. */
open: string[];
foreground: string | null;
};
const STATE_VERSION = 1;
const FILE_NAME = "open-apps.json";
// Coalesces bursts (restore, sidebar scrolling) into one write; short enough
// that a build-reinstall kill right after a change rarely loses it.
const WRITE_DELAY_MS = 1000;
function openAppsFilePath(): string {
return `${knownFolders.documents().path}/${FILE_NAME}`;
}
/** The saved open-app state, or an empty state if missing or unreadable. */
export function loadPersistedOpenApps(): PersistedOpenApps {
const empty: PersistedOpenApps = { open: [], foreground: null };
try {
const path = openAppsFilePath();
if (!File.exists(path)) return empty;
const parsed = JSON.parse(File.fromPath(path).readTextSync()) as {
version?: number;
open?: unknown;
foreground?: unknown;
};
if (parsed.version !== STATE_VERSION || !Array.isArray(parsed.open)) return empty;
return {
open: parsed.open.filter((appId): appId is string => typeof appId === "string"),
foreground: typeof parsed.foreground === "string" ? parsed.foreground : null,
};
} catch (error) {
console.warn("open-apps state read failed", error);
return empty;
}
}
let writeTimer: ReturnType<typeof setTimeout> | null = null;
let pendingState: PersistedOpenApps | null = null;
/** Save the open-app state (debounced; the latest state wins). */
export function savePersistedOpenApps(state: PersistedOpenApps): void {
pendingState = state;
if (writeTimer !== null) return;
writeTimer = setTimeout(() => {
writeTimer = null;
const state = pendingState;
pendingState = null;
if (!state) return;
try {
File.fromPath(openAppsFilePath()).writeTextSync(
JSON.stringify({ version: STATE_VERSION, ...state }),
);
} catch (error) {
console.warn("open-apps state write failed", error);
}
}, WRITE_DELAY_MS);
}
+5
View File
@@ -84,6 +84,8 @@ export type ShellConfig = {
requestShellRender: () => void;
/** Screen on/off changed: the controller blanks/unblanks the compositor. */
onScreenStateChanged: (on: boolean) => void;
/** Window registered/removed or foreground changed (persists the open-app list). */
onWindowsChanged?: () => void;
};
/** Which surfaces need re-rendering after an input event. */
@@ -246,6 +248,7 @@ class Shell {
this.windows.push(window);
if (this.windows.length - 1 === this.selectedIndex) this.noteWindowVisible(window.windowId);
}
this.config.onWindowsChanged?.();
}
/** Move a window to the front of the most-recently-visible order. */
@@ -290,6 +293,7 @@ class Shell {
next.requestRender();
}
}
this.config.onWindowsChanged?.();
this.config.requestShellRender();
}
@@ -619,6 +623,7 @@ class Shell {
previous?.setForeground?.(false);
next?.setForeground?.(true);
next?.requestRender();
this.config.onWindowsChanged?.();
}
private openVoiceDialog(options: {