Skip to content

Commit

Permalink
Fixed tray icon and update issue
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Feb 6, 2018
1 parent 1e94a22 commit af82345
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 30 deletions.
32 changes: 22 additions & 10 deletions ElectronClient/app/ElectronAppWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const url = require('url')
const path = require('path')
const urlUtils = require('lib/urlUtils.js');
const { dirname, basename } = require('lib/path-utils');
const fs = require('fs-extra');

class ElectronAppWrapper {

Expand All @@ -14,6 +15,7 @@ class ElectronAppWrapper {
this.win_ = null;
this.willQuitApp_ = false;
this.tray_ = null;
this.buildDir_ = null;
}

electronApp() {
Expand Down Expand Up @@ -116,20 +118,30 @@ class ElectronAppWrapper {
}

buildDir() {
let dir = __dirname;
if (dir.indexOf('.asar') >= 0 || basename(dir) === 'app') dir = dirname(dir);
return dir + '/build';
if (this.buildDir_) return this.buildDir_;
let dir = __dirname + '/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
createTray(contextMenu) {
this.tray_ = new Tray(this.buildDir() + '/icons/16x16.png')
this.tray_.setToolTip(this.electronApp_.getName())
this.tray_.setContextMenu(contextMenu)

this.tray_.on('click', () => {
this.window().show();
});
try {
this.tray_ = new Tray(this.buildDir() + '/icons/16x16.png')
this.tray_.setToolTip(this.electronApp_.getName())
this.tray_.setContextMenu(contextMenu)

this.tray_.on('click', () => {
this.window().show();
});
} catch (error) {
console.error("Cannot create tray", error);
}
}

destroyTray() {
Expand Down
60 changes: 40 additions & 20 deletions ElectronClient/app/checkForUpdates.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const { _ } = require('lib/locale.js');
let autoUpdateLogger_ = new Logger();
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.on('error', (error) => {
Expand All @@ -14,19 +17,36 @@ autoUpdater.on('error', (error) => {
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({
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')]
}, (buttonIndex) => {
if (buttonIndex === 0) {
try {
autoUpdater.downloadUpdate()
} catch (error) {
autoUpdateLogger_.error(error);
dialog.showErrorBox(_('Error'), _('Could not download the update: %s', error.message));
}
require('electron').shell.openExternal(downloadUrl);
}
})
})
Expand All @@ -37,18 +57,18 @@ autoUpdater.on('update-not-available', () => {
dialog.showMessageBox({ message: _('Current version is up-to-date.') })
})

autoUpdater.on('update-downloaded', () => {
dialog.showMessageBox({ message: _('New version downloaded - application will quit now and update...') }, () => {
setTimeout(() => {
try {
autoUpdater.quitAndInstall();
} catch (error) {
autoUpdateLogger_.error(error);
dialog.showErrorBox(_('Error'), _('Could not install the update: %s', error.message));
}
}, 100);
})
})
// autoUpdater.on('update-downloaded', () => {
// dialog.showMessageBox({ message: _('New version downloaded - application will quit now and update...') }, () => {
// setTimeout(() => {
// try {
// autoUpdater.quitAndInstall();
// } catch (error) {
// autoUpdateLogger_.error(error);
// dialog.showErrorBox(_('Error'), _('Could not install the update: %s', error.message));
// }
// }, 100);
// })
// })

function checkForUpdates(inBackground, logFilePath) {
if (logFilePath && !autoUpdateLogger_.targets().length) {
Expand Down

0 comments on commit af82345

Please sign in to comment.