Skip to content

The only downloader you need. 下载器的集大成者。

feat(macOS): 重做 Dock 速度显示样式,新增下载进度条

XiaoYouChR63f3ac018965
diff --git a/app/format.py b/app/format.py
index def4e8c5..8b858ccd 100644
--- a/app/format.py
+++ b/app/format.py
@@ -1,3 +1,16 @@
+def toDockSpeed(bytesPerSec: int) -> str:
+ if bytesPerSec < 1024:
+ return f"{bytesPerSec} B/s"
+ v = bytesPerSec / 1024
+ if v < 1024:
+ return f"{v:.1f} K/s"
+ v /= 1024
+ if v < 1024:
+ return f"{v:.1f} M/s"
+ v /= 1024
+ return f"{v:.1f} G/s"
+
+
def toReadableSize(size: int) -> str:
for unit in ["B", "KB", "MB", "GB"]:
if size < 1024.0:
diff --git a/app/services/task_service.py b/app/services/task_service.py
index e2860868..246e1c48 100644
--- a/app/services/task_service.py
+++ b/app/services/task_service.py
@@ -152,6 +152,21 @@ class TaskService(QObject):
def runningCount(self) -> int:
return self._queue.runningCount()
+ def runningProgress(self) -> float:
+ from app.models.task import TaskStatus
+ totalReceived = 0
+ totalSize = 0
+ for task in self._store.tasks.values():
+ if task.status != TaskStatus.RUNNING:
+ continue
+ _, _, receivedBytes = task.currentSnapshot()
+ if task.fileSize > 0:
+ totalReceived += receivedBytes
+ totalSize += task.fileSize
+ if totalSize == 0:
+ return -1.0
+ return min(100.0, totalReceived / totalSize * 100)
+
def add(self, task: Task) -> None:
if task.taskId in self._store.tasks:
return
diff --git a/app/view/shell/dock.py b/app/view/shell/dock.py
index c1d30eba..fb1743e1 100644
--- a/app/view/shell/dock.py
+++ b/app/view/shell/dock.py
@@ -1,14 +1,14 @@
-from PySide6.QtCore import QBuffer, QByteArray, QRectF, Qt
-from PySide6.QtGui import QColor, QFontMetricsF, QPainter, QPixmap
+from PySide6.QtCore import QBuffer, QByteArray, Qt
+from PySide6.QtGui import QColor, QFontMetricsF, QPainter, QPainterPath, QPen, QPixmap
from AppKit import (
- NSApp, NSApplication, NSImage, NSImageView,
+ NSApp, NSApplication, NSImage, NSImageView, NSProgressIndicator, NSView,
NSApplicationActivationPolicyRegular, NSApplicationActivationPolicyAccessory,
)
from Foundation import NSData
from app.config.cfg import cfg
-from app.format import toReadableSize
+from app.format import toDockSpeed
from app.services.speed_meter import speedMeter
@@ -22,11 +22,15 @@ def setDockIconVisible(visible: bool, activate: bool) -> None:
def setupDock() -> None:
+ from app.services.task_service import taskService
+
baseIcon = None
+ container = None
imageView = None
+ progressBar = None
- def render(text: str) -> None:
- nonlocal baseIcon, imageView
+ def update(text: str, progress: float) -> None:
+ nonlocal baseIcon, container, imageView, progressBar
if baseIcon is None:
baseIcon = QPixmap(":/image/logo_macOS.png").scaled(
@@ -40,24 +44,27 @@ def setupDock() -> None:
painter = QPainter(canvas)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
- margin = size * 0.11
- bandHeight = size * 0.24
- band = QRectF(margin * 1.3, size - margin - bandHeight, size - margin * 2.6, bandHeight)
-
- painter.setPen(Qt.PenStyle.NoPen)
- painter.setBrush(QColor(0, 0, 0, 165))
- painter.drawRoundedRect(band, bandHeight / 2, bandHeight / 2)
-
font = painter.font()
font.setBold(True)
- pixel = bandHeight * 0.5
- font.setPixelSize(int(pixel))
- while QFontMetricsF(font).horizontalAdvance(text) > band.width() * 0.88 and pixel > 6:
+ pixel = int(size * 0.14)
+ font.setPixelSize(pixel)
+ maxWidth = size * 0.85
+ while QFontMetricsF(font).horizontalAdvance(text) > maxWidth and pixel > 6:
pixel -= 1
- font.setPixelSize(int(pixel))
- painter.setFont(font)
- painter.setPen(QColor(255, 255, 255))
- painter.drawText(band, Qt.AlignmentFlag.AlignCenter, text)
+ font.setPixelSize(pixel)
+
+ fm = QFontMetricsF(font)
+ textX = (size - fm.horizontalAdvance(text)) / 2
+ textY = size * 0.82
+
+ path = QPainterPath()
+ path.addText(textX, textY, font, text)
+ painter.strokePath(
+ path,
+ QPen(QColor(0, 0, 0, 180), 12,
+ Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap, Qt.PenJoinStyle.RoundJoin),
+ )
+ painter.fillPath(path, QColor(255, 255, 255))
painter.end()
data = QByteArray()
@@ -69,37 +76,66 @@ def setupDock() -> None:
nsImage = NSImage.alloc().initWithData_(
NSData.dataWithBytes_length_(data.data(), data.size())
)
+
tile = (NSApp or NSApplication.sharedApplication()).dockTile()
- if imageView is None:
- imageView = NSImageView.alloc().init()
- tile.setContentView_(imageView)
+ if container is None:
+ tileSize = tile.size()
+ container = NSView.alloc().initWithFrame_(
+ ((0, 0), (tileSize.width, tileSize.height))
+ )
+ imageView = NSImageView.alloc().initWithFrame_(
+ ((0, 0), (tileSize.width, tileSize.height))
+ )
+ container.addSubview_(imageView)
+
+ barHeight = tileSize.height * 0.08
+ progressBar = NSProgressIndicator.alloc().initWithFrame_(
+ ((0, 0), (tileSize.width, barHeight))
+ )
+ progressBar.setStyle_(0)
+ progressBar.setMinValue_(0)
+ progressBar.setMaxValue_(100)
+ progressBar.setIndeterminate_(False)
+ container.addSubview_(progressBar)
+
+ tile.setContentView_(container)
+
imageView.setImage_(nsImage)
+
+ if progress >= 0:
+ progressBar.setHidden_(False)
+ progressBar.setDoubleValue_(progress)
+ else:
+ progressBar.setHidden_(True)
+
tile.display()
- def restore() -> None:
- nonlocal imageView
- if imageView is None:
+ def clear() -> None:
+ nonlocal container, imageView, progressBar
+ if container is None:
return
tile = (NSApp or NSApplication.sharedApplication()).dockTile()
tile.setContentView_(None)
tile.display()
+ container = None
imageView = None
+ progressBar = None
def onSpeedChanged(speed: int) -> None:
if not (cfg.shouldShowDockSpeed.value and cfg.shouldShowDockIcon.value):
return
if speed <= 0:
- restore()
+ clear()
return
- render(f"{toReadableSize(speed)}/s")
+ update(toDockSpeed(speed), taskService.runningProgress())
def onShowSpeedChanged(enabled: bool) -> None:
if not enabled:
- restore()
+ clear()
def onShowIconChanged(visible: bool) -> None:
setDockIconVisible(visible, activate=True)
- restore()
+ clear()
cfg.shouldShowDockIcon.valueChanged.connect(onShowIconChanged)
speedMeter.speedChanged.connect(onSpeedChanged)