Skip to content

Commit

Permalink
electron: Persist last window size and position between sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Dec 11, 2024
1 parent 10920ea commit 9de661d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
14 changes: 11 additions & 3 deletions electron/main.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -13,16 +14,23 @@ 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: {
preload: join(ROOT_PATH.dist, 'electron/preload.js'),
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.
Expand Down
55 changes: 55 additions & 0 deletions electron/services/config-store.ts
Original file line number Diff line number Diff line change
@@ -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<ElectronStoreSchema>({ schema: electronStoreSchema })

export default store

0 comments on commit 9de661d

Please sign in to comment.