From d4c6b0a255980f41f54f39117a862c4ffe394e43 Mon Sep 17 00:00:00 2001 From: Andy Shinn Date: Wed, 18 Jun 2025 02:51:15 -0500 Subject: [PATCH] save electron window size and location and use existing on launch --- electron/src/main/index.ts | 23 ++++++++++++++++++++-- electron/src/main/window.ts | 39 +++++++++++++++++++++++++++++++++++++ electron/tsconfig.node.json | 3 ++- 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 electron/src/main/window.ts diff --git a/electron/src/main/index.ts b/electron/src/main/index.ts index 29b497b..987e033 100644 --- a/electron/src/main/index.ts +++ b/electron/src/main/index.ts @@ -4,6 +4,7 @@ import { electronApp, optimizer } from '@electron-toolkit/utils' import icon from '../../resources/icon.png?asset' import { autoUpdater } from 'electron-updater' import { buildMenu } from './menu' +import { getWindowState, saveWindowState } from './window' let apiProcess: Electron.UtilityProcess let apiPort: any = 9999 @@ -19,10 +20,14 @@ async function updateCheckLoop() { } function createWindow(): void { + const windowState = getWindowState() + // Create the browser window. const mainWindow = new BrowserWindow({ - width: 1300, - height: 900, + x: windowState.x, + y: windowState.y, + width: windowState.width, + height: windowState.height, show: false, autoHideMenuBar: true, ...(process.platform === 'linux' ? { icon } : {}), @@ -32,6 +37,20 @@ function createWindow(): void { } }) + if (windowState.isMaximized) { + mainWindow.maximize() + } + + const saveState = () => saveWindowState(mainWindow) + mainWindow.on('resize', saveState) + mainWindow.on('move', saveState) + mainWindow.on('maximize', saveState) + mainWindow.on('unmaximize', saveState) + + mainWindow.on('close', () => { + saveWindowState(mainWindow) + }) + mainWindow.on('ready-to-show', () => { mainWindow.show() }) diff --git a/electron/src/main/window.ts b/electron/src/main/window.ts new file mode 100644 index 0000000..967416f --- /dev/null +++ b/electron/src/main/window.ts @@ -0,0 +1,39 @@ +import { BrowserWindow, app } from 'electron' +import { join } from 'path' +import { keyFileStorage } from 'key-file-storage' + +const userDataPath = app.getPath('userData') +const windowStateKfs = keyFileStorage(join(userDataPath, 'window-state')) + +export interface WindowState { + x?: number + y?: number + width: number + height: number + isMaximized?: boolean +} + +export function getWindowState(): WindowState { + try { + const state = windowStateKfs['window-state'] + return state || { width: 1300, height: 900 } + } catch { + return { width: 1300, height: 900 } + } +} + +export function saveWindowState(window: BrowserWindow): void { + try { + const bounds = window.getBounds() + const state: WindowState = { + x: bounds.x, + y: bounds.y, + width: bounds.width, + height: bounds.height, + isMaximized: window.isMaximized() + } + windowStateKfs['window-state'] = state + } catch (error) { + console.error('[electron] Failed to save window state:', error) + } +} diff --git a/electron/tsconfig.node.json b/electron/tsconfig.node.json index db23a68..8d85087 100644 --- a/electron/tsconfig.node.json +++ b/electron/tsconfig.node.json @@ -3,6 +3,7 @@ "include": ["electron.vite.config.*", "src/main/**/*", "src/preload/**/*"], "compilerOptions": { "composite": true, - "types": ["electron-vite/node"] + "types": ["electron-vite/node"], + "esModuleInterop": true } }