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 15, 2022
1 parent 0c380a1 commit eaeec2e
Show file tree
Hide file tree
Showing 2 changed files with 30 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
49 changes: 27 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 Down Expand Up @@ -90,29 +89,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
const note = new Notification({
title: i18n.t('updateDownloadedNotification.title'),
body: i18n.t('updateDownloadedNotification.message', { version })
})
note.on('click', feedbackDialog)
note.show()
}
})

Expand Down

0 comments on commit eaeec2e

Please sign in to comment.