From 251024ba840cb7d6ccd07dc1447ce4510069bba5 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Fri, 24 Oct 2025 17:32:29 +0800 Subject: [PATCH] store whether window is maximized closes #2574 #989 #2396 --- src/main/index.ts | 19 +++++++++++++++---- types.ts | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index fd3daa9b..cd486af1 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -112,7 +112,7 @@ async function sendApiAction(action: string, args?: unknown[]) { } // https://github.com/electron/electron/issues/526#issuecomment-563010533 -function getSizeOptions() { +function getSavedBounds() { const bounds = configStore.get('windowBounds'); const options: BrowserWindowConstructorOptions = {}; if (bounds) { @@ -133,7 +133,10 @@ function getSizeOptions() { options.height = bounds.height; } } - return options; + return { + options, + isMaximized: bounds != null ? bounds.isMaximized : false, + }; } function createWindow() { @@ -142,8 +145,10 @@ function createWindow() { // https://www.electronjs.org/docs/latest/tutorial/dark-mode if (darkMode) nativeTheme.themeSource = 'dark'; + const savedBounds = getSavedBounds(); + mainWindow = new BrowserWindow({ - ...getSizeOptions(), + ...savedBounds.options, darkTheme: true, webPreferences: { contextIsolation: false, @@ -154,6 +159,8 @@ function createWindow() { backgroundColor: darkMode ? '#333' : '#fff', }); + if (savedBounds.isMaximized) mainWindow.maximize(); + remote.enable(mainWindow.webContents); attachContextMenu(mainWindow); @@ -189,12 +196,16 @@ function createWindow() { } }); + // TODO replace with `windowStatePersistence` in the future? https://github.com/electron/rfcs/pull/16 const debouncedSaveWindowState = debounce(() => { if (!mainWindow || !configStore.get('storeWindowBounds')) return; const { x, y, width, height } = mainWindow.getNormalBounds(); - configStore.set('windowBounds', { x, y, width, height }); + const isMaximized = mainWindow.isMaximized(); + configStore.set('windowBounds', { x, y, width, height, isMaximized }); }, 500); + mainWindow.on('maximize', debouncedSaveWindowState); + mainWindow.on('unmaximize', debouncedSaveWindowState); mainWindow.on('resize', debouncedSaveWindowState); mainWindow.on('move', debouncedSaveWindowState); } diff --git a/types.ts b/types.ts index ba2aed0f..13fce5bf 100644 --- a/types.ts +++ b/types.ts @@ -90,7 +90,7 @@ export interface Config { treatOutputFileModifiedTimeAsStart: boolean | undefined | null, outFormatLocked: string | undefined, safeOutputFileName: boolean, - windowBounds: { x: number, y: number, width: number, height: number } | undefined, + windowBounds: { x: number, y: number, width: number, height: number, isMaximized?: boolean } | undefined, storeWindowBounds: boolean, enableAutoHtml5ify: boolean, keyBindings: KeyBinding[],