Skip to content

Commit

Permalink
Added download count/badge and progress (closes brave#3023)
Browse files Browse the repository at this point in the history
  • Loading branch information
timborden committed Jul 5, 2017
1 parent db1e1bc commit 5f267ea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
30 changes: 29 additions & 1 deletion app/browser/electronDownloadItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,44 @@ const app = electron.app
* Maps downloadId to an electron download-item
*/
const downloadMap = {}
/**
* Monitor progress of active downloads
*/
let completedBytes = 0
const activeDownloadItems = () => Object.keys(downloadMap).length
const progressDownloadItems = () => {
const receivedBytes = Object.keys(downloadMap).reduce((receivedBytes, downloadId) => {
receivedBytes += downloadMap[downloadId].getReceivedBytes()
return receivedBytes
}, completedBytes)
const totalBytes = Object.keys(downloadMap).reduce((totalBytes, downloadId) => {
totalBytes += downloadMap[downloadId].getTotalBytes()
return totalBytes
}, completedBytes)
return receivedBytes / totalBytes
}

module.exports.updateElectronDownloadItem = (downloadId, item, state) => {
module.exports.updateElectronDownloadItem = (win, downloadId, item, state) => {
if (state === downloadStates.INTERRUPTED || state === downloadStates.CANCELLED || state === downloadStates.COMPLETED) {
if (app.dock && state === downloadStates.COMPLETED) {
app.dock.downloadFinished(item.getSavePath())
}
completedBytes += downloadMap[downloadId].getTotalBytes()
delete downloadMap[downloadId]
} else {
downloadMap[downloadId] = item
}
if (['darwin', 'linux'].includes(process.platform)) {
app.setBadgeCount(activeDownloadItems())
}
if (win && !win.isDestroyed()) {
if (activeDownloadItems()) {
win.setProgressBar(progressDownloadItems())
} else {
win.setProgressBar(-1)
completedBytes = 0
}
}
}

module.exports.cancelDownload = (downloadId) =>
Expand Down
10 changes: 5 additions & 5 deletions app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ function registerPermissionHandler (session, partition) {
})
}

function updateDownloadState (downloadId, item, state) {
updateElectronDownloadItem(downloadId, item, state)
function updateDownloadState (win, downloadId, item, state) {
updateElectronDownloadItem(win, downloadId, item, state)

if (!item) {
appActions.mergeDownloadDetail(downloadId, { state: downloadStates.INTERRUPTED })
Expand Down Expand Up @@ -532,16 +532,16 @@ function registerForDownloadListener (session) {
appActions.changeSetting(settings.DEFAULT_DOWNLOAD_SAVE_PATH, path.dirname(savePath))

const downloadId = uuid.v4()
updateDownloadState(downloadId, item, downloadStates.PENDING)
updateDownloadState(win, downloadId, item, downloadStates.PENDING)
if (win) {
win.webContents.send(messages.SHOW_DOWNLOADS_TOOLBAR)
}
item.on('updated', function () {
const state = item.isPaused() ? downloadStates.PAUSED : downloadStates.IN_PROGRESS
updateDownloadState(downloadId, item, state)
updateDownloadState(win, downloadId, item, state)
})
item.on('done', function (e, state) {
updateDownloadState(downloadId, item, state)
updateDownloadState(win, downloadId, item, state)
})
})
}
Expand Down

0 comments on commit 5f267ea

Please sign in to comment.