Commit f5fe8b72 authored by Fushan Wen's avatar Fushan Wen 💬
Browse files

kcms/colors: port away from ugly hacks in saveColors

Use the proper DBus signal to notify the change of
accentColorFromWallpaper.

applyScheme needs the latest accentColor but when it's called from `KCMColors::saveColors()`,
the accent color in kdeglobals is still old, that's why the hack works
by saving the value before `ManagedConfigModule::save()`. Therefore we
can pass the latest accent color value to applyScheme to fix it and
remove the ugly hack.
parent 16ec9cdc
......@@ -407,31 +407,20 @@ void KCMColors::save()
m_activeSchemeEdited = false;
processPendingDeletions();
QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/kdeglobals"), QStringLiteral("org.kde.kconfig.notify"), QStringLiteral("ConfigChanged"));
const QHash<QString, QByteArrayList> changes = {{QStringLiteral("General"), {"accentColorFromWallpaper", "AccentColor"}}};
message.setArguments({QVariant::fromValue(changes)});
QDBusConnection::sessionBus().send(message);
}
void KCMColors::saveColors()
{
const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("color-schemes/%1.colors").arg(m_model->selectedScheme()));
// hard to figure out why mutating from the colours settings
// doesn't affect the applicator's view on config, but operating on the
// globalConfig directly works.
// code already a mess, so might as well just do what works.
KSharedConfigPtr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"));
auto setGlobals = [=]() {
globalConfig->group("General").writeEntry("AccentColor", QColor());
globalConfig->group("General").writeEntry("accentColorFromWallpaper", accentColorFromWallpaper(), KConfig::Notify);
if (accentColor() != QColor(Qt::transparent)) {
globalConfig->group("General").writeEntry("AccentColor", accentColor(), KConfig::Notify);
} else {
globalConfig->group("General").deleteEntry("AccentColor", KConfig::Notify);
}
};
setGlobals();
applyScheme(path, colorsSettings()->config());
// Can't use KConfig readEntry because the config is not saved yet
applyScheme(path, colorsSettings()->config(), KConfig::Normal, accentColor());
m_selectedSchemeDirty = false;
setGlobals();
}
QColor KCMColors::accentBackground(const QColor &accent, const QColor &background)
......
......@@ -121,20 +121,17 @@ static void copyEntry(KConfigGroup &from, KConfigGroup &to, const QString &entry
}
}
void applyScheme(const QString &colorSchemePath, KConfig *configOutput, KConfig::WriteConfigFlags writeConfigFlag)
void applyScheme(const QString &colorSchemePath, KConfig *configOutput, KConfig::WriteConfigFlags writeConfigFlag, std::optional<QColor> accentColor)
{
KSharedConfigPtr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"));
globalConfig->sync();
const auto getAccent = [globalConfig]() {
return globalConfig->group("General").readEntry<QColor>("AccentColor", QColor());
const auto getAccent = [configOutput, &accentColor]() {
return accentColor.value_or(configOutput->group("General").readEntry("AccentColor", QColor()));
};
const auto hasAccent = [globalConfig, &getAccent]() {
const auto hasAccent = [configOutput, &getAccent]() {
if (getAccent() == QColor(Qt::transparent)) {
return false;
}
return globalConfig->group("General").hasKey("AccentColor");
return configOutput->group("General").hasKey("AccentColor");
};
// Using KConfig::SimpleConfig because otherwise Header colors won't be
......
......@@ -7,6 +7,8 @@
#pragma once
#include <optional>
#include <QColor>
#include <QString>
......@@ -73,7 +75,10 @@ inline QColor accentForeground(const QColor &accent, const bool &isActive)
* @param colorFilePath The scheme color file path
* @param configOut The config which holds the information on which scheme is currently selected, and what colors it contains
*/
void applyScheme(const QString &colorSchemePath, KConfig *configOut, KConfig::WriteConfigFlags writeFlags = KConfig::Normal);
void applyScheme(const QString &colorSchemePath,
KConfig *configOut,
KConfig::WriteConfigFlags writeFlags = KConfig::Normal,
std::optional<QColor> accentColor = std::nullopt);
const qreal DefaultTintFactor = 0.15;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment