Skip to content

Commit f3aac0f

Browse files
committed
refactor: Improve download tracking by selecting active webContents
- Updated the logic in setupElectronDlWithTracking to select the first available webContents for tracking downloads, enhancing reliability. - Added a notification for download completion with improved localization support. - Introduced a unit test to mock the filesystem and verify behavior when the download directory does not exist. These changes enhance the robustness of the download management system and improve user experience through better notifications.
1 parent 6941224 commit f3aac0f

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/downloads/main/download-persistence.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ describe('Download Folder Persistence', () => {
197197
expect(onStartedCallback).toBeDefined();
198198

199199
// Mock fs functions to simulate non-existent directory
200+
// eslint-disable-next-line @typescript-eslint/no-var-requires
200201
const { existsSync } = require('fs');
201202
existsSync.mockReturnValueOnce(false); // Directory doesn't exist
202203

src/downloads/main/setup.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,20 @@ export const setupElectronDlWithTracking = () => {
6060
});
6161

6262
// Find the webContents that initiated this download
63+
// According to electron-dl docs, onStarted only receives DownloadItem
64+
// We need to find an active webContents for tracking purposes
6365
const webContentsArray = webContents.getAllWebContents();
6466

65-
const sourceWebContents =
66-
// electron-dl passes a BrowserWindow; fall back if it already gave us webContents.
67-
(browserWindowOrWebContents as Electron.BrowserWindow | undefined)?.webContents ??
68-
(browserWindowOrWebContents as Electron.WebContents | undefined);
67+
// Use the first available webContents for tracking
68+
let sourceWebContents = null;
69+
for (const wc of webContentsArray) {
70+
if (wc && typeof wc.isDestroyed === 'function' && !wc.isDestroyed()) {
71+
sourceWebContents = wc;
72+
break;
73+
}
74+
}
6975

70-
if (
71-
sourceWebContents &&
72-
typeof sourceWebContents.isDestroyed === 'function' &&
73-
!sourceWebContents.isDestroyed()
74-
) {
76+
if (sourceWebContents) {
7577
const fakeEvent = {
7678
defaultPrevented: false,
7779
preventDefault: () => {},
@@ -96,6 +98,11 @@ export const setupElectronDlWithTracking = () => {
9698
downloadStore.set('lastDownloadDirectory', downloadDirectory);
9799
}
98100

101+
createNotification({
102+
title: t('downloads.title', { defaultValue: 'Downloads' }),
103+
body: file.filename || 'Unknown file',
104+
subtitle: t('downloads.notifications.downloadFinished'),
105+
});
99106
} catch {
100107
// Silently handle any errors in onCompleted
101108
}

0 commit comments

Comments
 (0)