1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-27 10:32:58 +02:00
joplin/ElectronClient/app/ElectronAppWrapper.js

242 lines
6.1 KiB
JavaScript
Raw Normal View History

const { BrowserWindow, Tray } = require('electron');
const { shim } = require('lib/shim');
const url = require('url');
const path = require('path');
const { dirname } = require('lib/path-utils');
const fs = require('fs-extra');
class ElectronAppWrapper {
constructor(electronApp, env, profilePath) {
this.electronApp_ = electronApp;
this.env_ = env;
this.profilePath_ = profilePath;
this.win_ = null;
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;
}
electronApp() {
return this.electronApp_;
}
setLogger(v) {
this.logger_ = v;
}
logger() {
return this.logger_;
}
window() {
return this.win_;
}
createWindow() {
const windowStateKeeper = require('electron-window-state');
2017-11-14 20:02:58 +02:00
const stateOptions = {
2017-11-14 20:02:58 +02:00
defaultWidth: 800,
defaultHeight: 600,
2019-09-19 23:51:18 +02:00
file: `window-state-${this.env_}.json`,
};
if (this.profilePath_) stateOptions.path = this.profilePath_;
// Load the previous state with fallback to defaults
const windowState = windowStateKeeper(stateOptions);
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,
backgroundColor: '#fff', // required to enable sub pixel rendering, can't be in css
2019-02-24 12:17:37 +02:00
webPreferences: {
nodeIntegration: true,
},
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 = path.join(__dirname, '..', 'build/icons/128x128.png');
2017-12-15 09:31:57 +02:00
require('electron-context-menu')({
shouldShowMenu: (event, params) => {
// params.inputFieldType === 'none' when right-clicking the text editor. This is a bit of a hack to detect it because in this
// case we don't want to use the built-in context menu but a custom one.
return params.isEditable && params.inputFieldType !== 'none';
},
});
this.win_ = new BrowserWindow(windowOptions);
this.win_.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true,
}));
// Uncomment this to view errors if the application does not start
if (this.env_ === 'dev') this.win_.webContents.openDevTools();
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') {
2018-01-31 22:10:32 +02:00
if (this.willQuitApp_) {
this.win_ = null;
} else {
event.preventDefault();
this.hide();
2018-01-31 22:10:32 +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-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_);
}
async waitForElectronAppReady() {
if (this.electronApp().isReady()) return Promise.resolve();
return new Promise((resolve) => {
const iid = setInterval(() => {
if (this.electronApp().isReady()) {
clearInterval(iid);
resolve();
}
}, 10);
});
}
2018-03-07 21:11:55 +02:00
async quit() {
this.electronApp_.quit();
}
2018-03-07 21:11:55 +02:00
exit(errorCode = 0) {
this.electronApp_.exit(errorCode);
}
2018-01-31 22:10:32 +02:00
trayShown() {
return !!this.tray_;
}
// 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_;
2019-09-19 23:51:18 +02:00
let dir = `${__dirname}/build`;
2018-02-06 15:11:59 +02:00
if (!fs.pathExistsSync(dir)) {
2019-09-19 23:51:18 +02:00
dir = `${dirname(__dirname)}/build`;
if (!fs.pathExistsSync(dir)) throw new Error('Cannot find build dir');
2018-02-06 15:11:59 +02:00
}
this.buildDir_ = dir;
return dir;
2018-02-06 00:19:21 +02:00
}
trayIconFilename_() {
let output = '';
if (process.platform === 'darwin') {
output = 'macos-16x16Template.png'; // Electron Template Image format
} else {
output = '16x16.png';
}
if (this.env_ === 'dev') output = '16x16-dev.png';
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 {
2019-09-19 23:51:18 +02:00
this.tray_ = new Tray(`${this.buildDir()}/icons/${this.trayIconFilename_()}`);
this.tray_.setToolTip(this.electronApp_.getName());
this.tray_.setContextMenu(contextMenu);
2018-02-06 15:11:59 +02:00
this.tray_.on('click', () => {
2018-02-06 15:11:59 +02:00
this.window().show();
});
} catch (error) {
console.error('Cannot create tray', error);
2018-02-06 15:11:59 +02:00
}
2018-01-31 22:10:32 +02:00
}
destroyTray() {
if (!this.tray_) return;
this.tray_.destroy();
this.tray_ = null;
}
ensureSingleInstance() {
if (this.env_ === 'dev') return false;
const gotTheLock = this.electronApp_.requestSingleInstanceLock();
if (!gotTheLock) {
// Another instance is already running - exit
this.electronApp_.quit();
return true;
}
// Someone tried to open a second instance - focus our window instead
this.electronApp_.on('second-instance', () => {
const win = this.window();
if (!win) return;
if (win.isMinimized()) win.restore();
win.show();
win.focus();
});
return false;
}
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();
const alreadyRunning = this.ensureSingleInstance();
if (alreadyRunning) return;
this.createWindow();
this.electronApp_.on('before-quit', () => {
this.willQuitApp_ = true;
});
this.electronApp_.on('window-all-closed', () => {
this.electronApp_.quit();
});
this.electronApp_.on('activate', () => {
this.win_.show();
});
}
}
module.exports = { ElectronAppWrapper };