forked from Automattic/simplenote-electron
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "Check for Updates" menu item (Automattic#1090)
* Refactor updater * Add menu item to Check for Updates * Show update/download progress * Ensure that preDownloadProgressBar is closed * Add comment about delay * Refactor initProgressBar * Make only the download progress window closable * Remove unneeded variable * Rename progressUpdater * Extract progress updating logic * Fix breakage on Linux * Set OK button for Linux * Handle errors
- Loading branch information
Showing
10 changed files
with
190 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
const { app, dialog } = require('electron'); | ||
const ProgressBar = require('electron-progressbar'); | ||
const prettyBytes = require('pretty-bytes'); | ||
|
||
const progressBarBlue = '#4895d9'; | ||
|
||
/** | ||
* Set up progress bar dialogs, and add/remove listeners on the updater. | ||
*/ | ||
const setupProgressUpdates = ({ updater, willAutoDownload }) => { | ||
let progressBar; | ||
const title = 'Update Simplenote'; | ||
const style = { | ||
bar: { | ||
height: '8px', | ||
'border-radius': '0', | ||
'box-shadow': 'none', | ||
}, | ||
value: { | ||
'background-color': progressBarBlue, | ||
'border-radius': '0', | ||
'box-shadow': 'none', | ||
}, | ||
}; | ||
|
||
const preDownloadProgressBar = new ProgressBar({ | ||
title, | ||
text: 'Checking for Updates…', | ||
style, | ||
}); | ||
|
||
const notifyNoUpdate = () => { | ||
updater.removeListener('update-not-available', notifyNoUpdate); | ||
closeProgressAndShowMessage({ | ||
message: 'You’re up to date!', | ||
detail: `Simplenote ${app.getVersion()} is currently the newest version available.`, | ||
}); | ||
}; | ||
|
||
const notifyError = () => { | ||
updater.removeListener('error', notifyError); | ||
closeProgressAndShowMessage({ | ||
message: 'Something went wrong!', | ||
detail: `Please try again later.`, | ||
}); | ||
}; | ||
|
||
const closeProgressAndShowMessage = ({ message, detail }) => { | ||
preDownloadProgressBar.setCompleted(); | ||
setTimeout(() => { | ||
dialog.showMessageBox({ | ||
message, | ||
detail, | ||
buttons: ['OK'], // needs to be set explicitly for Linux | ||
}); | ||
}, 500); // Allow time for preDownloadProgressBar to close | ||
}; | ||
|
||
const initProgressBar = totalBytes => { | ||
progressBar = new ProgressBar({ | ||
indeterminate: false, | ||
title, | ||
text: 'Downloading…', | ||
maxValue: totalBytes, | ||
browserWindow: { closable: true }, | ||
style, | ||
}); | ||
progressBar.on('progress', value => { | ||
progressBar.detail = | ||
prettyBytes(value) + ` of ${prettyBytes(totalBytes)} downloaded…`; | ||
}); | ||
progressBar.on('aborted', () => | ||
updater.removeListener('download-progress', updateProgress) | ||
); | ||
}; | ||
|
||
const updateProgress = progress => { | ||
if (!progressBar) { | ||
preDownloadProgressBar.setCompleted(); | ||
initProgressBar(progress.total); | ||
} | ||
progressBar.value = progress.transferred; | ||
}; | ||
|
||
updater.on('error', notifyError); | ||
updater.on('update-not-available', notifyNoUpdate); | ||
updater.on('update-available', () => { | ||
if (!willAutoDownload) { | ||
preDownloadProgressBar.setCompleted(); | ||
|
||
// Allow time for preDownloadProgressBar to close | ||
setTimeout(() => updater.notify(), 500); | ||
} | ||
}); | ||
updater.on('download-progress', updateProgress); | ||
updater.on('update-downloaded', () => { | ||
if (preDownloadProgressBar) { | ||
preDownloadProgressBar.setCompleted(); | ||
} | ||
updater.removeListener('download-progress', updateProgress); | ||
}); | ||
}; | ||
|
||
module.exports = setupProgressUpdates; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters