2020-10-09 19:35:46 +02:00
|
|
|
import ElectronAppWrapper from './ElectronAppWrapper';
|
2020-11-05 18:58:23 +02:00
|
|
|
import shim from '@joplinapp/lib/shim';
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-05 18:58:23 +02:00
|
|
|
import { _, setLocale } from '@joplinapp/lib/locale';
|
|
|
|
const { dirname, toSystemSlashes } = require('@joplinapp/lib/path-utils');
|
2020-05-21 01:47:38 +02:00
|
|
|
const { BrowserWindow, nativeTheme } = require('electron');
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
interface LastSelectedPath {
|
|
|
|
file: string,
|
|
|
|
directory: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface LastSelectedPaths {
|
|
|
|
[key:string]: LastSelectedPath,
|
|
|
|
}
|
2017-11-05 02:17:48 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
export class Bridge {
|
|
|
|
|
|
|
|
private electronWrapper_:ElectronAppWrapper;
|
|
|
|
private lastSelectedPaths_:LastSelectedPaths;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-11 14:00:37 +02:00
|
|
|
electronApp() {
|
|
|
|
return this.electronWrapper_;
|
|
|
|
}
|
|
|
|
|
2019-12-18 13:49:44 +02:00
|
|
|
env() {
|
|
|
|
return this.electronWrapper_.env();
|
|
|
|
}
|
|
|
|
|
2017-11-05 02:17:48 +02:00
|
|
|
processArgv() {
|
|
|
|
return process.argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
window() {
|
|
|
|
return this.electronWrapper_.window();
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
newBrowserWindow(options:any) {
|
2019-12-17 11:44:48 +02:00
|
|
|
return new BrowserWindow(options);
|
|
|
|
}
|
|
|
|
|
2017-11-05 02:17:48 +02:00
|
|
|
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-05 02:17:48 +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] };
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
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;
|
2019-12-17 13:08:55 +02:00
|
|
|
const filePath = dialog.showSaveDialogSync(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;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
showOpenDialog(options:any) {
|
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';
|
|
|
|
if (!('defaultPath' in options) && this.lastSelectedPaths_[fileType]) options.defaultPath = this.lastSelectedPaths_[fileType];
|
2017-12-07 23:18:18 +02:00
|
|
|
if (!('createDirectory' in options)) options.createDirectory = true;
|
2019-12-17 13:08:55 +02:00
|
|
|
const filePaths = dialog.showOpenDialogSync(this.window(), options);
|
2017-12-02 01:26:08 +02:00
|
|
|
if (filePaths && filePaths.length) {
|
2020-04-15 01:05:57 +02:00
|
|
|
this.lastSelectedPaths_[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
|
2020-10-09 19:35:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
showConfirmMessageBox(message:string, options:any = null) {
|
2019-05-11 14:36:44 +02:00
|
|
|
if (options === null) options = {};
|
|
|
|
|
|
|
|
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,
|
2017-11-08 19:51:55 +02:00
|
|
|
buttons: [_('OK'), _('Cancel')],
|
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 */
|
2020-10-09 19:35:46 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
setLocale(locale:string) {
|
2018-05-20 13:54:42 +02:00
|
|
|
setLocale(locale);
|
|
|
|
}
|
|
|
|
|
2017-11-08 19:51:55 +02:00
|
|
|
get Menu() {
|
|
|
|
return require('electron').Menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
get MenuItem() {
|
|
|
|
return require('electron').MenuItem;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
openItem(fullPath:string) {
|
2019-07-30 09:35:42 +02:00
|
|
|
return require('electron').shell.openItem(fullPath);
|
2017-11-12 18:33:34 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
checkForUpdates(inBackground:boolean, window:any, logFilePath:string, options:any) {
|
2018-01-31 00:35:50 +02:00
|
|
|
const { checkForUpdates } = require('./checkForUpdates.js');
|
2019-01-12 01:40:05 +02:00
|
|
|
checkForUpdates(inBackground, window, logFilePath, options);
|
2018-01-31 00:35:50 +02:00
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2019-09-11 01:53:01 +02:00
|
|
|
buildDir() {
|
|
|
|
return this.electronApp().buildDir();
|
|
|
|
}
|
|
|
|
|
2019-12-30 16:10:43 +02:00
|
|
|
screen() {
|
|
|
|
return require('electron').screen;
|
|
|
|
}
|
|
|
|
|
2020-05-21 01:47:38 +02:00
|
|
|
shouldUseDarkColors() {
|
|
|
|
return nativeTheme.shouldUseDarkColors;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
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}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
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);
|
2020-09-01 21:55:12 +02:00
|
|
|
} else if (shim.isLinux()) {
|
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();
|
|
|
|
}
|
|
|
|
|
2017-11-05 02:17:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
let bridge_:Bridge = null;
|
2017-11-05 02:17:48 +02:00
|
|
|
|
2020-10-09 19:35:46 +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
|
|
|
}
|