|
| 1 | +import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs" |
| 2 | +import path from "node:path" |
| 3 | +import type { BrowserWindow, Rectangle } from "electron" |
| 4 | + |
| 5 | +// Remembers a window's bounds and maximized / fullscreen flags across launches in the same JSON |
| 6 | +// file electron-window-state wrote, so existing profiles restore where they were. |
| 7 | + |
| 8 | +export type WindowState = { |
| 9 | + x?: number |
| 10 | + y?: number |
| 11 | + width: number |
| 12 | + height: number |
| 13 | + displayBounds?: Rectangle |
| 14 | + isMaximized?: boolean |
| 15 | + isFullScreen?: boolean |
| 16 | +} |
| 17 | + |
| 18 | +export type Displays = { |
| 19 | + all: () => Rectangle[] |
| 20 | + primary: () => Rectangle |
| 21 | + matching: (bounds: Rectangle) => Rectangle |
| 22 | +} |
| 23 | + |
| 24 | +// Bounds are only trusted when they still fit on one of the current displays; otherwise the window |
| 25 | +// falls back to the default size on the primary display. A saved maximized or fullscreen state |
| 26 | +// without bounds is kept so manage() can restore it. |
| 27 | +export function resolveWindowState(saved: unknown, defaults: { width: number; height: number }, displays: Displays) { |
| 28 | + const state = isState(saved) ? saved : undefined |
| 29 | + if (!state) return { width: defaults.width, height: defaults.height } satisfies WindowState |
| 30 | + if (!hasBounds(state)) { |
| 31 | + return state.isMaximized || state.isFullScreen |
| 32 | + ? ({ ...state, width: defaults.width, height: defaults.height } satisfies WindowState) |
| 33 | + : ({ width: defaults.width, height: defaults.height } satisfies WindowState) |
| 34 | + } |
| 35 | + if (!state.displayBounds) return state |
| 36 | + const visible = displays.all().some((bounds) => within(state, bounds)) |
| 37 | + if (visible) return state |
| 38 | + return { |
| 39 | + width: defaults.width, |
| 40 | + height: defaults.height, |
| 41 | + x: 0, |
| 42 | + y: 0, |
| 43 | + displayBounds: displays.primary(), |
| 44 | + } satisfies WindowState |
| 45 | +} |
| 46 | + |
| 47 | +export function readWindowState(file: string): unknown { |
| 48 | + if (!existsSync(file)) return undefined |
| 49 | + try { |
| 50 | + return JSON.parse(readFileSync(file, "utf8")) |
| 51 | + } catch { |
| 52 | + return undefined |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Tracks the window until it closes and writes the final state then. Bounds are recorded while |
| 57 | +// the window is in its normal state so a maximized window restores to its previous size. |
| 58 | +export function manageWindowState(win: BrowserWindow, file: string, initial: WindowState, displays: Displays) { |
| 59 | + const state = { ...initial } |
| 60 | + if (state.isMaximized) win.maximize() |
| 61 | + if (state.isFullScreen) win.setFullScreen(true) |
| 62 | + let timer: ReturnType<typeof setTimeout> | undefined |
| 63 | + const update = () => { |
| 64 | + if (win.isDestroyed()) return |
| 65 | + const bounds = win.getBounds() |
| 66 | + if (!win.isMaximized() && !win.isMinimized() && !win.isFullScreen()) { |
| 67 | + state.x = bounds.x |
| 68 | + state.y = bounds.y |
| 69 | + state.width = bounds.width |
| 70 | + state.height = bounds.height |
| 71 | + } |
| 72 | + state.isMaximized = win.isMaximized() |
| 73 | + state.isFullScreen = win.isFullScreen() |
| 74 | + state.displayBounds = displays.matching(bounds) |
| 75 | + } |
| 76 | + const changed = () => { |
| 77 | + clearTimeout(timer) |
| 78 | + timer = setTimeout(update, 100) |
| 79 | + } |
| 80 | + const closed = () => { |
| 81 | + clearTimeout(timer) |
| 82 | + win.off("resize", changed) |
| 83 | + win.off("move", changed) |
| 84 | + win.off("close", update) |
| 85 | + win.off("closed", closed) |
| 86 | + mkdirSync(path.dirname(file), { recursive: true }) |
| 87 | + writeFileSync(`${file}.tmp`, JSON.stringify(state)) |
| 88 | + renameSync(`${file}.tmp`, file) |
| 89 | + } |
| 90 | + win.on("resize", changed) |
| 91 | + win.on("move", changed) |
| 92 | + win.on("close", update) |
| 93 | + win.on("closed", closed) |
| 94 | +} |
| 95 | + |
| 96 | +function isState(value: unknown): value is WindowState { |
| 97 | + return typeof value === "object" && value !== null |
| 98 | +} |
| 99 | + |
| 100 | +function hasBounds(state: WindowState): state is WindowState & Required<Pick<WindowState, "x" | "y">> { |
| 101 | + return ( |
| 102 | + Number.isInteger(state.x) && |
| 103 | + Number.isInteger(state.y) && |
| 104 | + Number.isInteger(state.width) && |
| 105 | + state.width > 0 && |
| 106 | + Number.isInteger(state.height) && |
| 107 | + state.height > 0 |
| 108 | + ) |
| 109 | +} |
| 110 | + |
| 111 | +function within(state: WindowState & { x: number; y: number }, bounds: Rectangle) { |
| 112 | + return ( |
| 113 | + state.x >= bounds.x && |
| 114 | + state.y >= bounds.y && |
| 115 | + state.x + state.width <= bounds.x + bounds.width && |
| 116 | + state.y + state.height <= bounds.y + bounds.height |
| 117 | + ) |
| 118 | +} |
0 commit comments