Skip to content

Commit

Permalink
use new QStyleHints::colorScheme feature to use light or dark mode
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu Gallien <[email protected]>
  • Loading branch information
mgallien committed Aug 28, 2024
1 parent 13c4026 commit ec6a55f
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/libsync/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <QStyle>
#include <QApplication>
#endif
#include <QGuiApplication>
#include <QStyleHints>
#include <QSslSocket>
#include <QSvgRenderer>
#include <QPainter>
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ec6a55f

Please sign in to comment.