use new QStyleHints::colorScheme feature to use light or dark mode
authorMatthieu Gallien <matthieu.gallien@nextcloud.com>
Wed, 28 Aug 2024 10:30:27 +0000 (12:30 +0200)
committerMatthieu Gallien <matthieu_gallien@yahoo.fr>
Fri, 6 Sep 2024 08:42:08 +0000 (10:42 +0200)
Signed-off-by: Matthieu Gallien <matthieu.gallien@nextcloud.com>
src/libsync/theme.cpp

index eb67495aa54f56dee410c969ab026973fc5a4f43..a1b1dfdb9e60b2245dce55bb1953e6d1c231ff47 100644 (file)
@@ -25,6 +25,8 @@
 #include <QStyle>
 #include <QApplication>
 #endif
+#include <QGuiApplication>
+#include <QStyleHints>
 #include <QSslSocket>
 #include <QSvgRenderer>
 #include <QPainter>
@@ -927,7 +929,7 @@ void Theme::connectToPaletteSignal()
     if (!_paletteSignalsConnected) {
         if (const auto ptr = qobject_cast<QGuiApplication *>(QGuiApplication::instance())) {
             connect(ptr, &QGuiApplication::paletteChanged, this, &Theme::systemPaletteChanged);
-            connect(ptr, &QGuiApplication::paletteChanged, this, &Theme::darkModeChanged);
+            connect(ptr->styleHints(), &QStyleHints::colorSchemeChanged, this, &Theme::darkModeChanged);
             _paletteSignalsConnected = true;
         }
     }
@@ -972,18 +974,17 @@ QVariantMap Theme::systemPalette()
 bool Theme::darkMode()
 {
     connectToPaletteSignal();
-// Windows: Check registry for dark mode
-#if defined(Q_OS_WIN)
-    const auto darkModeSubkey = QStringLiteral("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize");
-    if (Utility::registryKeyExists(HKEY_CURRENT_USER, darkModeSubkey) &&
-        !Utility::registryGetKeyValue(HKEY_CURRENT_USER, darkModeSubkey, QStringLiteral("AppsUseLightTheme")).toBool()) {
+    switch (qGuiApp->styleHints()->colorScheme())
+    {
+    case Qt::ColorScheme::Dark:
         return true;
+    case Qt::ColorScheme::Light:
+        return false;
+    case Qt::ColorScheme::Unknown:
+        return Theme::isDarkColor(QGuiApplication::palette().window().color());
     }
 
     return false;
-#else
-    return Theme::isDarkColor(QGuiApplication::palette().window().color());
-#endif
 }
 
 void Theme::setOverrideServerUrl(const QString &overrideServerUrl)