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')
|
|
|
|
|
|
|
|
class ElectronAppWrapper {
|
|
|
|
|
2017-11-04 15:23:15 +02:00
|
|
|
constructor(electronApp, app, store) {
|
|
|
|
this.app_ = app;
|
2017-11-04 14:38:53 +02:00
|
|
|
this.electronApp_ = electronApp;
|
2017-11-04 15:23:15 +02:00
|
|
|
this.store_ = store;
|
2017-11-04 14:38:53 +02:00
|
|
|
this.win_ = null;
|
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-04 21:46:37 +02:00
|
|
|
store() {
|
|
|
|
return this.store_;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(action) {
|
|
|
|
return this.store().dispatch(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
windowContentSize() {
|
|
|
|
if (!this.win_) return { width: 0, height: 0 };
|
|
|
|
const s = this.win_.getContentSize();
|
|
|
|
return { width: s[0], height: s[1] };
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:38:53 +02:00
|
|
|
createWindow() {
|
|
|
|
// Create the browser window.
|
|
|
|
this.win_ = new BrowserWindow({width: 800, height: 600})
|
|
|
|
|
|
|
|
// and load the index.html of the app.
|
|
|
|
this.win_.loadURL(url.format({
|
|
|
|
pathname: path.join(__dirname, 'index.html'),
|
|
|
|
protocol: 'file:',
|
|
|
|
slashes: true
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Open the DevTools.
|
|
|
|
this.win_.webContents.openDevTools()
|
|
|
|
|
|
|
|
// Emitted when the window is closed.
|
|
|
|
this.win_.on('closed', () => {
|
|
|
|
// Dereference the window object, usually you would store windows
|
|
|
|
// in an array if your app supports multi windows, this is the time
|
|
|
|
// when you should delete the corresponding element.
|
|
|
|
this.win_ = null
|
|
|
|
})
|
2017-11-04 21:46:37 +02:00
|
|
|
|
|
|
|
this.win_.on('resize', () => {
|
|
|
|
this.dispatch({
|
|
|
|
type: 'WINDOW_CONTENT_SIZE_SET',
|
|
|
|
size: this.windowContentSize(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.dispatch({
|
|
|
|
type: 'WINDOW_CONTENT_SIZE_SET',
|
|
|
|
size: this.windowContentSize(),
|
|
|
|
});
|
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();
|
|
|
|
|
|
|
|
// Quit when all windows are closed.
|
|
|
|
this.electronApp_.on('window-all-closed', () => {
|
|
|
|
// On macOS it is common for applications and their menu bar
|
|
|
|
// to stay active until the user quits explicitly with Cmd + Q
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.electronApp_.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
this.electronApp_.on('activate', () => {
|
|
|
|
// On macOS it's common to re-create a window in the app when the
|
|
|
|
// dock icon is clicked and there are no other windows open.
|
|
|
|
if (this.win_ === null) {
|
|
|
|
createWindow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { ElectronAppWrapper };
|