Skip to content

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

feat(runtime): 运行时安装按钮显示进度环

XiaoYouChR2c00595cb161
diff --git a/app/view/components/install_button.py b/app/view/components/install_button.py
new file mode 100644
index 00000000..d2bd80a1
--- /dev/null
+++ b/app/view/components/install_button.py
@@ -0,0 +1,72 @@
+from __future__ import annotations
+
+from PySide6.QtCore import QRectF, Qt
+from PySide6.QtGui import QColor, QPainter
+from PySide6.QtWidgets import QHBoxLayout
+from qfluentwidgets import IndeterminateProgressRing, PrimaryPushButton, ProgressRing, isDarkTheme
+
+RING_SIZE = 16
+STOP_SIZE = 6
+QWIDGETSIZE_MAX = (1 << 24) - 1
+
+
+class InstallButton(PrimaryPushButton):
+ def __init__(self, parent=None):
+ super().__init__(parent)
+ self.ring = ProgressRing(self, useAni=False)
+ self.spinner = IndeterminateProgressRing(self, start=False)
+ self._isInstalling = False
+
+ self._initWidget()
+ self._initLayout()
+
+ def _initWidget(self) -> None:
+ for ring in (self.ring, self.spinner):
+ ring.setFixedSize(RING_SIZE, RING_SIZE)
+ ring.setStrokeWidth(2)
+ ring.setCustomBarColor(QColor(255, 255, 255), QColor(0, 0, 0))
+ ring.setCustomBackgroundColor(QColor(255, 255, 255, 90), QColor(0, 0, 0, 60))
+ ring.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents)
+ ring.hide()
+
+ def _initLayout(self) -> None:
+ layout = QHBoxLayout(self)
+ layout.setContentsMargins(0, 0, 0, 0)
+ layout.addWidget(self.ring, 0, Qt.AlignmentFlag.AlignCenter)
+ layout.addWidget(self.spinner, 0, Qt.AlignmentFlag.AlignCenter)
+
+ def setInstalling(self, isInstalling: bool) -> None:
+ if isInstalling == self._isInstalling:
+ return
+ self._isInstalling = isInstalling
+ if isInstalling:
+ self.setFixedWidth(self.sizeHint().width())
+ self.setText("")
+ return
+ self.setMinimumWidth(0)
+ self.setMaximumWidth(QWIDGETSIZE_MAX)
+ self.ring.hide()
+ self.spinner.stop()
+ self.spinner.hide()
+
+ def setProgress(self, progress: int) -> None:
+ isDownloading = 0 < progress < 100
+ self.ring.setValue(progress)
+ self.ring.setVisible(isDownloading)
+ if isDownloading:
+ self.spinner.stop()
+ self.spinner.hide()
+ elif self.spinner.isHidden():
+ self.spinner.show()
+ self.spinner.start()
+
+ def paintEvent(self, e):
+ super().paintEvent(e)
+ if not self._isInstalling:
+ return
+ painter = QPainter(self)
+ painter.setRenderHint(QPainter.RenderHint.Antialiasing)
+ painter.setPen(Qt.PenStyle.NoPen)
+ painter.setBrush(QColor(0, 0, 0) if isDarkTheme() else QColor(255, 255, 255))
+ rect = QRectF((self.width() - STOP_SIZE) / 2, (self.height() - STOP_SIZE) / 2, STOP_SIZE, STOP_SIZE)
+ painter.drawRoundedRect(rect, 1.5, 1.5)
diff --git a/app/view/components/setting_cards.py b/app/view/components/setting_cards.py
index 977b55c4..a411209e 100644
--- a/app/view/components/setting_cards.py
+++ b/app/view/components/setting_cards.py
@@ -23,6 +23,7 @@ from app.config.cfg import cfg, proxy, currentHeaders, currentHeadersPresetIndex
from app.i18n import toLocalizedError
from app.models.task import toTaskError
from app.view.components.banners import WarningBanner
+from app.view.components.install_button import InstallButton
HOST_PATTERN = compile(
r"^(?:(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)"
@@ -809,8 +810,7 @@ class RuntimeCard(SettingCard):
self._runtime: BinaryRuntime = runtime
super().__init__(FluentIcon.INFO, runtime.name, self.tr("正在检测运行时..."), parent)
- self.installButton = PrimaryPushButton(self.tr("一键安装"), self)
- self.cancelButton = ToolButton(FluentIcon.CLOSE, self)
+ self.installButton = InstallButton(self)
self.refreshButton = ToolButton(FluentIcon.SYNC, self)
self.deleteButton = ToolButton(FluentIcon.DELETE, self)
@@ -822,9 +822,8 @@ class RuntimeCard(SettingCard):
def _initWidget(self) -> None:
if not self._runtime.canInstall:
self.installButton.hide()
- self.cancelButton.hide()
- self.cancelButton.setToolTip(self.tr("取消安装"))
- self.cancelButton.installEventFilter(ToolTipFilter(self.cancelButton))
+ self.installButton.setText(self.tr("一键安装"))
+ self.installButton.installEventFilter(ToolTipFilter(self.installButton))
self.deleteButton.hide()
self.deleteButton.setToolTip(self.tr("卸载"))
self.deleteButton.installEventFilter(ToolTipFilter(self.deleteButton))
@@ -836,7 +835,6 @@ class RuntimeCard(SettingCard):
self._buttonLayout.setContentsMargins(0, 0, 0, 0)
self._buttonLayout.setSpacing(8)
self._buttonLayout.addWidget(self.installButton)
- self._buttonLayout.addWidget(self.cancelButton)
self._buttonLayout.addWidget(self.refreshButton)
self._buttonLayout.addWidget(self.deleteButton)
self.hBoxLayout.addLayout(self._buttonLayout)
@@ -845,7 +843,6 @@ class RuntimeCard(SettingCard):
def _bind(self) -> None:
self._runtimeStatusService.statusChanged.connect(self._onRuntimeStatusChanged, owner=self)
self.installButton.clicked.connect(self._onInstallClicked)
- self.cancelButton.clicked.connect(self._onCancelClicked)
self.deleteButton.clicked.connect(self._onDeleteClicked)
self.refreshButton.clicked.connect(self._onRefreshClicked)
@@ -871,20 +868,22 @@ class RuntimeCard(SettingCard):
self.setContent(self.tr("正在下载... {0}%").format(status.progress))
else:
self.setContent(self.tr("正在安装..."))
- self.installButton.hide()
- self.cancelButton.setVisible(self._runtime.canInstall)
+ self.installButton.setInstalling(True)
+ self.installButton.setProgress(status.progress)
+ self.installButton.setToolTip(self.tr("取消安装"))
+ self.installButton.setVisible(self._runtime.canInstall)
self.deleteButton.hide()
return
+ self.installButton.setInstalling(False)
+ self.installButton.setToolTip("")
+
if status.isBusy:
self.setContent(self.tr("正在检测运行时..."))
self.installButton.hide()
- self.cancelButton.hide()
self.deleteButton.hide()
return
- self.cancelButton.hide()
-
if status.error is not None:
self.setContent(toLocalizedError(status.error))
elif isInstalled:
@@ -926,10 +925,10 @@ class RuntimeCard(SettingCard):
self.updateStatus(status)
def _onInstallClicked(self) -> None:
- self._runtimeStatusService.install(self._runtime)
-
- def _onCancelClicked(self) -> None:
- self._runtimeStatusService.cancelInstall(self._runtime)
+ if self._runtimeStatusService.status(self._runtime).isInstalling:
+ self._runtimeStatusService.cancelInstall(self._runtime)
+ else:
+ self._runtimeStatusService.install(self._runtime)
def _onDeleteClicked(self) -> None:
from qfluentwidgets import MessageBox