2018-05-20 12:54:42 +01:00
|
|
|
const { _, setLocale } = require('lib/locale.js');
|
2017-12-01 23:26:08 +00:00
|
|
|
const { dirname } = require('lib/path-utils.js');
|
2017-11-14 10:53:18 +00:00
|
|
|
const { Logger } = require('lib/logger.js');
|
2018-06-10 12:19:36 +01:00
|
|
|
const { powerSaveBlocker } = require('electron');
|
2017-11-08 17:51:55 +00:00
|
|
|
|
2017-11-05 00:17:48 +00:00
|
|
|
class Bridge {
|
|
|
|
|
|
|
|
constructor(electronWrapper) {
|
|
|
|
this.electronWrapper_ = electronWrapper;
|
2017-11-21 18:59:32 +00:00
|
|
|
this.autoUpdateLogger_ = null;
|
2017-12-01 23:26:08 +00:00
|
|
|
this.lastSelectedPath_ = null;
|
2018-06-10 12:19:36 +01:00
|
|
|
this.allowPowerSaveBlockerToggle_ = false;
|
2017-11-05 00:17:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 12:00:37 +00:00
|
|
|
electronApp() {
|
|
|
|
return this.electronWrapper_;
|
|
|
|
}
|
|
|
|
|
2017-11-05 00:17:48 +00:00
|
|
|
processArgv() {
|
|
|
|
return process.argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
window() {
|
|
|
|
return this.electronWrapper_.window();
|
|
|
|
}
|
|
|
|
|
2018-06-10 12:19:36 +01:00
|
|
|
setAllowPowerSaveBlockerToggle(v) {
|
|
|
|
this.allowPowerSaveBlockerToggle_ = v;
|
|
|
|
}
|
|
|
|
|
2017-11-05 00:17:48 +00: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 18:24:02 +00:00
|
|
|
}
|
2017-11-05 00:17:48 +00:00
|
|
|
|
2017-11-14 18:02:58 +00: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 21:18:18 +00:00
|
|
|
showSaveDialog(options) {
|
|
|
|
const {dialog} = require('electron');
|
|
|
|
if (!options) options = {};
|
|
|
|
if (!('defaultPath' in options) && this.lastSelectedPath_) options.defaultPath = this.lastSelectedPath_;
|
2018-02-15 23:05:04 +00:00
|
|
|
const filePath = dialog.showSaveDialog(this.window(), options);
|
2017-12-07 21:18:18 +00:00
|
|
|
if (filePath) {
|
|
|
|
this.lastSelectedPath_ = filePath;
|
|
|
|
}
|
|
|
|
return filePath;
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:18:00 +00:00
|
|
|
showOpenDialog(options) {
|
|
|
|
const {dialog} = require('electron');
|
2017-12-01 23:26:08 +00:00
|
|
|
if (!options) options = {};
|
|
|
|
if (!('defaultPath' in options) && this.lastSelectedPath_) options.defaultPath = this.lastSelectedPath_;
|
2017-12-07 21:18:18 +00:00
|
|
|
if (!('createDirectory' in options)) options.createDirectory = true;
|
2018-02-15 23:05:04 +00:00
|
|
|
const filePaths = dialog.showOpenDialog(this.window(), options);
|
2017-12-01 23:26:08 +00:00
|
|
|
if (filePaths && filePaths.length) {
|
|
|
|
this.lastSelectedPath_ = dirname(filePaths[0]);
|
|
|
|
}
|
|
|
|
return filePaths;
|
2017-11-10 22:18:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 18:24:02 +00:00
|
|
|
// Don't use this directly - call one of the showXxxxxxxMessageBox() instead
|
|
|
|
showMessageBox_(window, options) {
|
2017-11-06 18:35:04 +00:00
|
|
|
const {dialog} = require('electron');
|
2018-03-02 18:24:02 +00:00
|
|
|
const nativeImage = require('electron').nativeImage
|
|
|
|
if (!window) window = this.window();
|
2018-02-16 18:06:02 +00:00
|
|
|
return dialog.showMessageBox(window, options);
|
2017-11-06 18:35:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 17:51:55 +00:00
|
|
|
showErrorMessageBox(message) {
|
2018-03-02 18:24:02 +00:00
|
|
|
return this.showMessageBox_(this.window(), {
|
2017-11-08 17:51:55 +00:00
|
|
|
type: 'error',
|
|
|
|
message: message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
showConfirmMessageBox(message) {
|
2018-03-02 18:24:02 +00:00
|
|
|
const result = this.showMessageBox_(this.window(), {
|
2017-11-08 17:51:55 +00:00
|
|
|
type: 'question',
|
|
|
|
message: message,
|
2018-06-22 18:31:55 +00:00
|
|
|
cancelId: 1,
|
2017-11-08 17:51:55 +00:00
|
|
|
buttons: [_('OK'), _('Cancel')],
|
|
|
|
});
|
|
|
|
return result === 0;
|
|
|
|
}
|
|
|
|
|
2018-03-02 18:24:02 +00:00
|
|
|
showInfoMessageBox(message, options = {}) {
|
|
|
|
const result = this.showMessageBox_(this.window(), Object.assign({}, {
|
2017-12-07 21:18:18 +00:00
|
|
|
type: 'info',
|
|
|
|
message: message,
|
|
|
|
buttons: [_('OK')],
|
2018-03-02 18:24:02 +00:00
|
|
|
}, options));
|
2017-12-07 21:18:18 +00:00
|
|
|
return result === 0;
|
|
|
|
}
|
|
|
|
|
2018-05-20 12:54:42 +01:00
|
|
|
setLocale(locale) {
|
|
|
|
setLocale(locale);
|
|
|
|
}
|
|
|
|
|
2017-11-08 17:51:55 +00:00
|
|
|
get Menu() {
|
|
|
|
return require('electron').Menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
get MenuItem() {
|
|
|
|
return require('electron').MenuItem;
|
|
|
|
}
|
|
|
|
|
2017-11-11 12:00:37 +00:00
|
|
|
openExternal(url) {
|
|
|
|
return require('electron').shell.openExternal(url)
|
|
|
|
}
|
|
|
|
|
2017-11-12 16:33:34 +00:00
|
|
|
openItem(fullPath) {
|
|
|
|
return require('electron').shell.openItem(fullPath)
|
|
|
|
}
|
|
|
|
|
2018-02-15 23:05:04 +00:00
|
|
|
checkForUpdates(inBackground, window, logFilePath) {
|
2018-01-30 22:35:50 +00:00
|
|
|
const { checkForUpdates } = require('./checkForUpdates.js');
|
2018-02-15 23:05:04 +00:00
|
|
|
checkForUpdates(inBackground, window, logFilePath);
|
2018-01-30 22:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 12:19:36 +01:00
|
|
|
powerSaveBlockerStart(type) {
|
|
|
|
if (!this.allowPowerSaveBlockerToggle_) return null;
|
|
|
|
console.info('Enable powerSaveBlockerStart: ' + type);
|
|
|
|
return powerSaveBlocker.start(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
powerSaveBlockerStop(id) {
|
|
|
|
if (!this.allowPowerSaveBlockerToggle_) return null;
|
|
|
|
console.info('Disable powerSaveBlocker: ' + id);
|
|
|
|
return powerSaveBlocker.stop(id);
|
|
|
|
}
|
|
|
|
|
2017-11-05 00:17:48 +00: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 }
|