2017-11-04 14:38:53 +02:00
|
|
|
const { _ } = require('lib/locale.js');
|
2018-01-31 22:10:32 +02:00
|
|
|
const { BrowserWindow, Menu, Tray } = require('electron');
|
2017-12-15 09:31:57 +02:00
|
|
|
const { shim } = require('lib/shim');
|
2017-11-04 14:38:53 +02:00
|
|
|
const url = require('url')
|
|
|
|
const path = require('path')
|
2017-11-08 19:51:55 +02:00
|
|
|
const urlUtils = require('lib/urlUtils.js');
|
2018-02-06 15:05:36 +02:00
|
|
|
const { dirname, basename } = require('lib/path-utils');
|
2018-02-06 15:11:59 +02:00
|
|
|
const fs = require('fs-extra');
|
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;
|
2018-01-31 22:10:32 +02:00
|
|
|
this.tray_ = null;
|
2018-02-06 15:11:59 +02:00
|
|
|
this.buildDir_ = 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-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,
|
2018-02-27 22:04:38 +02:00
|
|
|
file: 'window-state-' + this.env_ + '.json',
|
2017-11-14 20:02:58 +02:00
|
|
|
});
|
|
|
|
|
2017-12-15 09:31:57 +02:00
|
|
|
const windowOptions = {
|
2018-02-27 22:04:38 +02:00
|
|
|
x: windowState.x,
|
|
|
|
y: windowState.y,
|
|
|
|
width: windowState.width,
|
|
|
|
height: windowState.height,
|
2017-12-15 09:31:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Linux icon workaround for bug https://github.com/electron-userland/electron-builder/issues/2098
|
|
|
|
// Fix: https://github.com/electron-userland/electron-builder/issues/2269
|
|
|
|
if (shim.isLinux()) windowOptions.icon = __dirname + '/build/icons/128x128.png';
|
|
|
|
|
|
|
|
this.win_ = new BrowserWindow(windowOptions)
|
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-28 00:50:46 +02:00
|
|
|
// Uncomment this to view errors if the application does not start
|
2018-02-16 20:08:02 +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) => {
|
2018-01-31 22:10:32 +02:00
|
|
|
// If it's on macOS, the app is completely closed only if the user chooses to close the app (willQuitApp_ will be true)
|
|
|
|
// otherwise the window is simply hidden, and will be re-open once the app is "activated" (which happens when the
|
|
|
|
// user clicks on the icon in the task bar).
|
|
|
|
|
|
|
|
// On Windows and Linux, the app is closed when the window is closed *except* if the tray icon is used. In which
|
|
|
|
// case the app must be explicitely closed with Ctrl+Q or by right-clicking on the tray icon and selecting "Exit".
|
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
if (this.willQuitApp_) {
|
|
|
|
this.win_ = null;
|
|
|
|
} else {
|
|
|
|
event.preventDefault();
|
2018-02-27 23:54:40 +02:00
|
|
|
this.hide();
|
2018-01-31 22:10:32 +02:00
|
|
|
}
|
2017-11-17 20:05:25 +02:00
|
|
|
} else {
|
2018-01-31 22:10:32 +02:00
|
|
|
if (this.trayShown() && !this.willQuitApp_) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.win_.hide();
|
|
|
|
} else {
|
|
|
|
this.win_ = null;
|
|
|
|
}
|
2017-11-17 20:05:25 +02:00
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2018-01-31 22:10:32 +02:00
|
|
|
trayShown() {
|
|
|
|
return !!this.tray_;
|
|
|
|
}
|
|
|
|
|
2018-02-27 23:54:40 +02:00
|
|
|
// This method is used in macOS only to hide the whole app (and not just the main window)
|
|
|
|
// including the menu bar. This follows the macOS way of hidding an app.
|
|
|
|
hide() {
|
|
|
|
this.electronApp_.hide();
|
|
|
|
}
|
|
|
|
|
2018-02-06 00:19:21 +02:00
|
|
|
buildDir() {
|
2018-02-06 15:11:59 +02:00
|
|
|
if (this.buildDir_) return this.buildDir_;
|
|
|
|
let dir = __dirname + '/build';
|
|
|
|
if (!fs.pathExistsSync(dir)) {
|
|
|
|
dir = dirname(__dirname) + '/build';
|
|
|
|
if (!fs.pathExistsSync(dir)) throw new Error('Cannot find build dir');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.buildDir_ = dir;
|
|
|
|
return dir;
|
2018-02-06 00:19:21 +02:00
|
|
|
}
|
|
|
|
|
2018-02-20 02:41:52 +02:00
|
|
|
trayIconFilename_() {
|
|
|
|
let output = '';
|
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
output = 'macos-16x16Template.png'; // Electron Template Image format
|
|
|
|
} else {
|
|
|
|
output = '16x16.png';
|
|
|
|
}
|
|
|
|
|
2018-02-23 19:51:23 +02:00
|
|
|
if (this.env_ === 'dev') output = '16x16-dev.png'
|
2018-02-20 02:41:52 +02:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2018-01-31 22:10:32 +02:00
|
|
|
// Note: this must be called only after the "ready" event of the app has been dispatched
|
|
|
|
createTray(contextMenu) {
|
2018-02-06 15:11:59 +02:00
|
|
|
try {
|
2018-02-20 02:41:52 +02:00
|
|
|
this.tray_ = new Tray(this.buildDir() + '/icons/' + this.trayIconFilename_())
|
2018-02-06 15:11:59 +02:00
|
|
|
this.tray_.setToolTip(this.electronApp_.getName())
|
|
|
|
this.tray_.setContextMenu(contextMenu)
|
|
|
|
|
|
|
|
this.tray_.on('click', () => {
|
|
|
|
this.window().show();
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Cannot create tray", error);
|
|
|
|
}
|
2018-01-31 22:10:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
destroyTray() {
|
|
|
|
if (!this.tray_) return;
|
|
|
|
this.tray_.destroy();
|
|
|
|
this.tray_ = null;
|
|
|
|
}
|
|
|
|
|
2018-02-23 19:51:23 +02:00
|
|
|
ensureSingleInstance() {
|
2018-03-01 22:14:06 +02:00
|
|
|
if (this.env_ === 'dev') return false;
|
|
|
|
|
2018-02-23 19:51:23 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const alreadyRunning = this.electronApp_.makeSingleInstance((commandLine, workingDirectory) => {
|
|
|
|
const win = this.window();
|
|
|
|
if (!win) return;
|
|
|
|
if (win.isMinimized()) win.restore();
|
|
|
|
win.show();
|
|
|
|
win.focus();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (alreadyRunning) this.electronApp_.quit();
|
|
|
|
|
|
|
|
resolve(alreadyRunning);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2018-02-23 19:51:23 +02:00
|
|
|
const alreadyRunning = await this.ensureSingleInstance();
|
|
|
|
if (alreadyRunning) return;
|
|
|
|
|
2017-11-04 14:38:53 +02:00
|
|
|
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 };
|