Skip to content

Commit ecb8312

Browse files
rafaellehmkuhlArturoManzoli
authored andcommitted
electron: Persist last window size and position between sessions
1 parent 9b3a7d1 commit ecb8312

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

electron/main.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { app, BrowserWindow, protocol, screen } from 'electron'
22
import { join } from 'path'
33

4+
import store from './services/config-store'
45
import { setupNetworkService } from './services/network'
56

67
export const ROOT_PATH = {
@@ -13,16 +14,23 @@ let mainWindow: BrowserWindow | null
1314
* Create electron window
1415
*/
1516
function createWindow(): void {
16-
const { width, height } = screen.getPrimaryDisplay().workAreaSize
1717
mainWindow = new BrowserWindow({
1818
icon: join(ROOT_PATH.dist, 'pwa-512x512.png'),
1919
webPreferences: {
2020
preload: join(ROOT_PATH.dist, 'electron/preload.js'),
2121
contextIsolation: true,
2222
nodeIntegration: false,
2323
},
24-
width,
25-
height,
24+
width: store.get('windowBounds')?.width ?? screen.getPrimaryDisplay().workAreaSize.width,
25+
height: store.get('windowBounds')?.height ?? screen.getPrimaryDisplay().workAreaSize.height,
26+
x: store.get('windowBounds')?.x ?? screen.getPrimaryDisplay().bounds.x,
27+
y: store.get('windowBounds')?.y ?? screen.getPrimaryDisplay().bounds.y,
28+
})
29+
30+
mainWindow.on('close', () => {
31+
const windowBounds = mainWindow!.getBounds()
32+
const { x, y, width, height } = windowBounds
33+
store.set('windowBounds', { x, y, width, height })
2634
})
2735

2836
// Test active push message to Renderer-process.

electron/services/config-store.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import Store from 'electron-store'
2+
3+
const electronStoreSchema = {
4+
windowBounds: {
5+
type: 'object',
6+
properties: {
7+
width: {
8+
type: 'number',
9+
},
10+
height: {
11+
type: 'number',
12+
},
13+
x: {
14+
type: 'number',
15+
},
16+
y: {
17+
type: 'number',
18+
},
19+
},
20+
},
21+
}
22+
23+
/**
24+
* Electron store schema
25+
* Stores configuration data
26+
*/
27+
export interface ElectronStoreSchema {
28+
/**
29+
* Window bounds
30+
*/
31+
windowBounds:
32+
| undefined
33+
| {
34+
/**
35+
* Last known window width
36+
*/
37+
width: number
38+
/**
39+
* Last known window height
40+
*/
41+
height: number
42+
/**
43+
* Last known window x position
44+
*/
45+
x: number
46+
/**
47+
* Last known window y position
48+
*/
49+
y: number
50+
}
51+
}
52+
53+
const store = new Store<ElectronStoreSchema>({ schema: electronStoreSchema })
54+
55+
export default store

0 commit comments

Comments
 (0)