Skip to content

Commit

Permalink
Implement freedesktop colorscheme preference
Browse files Browse the repository at this point in the history
Implement flatpak/xdg-desktop-portal#633

Currently only maps schemes from the breeze family to light/dark and returns no preference for third-party schemes

A fancier solution for the future would be to try and guess whether a scheme is light or dark like the colors KCM does
  • Loading branch information
nicolasfella committed Sep 29, 2021
1 parent a79b1cd commit bf03a40
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ void SettingsPortal::ReadAll(const QStringList &groups)
}

VariantMapMap result;

if (groupMatches(QStringLiteral("org.freedesktop.appearance"), groups)) {
QVariantMap appearanceSettings;
appearanceSettings.insert(QStringLiteral("color-scheme"), readFdoColorScheme().variant());

result.insert(QStringLiteral("org.freedesktop.appearance"), appearanceSettings);
}

const auto groupList = m_kdeglobals->groupList();
for (const QString &settingGroupName : groupList) {
// NOTE: use org.kde.kdeglobals prefix
Expand Down Expand Up @@ -173,8 +181,13 @@ void SettingsPortal::Read(const QString &group, const QString &key)
QDBusMessage reply;
QDBusMessage message = q_ptr->message();

// All our namespaces start with this prefix
if (!group.startsWith(QStringLiteral("org.kde.kdeglobals"))) {
if (group == QLatin1String("org.freedesktop.appearance") && key == QLatin1String("color-scheme")) {
reply = message.createReply(QVariant::fromValue(readFdoColorScheme()));
QDBusConnection::sessionBus().send(reply);
return;
}
// All other namespaces start with this prefix
else if (!group.startsWith(QStringLiteral("org.kde.kdeglobals"))) {
qCWarning(XdgDesktopPortalKdeSettings) << "Namespace " << group << " is not supported";
reply = message.createErrorReply(QDBusError::UnknownProperty, QStringLiteral("Namespace is not supported"));
QDBusConnection::sessionBus().send(reply);
Expand Down Expand Up @@ -210,6 +223,8 @@ void SettingsPortal::globalSettingChanged(int type, int arg)
Q_EMIT SettingChanged(QStringLiteral("org.kde.kdeglobals.General"),
QStringLiteral("ColorScheme"),
readProperty(QStringLiteral("org.kde.kdeglobals.General"), QStringLiteral("ColorScheme")));

Q_EMIT SettingChanged(QStringLiteral("org.freedesktop.appearance"), QStringLiteral("color-scheme"), readFdoColorScheme());
break;
case FontChanged:
fontChanged();
Expand Down Expand Up @@ -272,3 +287,20 @@ QDBusVariant SettingsPortal::readProperty(const QString &group, const QString &k

return QDBusVariant(configGroup.readEntry(key));
}

QDBusVariant SettingsPortal::readFdoColorScheme()
{
const KConfig kdeglobals(QStringLiteral("kdeglobals"));
const KConfigGroup general = kdeglobals.group(QStringLiteral("General"));
const QString colorSchemeName = general.readEntry(QStringLiteral("ColorScheme"), QStringLiteral("Breeze"));

uint result = 0;

if (colorSchemeName == QLatin1String("Breeze") || colorSchemeName == QLatin1String("BreezeLight") || colorSchemeName == QLatin1String("BreezeClassic")) {
result = 2;
} else if (colorSchemeName == QLatin1String("BreezeDark")) {
result = 1;
}

return QDBusVariant(result);
}
1 change: 1 addition & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private Q_SLOTS:

private:
QDBusVariant readProperty(const QString &group, const QString &key);
QDBusVariant readFdoColorScheme();

KSharedConfigPtr m_kdeglobals;
};
Expand Down

0 comments on commit bf03a40

Please sign in to comment.