From 9de661d0f68af86499b6db8f6b023198ec359f13 Mon Sep 17 00:00:00 2001 From: Rafael Araujo Lehmkuhl Date: Tue, 10 Dec 2024 18:35:01 -0300 Subject: [PATCH] electron: Persist last window size and position between sessions --- electron/main.ts | 14 ++++++-- electron/services/config-store.ts | 55 +++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 electron/services/config-store.ts diff --git a/electron/main.ts b/electron/main.ts index abe2a48d2..b9dd15441 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -1,6 +1,7 @@ import { app, BrowserWindow, protocol, screen } from 'electron' import { join } from 'path' +import store from './services/config-store' import { setupNetworkService } from './services/network' export const ROOT_PATH = { @@ -13,7 +14,6 @@ let mainWindow: BrowserWindow | null * Create electron window */ function createWindow(): void { - const { width, height } = screen.getPrimaryDisplay().workAreaSize mainWindow = new BrowserWindow({ icon: join(ROOT_PATH.dist, 'pwa-512x512.png'), webPreferences: { @@ -21,8 +21,16 @@ function createWindow(): void { contextIsolation: true, nodeIntegration: false, }, - width, - height, + width: store.get('windowBounds')?.width ?? screen.getPrimaryDisplay().workAreaSize.width, + height: store.get('windowBounds')?.height ?? screen.getPrimaryDisplay().workAreaSize.height, + x: store.get('windowBounds')?.x ?? screen.getPrimaryDisplay().bounds.x, + y: store.get('windowBounds')?.y ?? screen.getPrimaryDisplay().bounds.y, + }) + + mainWindow.on('close', () => { + const windowBounds = mainWindow!.getBounds() + const { x, y, width, height } = windowBounds + store.set('windowBounds', { x, y, width, height }) }) // Test active push message to Renderer-process. diff --git a/electron/services/config-store.ts b/electron/services/config-store.ts new file mode 100644 index 000000000..5f017b62b --- /dev/null +++ b/electron/services/config-store.ts @@ -0,0 +1,55 @@ +import Store from 'electron-store' + +const electronStoreSchema = { + windowBounds: { + type: 'object', + properties: { + width: { + type: 'number', + }, + height: { + type: 'number', + }, + x: { + type: 'number', + }, + y: { + type: 'number', + }, + }, + }, +} + +/** + * Electron store schema + * Stores configuration data + */ +export interface ElectronStoreSchema { + /** + * Window bounds + */ + windowBounds: + | undefined + | { + /** + * Last known window width + */ + width: number + /** + * Last known window height + */ + height: number + /** + * Last known window x position + */ + x: number + /** + * Last known window y position + */ + y: number + } +} + +const store = new Store({ schema: electronStoreSchema }) + +export default store