1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/ElectronClient/bridge.js

180 lines
4.1 KiB
JavaScript
Raw Normal View History

const { _, setLocale } = require('lib/locale.js');
const { dirname } = require('lib/path-utils.js');
const { BrowserWindow } = require('electron');
2017-11-08 19:51:55 +02:00
class Bridge {
constructor(electronWrapper) {
this.electronWrapper_ = electronWrapper;
this.autoUpdateLogger_ = null;
this.lastSelectedPath_ = null;
}
2017-11-11 14:00:37 +02:00
electronApp() {
return this.electronWrapper_;
}
env() {
return this.electronWrapper_.env();
}
processArgv() {
return process.argv;
}
window() {
return this.electronWrapper_.window();
}
newBrowserWindow(options) {
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, height) {
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();
}
showSaveDialog(options) {
const { dialog } = require('electron');
if (!options) options = {};
if (!('defaultPath' in options) && this.lastSelectedPath_) options.defaultPath = this.lastSelectedPath_;
2019-12-17 13:08:55 +02:00
const filePath = dialog.showSaveDialogSync(this.window(), options);
if (filePath) {
this.lastSelectedPath_ = filePath;
}
return filePath;
}
2017-11-11 00:18:00 +02:00
showOpenDialog(options) {
const { dialog } = require('electron');
if (!options) options = {};
if (!('defaultPath' in options) && this.lastSelectedPath_) options.defaultPath = this.lastSelectedPath_;
if (!('createDirectory' in options)) options.createDirectory = true;
2019-12-17 13:08:55 +02:00
const filePaths = dialog.showOpenDialogSync(this.window(), options);
if (filePaths && filePaths.length) {
this.lastSelectedPath_ = 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, options) {
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-08 19:51:55 +02:00
showErrorMessageBox(message) {
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, options = null) {
if (options === null) options = {};
const result = this.showMessageBox_(this.window(), Object.assign({}, {
2017-11-08 19:51:55 +02:00
type: 'question',
message: message,
cancelId: 1,
2017-11-08 19:51:55 +02:00
buttons: [_('OK'), _('Cancel')],
}, options));
2017-11-08 19:51:55 +02:00
return result === 0;
}
/* returns the index of the clicked button */
showMessageBox(message, options = null) {
if (options === null) options = {};
const result = this.showMessageBox_(this.window(), Object.assign({}, {
type: 'question',
message: message,
buttons: [_('OK'), _('Cancel')],
}, options));
return result;
}
2018-03-02 20:24:02 +02:00
showInfoMessageBox(message, options = {}) {
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) {
setLocale(locale);
}
2017-11-08 19:51:55 +02:00
get Menu() {
return require('electron').Menu;
}
get MenuItem() {
return require('electron').MenuItem;
}
2017-11-11 14:00:37 +02:00
openExternal(url) {
return require('electron').shell.openExternal(url);
2017-11-11 14:00:37 +02:00
}
openItem(fullPath) {
return require('electron').shell.openItem(fullPath);
}
checkForUpdates(inBackground, window, logFilePath, options) {
const { checkForUpdates } = require('./checkForUpdates.js');
checkForUpdates(inBackground, window, logFilePath, options);
}
buildDir() {
return this.electronApp().buildDir();
}
2019-12-30 16:10:43 +02:00
screen() {
return require('electron').screen;
}
}
let bridge_ = null;
function initBridge(wrapper) {
if (bridge_) throw new Error('Bridge already initialized');
bridge_ = new Bridge(wrapper);
return bridge_;
}
function bridge() {
if (!bridge_) throw new Error('Bridge not initialized');
return bridge_;
}
module.exports = { bridge, initBridge };