Skip to content

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

refactor(service): FeatureService 去 Qt,ClipboardListener 移至 View 层

XiaoYouChR3a5a487663a5
diff --git a/Ghost-Downloader-3.py b/Ghost-Downloader-3.py
index 0280abdc..2745d3c4 100644
--- a/Ghost-Downloader-3.py
+++ b/Ghost-Downloader-3.py
@@ -67,7 +67,7 @@ def startApp(application, isSilent=False):
from PySide6.QtGui import QIcon
from app.config.cfg import cfg
from app.config.paths import executableDir
- from app.services.clipboard_listener import ClipboardListener
+ from app.view.clipboard_listener import ClipboardListener
from app.signal_bus import signalBus
from app.startup import loadEngine, createServices, loadPacks, startEngine, bindNotifications, checkUpdateAtStartup, stopEngine
from app.view.windows.main_window import MainWindow
diff --git a/app/services/feature_service.py b/app/services/feature_service.py
index 5fde328c..0bdfc9e4 100644
--- a/app/services/feature_service.py
+++ b/app/services/feature_service.py
@@ -1,11 +1,10 @@
from __future__ import annotations
from dataclasses import replace
+from threading import Event
from typing import TYPE_CHECKING
from urllib.parse import urlparse
-from PySide6.QtCore import QObject
-
from app.config.paths import FEATURES_DIR
from app.models.task import TaskOptions
from app.platform import file_association
@@ -19,9 +18,8 @@ if TYPE_CHECKING:
from app.view.components.setting_card_group import CollapsibleSettingCardGroup
-class FeatureService(QObject):
- def __init__(self, taskService, categoryService, coroutineRunner, runtimeStatusService, parent=None):
- super().__init__(parent)
+class FeatureService:
+ def __init__(self, taskService, categoryService, coroutineRunner, runtimeStatusService):
self._taskService = taskService
self._categoryService = categoryService
self._coroutineRunner = coroutineRunner
@@ -39,12 +37,12 @@ class FeatureService(QObject):
for pack in loadPacks(FEATURES_DIR, services):
self._register(pack)
- def activate(self, coroutineRunner) -> None:
+ def activate(self) -> None:
async def activateAll():
for pack in self._packs:
await pack.activate()
- coroutineRunner.submit(activateAll())
+ self._coroutineRunner.submit(activateAll())
def _register(self, pack: FeaturePack) -> None:
self._packs.append(pack)
@@ -216,13 +214,15 @@ class FeatureService(QObject):
for s in enabledSchemes:
url_scheme.registerUrlScheme(s.scheme)
- def deactivate(self, coroutineRunner) -> None:
- from PySide6.QtCore import QEventLoop
+ def deactivate(self) -> None:
+ event = Event()
async def deactivateAll():
- for pack in self._packs:
- await pack.deactivate()
-
- loop = QEventLoop()
- coroutineRunner.submit(deactivateAll(), done=lambda _: loop.quit())
- loop.exec()
+ try:
+ for pack in self._packs:
+ await pack.deactivate()
+ finally:
+ event.set()
+
+ self._coroutineRunner.submit(deactivateAll())
+ event.wait()
diff --git a/app/startup.py b/app/startup.py
index bd81e89d..01a7a998 100644
--- a/app/startup.py
+++ b/app/startup.py
@@ -86,7 +86,7 @@ def startEngine(taskService, speedMeter, featureService, coroutineRunner):
taskService.taskStarted.connect(lambda _: speedMeter.start())
taskService.tasksAllCompleted.connect(speedMeter.stop)
taskService.resumeSaved()
- featureService.activate(coroutineRunner)
+ featureService.activate()
def bindNotifications(taskService, notifyCompleted, notifyDiskSpace):
@@ -107,7 +107,7 @@ def stopEngine(taskService, browserService, aria2RpcServer, featureService, coro
speedMeter.stop()
browserService.stop()
aria2RpcServer.stop()
- featureService.deactivate(coroutineRunner)
+ featureService.deactivate()
taskService.flush()
if updateService is not None:
updateService.apply()
diff --git a/app/services/clipboard_listener.py b/app/view/clipboard_listener.py
similarity index 100%
rename from app/services/clipboard_listener.py
rename to app/view/clipboard_listener.py