// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #pragma once #include #include #include #include #include #include #include #include #include // The themes to choose from, read off the folder the pictures are in. // // A theme is a picture behind the interface and nothing else: the colours belong // to the Plasma scheme, which Kirigami.Theme reads. Reading the folder means what // is offered is what can really be shown, without a list in QML going stale. // The theme is named as the picture is ("cloud-gin.jpg" -> "cloud-gin"), and the // setting stores that file's path, so theme and wallpaper are the same to the window. class ThemeLibrary : public QObject { Q_OBJECT QML_ELEMENT QML_SINGLETON // [{ name, label, path, url }]. Name identifies the theme and is the file name // without its extension; label is that name written to read; path is what a // wallpaper setting takes; url is what an Image takes - see FsUtils. Q_PROPERTY(QVariantList themes READ themes NOTIFY themesChanged) // The folder this was read off, which the settings show: the person looking // is entitled to know where the next picture goes. Q_PROPERTY(QString directory READ directory NOTIFY themesChanged) public: explicit ThemeLibrary(QObject *parent = nullptr); static ThemeLibrary *create(QQmlEngine *, QJSEngine *); QVariantList themes() const { return m_themes; } QString directory() const { return m_directory; } // Read the folder again: a picture put in while the settings were open is not // in the list they opened with, and a replaced picture is a different one. Q_INVOKABLE void reload(); // The colour a surface of that picture is painted in - a panel, a card, a // page behind a scrim: the picture's tone in the scheme's lightness, so a // wallpaper colours the window instead of sitting under a wash of grey. // // base is the scheme's background and the result is built on it: hue and // saturation are the picture's, held between a fifth and just under a half, // lightness is the scheme's, because the text on a surface is the scheme's // text and legibility is a question of lightness. A light scheme shows least // of this - a near-white surface holds a couple of percent of tint - and a // picture with no hue is washed in the scheme's. // // Anything that is not a readable picture answers with base itself, so the // caller has one colour to paint with. The average is read once per path and // remembered: the same picture under two schemes must still wash two ways. Q_INVOKABLE QColor toneFor(const QString &path, const QColor &base); // The surface and accent washed out of a hand-picked colour. The bargain is // the picture's - picker's hue and saturation, scheme's lightness - so text // keeps the contrast worked out for it. The clamps are the same as above. Q_INVOKABLE QColor pickedTone(const QColor &picked, const QColor &base); Q_INVOKABLE QColor pickedAccent(const QColor &picked, const QColor &base); // The accent: what the interface marks things with - a selected row, a sent // message, a focus ring. The same average pixel, washed for being looked at // rather than read against: at least half saturation, since a tint washed // down to a surface's worth is not an accent. base is the scheme's highlight // and its lightness is kept; an unreadable picture answers with base. Q_INVOKABLE QColor accentFor(const QString &path, const QColor &base); // Where pictures are looked for, in order: the first candidate holding one is // the folder the gallery shows. Public so the install rule and the test agree. static QString libraryDir(); // The pictures in dir, in reading order: what a wallpaper can be, folders and // non-pictures left out. static QList picturesIn(const QString &dir); // The names of those pictures - a file name without its extension, which is // what a theme is called. static QStringList themesIn(const QString &dir); // The whole list, for a folder of this program's choosing. Static and public // so the test can use two pictures with the same name. static QVariantList themesFrom(const QString &dir); // The theme's name written to read in a list: dashes and underscores become // spaces and every word gets its capital, so "cloud-gin" draws "Cloud Gin". // Two names that come out the same are told apart by the caller. static QString labelFor(const QString &name); Q_SIGNALS: // One signal for both: the folder and what is in it change together, and an // old list would show a count that does not match the tiles. void themesChanged(); private: // One pixel of one picture: its average colour, which is all a scrim wants, // and invalid for a file that did not decode. Read once per path. QColor averageOf(const QString &path); void build(); QVariantList m_themes; QString m_directory; QHash m_averages; };