-
Notifications
You must be signed in to change notification settings - Fork 0
/
windowState.ts
31 lines (25 loc) · 1.05 KB
/
windowState.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { app } from 'electron';
import { debounce } from 'lodash';
const path = require('path');
const fs = require('fs');
const userDataPath = app.getPath('userData');
const windowStateFile = path.join(userDataPath, 'windowState.json');
type WindowState = { width: number; height: number; x?: number; y?: number; maximise: boolean };
function getWindowState(): WindowState {
try {
const data = fs.readFileSync(windowStateFile, 'utf-8');
return JSON.parse(data);
} catch (error) {
// Return default values if the file does not exist or is invalid
return { width: 800, height: 600, maximise: true };
}
}
function saveWindowState(mainWindow: null | Electron.BrowserWindow) {
if (mainWindow) {
const { width, height, x, y } = mainWindow.getBounds();
const windowState: WindowState = { width, height, x, y, maximise: mainWindow.isMaximized() };
fs.writeFileSync(windowStateFile, JSON.stringify(windowState));
}
}
const debouncedSaveWindowState = debounce(saveWindowState, 2000);
export { getWindowState, debouncedSaveWindowState as saveWindowState };