From 80670efabd00118606572386d099857b4c02ba57 Mon Sep 17 00:00:00 2001 From: Joe Rickerby Date: Fri, 10 Mar 2017 19:15:02 +0000 Subject: [PATCH] Fix bug with Save Subclassing BrowserWindow in es6 style seems to cause some strange bugs in electron. I've changed this so that methods are added to the individual instances, as that keeps the object as type BrowserWindow. Not sure what the problem is with BrowserWindow subclasses. If anyone knows the reason, would love to hear if there's a better solution! --- app/TingappWindow.js | 89 ++++++++++++++++++++++---------------------- app/app.js | 4 +- 2 files changed, 46 insertions(+), 47 deletions(-) diff --git a/app/TingappWindow.js b/app/TingappWindow.js index 65c6a3e..955c298 100644 --- a/app/TingappWindow.js +++ b/app/TingappWindow.js @@ -27,59 +27,58 @@ function setDefaultZoomFactor(zoomFactor) { }); } -class TingappWindow extends BrowserWindow { - constructor(options) { - const defaultOptions = { - width: 700, - height: 600, - minWidth: 450, - minHeight: 300, - darkTheme: true, - backgroundColor: '#1c1c1c', - title: 'Tide', +function newTingappWindow(options) { + const defaultOptions = { + width: 700, + height: 600, + minWidth: 450, + minHeight: 300, + darkTheme: true, + backgroundColor: '#1c1c1c', + title: 'Tide', webPreferences: { // if zoom factor is available synchronously, use that zoomFactor: _defaultZoomFactor || undefined, } - }; + }; + options = Object.assign({}, defaultOptions, options); - // supply default options (but prefer options already there) - options = Object.assign({}, defaultOptions, options); + const win = new BrowserWindow(options); - super(options); - - // render index.html which will contain our root Vue component - this.loadURL('file://' + __dirname + '/index.html'); - - // if zoom factor isn't loaded yet, use the async method to load/set - if (_defaultZoomFactor === null) { - this.webContents.on('did-finish-load', () => { - getDefaultZoomFactor((zoomFactor) => { - console.log('setting zoomFactor to ', zoomFactor); - this.webContents.setZoomFactor(zoomFactor); - }); + // if zoom factor isn't loaded yet, use the async method to load/set + if (_defaultZoomFactor === null) { + win.webContents.on('did-finish-load', () => { + getDefaultZoomFactor((zoomFactor) => { + console.log('setting zoomFactor to ', zoomFactor); + win.webContents.setZoomFactor(zoomFactor); }); - } + }); } - resetZoom() { - this.webContents.setZoomFactor(1.0); - setDefaultZoomFactor(1.0); - } - zoomIn() { - this.webContents.getZoomFactor((zoomFactor) => { - zoomFactor *= 1.1; - this.webContents.setZoomFactor(zoomFactor); - setDefaultZoomFactor(zoomFactor); - }) - } - zoomOut() { - this.webContents.getZoomFactor((zoomFactor) => { - zoomFactor /= 1.1; - this.webContents.setZoomFactor(zoomFactor); - setDefaultZoomFactor(zoomFactor); - }) - } + // add some extra methods to win + Object.assign(win, { + resetZoom() { + this.webContents.setZoomFactor(1.0); + setDefaultZoomFactor(1.0); + }, + zoomIn() { + this.webContents.getZoomFactor((zoomFactor) => { + zoomFactor *= 1.1; + this.webContents.setZoomFactor(zoomFactor); + setDefaultZoomFactor(zoomFactor); + }) + }, + zoomOut() { + this.webContents.getZoomFactor((zoomFactor) => { + zoomFactor /= 1.1; + this.webContents.setZoomFactor(zoomFactor); + setDefaultZoomFactor(zoomFactor); + }) + }, + }); + + win.loadURL('file://' + __dirname + '/index.html'); + return win; } -module.exports = TingappWindow; +module.exports = newTingappWindow; diff --git a/app/app.js b/app/app.js index 42de5e8..3e41c93 100644 --- a/app/app.js +++ b/app/app.js @@ -7,14 +7,14 @@ const fs = require('fs'); const path = require('path'); const autoupdate = require('./autoupdate'); const menu = require('./menu'); -const TingappWindow = require('./TingappWindow'); +const newTingappWindow = require('./TingappWindow'); if (require('electron-squirrel-startup')) return; require('./src/utils/exceptionhandling.js').setup(); function createWindow(on_load) { - const newWindow = new TingappWindow(); + const newWindow = newTingappWindow(); if (on_load) { newWindow.webContents.on('did-finish-load', function() {