2020-10-09 19:35:46 +02:00
|
|
|
import ElectronAppWrapper from './ElectronAppWrapper';
|
2020-11-07 17:59:37 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
|
|
|
import { _, setLocale } from '@joplin/lib/locale';
|
2021-12-28 13:00:40 +02:00
|
|
|
import { BrowserWindow, nativeTheme, nativeImage } from 'electron';
|
2024-01-18 23:45:25 +02:00
|
|
|
import * as Sentry from '@sentry/electron/main';
|
2024-01-25 13:33:04 +02:00
|
|
|
import { writeFileSync } from 'fs';
|
|
|
|
import { homedir } from 'os';
|
|
|
|
import { msleep } from '@joplin/utils/time';
|
|
|
|
const { dirname, toSystemSlashes } = require('@joplin/lib/path-utils');
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
interface LastSelectedPath {
|
2020-11-12 21:29:22 +02:00
|
|
|
file: string;
|
|
|
|
directory: string;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 18:42:00 +02:00
|
|
|
interface OpenDialogOptions {
|
|
|
|
properties?: string[];
|
|
|
|
defaultPath?: string;
|
|
|
|
createDirectory?: boolean;
|
|
|
|
filters?: any[];
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
export class Bridge {
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private electronWrapper_: ElectronAppWrapper;
|
2021-12-28 13:00:40 +02:00
|
|
|
private lastSelectedPaths_: LastSelectedPath;
|
2024-01-18 23:45:25 +02:00
|
|
|
private autoUploadCrashDumps_ = false;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public constructor(electronWrapper: ElectronAppWrapper) {
|
2017-11-05 02:17:48 +02:00
|
|
|
this.electronWrapper_ = electronWrapper;
|
2020-04-15 01:05:57 +02:00
|
|
|
this.lastSelectedPaths_ = {
|
|
|
|
file: null,
|
|
|
|
directory: null,
|
|
|
|
};
|
2024-01-18 23:45:25 +02:00
|
|
|
|
|
|
|
Sentry.init({
|
|
|
|
dsn: 'https://cceec550871b1e8a10fee4c7a28d5cf2@o4506576757522432.ingest.sentry.io/4506594281783296',
|
2024-01-25 13:33:04 +02:00
|
|
|
beforeSend: event => {
|
|
|
|
try {
|
|
|
|
const date = (new Date()).toISOString().replace(/[:-]/g, '').split('.')[0];
|
|
|
|
writeFileSync(`${homedir()}/joplin_crash_dump_${date}.json`, JSON.stringify(event, null, '\t'), 'utf-8');
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore the error since we can't handle it here
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.autoUploadCrashDumps_) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
},
|
2024-01-18 23:45:25 +02:00
|
|
|
});
|
2017-11-05 02:17:48 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public electronApp() {
|
2017-11-11 14:00:37 +02:00
|
|
|
return this.electronWrapper_;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public electronIsDev() {
|
2021-10-01 20:35:27 +02:00
|
|
|
return !this.electronApp().electronApp().isPackaged;
|
|
|
|
}
|
|
|
|
|
2024-01-25 13:33:04 +02:00
|
|
|
public async captureException(error: any) {
|
|
|
|
Sentry.captureException(error);
|
|
|
|
// We wait to give the "beforeSend" event handler time to process the crash dump and write
|
|
|
|
// it to file.
|
|
|
|
await msleep(10);
|
2024-01-18 23:45:25 +02:00
|
|
|
}
|
|
|
|
|
2024-01-25 13:33:04 +02:00
|
|
|
public setAutoUploadCrashDumps(v: boolean) {
|
2024-01-18 23:45:25 +02:00
|
|
|
this.autoUploadCrashDumps_ = v;
|
|
|
|
}
|
|
|
|
|
2021-12-23 13:04:09 +02:00
|
|
|
// The build directory contains additional external files that are going to
|
|
|
|
// be packaged by Electron Builder. This is for files that need to be
|
|
|
|
// accessed outside of the Electron app (for example the application icon).
|
|
|
|
//
|
|
|
|
// Any static file that's accessed from within the app such as CSS or fonts
|
|
|
|
// should go in /vendor.
|
|
|
|
//
|
|
|
|
// The build folder location is dynamic, depending on whether we're running
|
|
|
|
// in dev or prod, which makes it hard to access it from static files (for
|
|
|
|
// example from plain HTML files that load CSS or JS files). For this reason
|
|
|
|
// it should be avoided as much as possible.
|
|
|
|
public buildDir() {
|
|
|
|
return this.electronApp().buildDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The vendor directory and its content is dynamically created from other
|
|
|
|
// dir (usually by pulling files from node_modules). It can also be accessed
|
|
|
|
// using a relative path such as "../../vendor/lib/file.js" because it will
|
|
|
|
// be at the same location in both prod and dev mode (unlike the build dir).
|
|
|
|
public vendorDir() {
|
|
|
|
return `${__dirname}/vendor`;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public env() {
|
2019-12-18 13:49:44 +02:00
|
|
|
return this.electronWrapper_.env();
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public processArgv() {
|
2017-11-05 02:17:48 +02:00
|
|
|
return process.argv;
|
|
|
|
}
|
|
|
|
|
2023-05-10 12:05:55 +02:00
|
|
|
public getLocale = () => {
|
|
|
|
return this.electronApp().electronApp().getLocale();
|
|
|
|
};
|
|
|
|
|
2021-10-01 20:35:27 +02:00
|
|
|
// Applies to electron-context-menu@3:
|
|
|
|
//
|
|
|
|
// For now we have to disable spell checking in non-editor text
|
|
|
|
// areas (such as the note title) because the context menu lives in
|
|
|
|
// the main process, and the spell checker service is in the
|
|
|
|
// renderer process. To get the word suggestions, we need to call
|
|
|
|
// the spellchecker service but that can only be done in an async
|
|
|
|
// way, and the menu is built synchronously.
|
|
|
|
//
|
|
|
|
// Moving the spellchecker to the main process would be hard because
|
|
|
|
// it depends on models and various other classes which are all in
|
|
|
|
// the renderer process.
|
|
|
|
//
|
|
|
|
// Perhaps the easiest would be to patch electron-context-menu to
|
|
|
|
// support the renderer process again. Or possibly revert to an old
|
|
|
|
// version of electron-context-menu.
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2021-10-01 20:35:27 +02:00
|
|
|
public setupContextMenu(_spellCheckerMenuItemsHandler: Function) {
|
|
|
|
require('electron-context-menu')({
|
|
|
|
allWindows: [this.window()],
|
|
|
|
|
|
|
|
electronApp: this.electronApp(),
|
|
|
|
|
|
|
|
shouldShowMenu: (_event: any, params: any) => {
|
2023-07-26 19:07:00 +02:00
|
|
|
return params.isEditable;
|
2021-10-01 20:35:27 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// menu: (actions: any, props: any) => {
|
|
|
|
// const items = spellCheckerMenuItemsHandler(props.misspelledWord, props.dictionarySuggestions);
|
|
|
|
// const spellCheckerMenuItems = items.map((item: any) => new MenuItem(item)); //SpellCheckerService.instance().contextMenuItems(props.misspelledWord, props.dictionarySuggestions).map((item: any) => new MenuItem(item));
|
|
|
|
|
|
|
|
// const output = [
|
|
|
|
// actions.cut(),
|
|
|
|
// actions.copy(),
|
|
|
|
// actions.paste(),
|
|
|
|
// ...spellCheckerMenuItems,
|
|
|
|
// ];
|
|
|
|
|
|
|
|
// return output;
|
|
|
|
// },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public window() {
|
2017-11-05 02:17:48 +02:00
|
|
|
return this.electronWrapper_.window();
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public showItemInFolder(fullPath: string) {
|
2020-07-22 18:24:01 +02:00
|
|
|
return require('electron').shell.showItemInFolder(toSystemSlashes(fullPath));
|
2020-05-09 12:10:47 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public newBrowserWindow(options: any) {
|
2019-12-17 11:44:48 +02:00
|
|
|
return new BrowserWindow(options);
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public windowContentSize() {
|
2017-11-05 02:17:48 +02:00
|
|
|
if (!this.window()) return { width: 0, height: 0 };
|
|
|
|
const s = this.window().getContentSize();
|
|
|
|
return { width: s[0], height: s[1] };
|
2018-03-02 20:24:02 +02:00
|
|
|
}
|
2017-11-05 02:17:48 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public windowSize() {
|
2017-11-14 20:02:58 +02:00
|
|
|
if (!this.window()) return { width: 0, height: 0 };
|
|
|
|
const s = this.window().getSize();
|
|
|
|
return { width: s[0], height: s[1] };
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public windowSetSize(width: number, height: number) {
|
2017-11-14 20:02:58 +02:00
|
|
|
if (!this.window()) return;
|
|
|
|
return this.window().setSize(width, height);
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public openDevTools() {
|
2019-12-17 19:06:55 +02:00
|
|
|
return this.window().webContents.openDevTools();
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public closeDevTools() {
|
2019-12-17 19:06:55 +02:00
|
|
|
return this.window().webContents.closeDevTools();
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async showSaveDialog(options: any) {
|
2020-02-05 00:09:34 +02:00
|
|
|
const { dialog } = require('electron');
|
2017-12-07 23:18:18 +02:00
|
|
|
if (!options) options = {};
|
2020-04-15 01:05:57 +02:00
|
|
|
if (!('defaultPath' in options) && this.lastSelectedPaths_.file) options.defaultPath = this.lastSelectedPaths_.file;
|
2021-11-01 09:38:06 +02:00
|
|
|
const { filePath } = await dialog.showSaveDialog(this.window(), options);
|
2017-12-07 23:18:18 +02:00
|
|
|
if (filePath) {
|
2020-04-15 01:05:57 +02:00
|
|
|
this.lastSelectedPaths_.file = filePath;
|
2017-12-07 23:18:18 +02:00
|
|
|
}
|
|
|
|
return filePath;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async showOpenDialog(options: OpenDialogOptions = null) {
|
2020-02-05 00:09:34 +02:00
|
|
|
const { dialog } = require('electron');
|
2017-12-02 01:26:08 +02:00
|
|
|
if (!options) options = {};
|
2020-04-15 01:05:57 +02:00
|
|
|
let fileType = 'file';
|
|
|
|
if (options.properties && options.properties.includes('openDirectory')) fileType = 'directory';
|
2021-12-28 13:00:40 +02:00
|
|
|
if (!('defaultPath' in options) && (this.lastSelectedPaths_ as any)[fileType]) options.defaultPath = (this.lastSelectedPaths_ as any)[fileType];
|
2017-12-07 23:18:18 +02:00
|
|
|
if (!('createDirectory' in options)) options.createDirectory = true;
|
2022-02-06 18:42:00 +02:00
|
|
|
const { filePaths } = await dialog.showOpenDialog(this.window(), options as any);
|
2017-12-02 01:26:08 +02:00
|
|
|
if (filePaths && filePaths.length) {
|
2021-12-28 13:00:40 +02:00
|
|
|
(this.lastSelectedPaths_ as any)[fileType] = dirname(filePaths[0]);
|
2017-12-02 01:26:08 +02:00
|
|
|
}
|
|
|
|
return filePaths;
|
2017-11-11 00:18:00 +02:00
|
|
|
}
|
|
|
|
|
2018-03-02 20:24:02 +02:00
|
|
|
// Don't use this directly - call one of the showXxxxxxxMessageBox() instead
|
2023-03-06 16:22:01 +02:00
|
|
|
private showMessageBox_(window: any, options: any): number {
|
2020-02-05 00:09:34 +02:00
|
|
|
const { dialog } = require('electron');
|
2018-03-02 20:24:02 +02:00
|
|
|
if (!window) window = this.window();
|
2019-12-17 13:08:55 +02:00
|
|
|
return dialog.showMessageBoxSync(window, options);
|
2017-11-06 20:35:04 +02:00
|
|
|
}
|
|
|
|
|
2023-10-24 13:20:54 +02:00
|
|
|
public showErrorMessageBox(message: string, options: any = null) {
|
|
|
|
options = {
|
|
|
|
buttons: [_('OK')],
|
|
|
|
...options,
|
|
|
|
};
|
|
|
|
|
2018-03-02 20:24:02 +02:00
|
|
|
return this.showMessageBox_(this.window(), {
|
2017-11-08 19:51:55 +02:00
|
|
|
type: 'error',
|
|
|
|
message: message,
|
2023-10-24 13:20:54 +02:00
|
|
|
buttons: options.buttons,
|
2017-11-08 19:51:55 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public showConfirmMessageBox(message: string, options: any = null) {
|
2020-11-19 14:34:49 +02:00
|
|
|
options = {
|
|
|
|
buttons: [_('OK'), _('Cancel')],
|
|
|
|
...options,
|
|
|
|
};
|
2019-05-11 14:36:44 +02:00
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
const result = this.showMessageBox_(this.window(), { type: 'question',
|
2017-11-08 19:51:55 +02:00
|
|
|
message: message,
|
2018-06-22 20:31:55 +02:00
|
|
|
cancelId: 1,
|
2023-06-01 13:02:36 +02:00
|
|
|
buttons: options.buttons, ...options });
|
2019-05-11 14:36:44 +02:00
|
|
|
|
2017-11-08 19:51:55 +02:00
|
|
|
return result === 0;
|
|
|
|
}
|
|
|
|
|
2020-03-13 01:13:18 +02:00
|
|
|
/* returns the index of the clicked button */
|
2023-03-06 16:22:01 +02:00
|
|
|
public showMessageBox(message: string, options: any = null) {
|
2020-03-13 01:13:18 +02:00
|
|
|
if (options === null) options = {};
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
const result = this.showMessageBox_(this.window(), { type: 'question',
|
2020-03-13 01:13:18 +02:00
|
|
|
message: message,
|
2023-06-01 13:02:36 +02:00
|
|
|
buttons: [_('OK'), _('Cancel')], ...options });
|
2020-03-13 01:13:18 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public showInfoMessageBox(message: string, options: any = {}) {
|
2023-06-01 13:02:36 +02:00
|
|
|
const result = this.showMessageBox_(this.window(), { type: 'info',
|
2017-12-07 23:18:18 +02:00
|
|
|
message: message,
|
2023-06-01 13:02:36 +02:00
|
|
|
buttons: [_('OK')], ...options });
|
2017-12-07 23:18:18 +02:00
|
|
|
return result === 0;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public setLocale(locale: string) {
|
2018-05-20 13:54:42 +02:00
|
|
|
setLocale(locale);
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public get Menu() {
|
2017-11-08 19:51:55 +02:00
|
|
|
return require('electron').Menu;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public get MenuItem() {
|
2017-11-08 19:51:55 +02:00
|
|
|
return require('electron').MenuItem;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public openExternal(url: string) {
|
2019-07-30 09:35:42 +02:00
|
|
|
return require('electron').shell.openExternal(url);
|
2017-11-11 14:00:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async openItem(fullPath: string) {
|
2022-09-17 21:19:12 +02:00
|
|
|
return require('electron').shell.openPath(toSystemSlashes(fullPath));
|
2017-11-12 18:33:34 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public screen() {
|
2019-12-30 16:10:43 +02:00
|
|
|
return require('electron').screen;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public shouldUseDarkColors() {
|
2020-05-21 01:47:38 +02:00
|
|
|
return nativeTheme.shouldUseDarkColors;
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2023-03-06 16:22:01 +02:00
|
|
|
public addEventListener(name: string, fn: Function) {
|
2020-05-21 01:47:38 +02:00
|
|
|
if (name === 'nativeThemeUpdated') {
|
|
|
|
nativeTheme.on('updated', fn);
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unsupported event: ${name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public restart(linuxSafeRestart = true) {
|
2020-08-02 13:28:50 +02:00
|
|
|
// Note that in this case we are not sending the "appClose" event
|
|
|
|
// to notify services and component that the app is about to close
|
|
|
|
// but for the current use-case it's not really needed.
|
|
|
|
const { app } = require('electron');
|
2020-08-19 00:51:23 +02:00
|
|
|
|
|
|
|
if (shim.isPortable()) {
|
|
|
|
const options = {
|
|
|
|
execPath: process.env.PORTABLE_EXECUTABLE_FILE,
|
|
|
|
};
|
|
|
|
app.relaunch(options);
|
2022-04-17 13:26:58 +02:00
|
|
|
} else if (shim.isLinux() && linuxSafeRestart) {
|
2020-08-29 12:27:13 +02:00
|
|
|
this.showInfoMessageBox(_('The app is now going to close. Please relaunch it to complete the process.'));
|
2020-08-19 00:51:23 +02:00
|
|
|
} else {
|
|
|
|
app.relaunch();
|
|
|
|
}
|
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
app.exit();
|
|
|
|
}
|
|
|
|
|
2021-12-28 13:00:40 +02:00
|
|
|
public createImageFromPath(path: string) {
|
|
|
|
return nativeImage.createFromPath(path);
|
|
|
|
}
|
|
|
|
|
2017-11-05 02:17:48 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
let bridge_: Bridge = null;
|
2017-11-05 02:17:48 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export function initBridge(wrapper: ElectronAppWrapper) {
|
2017-11-05 02:17:48 +02:00
|
|
|
if (bridge_) throw new Error('Bridge already initialized');
|
|
|
|
bridge_ = new Bridge(wrapper);
|
|
|
|
return bridge_;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
export default function bridge() {
|
2017-11-05 02:17:48 +02:00
|
|
|
if (!bridge_) throw new Error('Bridge not initialized');
|
|
|
|
return bridge_;
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|