1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00
joplin/packages/app-desktop/bridge.ts

280 lines
7.4 KiB
TypeScript
Raw Normal View History

import ElectronAppWrapper from './ElectronAppWrapper';
import shim from '@joplin/lib/shim';
import { _, setLocale } from '@joplin/lib/locale';
const { dirname, toSystemSlashes } = require('@joplin/lib/path-utils');
const { BrowserWindow, nativeTheme } = require('electron');
2017-11-08 19:51:55 +02:00
interface LastSelectedPath {
file: string;
directory: string;
}
interface LastSelectedPaths {
[key: string]: LastSelectedPath;
}
export class Bridge {
private electronWrapper_: ElectronAppWrapper;
private lastSelectedPaths_: LastSelectedPaths;
constructor(electronWrapper: ElectronAppWrapper) {
this.electronWrapper_ = electronWrapper;
this.lastSelectedPaths_ = {
file: null,
directory: null,
};
}
2017-11-11 14:00:37 +02:00
electronApp() {
return this.electronWrapper_;
}
electronIsDev() {
return !this.electronApp().electronApp().isPackaged;
}
env() {
return this.electronWrapper_.env();
}
processArgv() {
return process.argv;
}
// 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;
// },
});
}
window() {
return this.electronWrapper_.window();
}
showItemInFolder(fullPath: string) {
return require('electron').shell.showItemInFolder(toSystemSlashes(fullPath));
}
newBrowserWindow(options: any) {
return new BrowserWindow(options);
}
windowContentSize() {
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-14 20:02:58 +02:00
windowSize() {
if (!this.window()) return { width: 0, height: 0 };
const s = this.window().getSize();
return { width: s[0], height: s[1] };
}
windowSetSize(width: number, height: number) {
2017-11-14 20:02:58 +02:00
if (!this.window()) return;
return this.window().setSize(width, height);
}
2019-12-17 19:06:55 +02:00
openDevTools() {
return this.window().webContents.openDevTools();
}
closeDevTools() {
return this.window().webContents.closeDevTools();
}
async showSaveDialog(options: any) {
const { dialog } = require('electron');
if (!options) options = {};
if (!('defaultPath' in options) && this.lastSelectedPaths_.file) options.defaultPath = this.lastSelectedPaths_.file;
const { filePath } = await dialog.showSaveDialog(this.window(), options);
if (filePath) {
this.lastSelectedPaths_.file = filePath;
}
return filePath;
}
async showOpenDialog(options: any = null) {
const { dialog } = require('electron');
if (!options) options = {};
let fileType = 'file';
if (options.properties && options.properties.includes('openDirectory')) fileType = 'directory';
if (!('defaultPath' in options) && this.lastSelectedPaths_[fileType]) options.defaultPath = this.lastSelectedPaths_[fileType];
if (!('createDirectory' in options)) options.createDirectory = true;
const { filePaths } = await dialog.showOpenDialog(this.window(), options);
if (filePaths && filePaths.length) {
this.lastSelectedPaths_[fileType] = dirname(filePaths[0]);
}
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
showMessageBox_(window: any, options: any): number {
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);
}
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,
buttons: [_('OK')],
2017-11-08 19:51:55 +02:00
});
}
showConfirmMessageBox(message: string, options: any = null) {
options = {
buttons: [_('OK'), _('Cancel')],
...options,
};
const result = this.showMessageBox_(this.window(), Object.assign({}, {
2017-11-08 19:51:55 +02:00
type: 'question',
message: message,
cancelId: 1,
buttons: options.buttons,
}, options));
2017-11-08 19:51:55 +02:00
return result === 0;
}
/* returns the index of the clicked button */
showMessageBox(message: string, options: any = null) {
if (options === null) options = {};
const result = this.showMessageBox_(this.window(), Object.assign({}, {
type: 'question',
message: message,
buttons: [_('OK'), _('Cancel')],
}, options));
return result;
}
showInfoMessageBox(message: string, options: any = {}) {
2018-03-02 20:24:02 +02:00
const result = this.showMessageBox_(this.window(), Object.assign({}, {
type: 'info',
message: message,
buttons: [_('OK')],
2018-03-02 20:24:02 +02:00
}, options));
return result === 0;
}
setLocale(locale: string) {
setLocale(locale);
}
2017-11-08 19:51:55 +02:00
get Menu() {
return require('electron').Menu;
}
get MenuItem() {
return require('electron').MenuItem;
}
openExternal(url: string) {
return require('electron').shell.openExternal(url);
2017-11-11 14:00:37 +02:00
}
2020-11-19 23:01:19 +02:00
async openItem(fullPath: string) {
return require('electron').shell.openPath(fullPath);
}
buildDir() {
return this.electronApp().buildDir();
}
2019-12-30 16:10:43 +02:00
screen() {
return require('electron').screen;
}
shouldUseDarkColors() {
return nativeTheme.shouldUseDarkColors;
}
addEventListener(name: string, fn: Function) {
if (name === 'nativeThemeUpdated') {
nativeTheme.on('updated', fn);
} else {
throw new Error(`Unsupported event: ${name}`);
}
}
restart() {
// 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);
} else if (shim.isLinux()) {
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();
}
app.exit();
}
}
let bridge_: Bridge = null;
export function initBridge(wrapper: ElectronAppWrapper) {
if (bridge_) throw new Error('Bridge already initialized');
bridge_ = new Bridge(wrapper);
return bridge_;
}
export default function bridge() {
if (!bridge_) throw new Error('Bridge not initialized');
return bridge_;
}