Skip to content

Commit

Permalink
feat: add DontSaveApplciationTheme Attribute
Browse files Browse the repository at this point in the history
保存主题功能让非 widget 应用也能用上, 默认行为记住主题
如果不需要记住请构造 app 前 setAttibute(DontSaveApplicationTheme)
注意:因为可能无法正确获取 appid ,通过 dde-dconfig-editor
设置的主题可能无效,但是应用自行设置的主题可以记住

Log:
  • Loading branch information
kegechen committed Apr 14, 2023
1 parent 6c06cf3 commit 19492cb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/kernel/dguiapplicationhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions misc/org.deepin.dtk.ui.preference.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
30 changes: 30 additions & 0 deletions src/kernel/dguiapplicationhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QProcess>
#include <DConfig>
#endif
#include <QDir>
#include <QLockFile>
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
Expand All @@ -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销毁
Expand Down

0 comments on commit 19492cb

Please sign in to comment.