1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Fixed tray icon and update issue

This commit is contained in:
Laurent Cozic 2018-02-06 13:11:59 +00:00
parent 1e94a22986
commit af82345eb8
2 changed files with 61 additions and 29 deletions

View File

@ -5,6 +5,7 @@ const url = require('url')
const path = require('path') const path = require('path')
const urlUtils = require('lib/urlUtils.js'); const urlUtils = require('lib/urlUtils.js');
const { dirname, basename } = require('lib/path-utils'); const { dirname, basename } = require('lib/path-utils');
const fs = require('fs-extra');
class ElectronAppWrapper { class ElectronAppWrapper {
@ -14,6 +15,7 @@ class ElectronAppWrapper {
this.win_ = null; this.win_ = null;
this.willQuitApp_ = false; this.willQuitApp_ = false;
this.tray_ = null; this.tray_ = null;
this.buildDir_ = null;
} }
electronApp() { electronApp() {
@ -116,20 +118,30 @@ class ElectronAppWrapper {
} }
buildDir() { buildDir() {
let dir = __dirname; if (this.buildDir_) return this.buildDir_;
if (dir.indexOf('.asar') >= 0 || basename(dir) === 'app') dir = dirname(dir); let dir = __dirname + '/build';
return dir + '/build'; if (!fs.pathExistsSync(dir)) {
dir = dirname(__dirname) + '/build';
if (!fs.pathExistsSync(dir)) throw new Error('Cannot find build dir');
}
this.buildDir_ = dir;
return dir;
} }
// Note: this must be called only after the "ready" event of the app has been dispatched // Note: this must be called only after the "ready" event of the app has been dispatched
createTray(contextMenu) { createTray(contextMenu) {
this.tray_ = new Tray(this.buildDir() + '/icons/16x16.png') try {
this.tray_.setToolTip(this.electronApp_.getName()) this.tray_ = new Tray(this.buildDir() + '/icons/16x16.png')
this.tray_.setContextMenu(contextMenu) this.tray_.setToolTip(this.electronApp_.getName())
this.tray_.setContextMenu(contextMenu)
this.tray_.on('click', () => { this.tray_.on('click', () => {
this.window().show(); this.window().show();
}); });
} catch (error) {
console.error("Cannot create tray", error);
}
} }
destroyTray() { destroyTray() {

View File

@ -6,6 +6,9 @@ const { _ } = require('lib/locale.js');
let autoUpdateLogger_ = new Logger(); let autoUpdateLogger_ = new Logger();
let checkInBackground_ = false; let checkInBackground_ = false;
// Note: Electron Builder's autoUpdater is incredibly buggy so currently it's only used
// to detect if a new version is present. If it is, the download link is simply opened
// in a new browser window.
autoUpdater.autoDownload = false; autoUpdater.autoDownload = false;
autoUpdater.on('error', (error) => { autoUpdater.on('error', (error) => {
@ -14,19 +17,36 @@ autoUpdater.on('error', (error) => {
dialog.showErrorBox(_('Error'), error == null ? "unknown" : (error.stack || error).toString()) dialog.showErrorBox(_('Error'), error == null ? "unknown" : (error.stack || error).toString())
}) })
autoUpdater.on('update-available', () => { function htmlToText_(html) {
let output = html.replace(/\n/g, '');
output = output.replace(/<li>/g, '- ');
output = output.replace(/<\/li>/g, '\n');
output = output.replace(/<ul>/g, '');
output = output.replace(/<\/ul>/g, '');
output = output.replace(/<.*?>/g, '');
output = output.replace(/<\/.*?>/g, '');
return output;
}
autoUpdater.on('update-available', (info) => {
if (!info.version || !info.path) {
if (checkInBackground_) return;
dialog.showErrorBox(_('Error'), ('Could not get version info: ' + JSON.stringify(info)));
return;
}
const downloadUrl = 'https://github.com/laurent22/joplin/releases/download/v' + info.version + '/' + info.path;
let releaseNotes = info.releaseNotes + '';
if (releaseNotes) releaseNotes = '\n\n' + _('Release notes:\n\n%s', htmlToText_(releaseNotes));
dialog.showMessageBox({ dialog.showMessageBox({
type: 'info', type: 'info',
message: _('An update is available, do you want to update now?'), message: _('An update is available, do you want to download it now?' + releaseNotes),
buttons: [_('Yes'), _('No')] buttons: [_('Yes'), _('No')]
}, (buttonIndex) => { }, (buttonIndex) => {
if (buttonIndex === 0) { if (buttonIndex === 0) {
try { require('electron').shell.openExternal(downloadUrl);
autoUpdater.downloadUpdate()
} catch (error) {
autoUpdateLogger_.error(error);
dialog.showErrorBox(_('Error'), _('Could not download the update: %s', error.message));
}
} }
}) })
}) })
@ -37,18 +57,18 @@ autoUpdater.on('update-not-available', () => {
dialog.showMessageBox({ message: _('Current version is up-to-date.') }) dialog.showMessageBox({ message: _('Current version is up-to-date.') })
}) })
autoUpdater.on('update-downloaded', () => { // autoUpdater.on('update-downloaded', () => {
dialog.showMessageBox({ message: _('New version downloaded - application will quit now and update...') }, () => { // dialog.showMessageBox({ message: _('New version downloaded - application will quit now and update...') }, () => {
setTimeout(() => { // setTimeout(() => {
try { // try {
autoUpdater.quitAndInstall(); // autoUpdater.quitAndInstall();
} catch (error) { // } catch (error) {
autoUpdateLogger_.error(error); // autoUpdateLogger_.error(error);
dialog.showErrorBox(_('Error'), _('Could not install the update: %s', error.message)); // dialog.showErrorBox(_('Error'), _('Could not install the update: %s', error.message));
} // }
}, 100); // }, 100);
}) // })
}) // })
function checkForUpdates(inBackground, logFilePath) { function checkForUpdates(inBackground, logFilePath) {
if (logFilePath && !autoUpdateLogger_.targets().length) { if (logFilePath && !autoUpdateLogger_.targets().length) {