Skip to content

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

refactor(theme): 重构主题颜色同步逻辑

XiaoYouChR1abd3552a69c
diff --git a/Ghost-Downloader-3.py b/Ghost-Downloader-3.py
index 20941aa9..9320b5fb 100644
--- a/Ghost-Downloader-3.py
+++ b/Ghost-Downloader-3.py
@@ -24,10 +24,6 @@ application = SingletonApplication(sys.argv, "gd3")
from loguru import logger
import time
import warnings
-from getpass import getuser
-from qfluentwidgets import setThemeColor
-from qframelesswindow.utils import getSystemAccentColor
-from PySide6.QtGui import QColor
from PySide6.QtCore import QTranslator
from app.supports.config import VERSION
@@ -56,21 +52,6 @@ featureService.loadFeatures(mainWindow)
taskRecorder.load()
mainWindow.taskPage.resumeMemorizedTasks()
-if sys.platform in {"win32", "darwin"}:
- setThemeColor(getSystemAccentColor(), save=False)
-if sys.platform == "linux":
-
- if 'KDE_SESSION_UID' in os.environ: # KDE Plasma
-
- import configparser
- config = configparser.ConfigParser()
-
- config.read(f"/home/{getuser()}/.config/kdeglobals")
-
- if 'Colors:Window' in config:
- color = list(map(int, config.get('Colors:Window', 'DecorationFocus').split(",")))
- setThemeColor(QColor(*color))
-
if not isSilently:
mainWindow.splashScreen.finish()
diff --git a/app/supports/utils.py b/app/supports/utils.py
index 0d4f5c0c..9b8347b1 100644
--- a/app/supports/utils.py
+++ b/app/supports/utils.py
@@ -6,38 +6,12 @@ from time import sleep
from typing import Callable
from PySide6.QtCore import QUrl, QOperatingSystemVersion, Qt
-from PySide6.QtGui import QDesktopServices, QColor
+from PySide6.QtGui import QDesktopServices
from loguru import logger
-from qfluentwidgets import MessageBox, setThemeColor
-from qframelesswindow.utils import getSystemAccentColor
+from qfluentwidgets import MessageBox
from app.supports.config import cfg
-
-def setAppColor(color: QColor | None = None):
- if color is None or not isinstance(color, QColor):
- if sys.platform == "win32" or "darwin":
- setThemeColor(getSystemAccentColor(), save=False)
- if sys.platform == "linux":
- if "KDE_SESSION_UID" in os.environ: # KDE Plasma
- import configparser
-
- config = configparser.ConfigParser()
- config.read(f"/home/{os.getlogin()}/.config/kdeglobals")
-
- # 获取 DecorationFocus 的值
- if "Colors:Window" in config:
- color = list(
- map(
- int,
- config.get("Colors:Window", "DecorationFocus").split(","),
- )
- )
- setThemeColor(QColor(*color))
- else:
- setThemeColor(color, save=False)
-
-
def isGreaterEqualWin10():
"""determine if the Windows version ≥ Win10"""
cv = QOperatingSystemVersion.current()
diff --git a/app/view/windows/main_window.py b/app/view/windows/main_window.py
index 206dcd8d..dc6b8cc0 100644
--- a/app/view/windows/main_window.py
+++ b/app/view/windows/main_window.py
@@ -4,12 +4,12 @@ from typing import TYPE_CHECKING
from urllib.parse import urlparse
import darkdetect
-from PySide6.QtCore import QRect, QPropertyAnimation, Qt, QUrl, QTimer
-from PySide6.QtGui import QDesktopServices, QIcon, QColor
+from PySide6.QtCore import QRect, QPropertyAnimation, Qt, QUrl, QEvent
+from PySide6.QtGui import QDesktopServices, QIcon, QColor, QPalette
from PySide6.QtWidgets import QApplication, QGraphicsOpacityEffect, QDialog
from loguru import logger
from qfluentwidgets import MSFluentWindow, SplashScreen, FluentIcon, NavigationItemPosition, InfoBar, InfoBarPosition, \
- PushButton, PrimaryPushButton, setTheme, Theme, isDarkTheme
+ PushButton, PrimaryPushButton, setTheme, Theme, isDarkTheme, setThemeColor, qconfig
from app.services.browser_service import BrowserService
from app.services.core_service import coreService
@@ -30,6 +30,14 @@ if TYPE_CHECKING:
from typing import Literal
from PySide6.QtGui import QClipboard
+
+BACKGROUND_EFFECT_REFRESH_EVENTS = {
+ QEvent.Type.PaletteChange,
+ QEvent.Type.StyleChange,
+ QEvent.Type.ThemeChange
+}
+
+
class CustomSplashScreen(SplashScreen):
def finish(self):
@@ -47,6 +55,7 @@ class CustomSplashScreen(SplashScreen):
class MainWindow(MSFluentWindow):
def __init__(self, isSilently = False):
+ self._pendingBackgroundEffectRefresh = False
super().__init__(parent = None)
self.setMicaEffectEnabled(False) # 禁用 QFluentWidgets 管理的背景效果
self.initWindow()
@@ -65,6 +74,7 @@ class MainWindow(MSFluentWindow):
self.connectSignalToSlot()
self._syncClipboardListener()
self._onThemeChanged(cfg.customThemeMode.value)
+ self._syncThemeColor()
if platform == 'win32':
self._applyBackgroundEffectByCfg(cfg.backgroundEffect.value)
@@ -89,6 +99,7 @@ class MainWindow(MSFluentWindow):
setTheme(Theme.LIGHT, save=False)
IconBodyLabel.clearCache()
+ self._pendingBackgroundEffectRefresh = False
if platform == 'win32':
self._applyBackgroundEffectByCfg(cfg.backgroundEffect.value)
@@ -109,6 +120,17 @@ class MainWindow(MSFluentWindow):
return self._darkBackgroundColor if isDarkTheme() else self._lightBackgroundColor
return QColor(0, 0, 0, 0)
+
+ def _syncThemeColor(self):
+ palette = self.palette()
+
+ for role in (QPalette.ColorRole.Accent, QPalette.ColorRole.Highlight):
+ color = palette.color(role)
+ if not color.isValid() or qconfig.themeColor.value == color:
+ continue
+
+ setThemeColor(color, save=False)
+ return
def _applyBackgroundEffectByCfg(self, value: "Literal['Acrylic', 'Mica', 'MicaBlur', 'MicaAlt', 'Aero', 'None']"):
if platform == 'win32':
@@ -143,15 +165,35 @@ class MainWindow(MSFluentWindow):
self.titleBar.maxBtn.show()
def _toggleTheme(self, callback: str):
- if callback == 'Dark': # Microsoft 特性,需要重试
+ if callback == 'Dark':
setTheme(Theme.DARK, save=False)
- if cfg.backgroundEffect.value in ['Mica', 'MicaBlur', 'MicaAlt']:
- QTimer.singleShot(500, lambda: self._applyBackgroundEffectByCfg(cfg.backgroundEffect.value))
-
elif callback == 'Light':
setTheme(Theme.LIGHT, save=False)
+ else:
+ setTheme(Theme.AUTO, save=False)
IconBodyLabel.clearCache()
+
+ if platform == 'win32' and cfg.backgroundEffect.value in ['Mica', 'MicaBlur', 'MicaAlt']:
+ self._pendingBackgroundEffectRefresh = True
+ return
+
+ self._pendingBackgroundEffectRefresh = False
+ self._applyBackgroundEffectByCfg(cfg.backgroundEffect.value)
+
+ def changeEvent(self, event):
+ super().changeEvent(event)
+
+ if event.type() in BACKGROUND_EFFECT_REFRESH_EVENTS:
+ self._syncThemeColor()
+
+ if not self._pendingBackgroundEffectRefresh:
+ return
+
+ if event.type() not in BACKGROUND_EFFECT_REFRESH_EVENTS:
+ return
+
+ self._pendingBackgroundEffectRefresh = False
self._applyBackgroundEffectByCfg(cfg.backgroundEffect.value)
def _syncClipboardListener(self):