mirror of
https://github.com/KenanZhu/AutoLibrary.git
synced 2026-06-21 00:43:03 +08:00
refactor(theme): 将重复的主题逻辑下沉至 ThemeManager
ThemeManager 新增: - clearTheme(theme) — 清除 QSS 并应用指定色调 - applyThemeOrClear(name, fallback) — 应用或回退的封装 - _applyColorScheme(theme) — Qt ColorScheme 设置的统一入口 - themeToReadable(need_theme) — 静态工具方法 ALSettingsWidget 移除: - _clearCustomTheme → 改用 themeInstance().clearTheme() - _applyCustomTheme → 改用 themeInstance().applyThemeOrClear() - _themeToReadable → 改用 ThemeManager.themeToReadable() ALSettingsWidget 仅保留 _applyTheme(含 setStyle 逻辑,供 AppInitializer 使用)
This commit is contained in:
@@ -31,7 +31,10 @@ from PySide6.QtWidgets import (
|
||||
)
|
||||
|
||||
import managers.config.ConfigManager as ConfigManager
|
||||
from managers.theme.ThemeManager import instance as themeInstance
|
||||
from managers.theme.ThemeManager import (
|
||||
ThemeManager,
|
||||
instance as themeInstance
|
||||
)
|
||||
|
||||
from gui.resources.ui.Ui_ALSettingsWidget import Ui_ALSettingsWidget
|
||||
from interfaces.ConfigProvider import (
|
||||
@@ -50,28 +53,6 @@ def _setActiveStyleName(
|
||||
global _active_style_name
|
||||
_active_style_name = name
|
||||
|
||||
def _clearCustomTheme(
|
||||
theme: str
|
||||
):
|
||||
|
||||
app : QApplication | None = QApplication.instance()
|
||||
if app:
|
||||
app.setStyleSheet("")
|
||||
_applyTheme(theme)
|
||||
|
||||
def _applyCustomTheme(
|
||||
name: str,
|
||||
fallback_theme: str = "system"
|
||||
):
|
||||
|
||||
if not name:
|
||||
_clearCustomTheme(fallback_theme)
|
||||
return
|
||||
try:
|
||||
themeInstance().applyTheme(name)
|
||||
except Exception:
|
||||
_clearCustomTheme(fallback_theme)
|
||||
|
||||
def _applyTheme(
|
||||
theme: str
|
||||
):
|
||||
@@ -94,19 +75,6 @@ def _restartApp(
|
||||
QApplication.instance().quit()
|
||||
QProcess.startDetached(sys.executable, sys.argv)
|
||||
|
||||
def _themeToReadable(
|
||||
theme: str
|
||||
) -> str:
|
||||
|
||||
if theme == "dark":
|
||||
return "深色"
|
||||
elif theme == "light":
|
||||
return "浅色"
|
||||
elif theme == "both":
|
||||
return "所有"
|
||||
else:
|
||||
return "未知"
|
||||
|
||||
class ALSettingsWidget(QWidget, Ui_ALSettingsWidget):
|
||||
|
||||
settingsWidgetIsClosed = Signal()
|
||||
@@ -136,8 +104,8 @@ class ALSettingsWidget(QWidget, Ui_ALSettingsWidget):
|
||||
self.setNavigationIcons()
|
||||
self.ThemeInfoLabel.setTextFormat(Qt.TextFormat.RichText)
|
||||
self.ThemeInfoLabel.setStyleSheet(
|
||||
"border: 1px solid #ccc; " \
|
||||
"border-radius: 2px;" \
|
||||
"border: 1px solid palette(mid);"\
|
||||
"border-radius: 2px;"\
|
||||
"padding: 5px;"
|
||||
)
|
||||
|
||||
@@ -256,7 +224,7 @@ class ALSettingsWidget(QWidget, Ui_ALSettingsWidget):
|
||||
need_theme = t.get("need_theme", "both")
|
||||
brief = t.get("brief", "没有相关简介")
|
||||
self.ThemeInfoLabel.setText(
|
||||
f"<b>{name}</b> - 适用于 <i>{_themeToReadable(need_theme)}</i> 主题<br>"
|
||||
f"<b>{name}</b> - 适用于 <i>{ThemeManager.themeToReadable(need_theme)}</i> 主题<br>"
|
||||
f"作者:{author}<br><br>"
|
||||
f"{brief}"
|
||||
)
|
||||
@@ -299,7 +267,7 @@ class ALSettingsWidget(QWidget, Ui_ALSettingsWidget):
|
||||
theme, style, custom_theme = self.collectSettings()
|
||||
self.__cfg_mgr.set(CfgKey.GLOBAL.APPEARANCE.STYLE, style)
|
||||
self.__cfg_mgr.set(CfgKey.GLOBAL.APPEARANCE.CUSTOM_THEME, custom_theme)
|
||||
_applyCustomTheme(custom_theme, theme)
|
||||
themeInstance().applyThemeOrClear(custom_theme, theme)
|
||||
self.syncRadioFromNeedTheme(custom_theme)
|
||||
theme, _, _ = self.collectSettings()
|
||||
self.__cfg_mgr.set(CfgKey.GLOBAL.APPEARANCE.THEME, theme)
|
||||
@@ -391,6 +359,7 @@ class ALSettingsWidget(QWidget, Ui_ALSettingsWidget):
|
||||
self.DarkThemeRadio.setChecked(True)
|
||||
else:
|
||||
self.SystemThemeRadio.setChecked(True)
|
||||
themeInstance().clearTheme(self.__original_theme)
|
||||
self.updateThemeInfo()
|
||||
|
||||
@Slot()
|
||||
|
||||
Reference in New Issue
Block a user