2017-11-08 19:51:55 +02:00
|
|
|
const { _ } = require('lib/locale.js');
|
2017-12-02 01:26:08 +02:00
|
|
|
const { dirname } = require('lib/path-utils.js');
|
2017-11-14 12:53:18 +02:00
|
|
|
const { Logger } = require('lib/logger.js');
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2017-11-05 02:17:48 +02:00
|
|
|
class Bridge {
|
|
|
|
|
|
|
|
constructor(electronWrapper) {
|
|
|
|
this.electronWrapper_ = electronWrapper;
|
2017-11-21 20:59:32 +02:00
|
|
|
this.autoUpdateLogger_ = null;
|
2017-12-02 01:26:08 +02:00
|
|
|
this.lastSelectedPath_ = null;
|
2017-11-05 02:17:48 +02:00
|
|
|
}
|
|
|
|
|
2017-11-11 14:00:37 +02:00
|
|
|
electronApp() {
|
|
|
|
return this.electronWrapper_;
|
|
|
|
}
|
|
|
|
|
2017-11-05 02:17:48 +02:00
|
|
|
processArgv() {
|
|
|
|
return process.argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
window() {
|
|
|
|
return this.electronWrapper_.window();
|
|
|
|
}
|
|
|
|
|
|
|
|
windowContentSize() {
|
|
|
|
if (!this.window()) return { width: 0, height: 0 };
|
|
|
|
const s = this.window().getContentSize();
|
|
|
|
return { width: s[0], height: s[1] };
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-07 23:18:18 +02:00
|
|
|
showSaveDialog(options) {
|
|
|
|
const {dialog} = require('electron');
|
|
|
|
if (!options) options = {};
|
|
|
|
if (!('defaultPath' in options) && this.lastSelectedPath_) options.defaultPath = this.lastSelectedPath_;
|
|
|
|
const filePath = dialog.showSaveDialog(options);
|
|
|
|
if (filePath) {
|
|
|
|
this.lastSelectedPath_ = filePath;
|
|
|
|
}
|
|
|
|
return filePath;
|
|
|
|
}
|
|
|
|
|
2017-11-11 00:18:00 +02:00
|
|
|
showOpenDialog(options) {
|
|
|
|
const {dialog} = require('electron');
|
2017-12-02 01:26:08 +02:00
|
|
|
if (!options) options = {};
|
|
|
|
if (!('defaultPath' in options) && this.lastSelectedPath_) options.defaultPath = this.lastSelectedPath_;
|
2017-12-07 23:18:18 +02:00
|
|
|
if (!('createDirectory' in options)) options.createDirectory = true;
|
2017-12-02 01:26:08 +02:00
|
|
|
const filePaths = dialog.showOpenDialog(options);
|
|
|
|
if (filePaths && filePaths.length) {
|
|
|
|
this.lastSelectedPath_ = dirname(filePaths[0]);
|
|
|
|
}
|
|
|
|
return filePaths;
|
2017-11-11 00:18:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-06 20:35:04 +02:00
|
|
|
showMessageBox(options) {
|
|
|
|
const {dialog} = require('electron');
|
|
|
|
return dialog.showMessageBox(options);
|
|
|
|
}
|
|
|
|
|
2017-11-08 19:51:55 +02:00
|
|
|
showErrorMessageBox(message) {
|
|
|
|
return this.showMessageBox({
|
|
|
|
type: 'error',
|
|
|
|
message: message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
showConfirmMessageBox(message) {
|
|
|
|
const result = this.showMessageBox({
|
|
|
|
type: 'question',
|
|
|
|
message: message,
|
|
|
|
buttons: [_('OK'), _('Cancel')],
|
|
|
|
});
|
|
|
|
return result === 0;
|
|
|
|
}
|
|
|
|
|
2017-12-07 23:18:18 +02:00
|
|
|
showInfoMessageBox(message) {
|
|
|
|
const result = this.showMessageBox({
|
|
|
|
type: 'info',
|
|
|
|
message: message,
|
|
|
|
buttons: [_('OK')],
|
|
|
|
});
|
|
|
|
return result === 0;
|
|
|
|
}
|
|
|
|
|
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-12 18:33:34 +02:00
|
|
|
openItem(fullPath) {
|
|
|
|
return require('electron').shell.openItem(fullPath)
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:59:32 +02:00
|
|
|
async checkForUpdatesAndNotify(logFilePath) {
|
2017-11-14 12:53:18 +02:00
|
|
|
if (!this.autoUpdater_) {
|
2017-11-21 20:59:32 +02:00
|
|
|
this.autoUpdateLogger_ = new Logger();
|
|
|
|
this.autoUpdateLogger_.addTarget('file', { path: logFilePath });
|
|
|
|
this.autoUpdateLogger_.setLevel(Logger.LEVEL_DEBUG);
|
2017-11-28 02:22:38 +02:00
|
|
|
this.autoUpdateLogger_.info('checkForUpdatesAndNotify: Initializing...');
|
2017-11-14 12:53:18 +02:00
|
|
|
this.autoUpdater_ = require("electron-updater").autoUpdater;
|
2017-11-21 20:59:32 +02:00
|
|
|
this.autoUpdater_.logger = this.autoUpdateLogger_;
|
2017-11-14 12:53:18 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 20:59:32 +02:00
|
|
|
try {
|
|
|
|
await this.autoUpdater_.checkForUpdatesAndNotify();
|
|
|
|
} catch (error) {
|
|
|
|
this.autoUpdateLogger_.error(error);
|
|
|
|
}
|
2017-11-14 12:18:18 +02:00
|
|
|
}
|
2017-11-14 01:04:27 +02:00
|
|
|
|
2017-11-05 02:17:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 }
|