From 8a4c6221535e3a391d17c8e83d7bd0bbefdfdbae Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Wed, 3 Nov 2021 12:41:25 +0000 Subject: [PATCH] Apply colors from lnf when missing the normal defaults cascading mechanism for application colors doesn't work, as header colors would be wrong, so check if colors are missing from kdeglobals and in that case apply the colors from lnf to kdeglobals --- kcms/lookandfeel/lookandfeelmanager.cpp | 60 +++++++++++++------------ kcms/lookandfeel/lookandfeelmanager.h | 2 + startkde/startplasma.cpp | 18 ++++++++ 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/kcms/lookandfeel/lookandfeelmanager.cpp b/kcms/lookandfeel/lookandfeelmanager.cpp index 8932dcf16..901f0a14c 100644 --- a/kcms/lookandfeel/lookandfeelmanager.cpp +++ b/kcms/lookandfeel/lookandfeelmanager.cpp @@ -222,6 +222,35 @@ KConfig LookAndFeelManager::configDefaults(const QString &filename) return KConfig(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1String("/kdedefaults/") + filename, KConfig::SimpleConfig); } +QString LookAndFeelManager::colorSchemeFile(const QString &schemeName) +{ + QString colorScheme(schemeName); + colorScheme.remove(QLatin1Char('\'')); // So Foo's does not become FooS + QRegExp fixer(QStringLiteral("[\\W,.-]+(.?)")); + int offset; + while ((offset = fixer.indexIn(colorScheme)) >= 0) { + colorScheme.replace(offset, fixer.matchedLength(), fixer.cap(1).toUpper()); + } + colorScheme.replace(0, 1, colorScheme.at(0).toUpper()); + + // NOTE: why this loop trough all the scheme files? + // the scheme theme name is an heuristic, there is no plugin metadata whatsoever. + // is based on the file name stripped from weird characters or the + // eventual id- prefix store.kde.org puts, so we can just find a + // theme that ends as the specified name + const QStringList schemeDirs = + QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("color-schemes"), QStandardPaths::LocateDirectory); + for (const QString &dir : schemeDirs) { + const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.colors")); + for (const QString &file : fileNames) { + if (file.endsWith(colorScheme + QStringLiteral(".colors"))) { + return dir + QLatin1Char('/') + file; + } + } + } + return QString(); +} + void LookAndFeelManager::save(const KPackage::Package &package, const KPackage::Package &previousPackage) { if (m_resetDefaultLayout && m_mode == Mode::Apply) { @@ -264,34 +293,9 @@ void LookAndFeelManager::save(const KPackage::Package &package, const KPackage:: setColors(package.metadata().name(), colorsFile); } } else if (!colorScheme.isEmpty()) { - colorScheme.remove(QLatin1Char('\'')); // So Foo's does not become FooS - QRegExp fixer(QStringLiteral("[\\W,.-]+(.?)")); - int offset; - while ((offset = fixer.indexIn(colorScheme)) >= 0) { - colorScheme.replace(offset, fixer.matchedLength(), fixer.cap(1).toUpper()); - } - colorScheme.replace(0, 1, colorScheme.at(0).toUpper()); - - // NOTE: why this loop trough all the scheme files? - // the scheme theme name is an heuristic, there is no plugin metadata whatsoever. - // is based on the file name stripped from weird characters or the - // eventual id- prefix store.kde.org puts, so we can just find a - // theme that ends as the specified name - bool schemeFound = false; - const QStringList schemeDirs = - QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("color-schemes"), QStandardPaths::LocateDirectory); - for (const QString &dir : schemeDirs) { - const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.colors")); - for (const QString &file : fileNames) { - if (file.endsWith(colorScheme + QStringLiteral(".colors"))) { - setColors(colorScheme, dir + QLatin1Char('/') + file); - schemeFound = true; - break; - } - } - if (schemeFound) { - break; - } + QString path = colorSchemeFile(colorScheme); + if (!path.isEmpty()) { + setColors(colorScheme, path); } } } diff --git a/kcms/lookandfeel/lookandfeelmanager.h b/kcms/lookandfeel/lookandfeelmanager.h index 1ff10155c..bc5f7219f 100644 --- a/kcms/lookandfeel/lookandfeelmanager.h +++ b/kcms/lookandfeel/lookandfeelmanager.h @@ -38,6 +38,8 @@ public: */ void save(const KPackage::Package &package, const KPackage::Package &oldPackage); + QString colorSchemeFile(const QString &schemeName); + bool resetDefaultLayout() const; void setResetDefaultLayout(bool reset); diff --git a/startkde/startplasma.cpp b/startkde/startplasma.cpp index 6994ba712..5521869f7 100644 --- a/startkde/startplasma.cpp +++ b/startkde/startplasma.cpp @@ -372,6 +372,24 @@ void setupPlasmaEnvironment() LookAndFeelManager lnfManager; lnfManager.setMode(LookAndFeelManager::Mode::Defaults); lnfManager.save(package, KPackage::Package()); + const QString colorScheme = KConfigGroup(&globals, QStringLiteral("General")).readEntry("ColorScheme", QStringLiteral("BreezeLight")); + } + // If no colors are saved, take them from the LNF and apply + if (!globals.hasGroup("Colors:Window") || !globals.hasGroup("Colors:View") || + !globals.hasGroup("Colors:Button") || !globals.hasGroup("Colors:Tooltip") || + !globals.hasGroup("Colors:Selection")) { + LookAndFeelManager lnfManager; + lnfManager.setMode(LookAndFeelManager::Mode::Apply); + const KConfig globals; // Reload the config + const QString colorScheme = KConfigGroup(&globals, QStringLiteral("General")).readEntry("ColorScheme", QStringLiteral("BreezeLight")); + QString path = lnfManager.colorSchemeFile(colorScheme); + if (!path.isEmpty()) { + lnfManager.setColors(colorScheme, path); + } + const QString svgCache = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1Char('/') + QStringLiteral("plasma-svgelements"); + if (!svgCache.isEmpty()) { + QFile::remove(svgCache); + } } }