Skip to content

Commit

Permalink
Update Menu labels for auto update
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric committed Oct 5, 2018
1 parent a9f1695 commit 57f8d6a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
23 changes: 20 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { app, Menu } = require('electron')
const isDev = ('NODE_ENV' in process.env && process.env.NODE_ENV === 'dev')

const { checkForUpdates } = require('./update.js')
const { checkForUpdates, installUpdate } = require('./update.js')

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
Expand All @@ -17,8 +17,24 @@ app.on('ready', () => {
label: 'About CCA',
click: () => about.init()
}, {
label: 'Check for updates...',
click: checkForUpdates
id: 'menuUpdateChecking',
label: 'Checking for updates...',
enabled: false
}, {
id: 'menuUpdateNotFound',
label: 'Current version is up-to-date',
enabled: false,
visible: false
}, {
id: 'menuUpdateFound',
label: 'Found updates, downloading...',
enabled: false,
visible: false
}, {
id: 'menuUpdateInstall',
label: 'Install update',
click: installUpdate,
visible: false
}, {
type: 'separator'
}, {
Expand Down Expand Up @@ -107,6 +123,7 @@ app.on('ready', () => {

const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
checkForUpdates()
})

// Quit when all windows are closed.
Expand Down
65 changes: 30 additions & 35 deletions src/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,50 @@
* 1. create `updater.js` for the code snippet
* 2. require `updater.js` for menu implementation, and set `checkForUpdates` callback from `updater` for the click property of `Check Updates...` MenuItem.
*/
const { dialog } = require('electron')
const { dialog, Menu } = require('electron')
const { autoUpdater } = require('electron-updater')

let updater
let releaseName, releaseNotes
autoUpdater.autoDownload = false

autoUpdater.on('error', (error) => {
dialog.showErrorBox('Error: ', error == null ? "unknown" : (error.stack || error).toString())
dialog.showErrorBox('Auto Update Error: ', error == null ? "unknown" : (error.stack || error).toString())
})

autoUpdater.on('update-available', () => {
dialog.showMessageBox({
type: 'info',
title: 'Found Updates',
message: 'Found updates, do you want update now?',
buttons: ['Sure', 'No']
}, (buttonIndex) => {
if (buttonIndex === 0) {
autoUpdater.downloadUpdate()
}
else {
updater.enabled = true
updater = null
}
})
autoUpdater.on('update-available', (ev) => {
autoUpdater.downloadUpdate()
var menu = Menu.getApplicationMenu();
menu.getMenuItemById('menuUpdateChecking').visible = false
menu.getMenuItemById('menuUpdateFound').visible = true
})

autoUpdater.on('update-not-available', () => {
dialog.showMessageBox({
title: 'No Updates',
message: 'Current version is up-to-date.'
})
updater.enabled = true
updater = null
autoUpdater.on('update-not-available', (ev) => {
var menu = Menu.getApplicationMenu();
menu.getMenuItemById('menuUpdateChecking').visible = false
menu.getMenuItemById('menuUpdateNotFound').visible = true
})

autoUpdater.on('update-downloaded', (ev) => {
releaseName = ev.releaseName
releaseNotes = ev.releaseNotes
var menu = Menu.getApplicationMenu();
menu.getMenuItemById('menuUpdateFound').visible = false
menu.getMenuItemById('menuUpdateInstall').visible = true

})

autoUpdater.on('update-downloaded', () => {
function checkForUpdates() {
autoUpdater.checkForUpdates()
}

function installUpdate() {
dialog.showMessageBox({
title: 'Install Updates',
message: 'Updates downloaded, application will be quit for update...'
message: `${releaseName} downloaded, application will be quit for update...`
}, () => {
setImmediate(() => autoUpdater.quitAndInstall())
})
})

// export this to MenuItem click callback
function checkForUpdates (menuItem, focusedWindow, event) {
updater = menuItem
updater.enabled = false
autoUpdater.checkForUpdates()
}
module.exports.checkForUpdates = checkForUpdates

module.exports.checkForUpdates = checkForUpdates
module.exports.installUpdate = installUpdate

0 comments on commit 57f8d6a

Please sign in to comment.