feat: add DontSaveApplciationTheme Attribute
保存主题功能让非 widget 应用也能用上, 默认行为记住主题 如果不需要记住请构造 app 前 setAttibute(DontSaveApplicationTheme) 注意:因为可能无法正确获取 appid ,通过 dde-dconfig-editor 设置的主题可能无效,但是应用自行设置的主题可以记住 Log:
This commit is contained in:
parent
6c06cf37cd
commit
19492cbfb7
|
|
@ -50,6 +50,7 @@ public:
|
|||
enum Attribute {
|
||||
UseInactiveColorGroup = 1 << 0,
|
||||
ColorCompositing = 1 << 1,
|
||||
DontSaveApplicationTheme = 1 << 2,
|
||||
|
||||
/* readonly flag */
|
||||
ReadOnlyLimit = 1 << 22,
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@
|
|||
#include <QDBusConnection>
|
||||
#include <QDBusConnectionInterface>
|
||||
#include <QProcess>
|
||||
#include <DConfig>
|
||||
#endif
|
||||
#include <QDir>
|
||||
#include <QLockFile>
|
||||
|
|
@ -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销毁
|
||||
|
|
|
|||
Loading…
Reference in New Issue