electron: track window state (bounds, x, y)

This commit is contained in:
Tor Hveem 2016-07-30 14:12:11 +02:00
parent afe03fd9ea
commit 03f8f2c511
No known key found for this signature in database
GPG key ID: 7C3ECF7851B7F195

29
electron-main.js Normal file → Executable file
View file

@ -7,6 +7,8 @@
const ipcMain = require('electron').ipcMain;
const nativeImage = require('electron').nativeImage;
const Menu = require('electron').Menu;
// Node fs module
const fs = require("fs");
var template;
@ -184,8 +186,25 @@
var menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
const initPath = __dirname + "/init.json";
var data;
mainWindow = new BrowserWindow({width: 1280, height: 800, 'min-width': 1024, 'min-height': 600, 'autoHideMenuBar': true, 'web-security': true, 'java': false, 'accept-first-mouse': true, defaultEncoding: 'UTF-8', 'icon':'file://'+__dirname + '/assets/img/favicon.png'});
// read saved state from file (e.g. window bounds)
try {
data = JSON.parse(fs.readFileSync(initPath, 'utf8'));
}
catch(e) {
console.log('Unable to read init.json: ', e);
}
const bounds = (data && data.bounds) ? data.bounds : {width: 1280, height:800 };
var bwdata = {width: bounds.width, height: bounds.height, 'min-width': 1024, 'min-height': 600, 'autoHideMenuBar': true, 'web-security': true, 'java': false, 'accept-first-mouse': true, defaultEncoding: 'UTF-8', 'icon':'file://'+__dirname + '/assets/img/favicon.png'}
// Remembe window position
if (data && data.bounds.x && data.bounds.y) {
bwdata.x = data.bounds.x;
bwdata.y = data.bounds.y;
}
mainWindow = new BrowserWindow(bwdata);
mainWindow.loadURL('file://' + __dirname + '/electron-start.html');
mainWindow.focus();
@ -212,6 +231,14 @@
mainWindow.webContents.executeJavaScript("document.getElementById('glowingbear').openDevTools();");
});
mainWindow.on('close', function() {
// Save window bounds to disk
var data = {
bounds: mainWindow.getBounds()
};
fs.writeFileSync(initPath, JSON.stringify(data));
});
mainWindow.on('closed', function() {
app.quit();
});