Skip to content

A hardened Linux desktop, rebuilt from scratch, with your Windows apps built in. Free and open source.

3.8 KB
Raw Blame
#include "style.h"

#include <KConfigGroup>
#include <KSharedConfig>

#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QStandardPaths>

#include <algorithm>

namespace
{
const QColor glacier(0x8f, 0xb4, 0xff);
const QColor darkReference = QColor::fromRgbF(24 / 255.0, 26 / 255.0, 34 / 255.0, 0.95);
const QColor lightReference = QColor::fromRgbF(243 / 255.0, 243 / 255.0, 243 / 255.0, 0.96);

QColor parseColour(const QString &value)
{
    const QStringList parts = value.split(QLatin1Char(','));
    if (parts.size() >= 3) {
        return QColor(parts[0].trimmed().toInt(), parts[1].trimmed().toInt(), parts[2].trimmed().toInt());
    }
    return value.isEmpty() ? QColor() : QColor(value);
}

QColor jsonColour(const QJsonValue &value)
{
    if (value.isString()) {
        return QColor(value.toString());
    }
    const QJsonObject object = value.toObject();
    QColor colour(object.value(QStringLiteral("colour")).toString());
    if (colour.isValid()) {
        colour.setAlphaF(object.value(QStringLiteral("alpha")).toDouble(1.0));
    }
    return colour;
}

// Style.tinted: each channel scaled by the theme's share of the reference
// glass, the alpha moved by the theme's difference.
QColor tinted(const QColor &tint, const QColor &ref, int r, int g, int b, double a)
{
    return QColor::fromRgbF(std::min(1.0, r / 255.0 * tint.redF() / ref.redF()), std::min(1.0, g / 255.0 * tint.greenF() / ref.greenF()),
                            std::min(1.0, b / 255.0 * tint.blueF() / ref.blueF()),
                            std::clamp(a + tint.alphaF() - ref.alphaF(), 0.0, 1.0));
}
}

Style Style::read()
{
    Style s;
    KSharedConfigPtr globals = KSharedConfig::openConfig(QStringLiteral("kdeglobals"));
    const KConfigGroup general = globals->group(QStringLiteral("General"));
    s.accent = parseColour(general.readEntry("AccentColor"));
    if (!s.accent.isValid()) {
        s.accent = glacier;
    }
    const QString scheme = general.readEntry("ColorScheme", QStringLiteral("WildOS"));
    const QColor window = parseColour(globals->group(QStringLiteral("Colors:Window")).readEntry("BackgroundNormal"));
    s.contrast = scheme == QLatin1String("WildOSHighContrast");
    if (s.contrast) {
        s.dark = true;
    } else if (window.isValid()) {
        s.dark = window.lightnessF() < 0.5;
    } else {
        s.dark = !scheme.contains(QLatin1String("Light"));
    }
    s.reducedMotion = globals->group(QStringLiteral("KDE")).readEntry("AnimationDurationFactor", 1.0) <= 0.0;

    const QString id = KSharedConfig::openConfig(QStringLiteral("wildosrc"))->group(QStringLiteral("Theme")).readEntry("Id", QStringLiteral("dawn"));
    QJsonObject theme;
    const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("wildos/themes/%1.json").arg(id));
    QFile file(path);
    if (!path.isEmpty() && file.open(QIODevice::ReadOnly)) {
        theme = QJsonDocument::fromJson(file.readAll()).object();
    }
    const QString mode = theme.value(QStringLiteral("mode")).toString();
    const QColor themeGlass = jsonColour(theme.value(QStringLiteral("glass")));
    const QColor ref = s.dark ? darkReference : lightReference;
    const bool themed = !s.contrast && !mode.isEmpty() && (mode == QLatin1String("dark")) == s.dark && themeGlass.isValid();
    const QColor tint = themed ? themeGlass : ref;

    if (s.contrast) {
        s.glass = Qt::black;
        s.border = Qt::white;
        s.ink = Qt::white;
    } else if (s.dark) {
        s.glass = tinted(tint, ref, 24, 26, 34, 0.95);
        s.border = QColor::fromRgbF(1, 1, 1, 0.093);
        s.ink = Qt::white;
    } else {
        s.glass = tinted(tint, ref, 243, 243, 243, 0.96);
        s.border = QColor::fromRgbF(0, 0, 0, 0.073);
        s.ink = QColor::fromRgbF(0, 0, 0, 0.896);
    }
    return s;
}