From 03f8f2c511e9a965799da4305ba95ef475fefcb1 Mon Sep 17 00:00:00 2001
From: Tor Hveem <tor@hveem.no>
Date: Sat, 30 Jul 2016 14:12:11 +0200
Subject: [PATCH] electron: track window state (bounds, x, y)

---
 electron-main.js | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
 mode change 100644 => 100755 electron-main.js

diff --git a/electron-main.js b/electron-main.js
old mode 100644
new mode 100755
index 729323e..a0ea7aa
--- a/electron-main.js
+++ b/electron-main.js
@@ -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();
         });