diff --git a/include/kernel/dguiapplicationhelper.h b/include/kernel/dguiapplicationhelper.h index 6d210cd7..bacb6881 100644 --- a/include/kernel/dguiapplicationhelper.h +++ b/include/kernel/dguiapplicationhelper.h @@ -50,6 +50,7 @@ class DGuiApplicationHelper : public QObject, public DCORE_NAMESPACE::DObject enum Attribute { UseInactiveColorGroup = 1 << 0, ColorCompositing = 1 << 1, + DontSaveApplicationTheme = 1 << 2, /* readonly flag */ ReadOnlyLimit = 1 << 22, diff --git a/misc/org.deepin.dtk.ui.preference.json b/misc/org.deepin.dtk.ui.preference.json index 089f4348..47066cfe 100644 --- a/misc/org.deepin.dtk.ui.preference.json +++ b/misc/org.deepin.dtk.ui.preference.json @@ -11,6 +11,17 @@ "description[zh_CN]": "配置标题栏的高度", "permissions": "readwrite", "visibility": "public" + }, + "themeType": { + "value": 0, + "serial": 0, + "flags": [], + "name": "The application theme type", + "name[zh_CN]": "应用主题的颜色", + "description": "The application theme type, which can be set to follow the system theme (0), light theme (1), dark theme (2)", + "description[zh_CN]": "应用主题的颜色,可以设置为跟随系统(0)、浅色(1)、 深色(2),默认为跟随系统", + "permissions": "readwrite", + "visibility": "public" } } } diff --git a/src/kernel/dguiapplicationhelper.cpp b/src/kernel/dguiapplicationhelper.cpp index e9691800..0f20d748 100644 --- a/src/kernel/dguiapplicationhelper.cpp +++ b/src/kernel/dguiapplicationhelper.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #endif #include #include @@ -106,6 +107,9 @@ Q_GLOBAL_STATIC(DFontManager, _globalFM) #define WINDOW_THEME_KEY "_d_platform_theme" +#define APP_THEME_TYPE "themeType" +Q_GLOBAL_STATIC_WITH_ARGS(DTK_CORE_NAMESPACE::DConfig, _d_dconfig, ("org.deepin.dtk.ui.preference")); + /*! @private */ @@ -201,6 +205,16 @@ void DGuiApplicationHelperPrivate::init() } } +static void applyThemeType() +{ + DCORE_USE_NAMESPACE + int ct = _d_dconfig->value(APP_THEME_TYPE, DGuiApplicationHelper::UnknownType).toInt(); + if (ct > DGuiApplicationHelper::DarkType || ct < DGuiApplicationHelper::UnknownType) + ct = DGuiApplicationHelper::UnknownType; + + DGuiApplicationHelper::instance()->setPaletteType(DGuiApplicationHelper::ColorType(ct)); +} + void DGuiApplicationHelperPrivate::initApplication(QGuiApplication *app) { D_Q(DGuiApplicationHelper); @@ -212,6 +226,22 @@ void DGuiApplicationHelperPrivate::initApplication(QGuiApplication *app) // 直接对应到系统级别的主题, 不再对外提供为某个单独程序设置主题的接口. // 程序设置自身主题相关的东西皆可通过 setPaletteType 和 setApplicationPalette 实现. appTheme = systemTheme; + + if (!q->testAttribute(DGuiApplicationHelper::DontSaveApplicationTheme)) { + applyThemeType(); + + DCORE_USE_NAMESPACE + auto con = QObject::connect(_d_dconfig, &DConfig::valueChanged, _d_dconfig, [](const QString &key){ + if (key != APP_THEME_TYPE) + return; + applyThemeType(); + }); + QObject::connect(qGuiApp, &QGuiApplication::aboutToQuit, _d_dconfig, [con](){ + QObject::disconnect(con); + int paletteType = DGuiApplicationHelper::instance()->paletteType(); + _d_dconfig->setValue(APP_THEME_TYPE, paletteType); + }); + } } // 跟随application销毁