diff --git a/src/main/main.ts b/src/main/main.ts index 4c91649..fb59650 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -19,6 +19,7 @@ interface AppSettings { nightLightSceneAssignments?: Record; lastView?: "list" | "settings" | "scenes"; confirmOnOffPressActions?: Record; + windowBounds?: { width: number; height: number; x: number; y: number }; } const schema = { @@ -49,6 +50,15 @@ const schema = { default: {}, additionalProperties: { type: "boolean" }, }, + windowBounds: { + type: "object", + properties: { + width: { type: "number" }, + height: { type: "number" }, + x: { type: "number" }, + y: { type: "number" }, + }, + }, }; // Migrate config from v1.1.0 ("app/switchbot-client-config.json") to v1.2.0. @@ -79,9 +89,13 @@ const store = new Store({ function createWindow(): BrowserWindow { + const savedBounds = store.get("windowBounds") as AppSettings["windowBounds"]; + const mainWindow = new BrowserWindow({ - width: 1000, // Increased width a bit - height: 700, // Increased height a bit + width: savedBounds?.width ?? 1000, + height: savedBounds?.height ?? 700, + x: savedBounds?.x, + y: savedBounds?.y, webPreferences: { preload: path.join(__dirname, "../preload/preload.js"), contextIsolation: true, @@ -90,6 +104,19 @@ function createWindow(): BrowserWindow { autoHideMenuBar: true, // Hide the default menu bar }); + // Save window bounds on move and resize (debounced) + let saveTimeout: ReturnType; + const saveBounds = () => { + clearTimeout(saveTimeout); + saveTimeout = setTimeout(() => { + if (!mainWindow.isDestroyed() && !mainWindow.isMinimized() && !mainWindow.isMaximized()) { + store.set("windowBounds", mainWindow.getBounds()); + } + }, 500); + }; + mainWindow.on("resize", saveBounds); + mainWindow.on("move", saveBounds); + if (process.env.NODE_ENV === "development") { mainWindow.loadURL("http://localhost:5173"); // Vite dev server URL mainWindow.webContents.openDevTools();