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

+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: {