Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement org.freedesktop.appearance.color-scheme support on Linux #7422

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/gui/osutils/nixutils/NixUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "NixUtils.h"

#include <QApplication>
#include <QDBusInterface>
#include <QDir>
#include <QPointer>
#include <QStandardPaths>
Expand Down Expand Up @@ -60,6 +61,15 @@ NixUtils::NixUtils(QObject* parent)
{
dpy = QX11Info::display();
rootWindow = QX11Info::appRootWindow();

// notify about system color scheme changes
QDBusConnection sessionBus = QDBusConnection::sessionBus();
sessionBus.connect("org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.Settings",
"SettingChanged",
this,
SLOT(handleColorSchemeChanged(QString, QString, QDBusVariant)));
}

NixUtils::~NixUtils()
Expand All @@ -68,6 +78,11 @@ NixUtils::~NixUtils()

bool NixUtils::isDarkMode() const
{
// prefer freedesktop "org.freedesktop.appearance color-scheme" setting
if (m_systemColorschemePref != ColorschemePref::PreferNone) {
return m_systemColorschemePref == ColorschemePref::PreferDark;
}

if (!qApp || !qApp->style()) {
return false;
}
Expand Down Expand Up @@ -257,3 +272,11 @@ bool NixUtils::unregisterGlobalShortcut(const QString& name)
m_globalShortcuts.remove(name);
return true;
}

void NixUtils::handleColorSchemeChanged(QString ns, QString key, QDBusVariant value)
{
if (ns == "org.freedesktop.appearance" && key == "color-scheme") {
m_systemColorschemePref = static_cast<ColorschemePref>(value.variant().toInt());
emit interfaceThemeChanged();
}
}
14 changes: 14 additions & 0 deletions src/gui/osutils/nixutils/NixUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "gui/osutils/OSUtilsBase.h"
#include <QAbstractNativeEventFilter>
#include <QSharedPointer>
#include <QtDBus/QDBusVariant>

class NixUtils : public OSUtilsBase, QAbstractNativeEventFilter
{
Expand Down Expand Up @@ -48,6 +49,9 @@ class NixUtils : public OSUtilsBase, QAbstractNativeEventFilter
return false;
}

private slots:
void handleColorSchemeChanged(QString ns, QString key, QDBusVariant value);

private:
explicit NixUtils(QObject* parent = nullptr);
~NixUtils() override;
Expand All @@ -66,6 +70,16 @@ class NixUtils : public OSUtilsBase, QAbstractNativeEventFilter
};
QHash<QString, QSharedPointer<globalShortcut>> m_globalShortcuts;

// defined as per "org.freedesktop.appearance color-scheme" spec in
// https://github.com/flatpak/xdg-desktop-portal/blob/d7a304a00697d7d608821253cd013f3b97ac0fb6/data/org.freedesktop.impl.portal.Settings.xml#L33-L45
enum ColorschemePref
{
PreferNone,
PreferDark,
PreferLight
};
ColorschemePref m_systemColorschemePref = ColorschemePref::PreferNone;

Q_DISABLE_COPY(NixUtils)
};

Expand Down