2222#include < QLoggingCategory>
2323#include < QGuiApplication>
2424#include < QQuickItem>
25+ #include < QDBusInterface>
26+ #include < QDBusReply>
27+ #include < QDBusVariant>
2528#include < DGuiApplicationHelper>
2629#include < DPlatformTheme>
2730
@@ -31,6 +34,24 @@ Q_LOGGING_CATEGORY(dockLog, "org.deepin.dde.shell.dock")
3134
3235namespace dock {
3336
37+ namespace {
38+ constexpr auto kAppearanceService = " org.deepin.dde.Appearance1" ;
39+ constexpr auto kAppearancePath = " /org/deepin/dde/Appearance1" ;
40+ constexpr auto kAppearanceInterface = " org.deepin.dde.Appearance1" ;
41+ constexpr auto kPropertiesInterface = " org.freedesktop.DBus.Properties" ;
42+
43+ ColorTheme colorThemeFromGlobalThemeName (const QString &themeName, const ColorTheme &fallbackTheme)
44+ {
45+ if (themeName.endsWith (QStringLiteral (" .dark" ), Qt::CaseInsensitive))
46+ return Dark;
47+
48+ if (themeName.endsWith (QStringLiteral (" .light" ), Qt::CaseInsensitive))
49+ return Light;
50+
51+ return fallbackTheme;
52+ }
53+ }
54+
3455DockPanel::DockPanel (QObject *parent)
3556 : DPanel (parent)
3657 , m_theme (ColorTheme::Dark)
@@ -160,9 +181,13 @@ bool DockPanel::init()
160181 QObject::connect (guiHelper, &Dtk::Gui::DGuiApplicationHelper::themeTypeChanged,
161182 this , &DockPanel::syncColorThemeWithSystem);
162183 if (auto *platformTheme = guiHelper->applicationTheme ()) {
163- QObject::connect (platformTheme, &DPlatformTheme::themeNameChanged,
184+ QObject::connect (platformTheme, &Dtk::Gui:: DPlatformTheme::themeNameChanged,
164185 this , &DockPanel::syncColorThemeWithSystem);
165186 }
187+ QDBusConnection::sessionBus ().connect (kAppearanceService , kAppearancePath , kAppearanceInterface ,
188+ " Changed" , this , SLOT (onAppearanceChanged (QString,QString)));
189+ QDBusConnection::sessionBus ().connect (kAppearanceService , kAppearancePath , kAppearanceInterface ,
190+ " Refreshed" , this , SLOT (onAppearanceRefreshed (QString)));
166191 syncColorThemeWithSystem ();
167192
168193 auto platformName = QGuiApplication::platformName ();
@@ -239,9 +264,41 @@ void DockPanel::setColorTheme(const ColorTheme& theme)
239264 Q_EMIT this ->colorThemeChanged (theme);
240265}
241266
267+ void DockPanel::onAppearanceChanged (const QString &type, const QString &value)
268+ {
269+ Q_UNUSED (value)
270+ if (type.compare (QStringLiteral (" GlobalTheme" ), Qt::CaseInsensitive) != 0 )
271+ return ;
272+
273+ syncColorThemeWithSystem ();
274+ }
275+
276+ void DockPanel::onAppearanceRefreshed (const QString &type)
277+ {
278+ if (type.compare (QStringLiteral (" GlobalTheme" ), Qt::CaseInsensitive) != 0 )
279+ return ;
280+
281+ syncColorThemeWithSystem ();
282+ }
283+
242284void DockPanel::syncColorThemeWithSystem ()
243285{
244- setColorTheme (static_cast <ColorTheme>(Dtk::Gui::DGuiApplicationHelper::instance ()->themeType ()));
286+ const auto fallbackTheme = static_cast <ColorTheme>(Dtk::Gui::DGuiApplicationHelper::instance ()->themeType ());
287+ QDBusInterface appearanceProperties (kAppearanceService ,
288+ kAppearancePath ,
289+ kPropertiesInterface ,
290+ QDBusConnection::sessionBus ());
291+ if (appearanceProperties.isValid ()) {
292+ const QDBusReply<QDBusVariant> reply = appearanceProperties.call (QStringLiteral (" Get" ),
293+ QString::fromLatin1 (kAppearanceInterface ),
294+ QStringLiteral (" GlobalTheme" ));
295+ if (reply.isValid ()) {
296+ setColorTheme (colorThemeFromGlobalThemeName (reply.value ().variant ().toString (), fallbackTheme));
297+ return ;
298+ }
299+ }
300+
301+ setColorTheme (fallbackTheme);
245302}
246303
247304uint DockPanel::dockSize ()
0 commit comments