Skip to content

Commit

Permalink
refactor: unobtrusive autoupdate notification
Browse files Browse the repository at this point in the history
This does not block main process nor interrupt user in any way.
Notification can be safely ignored, in which case the update will be
installed on exit.
  • Loading branch information
lidel committed Feb 16, 2022
1 parent 0c380a1 commit 9a17529
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
6 changes: 3 additions & 3 deletions assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@
"message": "It was not possible to download the update. Please check your Internet connection and try again."
},
"updateDownloadedDialog": {
"title": "Update IPFS Desktop",
"title": "IPFS Desktop Update",
"message": "An update to IPFS Desktop { version } is available. Would you like to install it now?",
"later": "Later",
"now": "Install now"
},
"updateDownloadedNotification": {
"title": "Update downloaded",
"message": "Update for version { version } of IPFS Desktop downloaded."
"title": "IPFS Desktop Update",
"message": "An update to IPFS Desktop { version } is available."
},
"runGarbageCollectorWarning": {
"title": "Garbage collector",
Expand Down
50 changes: 28 additions & 22 deletions src/auto-updater/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const { shell, app, BrowserWindow } = require('electron')
const { shell, app, BrowserWindow, Notification } = require('electron')
const { autoUpdater } = require('electron-updater')
const i18n = require('i18next')
const { ipcMain } = require('electron')
const logger = require('../common/logger')
const { notify } = require('../common/notify')
const { showDialog } = require('../dialogs')
const { IS_MAC, IS_WIN, IS_APPIMAGE } = require('../common/consts')

Expand All @@ -13,6 +12,7 @@ function isAutoUpdateSupported () {
return IS_MAC || IS_WIN || IS_APPIMAGE
}

let updateNotification = null // must be a global to avoid gc
let feedback = false

function setup (ctx) {
Expand Down Expand Up @@ -90,29 +90,35 @@ function setup (ctx) {
autoUpdater.on('update-downloaded', ({ version }) => {
logger.info(`[updater] update to ${version} downloaded`)

if (!feedback) {
notify({
title: i18n.t('updateDownloadedNotification.title'),
body: i18n.t('updateDownloadedNotification.message', { version })
const feedbackDialog = () => {
const opt = showDialog({
title: i18n.t('updateDownloadedDialog.title'),
message: i18n.t('updateDownloadedDialog.message', { version }),
type: 'info',
buttons: [
i18n.t('updateDownloadedDialog.later'),
i18n.t('updateDownloadedDialog.now')
]
})
if (opt === 1) { // now
setImmediate(async () => {
await beforeQuitCleanup() // just to be sure (we had regressions before)
autoUpdater.quitAndInstall()
})
}
}

feedback = false

const opt = showDialog({
title: i18n.t('updateDownloadedDialog.title'),
message: i18n.t('updateDownloadedDialog.message', { version }),
type: 'info',
buttons: [
i18n.t('updateDownloadedDialog.later'),
i18n.t('updateDownloadedDialog.now')
]
})
if (opt === 1) { // now
setImmediate(async () => {
await beforeQuitCleanup() // just to be sure (we had regressions before)
autoUpdater.quitAndInstall()
if (feedback) {
feedback = false
// when in instant feedback mode, show dialog immediatelly
feedbackDialog()
} else {
// show unobtrusive notification + dialog on click
updateNotification = new Notification({
title: i18n.t('updateDownloadedNotification.title'),
body: i18n.t('updateDownloadedNotification.message', { version })
})
updateNotification.on('click', feedbackDialog)
updateNotification.show()
}
})

Expand Down

0 comments on commit 9a17529

Please sign in to comment.