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';
|
2020-11-07 17:59:37 +02:00
|
|
|
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;
|
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,
|
|
|
|
};
|
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;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
public setupContextMenu(_spellCheckerMenuItemsHandler: Function) {
|
|
|
|
require('electron-context-menu')({
|
|
|
|
allWindows: [this.window()],
|
|
|
|
|
|
|
|
electronApp: this.electronApp(),
|
|
|
|
|
|
|
|
shouldShowMenu: (_event: any, params: any) => {
|
|
|
|
// 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';
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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-03-06 16:22:01 +02:00
|
|
|
public showErrorMessageBox(message: string) {
|
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,
|
2020-02-05 23:50:05 +02:00
|
|
|
buttons: [_('OK')],
|
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
|
|
|
|
|
|
|
const result = this.showMessageBox_(this.window(), Object.assign({}, {
|
2017-11-08 19:51:55 +02:00
|
|
|
type: 'question',
|
|
|
|
message: message,
|
2018-06-22 20:31:55 +02:00
|
|
|
cancelId: 1,
|
2020-11-19 14:34:49 +02:00
|
|
|
buttons: options.buttons,
|
2019-05-11 14:36:44 +02:00
|
|
|
}, options));
|
|
|
|
|
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 = {};
|
|
|
|
|
|
|
|
const result = this.showMessageBox_(this.window(), Object.assign({}, {
|
|
|
|
type: 'question',
|
|
|
|
message: message,
|
|
|
|
buttons: [_('OK'), _('Cancel')],
|
|
|
|
}, options));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public showInfoMessageBox(message: string, options: any = {}) {
|
2018-03-02 20:24:02 +02:00
|
|
|
const result = this.showMessageBox_(this.window(), Object.assign({}, {
|
2017-12-07 23:18:18 +02:00
|
|
|
type: 'info',
|
|
|
|
message: message,
|
|
|
|
buttons: [_('OK')],
|
2018-03-02 20:24:02 +02:00
|
|
|
}, 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-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
|
|
|
}
|