Remember window size and position
@@ -13,6 +13,8 @@ import * as fs from 'fs';
let win: BrowserWindow = null;
let serverPid: any = null;
+let state: any = {};
+let statePath: string = null;
const args = process.argv.slice(1);
const local = args.some(val => val === '--local');
@@ -59,12 +61,34 @@ const template: Electron.MenuItemConstructorOptions[] = [
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
+let saveBoundsCookie;
+
+function saveCurrentState() {
+ fs.writeFileSync(statePath, JSON.stringify(state, null, 2));
+}
+
+function loadSavedState() {
+ try {
+ state = JSON.parse(fs.readFileSync(statePath, 'utf8'));
+ } catch (e) {}
+}
+
+function saveBoundsSoon() {
+ if (saveBoundsCookie) clearTimeout(saveBoundsCookie);
+ saveBoundsCookie = setTimeout(() => {
+ saveBoundsCookie = undefined;
+ state.bounds = win.getBounds();
+ saveCurrentState();
+ }, 1000);
+}
+
function createWindow(): BrowserWindow {
const electronScreen = screen;
const size = electronScreen.getPrimaryDisplay().workAreaSize;
- // Create the browser window.
- win = new BrowserWindow({
+ var options = {
+ x: null,
+ y: null,
width: size.width,
height: size.height,
title: '',
@@ -76,7 +100,29 @@ function createWindow(): BrowserWindow {
enableRemoteModule: true, // true if you want to run 2e2 test with Spectron or use remote module in renderer context (ie. Angular)
// preload: path.join(__dirname, 'preload.js'),
},
- });
+ };
+
+ if (state?.bounds) {
+ const bounds = state.bounds;
+ const area = electronScreen.getDisplayMatching(bounds).workArea;
+ if (
+ bounds.x >= area.x &&
+ bounds.y >= area.y &&
+ bounds.x + bounds.width <= area.x + area.width &&
+ bounds.y + bounds.height <= area.y + area.height
+ ) {
+ options.x = bounds.x;
+ options.y = bounds.y;
+ }
+ // If the saved size is still valid, use it.
+ if (bounds.width <= area.width || bounds.height <= area.height) {
+ options.width = bounds.width;
+ options.height = bounds.height;
+ }
+ }
+
+ // Create the browser window.
+ win = new BrowserWindow(options);
win.setIcon(path.join(__dirname, 'dist/dash-frontend/assets/icons/icon.png'));
@@ -96,6 +142,8 @@ function createWindow(): BrowserWindow {
// when you should delete the corresponding element.
win = null;
});
+ win.on('resize', saveBoundsSoon);
+ win.on('move', saveBoundsSoon);
return win;
}
@@ -138,6 +186,9 @@ try {
});
app.on('ready', async () => {
+ statePath = path.join(app.getPath('userData'), 'state.json');
+ loadSavedState();
+
const getPort = require('get-port');
const port = await getPort();
startBinary(port);
