Skip to content

Commit

Permalink
Fix bug with Save
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
joerick committed Mar 10, 2017
1 parent e72c6ca commit 80670ef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
89 changes: 44 additions & 45 deletions app/TingappWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 2 additions & 2 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 80670ef

Please sign in to comment.