// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #include "ThemeLibrary.h" #include #include #include #include #include #include #include #include #include #include #include #include namespace { // The pictures a wallpaper can be: the same list the wallpaper file dialog // offers, so a picture that could have been chosen by hand is not turned away // for being in the folder already. Not the avatar's list - a GIF is a banner and // not a background, and the theme names would be filed under an extension nobody // chose for them. QStringList pictureFilters() { return { QStringLiteral("*.jpg"), QStringLiteral("*.jpeg"), QStringLiteral("*.png"), QStringLiteral("*.webp"), QStringLiteral("*.bmp"), QStringLiteral("*.gif"), }; } // The saturation a scrim is allowed, in the 0 to 255 of QColor's HSL. A fifth is // the least that still reads as a colour rather than as the grey it replaced, // and just under a half is as much as a wash over a photograph can carry // before the photograph is a background of one colour. constexpr int kToneSaturationFloor = 51; constexpr int kToneSaturationCeiling = 114; // The saturation an accent is allowed, in the same 0 to 255. Half is as little as // a colour still reads as one at a glance, which is what an accent is for: a tint // washed down to a surface's worth of saturation is not an accent at all. Just over // nine tenths is where a colour starts to be poster paint rather than a highlight, // and the scheme's own highlights sit at about four fifths. constexpr int kAccentSaturationFloor = 128; constexpr int kAccentSaturationCeiling = 230; } QList ThemeLibrary::picturesIn(const QString &dir) { const QDir root(dir); if (!root.exists()) return {}; const QFileInfoList found = root.entryInfoList(pictureFilters(), QDir::Files | QDir::NoDotAndDotDot, QDir::Name); // Sorted by file name in a collator that knows how to order names, rather // than by byte: a byte comparison puts every Cyrillic name after every // Latin one, which is not the order the folder lists them in either. The // names are sorted rather than the QFileInfos because QList's iterators are // not what std::sort wants to shuffle a list of non-trivial values around // with. QStringList fileNames; fileNames.reserve(found.size()); for (const QFileInfo &file : found) { fileNames << file.fileName(); } QCollator collator(QLocale::system()); std::sort(fileNames.begin(), fileNames.end(), [&collator](const QString &a, const QString &b) { return collator.compare(a, b) < 0; }); QList pictures; pictures.reserve(fileNames.size()); for (const QString &fileName : fileNames) { pictures << QFileInfo(root.filePath(fileName)); } return pictures; } QStringList ThemeLibrary::themesIn(const QString &dir) { QStringList names; const QFileInfoList files = picturesIn(dir); names.reserve(files.size()); for (const QFileInfo &file : files) { // completeBaseName, not baseName: "a.b.jpg" is a theme called "a.b" and // not one called "a" with a second theme lost off the end of it. names << file.completeBaseName(); } return names; } QString ThemeLibrary::libraryDir() { const QString exeDir = QCoreApplication::applicationDirPath(); const QStringList candidates = { // The user's own, which is the only one of these a person can add to // without a build. QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/themes"), // Where the install rule puts them. QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/koutnet/themes"), // The folder in the source tree: where they are kept while the // application is being worked on, and what the install rule ships. Both // depths are here because a build tree is either inside the source tree // or beside it, and a developer running the build rather than an // installed copy is the one who needs this to resolve. exeDir + QStringLiteral("/../../src/themes"), exeDir + QStringLiteral("/../src/themes"), exeDir + QStringLiteral("/../share/koutnet/themes"), qEnvironmentVariable("APPDIR") + QStringLiteral("/usr/share/koutnet/themes"), QStringLiteral("/usr/share/koutnet/themes"), }; for (const QString &candidate : candidates) { if (!picturesIn(candidate).isEmpty()) { // AbsolutePath, which is also what cleans out the ".." a candidate // above is spelled with: the folder is shown in the settings and // opened from there, and a path with a ".." in the middle of it is // neither. return QDir(candidate).absolutePath(); } } // Nothing to offer. The first candidate anyway, said as a path rather than // as an empty one: the folder the gallery would have read is worth showing // with the gallery empty, which is what tells a person where to put the // first picture. return QDir(candidates.first()).absolutePath(); } QString ThemeLibrary::labelFor(const QString &name) { // The name the file spells, with what a name is not written with taken off // and what a file has to be spelled with put in: the dashes and underscores // are separators to a file system and word breaks to a reader. The // underscore becomes a dash first and the split is on dashes, so that both // of them are word breaks - a name spelled with one has as many words in it // as the same name spelled with the other. QStringList words = QStringList{name}; words[0].replace(QLatin1Char('_'), QLatin1Char('-')); words = words[0].split(QLatin1Char('-')); // Every word capitalized, which a file name is not and a name is: these are // names of places, and a grid of tiles drawn "cloud gin" next to "village" // reads as a list of files that happen to be pictures. In a script with no // case this does nothing, which is right. for (QString &word : words) { word = word.simplified(); if (!word.isEmpty()) word[0] = word.at(0).toUpper(); } return words.join(QLatin1Char(' ')); } ThemeLibrary::ThemeLibrary(QObject *parent) : QObject(parent) { build(); } ThemeLibrary *ThemeLibrary::create(QQmlEngine *, QJSEngine *) { // Constructed here rather than by the engine, so the folder is read once, and // on startup rather than while the settings are being drawn. return new ThemeLibrary; } void ThemeLibrary::reload() { // The averages go with the list: the reload button is what a person presses // after replacing a picture in the folder, and an average read out of the old // file would be the average of a picture that is no longer there. m_averages.clear(); build(); } QColor ThemeLibrary::averageOf(const QString &path) { const auto remembered = m_averages.constFind(path); if (remembered != m_averages.constEnd()) return remembered.value(); // One pixel, and the decoder does the averaging on the way down to it: the // whole of what a scrim wants out of a photograph is its colour, and none of // the pixels are that. QImageReader reader(path); reader.setScaledSize(QSize(1, 1)); const QImage one = reader.read(); // Invalid rather than transparent black for a file that did not decode, so // that asking for it again after the file is put there is asking again. const QColor average = one.isNull() ? QColor() : one.pixelColor(0, 0); m_averages.insert(path, average); return average; } QColor ThemeLibrary::toneFor(const QString &path, const QColor &base) { if (path.isEmpty() || !base.isValid()) return base; // What the picture is read for is its tone and nothing else, and the tone is // half a scrim: the other half is the scheme, which is the caller's to say and // the same picture asked about under two schemes has to wash two ways. const QColor average = averageOf(path); if (!average.isValid()) return base; const QColor picture = average.toHsl(); const QColor scheme = base.toHsl(); // A picture with no colour of its own - a photograph in black and white - has // no hue at all, and -1 is how Qt says so. Fed straight back into a colour it // is grey again, which is the one thing this is not, so it is given the // scheme's hue: the smallest possible amount of the tone the window already // has. A scheme with no hue of its own, which is a grey one, has none to // give, and a grey picture under a grey scheme washes grey. const int hue = picture.hue() >= 0 ? picture.hue() : scheme.hue(); return QColor::fromHsl(hue, std::clamp(picture.saturation(), kToneSaturationFloor, kToneSaturationCeiling), scheme.lightness()); } // A colour a person picked keeps its own hue, saturation and lightness, unlike // a picture's tone which borrows the scheme's lightness. The clamps are the same // pairs as before: the surface pair keeps a picked surface from reading as // poster paint on one side and as grey on the other, the accent pair keeps an // accent reading as a colour at a glance. QColor ThemeLibrary::pickedTone(const QColor &picked, const QColor &base) { if (!picked.isValid() || !base.isValid()) return base; const QColor given = picked.toHsl(); const QColor scheme = base.toHsl(); const int hue = given.hue() >= 0 ? given.hue() : scheme.hue(); // A pick is used at its own lightness: the scheme's near-white used to wash // it out until a picked colour all but disappeared under a light theme. The // floor and ceiling keep text readable - a dark pick cannot turn a light // surface into one dark enough to swallow dark text, and the other way round. const int lightness = scheme.lightness() >= 128 ? std::max(given.lightness(), 115) : std::min(given.lightness(), 153); return QColor::fromHsl(hue, std::clamp(given.saturation(), kToneSaturationFloor, kToneSaturationCeiling), lightness); } QColor ThemeLibrary::pickedAccent(const QColor &picked, const QColor &base) { if (!picked.isValid() || !base.isValid()) return base; const QColor given = picked.toHsl(); const QColor scheme = base.toHsl(); const int hue = given.hue() >= 0 ? given.hue() : scheme.hue(); // The same as a picked surface: the chosen lightness is what the user sees, // with only the range that keeps it an accent rather than a wall or ink. const int lightness = std::clamp(given.lightness(), 64, 217); return QColor::fromHsl(hue, std::clamp(given.saturation(), kAccentSaturationFloor, kAccentSaturationCeiling), lightness); } QColor ThemeLibrary::accentFor(const QString &path, const QColor &base) { if (path.isEmpty() || !base.isValid()) return base; // The same picture as for a surface, read the same way and remembered the same // way; only the washing of it is different. const QColor average = averageOf(path); if (!average.isValid()) return base; const QColor picture = average.toHsl(); const QColor scheme = base.toHsl(); // A picture with no hue of its own is given the scheme's, as it is for a // surface: an accent of the scheme's own colour is a better accent than no // accent, and a grey highlight is not a highlight. const int hue = picture.hue() >= 0 ? picture.hue() : scheme.hue(); // The lightness is the scheme's highlight lightness, and that is deliberate // where a surface keeps the scheme's background lightness: an accent has to sit // on top of a surface of that lightness, and a highlight picked for its own // sake can end up neither readable on the surface nor readable on itself. return QColor::fromHsl(hue, std::clamp(picture.saturation(), kAccentSaturationFloor, kAccentSaturationCeiling), scheme.lightness()); } QVariantList ThemeLibrary::themesFrom(const QString &dir) { QVariantList themes; const QFileInfoList files = picturesIn(dir); themes.reserve(files.size()); // Labels already on a tile, so two pictures whose names come out the same // way are not two tiles saying the same thing. "a-b.jpg" and "a b.jpg" are // two themes and "A B" is one name, and the second tile keeps the name its // file spells, which is what tells the two apart. QSet labels; for (const QFileInfo &file : files) { const QString name = file.completeBaseName(); QString label = labelFor(name); if (labels.contains(label)) { label = name; } labels.insert(label); const QString path = file.absoluteFilePath(); themes << QVariantMap{ {QStringLiteral("name"), name}, {QStringLiteral("label"), label}, {QStringLiteral("path"), path}, {QStringLiteral("url"), QUrl::fromLocalFile(path).toString()}, }; } return themes; } void ThemeLibrary::build() { m_directory = libraryDir(); m_themes = themesFrom(m_directory); Q_EMIT themesChanged(); }