From 0acb44d696e32d4fbe43556d10fd66250e7d8709 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 14 Apr 2021 10:58:11 +0100 Subject: [PATCH] Wire up electron download progress to toasts --- src/preload.js | 10 ++++-- src/webcontents-handler.js | 65 +++++++++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/src/preload.js b/src/preload.js index 64849f57e..7f863cc4e 100644 --- a/src/preload.js +++ b/src/preload.js @@ -32,8 +32,7 @@ const CHANNELS = [ "seshatReply", "setBadgeCount", "update-downloaded", - "userDownloadCompleted", - "userDownloadOpen", + "userDownload", ]; contextBridge.exposeInMainWorld( @@ -46,6 +45,13 @@ contextBridge.exposeInMainWorld( } ipcRenderer.on(channel, listener); }, + removeListener(channel, listener) { + if (!CHANNELS.includes(channel)) { + console.error(`Unknown IPC channel ${channel} ignored`); + return; + } + ipcRenderer.removeListener(channel, listener); + }, send(channel, ...args) { if (!CHANNELS.includes(channel)) { console.error(`Unknown IPC channel ${channel} ignored`); diff --git a/src/webcontents-handler.js b/src/webcontents-handler.js index 3201772e6..cfc0355ab 100644 --- a/src/webcontents-handler.js +++ b/src/webcontents-handler.js @@ -206,10 +206,6 @@ function onEditableContextMenu(ev, params) { ev.preventDefault(); } -ipcMain.on('userDownloadOpen', function(ev, {path}) { - shell.openPath(path); -}); - module.exports = (webContents) => { webContents.on('new-window', onWindowOrNavigate); webContents.on('will-navigate', (ev, target) => { @@ -228,13 +224,60 @@ module.exports = (webContents) => { }); webContents.session.on('will-download', (event, item) => { - item.once('done', (event, state) => { - if (state === 'completed') { - const savePath = item.getSavePath(); - webContents.send('userDownloadCompleted', { - path: savePath, - name: path.basename(savePath), - }); + let started = false; + + const ipcHandler = function(ev, {action, path}) { + if (path !== item.savePath) return; + + switch (action) { + case "download": + shell.openPath(path); + ipcMain.off("userDownload", ipcHandler); + break; + case "pause": + item.pause(); + break; + case "resume": + item.resume(); + break; + case "cancel": + item.cancel(); + ipcMain.off("userDownload", ipcHandler); + break; + case "done": + ipcMain.off("userDownload", ipcHandler); + break; + } + }; + + ipcMain.on("userDownload", ipcHandler); + + item.on("updated", (event, state) => { + if (!item.savePath) return; + webContents.send("userDownload", { + state, + path: item.savePath, + name: path.basename(item.savePath), + totalBytes: item.getTotalBytes(), + receivedBytes: item.getReceivedBytes(), + begin: !started, + }); + + if (!started) started = true; + }); + item.once("done", (event, state) => { + if (!item.savePath) return; + webContents.send("userDownload", { + state, + path: item.savePath, + name: path.basename(item.savePath), + totalBytes: item.getTotalBytes(), + receivedBytes: item.getReceivedBytes(), + terminal: true, + }); + + if (state === "interrupted") { + ipcMain.off("userDownload", ipcHandler); } }); });