Skip to content

Remember Window Size

The windowState API in @tynd/plugin-window-state/client handles persistence, debouncing, disk I/O and lifecycle hooks for you. There’s nothing to hand-roll: init() once at boot, restore() to apply the previous geometry, save() from your close handler so the last drag survives the quit.

Install the required plugin:

Terminal window
tynd add window-state
import { windowState } from "@tynd/plugin-window-state/client";
import { tyndWindow } from "@tynd/core/client";
// At app boot, before showing the window. `init` is idempotent — safe
// to await from multiple boot sites. It loads previously saved states
// from disk, attaches resize / move / maximize / fullscreen listeners,
// and starts auto-saving (debounced 500 ms by default).
await windowState.init();
// Re-apply the snapshot saved during the previous session, if any.
// Returns `false` on cold-start (nothing on disk yet).
await windowState.restore();
// Force-flush the latest geometry just before the user actually quits,
// bypassing the debounce so a final resize isn't lost.
tyndWindow.onCloseRequested(async () => {
await windowState.save();
});

That’s the whole recipe. The on-disk JSON lives at <appDataDir>/window-states.json by default (os.appDataDir(), named after bundle.identifier, so two apps never share it) — pass storagePath to init() to override.

Option Default When to change
debounceMs 500 Lower (e.g. 100) for snappier persistence on tile-managers; higher for ultra-low-spec disks.
storagePath <appDataDir>/window-states.json Set when you ship a portable app and want state alongside the binary (add that folder to security.fs.allow).

init() attaches listeners for every window opened from this process. Each window’s snapshot is keyed by its tyndWindow.label(), so child windows persist independently. Call restore(label) from inside each child window to apply its own saved geometry on open.

Related: tyndWindow API · store API (use this for arbitrary k/v, not for window geometry).