Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

feat: add taskbar progress bar and open-in-folder on download completion (#212)

Dhruvil Patelb3b2be6f1637
diff --git a/e2e/taskbarProgress.spec.ts b/e2e/taskbarProgress.spec.ts
new file mode 100644
index 0000000..9f81930
--- /dev/null
+++ b/e2e/taskbarProgress.spec.ts
@@ -0,0 +1,88 @@
+import { expect, test } from '@playwright/test'
+import { taskbarProgress, wantedBytes, type TaskbarDownload } from '../src/shared/taskbarProgress'
+
+test.describe('taskbar progress @smoke', () => {
+ test('nothing active clears the progress bar', () => {
+ expect(taskbarProgress([])).toEqual({ progress: -1, mode: 'none' })
+
+ const nonActive: TaskbarDownload[] = [
+ { status: 'completed', totalBytes: 1000, bytesDownloaded: 1000 },
+ { status: 'error', totalBytes: 1000, bytesDownloaded: 200 },
+ { status: 'queued', totalBytes: 1000, bytesDownloaded: 0 }
+ ]
+ expect(taskbarProgress(nonActive)).toEqual({ progress: -1, mode: 'none' })
+ })
+
+ test('downloading calculates combined progress in normal mode', () => {
+ const single: TaskbarDownload[] = [
+ { status: 'downloading', totalBytes: 1000, bytesDownloaded: 500 }
+ ]
+ expect(taskbarProgress(single)).toEqual({ progress: 0.5, mode: 'normal' })
+
+ const multiple: TaskbarDownload[] = [
+ { status: 'downloading', totalBytes: 1000, bytesDownloaded: 300 },
+ { status: 'downloading', totalBytes: 2000, bytesDownloaded: 900 }
+ ]
+ // (300 + 900) / (1000 + 2000) = 1200 / 3000 = 0.4
+ expect(taskbarProgress(multiple)).toEqual({ progress: 0.4, mode: 'normal' })
+ })
+
+ test('paused only shows paused progress when nothing is downloading', () => {
+ const paused: TaskbarDownload[] = [{ status: 'paused', totalBytes: 1000, bytesDownloaded: 250 }]
+ expect(taskbarProgress(paused)).toEqual({ progress: 0.25, mode: 'paused' })
+
+ const multiplePaused: TaskbarDownload[] = [
+ { status: 'paused', totalBytes: 2000, bytesDownloaded: 500 },
+ { status: 'paused', totalBytes: 2000, bytesDownloaded: 1500 }
+ ]
+ // (500 + 1500) / (2000 + 2000) = 2000 / 4000 = 0.5
+ expect(taskbarProgress(multiplePaused)).toEqual({ progress: 0.5, mode: 'paused' })
+ })
+
+ test('unknown size downloading shows indeterminate mode', () => {
+ const unknown: TaskbarDownload[] = [
+ { status: 'downloading', totalBytes: 0, bytesDownloaded: 500 }
+ ]
+ expect(taskbarProgress(unknown)).toEqual({ progress: 2, mode: 'indeterminate' })
+
+ // If one has unknown size and one has known size, known size determines progress
+ const mixed: TaskbarDownload[] = [
+ { status: 'downloading', totalBytes: 0, bytesDownloaded: 500 },
+ { status: 'downloading', totalBytes: 1000, bytesDownloaded: 750 }
+ ]
+ expect(taskbarProgress(mixed)).toEqual({ progress: 0.75, mode: 'normal' })
+ })
+
+ test('torrent with skipped files reaches 100% using wanted bytes', () => {
+ const torrent: TaskbarDownload = {
+ status: 'downloading',
+ kind: 'torrent',
+ totalBytes: 1000,
+ skippedBytes: 200,
+ bytesDownloaded: 400
+ }
+ // wantedBytes: 1000 - 200 = 800
+ expect(wantedBytes(torrent)).toBe(800)
+ // 400 / 800 = 0.5
+ expect(taskbarProgress([torrent])).toEqual({ progress: 0.5, mode: 'normal' })
+
+ const finishedWanted: TaskbarDownload = {
+ status: 'downloading',
+ kind: 'torrent',
+ totalBytes: 1000,
+ skippedBytes: 200,
+ bytesDownloaded: 800
+ }
+ expect(taskbarProgress([finishedWanted])).toEqual({ progress: 1, mode: 'normal' })
+
+ const pausedTorrent: TaskbarDownload = {
+ status: 'paused',
+ kind: 'torrent',
+ totalBytes: 1000,
+ skippedBytes: 200,
+ bytesDownloaded: 600
+ }
+ // 600 / 800 = 0.75
+ expect(taskbarProgress([pausedTorrent])).toEqual({ progress: 0.75, mode: 'paused' })
+ })
+})
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.ts
index b2b4456..cd045f5 100644
--- a/src/main/download/downloadManager.ts
+++ b/src/main/download/downloadManager.ts
@@ -7,6 +7,7 @@ import type { BrowserWindow } from 'electron'
import { app, Notification, powerSaveBlocker, safeStorage, shell } from 'electron'
import { scheduleWindow } from '../../shared/downloadSchedule'
import { IpcChannels } from '../../shared/ipc-channels'
+import { taskbarProgress, type TaskbarDownload } from '../../shared/taskbarProgress'
import {
DOWNLOADS_AT_ONCE,
SPEED_HISTORY_SECONDS,
@@ -354,6 +355,8 @@ export class DownloadManager {
private runtimes = new Map<string, DownloadRuntime>()
/** Downloads whose new link is being checked: two at once could both pass and both resume. */
private relinking = new Set<string>()
+ /** Downloads that were paused during this session (not restored as paused on startup). */
+ private pausedInSession = new Set<string>()
private readonly initialization: Promise<void>
private suspending = false
private scheduleLoaded = false
@@ -743,7 +746,9 @@ export class DownloadManager {
return
}
this.runtimes.delete(runtime.state.id)
+ this.pausedInSession.delete(runtime.state.id)
await this.removePersistedDownload(runtime, false)
+ this.updateTaskbarProgress()
this.historyChanged()
}
@@ -1068,6 +1073,7 @@ export class DownloadManager {
runtime.state.status = 'paused'
runtime.state.queuedAt = undefined
runtime.state.pausedAt ??= Date.now()
+ this.pausedInSession.add(id)
this.pushUpdate(runtime)
await runtime.runPromise
await this.persistNow(runtime)
@@ -1081,6 +1087,7 @@ export class DownloadManager {
} else {
runtime.state.status = 'paused'
runtime.state.scheduled = undefined
+ this.pausedInSession.add(id)
}
runtime.state.pausedAt = Date.now()
clearSpeeds(runtime.state)
@@ -1286,6 +1293,7 @@ export class DownloadManager {
void this.persistNow(runtime)
return
}
+ this.pausedInSession.delete(runtime.state.id)
runtime.state.scheduled = this.downloadSchedule?.enabled || undefined
const { networks } = runtime.state
runtime.state.status = 'downloading'
@@ -1590,7 +1598,9 @@ export class DownloadManager {
await trashDownload(runtime.state, owned)
}
this.runtimes.delete(id)
+ this.pausedInSession.delete(id)
await this.removePersistedDownload(runtime)
+ this.updateTaskbarProgress()
if (runtime.state.status === 'completed') {
await removeFromHistory([id])
this.historyChanged()
@@ -1652,6 +1662,7 @@ export class DownloadManager {
})
)
await this.limits.save()
+ this.updateTaskbarProgress()
}
/**
@@ -1768,7 +1779,11 @@ export class DownloadManager {
runtime.state.bytesDownloaded
await this.persistNow(runtime)
await runtime.file.discardLeftover().catch(() => {})
- this.notify('Download complete', `${runtime.state.fileName} has finished downloading.`)
+ this.notify(
+ 'Download complete',
+ `${runtime.state.fileName} has finished downloading. Click to show in folder.`,
+ runtime.state.id
+ )
} catch (error) {
runtime.state.status = 'error'
runtime.state.error = error instanceof Error ? error.message : String(error)
@@ -1793,6 +1808,7 @@ export class DownloadManager {
* `discard`: bytes that are no use any more. */
private failDownload(runtime: DownloadRuntime, message: string, discard = false): void {
if (runtime.state.status !== 'downloading') return
+ this.pausedInSession.delete(runtime.state.id)
runtime.state.status = 'error'
runtime.state.error = message
runtime.state.resumable = !discard
@@ -1888,16 +1904,20 @@ export class DownloadManager {
runtime.transfer.reconcile()
}
- private notify(title: string, body: string): void {
+ private notify(title: string, body: string, downloadId?: string): void {
if (testKnobs.userDataDir || !Notification.isSupported()) return
try {
const notification = new Notification({ title, body })
notification.on('click', () => {
- const window = this.getWindow()
- if (window && !window.isDestroyed()) {
- if (window.isMinimized()) window.restore()
- window.show()
- window.focus()
+ if (downloadId) {
+ void this.reveal(downloadId)
+ } else {
+ const window = this.getWindow()
+ if (window && !window.isDestroyed()) {
+ if (window.isMinimized()) window.restore()
+ window.show()
+ window.focus()
+ }
}
})
notification.show()
@@ -1906,6 +1926,30 @@ export class DownloadManager {
}
}
+ private updateTaskbarProgress(): void {
+ if (testKnobs.userDataDir) return
+ const window = this.getWindow()
+ if (!window || window.isDestroyed()) return
+
+ const states: TaskbarDownload[] = []
+ for (const runtime of this.runtimes.values()) {
+ if (runtime.state.status === 'downloading') {
+ states.push(runtime.state)
+ } else if (runtime.state.status === 'paused' && this.pausedInSession.has(runtime.state.id)) {
+ states.push(runtime.state)
+ }
+ }
+
+ const { progress, mode } = taskbarProgress(states)
+ if (mode === 'indeterminate') {
+ window.setProgressBar(2, { mode: 'indeterminate' })
+ } else if (mode === 'none' || progress < 0) {
+ window.setProgressBar(-1, { mode: 'none' })
+ } else {
+ window.setProgressBar(progress, { mode })
+ }
+ }
+
private scheduleUpdate(runtime: DownloadRuntime): void {
if (runtime.pushScheduled) return
runtime.pushScheduled = true
@@ -1926,6 +1970,7 @@ export class DownloadManager {
if (runtime.state.status === 'paused' || runtime.state.status === 'cancelled') {
clearSpeeds(runtime.state)
}
+ this.updateTaskbarProgress()
window.webContents.send(IpcChannels.downloadUpdated, this.takeUpdate(runtime))
}
diff --git a/src/shared/taskbarProgress.ts b/src/shared/taskbarProgress.ts
new file mode 100644
index 0000000..ec18715
--- /dev/null
+++ b/src/shared/taskbarProgress.ts
@@ -0,0 +1,84 @@
+import type { DownloadStatus } from './types'
+
+export type TaskbarProgressMode = 'normal' | 'paused' | 'indeterminate' | 'none'
+
+export interface TaskbarProgressResult {
+ progress: number
+ mode: TaskbarProgressMode
+}
+
+export interface TaskbarDownload {
+ status: DownloadStatus
+ totalBytes: number
+ bytesDownloaded: number
+ kind?: 'http' | 'torrent'
+ skippedBytes?: number
+}
+
+/** What a download fetches: all of it, bar a torrent's pieces no chosen file needs. */
+export function wantedBytes(
+ download: Pick<TaskbarDownload, 'totalBytes' | 'kind' | 'skippedBytes'>
+): number {
+ return download.totalBytes - (download.kind === 'torrent' ? (download.skippedBytes ?? 0) : 0)
+}
+
+/**
+ * Calculates overall taskbar progress across active or paused downloads.
+ * Returns progress between 0 and 1 (or 2 for indeterminate, -1 for none) and the mode.
+ */
+export function taskbarProgress(states: Iterable<TaskbarDownload>): TaskbarProgressResult {
+ const downloading: TaskbarDownload[] = []
+ const paused: TaskbarDownload[] = []
+
+ for (const s of states) {
+ if (s.status === 'downloading') {
+ downloading.push(s)
+ } else if (s.status === 'paused') {
+ paused.push(s)
+ }
+ }
+
+ if (downloading.length === 0) {
+ if (paused.length > 0) {
+ let totalWanted = 0
+ let downloaded = 0
+ for (const r of paused) {
+ const wanted = wantedBytes(r)
+ if (wanted > 0) {
+ totalWanted += wanted
+ downloaded += r.bytesDownloaded || 0
+ }
+ }
+ if (totalWanted > 0) {
+ const ratio = Math.min(Math.max(downloaded / totalWanted, 0), 1)
+ return { progress: ratio, mode: 'paused' }
+ }
+ }
+ return { progress: -1, mode: 'none' }
+ }
+
+ let totalWanted = 0
+ let downloaded = 0
+ let indeterminate = false
+
+ for (const r of downloading) {
+ const wanted = wantedBytes(r)
+ if (wanted > 0) {
+ totalWanted += wanted
+ downloaded += r.bytesDownloaded || 0
+ } else {
+ indeterminate = true
+ }
+ }
+
+ if (indeterminate && totalWanted === 0) {
+ return { progress: 2, mode: 'indeterminate' }
+ }
+
+ if (totalWanted > 0) {
+ const ratio = Math.min(Math.max(downloaded / totalWanted, 0), 1)
+ return { progress: ratio, mode: 'normal' }
+ }
+
+ return { progress: -1, mode: 'none' }
+}