2023-07-27 17:05:56 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
2020-10-09 19:35:46 +02:00
|
|
|
import { PluginMessage } from './services/plugins/PluginRunner';
|
2020-11-07 17:59:37 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
2021-10-16 11:07:41 +02:00
|
|
|
import { isCallbackUrl } from '@joplin/lib/callbackUrlUtils';
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2023-02-22 20:15:21 +02:00
|
|
|
import { BrowserWindow, Tray, screen } from 'electron';
|
2023-10-22 12:52:06 +02:00
|
|
|
import bridge from './bridge';
|
2019-07-30 09:35:42 +02:00
|
|
|
const url = require('url');
|
|
|
|
const path = require('path');
|
2020-11-07 17:59:37 +02:00
|
|
|
const { dirname } = require('@joplin/lib/path-utils');
|
2018-03-09 22:59:12 +02:00
|
|
|
const fs = require('fs-extra');
|
2023-10-31 17:05:28 +02:00
|
|
|
|
|
|
|
import { dialog, ipcMain } from 'electron';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import restartInSafeModeFromMain from './utils/restartInSafeModeFromMain';
|
2023-12-13 21:45:29 +02:00
|
|
|
import { clearTimeout, setTimeout } from 'timers';
|
2017-11-04 14:38:53 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
interface RendererProcessQuitReply {
|
2020-11-12 21:29:22 +02:00
|
|
|
canClose: boolean;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface PluginWindows {
|
2020-11-12 21:29:22 +02:00
|
|
|
[key: string]: any;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
export default class ElectronAppWrapper {
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private logger_: Logger = null;
|
|
|
|
private electronApp_: any;
|
|
|
|
private env_: string;
|
|
|
|
private isDebugMode_: boolean;
|
|
|
|
private profilePath_: string;
|
2023-02-22 20:15:21 +02:00
|
|
|
private win_: BrowserWindow = null;
|
2023-06-30 10:07:03 +02:00
|
|
|
private willQuitApp_ = false;
|
2020-11-12 21:13:28 +02:00
|
|
|
private tray_: any = null;
|
|
|
|
private buildDir_: string = null;
|
|
|
|
private rendererProcessQuitReply_: RendererProcessQuitReply = null;
|
|
|
|
private pluginWindows_: PluginWindows = {};
|
2021-10-16 11:07:41 +02:00
|
|
|
private initialCallbackUrl_: string = null;
|
2020-11-12 21:13:28 +02:00
|
|
|
|
2023-10-31 17:05:28 +02:00
|
|
|
public constructor(electronApp: any, env: string, profilePath: string|null, isDebugMode: boolean, initialCallbackUrl: string) {
|
2017-11-04 14:38:53 +02:00
|
|
|
this.electronApp_ = electronApp;
|
2017-11-13 20:47:35 +02:00
|
|
|
this.env_ = env;
|
2020-09-01 23:25:23 +02:00
|
|
|
this.isDebugMode_ = isDebugMode;
|
2018-04-23 21:50:29 +02:00
|
|
|
this.profilePath_ = profilePath;
|
2021-10-16 11:07:41 +02:00
|
|
|
this.initialCallbackUrl_ = initialCallbackUrl;
|
2017-11-04 15:23:15 +02:00
|
|
|
}
|
2017-11-04 14:38:53 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public electronApp() {
|
2017-11-04 15:23:15 +02:00
|
|
|
return this.electronApp_;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public setLogger(v: Logger) {
|
2017-11-04 15:23:15 +02:00
|
|
|
this.logger_ = v;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public logger() {
|
2017-11-04 15:23:15 +02:00
|
|
|
return this.logger_;
|
2017-11-04 14:38:53 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public window() {
|
2017-11-05 02:17:48 +02:00
|
|
|
return this.win_;
|
2017-11-04 21:46:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public env() {
|
2019-12-18 13:49:44 +02:00
|
|
|
return this.env_;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public initialCallbackUrl() {
|
2021-10-16 11:07:41 +02:00
|
|
|
return this.initialCallbackUrl_;
|
|
|
|
}
|
|
|
|
|
2023-10-31 17:05:28 +02:00
|
|
|
// Call when the app fails in a significant way.
|
|
|
|
//
|
|
|
|
// Assumes that the renderer process may be in an invalid state and so cannot
|
|
|
|
// be accessed.
|
|
|
|
public async handleAppFailure(errorMessage: string, canIgnore: boolean, isTesting?: boolean) {
|
2024-01-25 13:33:04 +02:00
|
|
|
await bridge().captureException(new Error(errorMessage));
|
|
|
|
|
2023-10-31 17:05:28 +02:00
|
|
|
const buttons = [];
|
|
|
|
buttons.push(_('Quit'));
|
|
|
|
const exitIndex = 0;
|
|
|
|
|
|
|
|
if (canIgnore) {
|
|
|
|
buttons.push(_('Ignore'));
|
|
|
|
}
|
|
|
|
const restartIndex = buttons.length;
|
|
|
|
buttons.push(_('Restart in safe mode'));
|
|
|
|
|
|
|
|
const { response } = await dialog.showMessageBox({
|
|
|
|
message: _('An error occurred: %s', errorMessage),
|
|
|
|
buttons,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response === restartIndex) {
|
|
|
|
await restartInSafeModeFromMain();
|
|
|
|
|
|
|
|
// A hung renderer seems to prevent the process from exiting completely.
|
|
|
|
// In this case, crashing the renderer allows the window to close.
|
|
|
|
//
|
|
|
|
// Also only run this if not testing (crashing the renderer breaks automated
|
|
|
|
// tests).
|
|
|
|
if (this.win_ && !this.win_.webContents.isCrashed() && !isTesting) {
|
|
|
|
this.win_.webContents.forcefullyCrashRenderer();
|
|
|
|
}
|
|
|
|
} else if (response === exitIndex) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public createWindow() {
|
2019-12-17 14:09:57 +02:00
|
|
|
// Set to true to view errors if the application does not start
|
2020-09-01 23:25:23 +02:00
|
|
|
const debugEarlyBugs = this.env_ === 'dev' || this.isDebugMode_;
|
2019-12-17 14:09:57 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
const windowStateKeeper = require('electron-window-state');
|
2017-11-14 20:02:58 +02:00
|
|
|
|
2020-02-27 20:38:08 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const stateOptions: any = {
|
2020-03-14 01:52:40 +02:00
|
|
|
defaultWidth: Math.round(0.8 * screen.getPrimaryDisplay().workArea.width),
|
|
|
|
defaultHeight: Math.round(0.8 * screen.getPrimaryDisplay().workArea.height),
|
2019-09-19 23:51:18 +02:00
|
|
|
file: `window-state-${this.env_}.json`,
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2018-04-23 21:50:29 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const windowOptions: any = {
|
2018-02-27 22:04:38 +02:00
|
|
|
x: windowState.x,
|
|
|
|
y: windowState.y,
|
|
|
|
width: windowState.width,
|
|
|
|
height: windowState.height,
|
2020-02-27 20:38:08 +02:00
|
|
|
minWidth: 100,
|
|
|
|
minHeight: 100,
|
2019-07-30 09:35:42 +02:00
|
|
|
backgroundColor: '#fff', // required to enable sub pixel rendering, can't be in css
|
2019-02-24 12:17:37 +02:00
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
2021-10-01 20:35:27 +02:00
|
|
|
contextIsolation: false,
|
2020-11-05 18:58:23 +02:00
|
|
|
spellcheck: true,
|
2020-11-19 23:01:19 +02:00
|
|
|
enableRemoteModule: true,
|
2019-02-24 12:17:37 +02:00
|
|
|
},
|
2019-12-17 14:09:57 +02:00
|
|
|
webviewTag: true,
|
2019-11-05 19:03:24 +02:00
|
|
|
// We start with a hidden window, which is then made visible depending on the showTrayIcon setting
|
|
|
|
// https://github.com/laurent22/joplin/issues/2031
|
2023-07-10 12:59:09 +02:00
|
|
|
//
|
|
|
|
// On Linux/GNOME, however, the window doesn't show correctly if show is false initially:
|
|
|
|
// https://github.com/laurent22/joplin/issues/8256
|
|
|
|
show: debugEarlyBugs || shim.isGNOME(),
|
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
|
2019-01-09 19:05:28 +02:00
|
|
|
if (shim.isLinux()) windowOptions.icon = path.join(__dirname, '..', 'build/icons/128x128.png');
|
2017-12-15 09:31:57 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
this.win_ = new BrowserWindow(windowOptions);
|
2017-11-04 14:38:53 +02:00
|
|
|
|
2021-10-01 20:35:27 +02:00
|
|
|
require('@electron/remote/main').enable(this.win_.webContents);
|
|
|
|
|
2020-02-27 20:38:08 +02:00
|
|
|
if (!screen.getDisplayMatching(this.win_.getBounds())) {
|
|
|
|
const { width: windowWidth, height: windowHeight } = this.win_.getBounds();
|
|
|
|
const { width: primaryDisplayWidth, height: primaryDisplayHeight } = screen.getPrimaryDisplay().workArea;
|
2020-03-14 01:52:40 +02:00
|
|
|
this.win_.setPosition(primaryDisplayWidth / 2 - windowWidth, primaryDisplayHeight / 2 - windowHeight);
|
2020-02-27 20:38:08 +02:00
|
|
|
}
|
|
|
|
|
2023-12-13 21:45:29 +02:00
|
|
|
let unresponsiveTimeout: ReturnType<typeof setTimeout>|null = null;
|
|
|
|
|
|
|
|
this.win_.webContents.on('unresponsive', () => {
|
|
|
|
// Don't show the "unresponsive" dialog immediately -- the "unresponsive" event
|
|
|
|
// can be fired when showing a dialog or modal (e.g. the update dialog).
|
|
|
|
//
|
|
|
|
// This gives us an opportunity to cancel it.
|
|
|
|
if (unresponsiveTimeout === null) {
|
|
|
|
const delayMs = 1000;
|
|
|
|
|
|
|
|
unresponsiveTimeout = setTimeout(() => {
|
|
|
|
unresponsiveTimeout = null;
|
|
|
|
void this.handleAppFailure(_('Window unresponsive.'), true);
|
|
|
|
}, delayMs);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.win_.webContents.on('responsive', () => {
|
|
|
|
if (unresponsiveTimeout !== null) {
|
|
|
|
clearTimeout(unresponsiveTimeout);
|
|
|
|
unresponsiveTimeout = null;
|
|
|
|
}
|
2023-10-31 17:05:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.win_.webContents.on('render-process-gone', async _event => {
|
|
|
|
await this.handleAppFailure('Renderer process gone.', false);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.win_.webContents.on('did-fail-load', async event => {
|
|
|
|
if ((event as any).isMainFrame) {
|
|
|
|
await this.handleAppFailure('Renderer process failed to load', false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-22 20:15:21 +02:00
|
|
|
void this.win_.loadURL(url.format({
|
2018-03-09 22:59:12 +02:00
|
|
|
pathname: path.join(__dirname, 'index.html'),
|
|
|
|
protocol: 'file:',
|
2019-07-30 09:35:42 +02:00
|
|
|
slashes: true,
|
|
|
|
}));
|
2017-11-04 14:38:53 +02:00
|
|
|
|
2019-12-17 14:09:57 +02:00
|
|
|
// Note that on Windows, calling openDevTools() too early results in a white window with no error message.
|
|
|
|
// Waiting for one of the ready events might work but they might not be triggered if there's an error, so
|
|
|
|
// the easiest is to use a timeout. Keep in mind that if you get a white window on Windows it might be due
|
|
|
|
// to this line though.
|
2020-09-12 00:33:34 +02:00
|
|
|
if (debugEarlyBugs) {
|
|
|
|
setTimeout(() => {
|
|
|
|
try {
|
|
|
|
this.win_.webContents.openDevTools();
|
|
|
|
} catch (error) {
|
|
|
|
// This will throw an exception "Object has been destroyed" if the app is closed
|
|
|
|
// in less that the timeout interval. It can be ignored.
|
|
|
|
console.warn('Error opening dev tools', error);
|
|
|
|
}
|
|
|
|
}, 3000);
|
|
|
|
}
|
2017-11-04 14:38:53 +02:00
|
|
|
|
2023-10-22 12:52:06 +02:00
|
|
|
// will-frame-navigate is fired by clicking on a link within the BrowserWindow.
|
|
|
|
this.win_.webContents.on('will-frame-navigate', event => {
|
|
|
|
// If the link changes the URL of the browser window,
|
|
|
|
if (event.isMainFrame) {
|
|
|
|
event.preventDefault();
|
|
|
|
void bridge().openExternal(event.url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
this.win_.on('close', (event: any) => {
|
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
|
2019-10-29 11:02:42 +02:00
|
|
|
// case the app must be explicitly closed with Ctrl+Q or by right-clicking on the tray icon and selecting "Exit".
|
2018-01-31 22:10:32 +02:00
|
|
|
|
2020-04-09 19:57:20 +02:00
|
|
|
let isGoingToExit = false;
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (process.platform === 'darwin') {
|
2018-01-31 22:10:32 +02:00
|
|
|
if (this.willQuitApp_) {
|
2020-04-09 19:57:20 +02:00
|
|
|
isGoingToExit = true;
|
2018-01-31 22:10:32 +02:00
|
|
|
} 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 {
|
2020-04-09 19:57:20 +02:00
|
|
|
isGoingToExit = true;
|
2018-01-31 22:10:32 +02:00
|
|
|
}
|
2017-11-17 20:05:25 +02:00
|
|
|
}
|
2020-04-09 19:57:20 +02:00
|
|
|
|
|
|
|
if (isGoingToExit) {
|
|
|
|
if (!this.rendererProcessQuitReply_) {
|
|
|
|
// If we haven't notified the renderer process yet, do it now
|
|
|
|
// so that it can tell us if we can really close the app or not.
|
|
|
|
// Search for "appClose" event for closing logic on renderer side.
|
|
|
|
event.preventDefault();
|
2023-08-04 12:00:39 +02:00
|
|
|
if (this.win_) this.win_.webContents.send('appClose');
|
2020-04-09 19:57:20 +02:00
|
|
|
} else {
|
|
|
|
// If the renderer process has responded, check if we can close or not
|
|
|
|
if (this.rendererProcessQuitReply_.canClose) {
|
|
|
|
// Really quit the app
|
|
|
|
this.rendererProcessQuitReply_ = null;
|
|
|
|
this.win_ = null;
|
|
|
|
} else {
|
|
|
|
// Wait for renderer to finish task
|
|
|
|
event.preventDefault();
|
|
|
|
this.rendererProcessQuitReply_ = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
ipcMain.on('asynchronous-message', (_event: any, message: string, args: any) => {
|
2020-04-09 19:57:20 +02:00
|
|
|
if (message === 'appCloseReply') {
|
|
|
|
// We got the response from the renderer process:
|
|
|
|
// save the response and try quit again.
|
|
|
|
this.rendererProcessQuitReply_ = args;
|
2022-04-20 18:34:58 +02:00
|
|
|
this.quit();
|
2020-04-09 19:57:20 +02:00
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|
2017-11-14 20:02:58 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
// This handler receives IPC messages from a plugin or from the main window,
|
|
|
|
// and forwards it to the main window or the plugin window.
|
2020-11-12 21:13:28 +02:00
|
|
|
ipcMain.on('pluginMessage', (_event: any, message: PluginMessage) => {
|
2021-04-11 11:58:45 +02:00
|
|
|
try {
|
|
|
|
if (message.target === 'mainWindow') {
|
|
|
|
this.win_.webContents.send('pluginMessage', message);
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2021-04-11 11:58:45 +02:00
|
|
|
if (message.target === 'plugin') {
|
|
|
|
const win = this.pluginWindows_[message.pluginId];
|
|
|
|
if (!win) {
|
|
|
|
this.logger().error(`Trying to send IPC message to non-existing plugin window: ${message.pluginId}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
win.webContents.send('pluginMessage', message);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// An error might happen when the app is closing and a plugin
|
|
|
|
// sends a message. In which case, the above code would try to
|
|
|
|
// access a destroyed webview.
|
|
|
|
// https://github.com/laurent22/joplin/issues/4570
|
|
|
|
console.error('Could not process plugin message:', message);
|
|
|
|
console.error(error);
|
2020-10-09 19:35:46 +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_);
|
2020-02-06 00:15:40 +02:00
|
|
|
|
2020-02-05 13:26:57 +02:00
|
|
|
// HACK: Ensure the window is hidden, as `windowState.manage` may make the window
|
|
|
|
// visible with isMaximized set to true in window-state-${this.env_}.json.
|
|
|
|
// https://github.com/laurent22/joplin/issues/2365
|
|
|
|
if (!windowOptions.show) {
|
|
|
|
this.win_.hide();
|
|
|
|
}
|
2017-11-04 14:38:53 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public registerPluginWindow(pluginId: string, window: any) {
|
2020-10-09 19:35:46 +02:00
|
|
|
this.pluginWindows_[pluginId] = window;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public 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
|
|
|
|
2021-10-16 11:07:41 +02:00
|
|
|
return new Promise<void>((resolve) => {
|
2017-11-04 14:38:53 +02:00
|
|
|
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);
|
2021-09-06 17:57:07 +02:00
|
|
|
resolve(null);
|
2017-11-04 14:38:53 +02:00
|
|
|
}
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public quit() {
|
2017-11-04 15:23:15 +02:00
|
|
|
this.electronApp_.quit();
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public exit(errorCode = 0) {
|
2018-03-07 21:11:55 +02:00
|
|
|
this.electronApp_.exit(errorCode);
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public trayShown() {
|
2018-01-31 22:10:32 +02:00
|
|
|
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)
|
2019-10-29 11:02:42 +02:00
|
|
|
// including the menu bar. This follows the macOS way of hiding an app.
|
2023-03-06 16:22:01 +02:00
|
|
|
public hide() {
|
2018-02-27 23:54:40 +02:00
|
|
|
this.electronApp_.hide();
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public 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`;
|
2018-03-09 22:59:12 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
private trayIconFilename_() {
|
2018-03-09 22:59:12 +02:00
|
|
|
let output = '';
|
2018-02-20 02:41:52 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
output = 'macos-16x16Template.png'; // Electron Template Image format
|
2018-02-20 02:41:52 +02:00
|
|
|
} else {
|
2018-03-09 22:59:12 +02:00
|
|
|
output = '16x16.png';
|
2018-02-20 02:41:52 +02:00
|
|
|
}
|
|
|
|
|
2019-07-30 09:35:42 +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
|
2023-03-06 16:22:01 +02:00
|
|
|
public createTray(contextMenu: any) {
|
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_()}`);
|
2020-01-26 19:32:00 +02:00
|
|
|
this.tray_.setToolTip(this.electronApp_.name);
|
2019-07-30 09:35:42 +02:00
|
|
|
this.tray_.setContextMenu(contextMenu);
|
2018-02-06 15:11:59 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.tray_.on('click', () => {
|
2024-02-08 18:51:32 +02:00
|
|
|
if (!this.window()) {
|
|
|
|
console.warn('The window object was not available during the click event from tray icon');
|
|
|
|
return;
|
|
|
|
}
|
2018-02-06 15:11:59 +02:00
|
|
|
this.window().show();
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2019-07-30 09:35:42 +02:00
|
|
|
console.error('Cannot create tray', error);
|
2018-02-06 15:11:59 +02:00
|
|
|
}
|
2018-01-31 22:10:32 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public destroyTray() {
|
2018-01-31 22:10:32 +02:00
|
|
|
if (!this.tray_) return;
|
|
|
|
this.tray_.destroy();
|
|
|
|
this.tray_ = null;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public ensureSingleInstance() {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (this.env_ === 'dev') return false;
|
2018-03-01 22:14:06 +02:00
|
|
|
|
2019-02-16 15:10:37 +02:00
|
|
|
const gotTheLock = this.electronApp_.requestSingleInstanceLock();
|
2018-02-23 19:51:23 +02:00
|
|
|
|
2019-02-16 15:10:37 +02:00
|
|
|
if (!gotTheLock) {
|
|
|
|
// Another instance is already running - exit
|
2022-04-20 18:34:58 +02:00
|
|
|
this.quit();
|
2019-02-16 15:10:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-02-23 19:51:23 +02:00
|
|
|
|
2019-02-16 15:10:37 +02:00
|
|
|
// Someone tried to open a second instance - focus our window instead
|
2021-10-16 11:07:41 +02:00
|
|
|
this.electronApp_.on('second-instance', (_e: any, argv: string[]) => {
|
2019-02-16 15:10:37 +02:00
|
|
|
const win = this.window();
|
|
|
|
if (!win) return;
|
|
|
|
if (win.isMinimized()) win.restore();
|
|
|
|
win.show();
|
|
|
|
win.focus();
|
2021-10-16 11:07:41 +02:00
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
const url = argv.find((arg) => isCallbackUrl(arg));
|
|
|
|
if (url) {
|
|
|
|
void this.openCallbackUrl(url);
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 19:51:23 +02:00
|
|
|
});
|
2019-02-16 15:10:37 +02:00
|
|
|
|
|
|
|
return false;
|
2018-02-23 19:51:23 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async start() {
|
2017-11-04 14:38:53 +02:00
|
|
|
// 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();
|
|
|
|
|
2019-02-16 15:10:37 +02:00
|
|
|
const alreadyRunning = this.ensureSingleInstance();
|
2019-07-30 09:35:42 +02:00
|
|
|
if (alreadyRunning) return;
|
2018-02-23 19:51:23 +02:00
|
|
|
|
2017-11-04 14:38:53 +02:00
|
|
|
this.createWindow();
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.electronApp_.on('before-quit', () => {
|
2017-11-17 20:05:25 +02:00
|
|
|
this.willQuitApp_ = true;
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|
2017-11-17 20:05:25 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.electronApp_.on('window-all-closed', () => {
|
2022-04-20 18:34:58 +02:00
|
|
|
this.quit();
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|
2017-11-04 14:38:53 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.electronApp_.on('activate', () => {
|
2017-11-17 20:05:25 +02:00
|
|
|
this.win_.show();
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|
2021-10-16 11:07:41 +02:00
|
|
|
|
|
|
|
this.electronApp_.on('open-url', (event: any, url: string) => {
|
|
|
|
event.preventDefault();
|
|
|
|
void this.openCallbackUrl(url);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async openCallbackUrl(url: string) {
|
2021-10-16 11:07:41 +02:00
|
|
|
this.win_.webContents.send('asynchronous-message', 'openCallbackUrl', {
|
|
|
|
url: url,
|
|
|
|
});
|
2017-11-04 14:38:53 +02:00
|
|
|
}
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2017-11-04 14:38:53 +02:00
|
|
|
}
|