Fix dark mode detection on Linux

Fixes #7817
This commit is contained in:
felipdsa21 2022-09-16 19:58:48 -03:00 committed by Jonathan White
parent 5caf68988e
commit e7fe3ff968
2 changed files with 24 additions and 3 deletions

View File

@ -74,6 +74,11 @@ NixUtils::NixUtils(QObject* parent)
"SettingChanged", "SettingChanged",
this, this,
SLOT(handleColorSchemeChanged(QString, QString, QDBusVariant))); SLOT(handleColorSchemeChanged(QString, QString, QDBusVariant)));
QDBusMessage msg = QDBusMessage::createMethodCall(
"org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", "org.freedesktop.portal.Settings", "Read");
msg << QVariant("org.freedesktop.appearance") << QVariant("color-scheme");
sessionBus.callWithCallback(msg, this, SLOT(handleColorSchemeRead(QDBusVariant)));
} }
NixUtils::~NixUtils() NixUtils::~NixUtils()
@ -83,7 +88,7 @@ NixUtils::~NixUtils()
bool NixUtils::isDarkMode() const bool NixUtils::isDarkMode() const
{ {
// prefer freedesktop "org.freedesktop.appearance color-scheme" setting // prefer freedesktop "org.freedesktop.appearance color-scheme" setting
if (m_systemColorschemePref != ColorschemePref::PreferNone) { if (m_systemColorschemePrefExists) {
return m_systemColorschemePref == ColorschemePref::PreferDark; return m_systemColorschemePref == ColorschemePref::PreferDark;
} }
@ -299,10 +304,22 @@ bool NixUtils::unregisterGlobalShortcut(const QString& name)
return true; return true;
} }
void NixUtils::handleColorSchemeRead(QDBusVariant value)
{
value = qvariant_cast<QDBusVariant>(value.variant());
setColorScheme(value);
}
void NixUtils::handleColorSchemeChanged(QString ns, QString key, QDBusVariant value) void NixUtils::handleColorSchemeChanged(QString ns, QString key, QDBusVariant value)
{ {
if (ns == "org.freedesktop.appearance" && key == "color-scheme") { if (ns == "org.freedesktop.appearance" && key == "color-scheme") {
m_systemColorschemePref = static_cast<ColorschemePref>(value.variant().toInt()); setColorScheme(value);
emit interfaceThemeChanged();
} }
} }
void NixUtils::setColorScheme(QDBusVariant value)
{
m_systemColorschemePref = static_cast<ColorschemePref>(value.variant().toInt());
m_systemColorschemePrefExists = true;
emit interfaceThemeChanged();
}

View File

@ -50,6 +50,7 @@ public:
} }
private slots: private slots:
void handleColorSchemeRead(QDBusVariant value);
void handleColorSchemeChanged(QString ns, QString key, QDBusVariant value); void handleColorSchemeChanged(QString ns, QString key, QDBusVariant value);
private: private:
@ -79,6 +80,9 @@ private:
PreferLight PreferLight
}; };
ColorschemePref m_systemColorschemePref = ColorschemePref::PreferNone; ColorschemePref m_systemColorschemePref = ColorschemePref::PreferNone;
bool m_systemColorschemePrefExists;
void setColorScheme(QDBusVariant value);
Q_DISABLE_COPY(NixUtils) Q_DISABLE_COPY(NixUtils)
}; };