From 49f05b043e382761dbf913677af5072f95d2df19 Mon Sep 17 00:00:00 2001 From: Oliver Eftevaag Date: Fri, 28 Mar 2025 09:56:27 +0100 Subject: [PATCH] GnomeTheme: read the initial color-scheme value from dbus The initial color-scheme value was never initialized, and would thus, always be 'Unknown', until the user changes the color scheme in the system settings. The xdg settings portal method ReadAll is currently omitting the contrast setting from the reply. Because of this, two separate dbus calls are necessary to get both the initial color-scheme and contrast setting. Change-Id: Ie32371efd830e19727922efd2c9c32595395d11a Reviewed-by: Axel Spoerl --- src/gui/platform/unix/qgnometheme.cpp | 57 ++++++++++++++++++--------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/src/gui/platform/unix/qgnometheme.cpp b/src/gui/platform/unix/qgnometheme.cpp index bce0eca0629..1095a665324 100644 --- a/src/gui/platform/unix/qgnometheme.cpp +++ b/src/gui/platform/unix/qgnometheme.cpp @@ -31,30 +31,51 @@ const char *QGnomeTheme::name = "gnome"; QGnomeThemePrivate::QGnomeThemePrivate() { #ifndef QT_NO_DBUS - QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.portal.Desktop"), - QLatin1String("/org/freedesktop/portal/desktop"), - QLatin1String("org.freedesktop.portal.Settings"), - QLatin1String("ReadOne")); static constexpr QLatin1String appearanceNamespace("org.freedesktop.appearance"); + static constexpr QLatin1String colorSchemeKey("color-scheme"); static constexpr QLatin1String contrastKey("contrast"); - message << appearanceNamespace << contrastKey; - QDBusConnection dbus = QDBusConnection::sessionBus(); - if (!dbus.isConnected()) - qCWarning(lcQpaThemeGnome) << "dbus connection failed. Last error: " << dbus.lastError(); + if (dbus.isConnected()) { + // ReadAll appears to omit the contrast setting on Ubuntu. + QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.portal.Desktop"), + QLatin1String("/org/freedesktop/portal/desktop"), + QLatin1String("org.freedesktop.portal.Settings"), + QLatin1String("ReadOne")); - QDBusPendingCall pendingCall = dbus.asyncCall(message); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall); - QObject::connect(watcher, &QDBusPendingCallWatcher::finished, watcher, [this](QDBusPendingCallWatcher *watcher) { - if (!watcher->isError()) { - QDBusPendingReply reply = *watcher; - if (Q_LIKELY(reply.isValid())) - m_contrast = static_cast(reply.value().toUInt()); + message << appearanceNamespace << colorSchemeKey; + QDBusReply reply = dbus.call(message); + if (Q_LIKELY(reply.isValid())) { + uint xdgColorSchemeValue = reply.value().toUInt(); + switch (xdgColorSchemeValue) { + case 1: + m_colorScheme = Qt::ColorScheme::Dark; + QWindowSystemInterface::handleThemeChange(); + break; + case 2: + m_colorScheme = Qt::ColorScheme::Light; + QWindowSystemInterface::handleThemeChange(); + break; + default: + break; + } } - watcher->deleteLater(); - initDbus(); - }); + + message.setArguments({}); + message << appearanceNamespace << contrastKey; + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(dbus.asyncCall(message)); + QObject::connect(watcher, &QDBusPendingCallWatcher::finished, watcher, [this](QDBusPendingCallWatcher *watcher) { + if (!watcher->isError()) { + QDBusPendingReply reply = *watcher; + if (Q_LIKELY(reply.isValid())) + updateHighContrast(static_cast(reply.value().toUInt())); + } + initDbus(); + watcher->deleteLater(); + }); + } else { + qCWarning(lcQpaThemeGnome) << "dbus connection failed. Last error: " << dbus.lastError(); + } #endif // QT_NO_DBUS } QGnomeThemePrivate::~QGnomeThemePrivate()