2017-11-04 14:38:53 +02:00
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const { BrowserWindow } = require('electron');
|
|
|
|
const url = require('url')
|
|
|
|
const path = require('path')
|
2017-11-08 19:51:55 +02:00
|
|
|
const urlUtils = require('lib/urlUtils.js');
|
2017-11-04 14:38:53 +02:00
|
|
|
|
|
|
|
class ElectronAppWrapper {
|
|
|
|
|
2017-11-13 20:47:35 +02:00
|
|
|
constructor(electronApp, env) {
|
2017-11-04 14:38:53 +02:00
|
|
|
this.electronApp_ = electronApp;
|
2017-11-13 20:47:35 +02:00
|
|
|
this.env_ = env;
|
2017-11-04 14:38:53 +02:00
|
|
|
this.win_ = null;
|
2017-11-17 20:05:25 +02:00
|
|
|
this.willQuitApp_ = false;
|
2017-11-04 15:23:15 +02:00
|
|
|
}
|
2017-11-04 14:38:53 +02:00
|
|
|
|
2017-11-04 15:23:15 +02:00
|
|
|
electronApp() {
|
|
|
|
return this.electronApp_;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLogger(v) {
|
|
|
|
this.logger_ = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger() {
|
|
|
|
return this.logger_;
|
2017-11-04 14:38:53 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 02:17:48 +02:00
|
|
|
window() {
|
|
|
|
return this.win_;
|
2017-11-04 21:46:37 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 14:38:53 +02:00
|
|
|
createWindow() {
|
2017-11-14 20:02:58 +02:00
|
|
|
const windowStateKeeper = require('electron-window-state');
|
|
|
|
|
|
|
|
// Load the previous state with fallback to defaults
|
|
|
|
const windowState = windowStateKeeper({
|
|
|
|
defaultWidth: 800,
|
|
|
|
defaultHeight: 600,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.win_ = new BrowserWindow({
|
|
|
|
'x': windowState.x,
|
|
|
|
'y': windowState.y,
|
|
|
|
'width': windowState.width,
|
|
|
|
'height': windowState.height
|
|
|
|
})
|
2017-11-04 14:38:53 +02:00
|
|
|
|
|
|
|
this.win_.loadURL(url.format({
|
|
|
|
pathname: path.join(__dirname, 'index.html'),
|
|
|
|
protocol: 'file:',
|
|
|
|
slashes: true
|
|
|
|
}))
|
|
|
|
|
2017-11-17 20:02:01 +02:00
|
|
|
//if (this.env_ === 'dev') this.win_.webContents.openDevTools();
|
2017-11-04 14:38:53 +02:00
|
|
|
|
2017-11-17 20:05:25 +02:00
|
|
|
this.win_.on('close', (event) => {
|
|
|
|
if (this.willQuitApp_ || process.platform !== 'darwin') {
|
|
|
|
this.win_ = null;
|
|
|
|
} else {
|
|
|
|
event.preventDefault();
|
|
|
|
this.win_.hide();
|
|
|
|
}
|
2017-11-04 14:38:53 +02:00
|
|
|
})
|
2017-11-14 20:02:58 +02:00
|
|
|
|
|
|
|
// Let us register listeners on the window, so we can update the state
|
|
|
|
// automatically (the listeners will be removed when the window is closed)
|
|
|
|
// and restore the maximized or full screen state
|
|
|
|
windowState.manage(this.win_);
|
2017-11-04 14:38:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async waitForElectronAppReady() {
|
2017-11-04 15:23:15 +02:00
|
|
|
if (this.electronApp().isReady()) return Promise.resolve();
|
2017-11-04 14:38:53 +02:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const iid = setInterval(() => {
|
2017-11-04 15:23:15 +02:00
|
|
|
if (this.electronApp().isReady()) {
|
2017-11-04 14:38:53 +02:00
|
|
|
clearInterval(iid);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-04 15:23:15 +02:00
|
|
|
async exit() {
|
|
|
|
this.electronApp_.quit();
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:38:53 +02:00
|
|
|
async start() {
|
|
|
|
// Since we are doing other async things before creating the window, we might miss
|
|
|
|
// the "ready" event. So we use the function below to make sure that the app is ready.
|
|
|
|
await this.waitForElectronAppReady();
|
|
|
|
|
|
|
|
this.createWindow();
|
|
|
|
|
2017-11-17 20:05:25 +02:00
|
|
|
this.electronApp_.on('before-quit', () => {
|
|
|
|
this.willQuitApp_ = true;
|
|
|
|
})
|
|
|
|
|
2017-11-04 14:38:53 +02:00
|
|
|
this.electronApp_.on('window-all-closed', () => {
|
2017-11-17 20:05:25 +02:00
|
|
|
this.electronApp_.quit();
|
2017-11-04 14:38:53 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
this.electronApp_.on('activate', () => {
|
2017-11-17 20:05:25 +02:00
|
|
|
this.win_.show();
|
2017-11-04 14:38:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { ElectronAppWrapper };
|