// SPDX-License-Identifier: GPL-3.0-or-later #include "theme.h" #include #include namespace xpl { namespace { QStringList utf16List(const QByteArray &data) { const QString text = QString::fromUtf16(reinterpret_cast(data.constData()), int(data.size() / 2)); QStringList out; for (const QString &s : text.split(QChar(0))) if (!s.isEmpty()) out.append(s); return out; } } // namespace std::shared_ptr Theme::load(const QString &path, const QString &variant, QString *error) { QFile file(path); // Read into memory, never map: the file may be replaced while we run. if (!file.open(QIODevice::ReadOnly)) { if (error) *error = file.errorString(); return nullptr; } return fromData(file.readAll(), variant, error); } std::shared_ptr Theme::fromData(const QByteArray &file, const QString &variant, QString *error) { const auto pe = PeResources::parse(file, error); if (!pe) return nullptr; std::shared_ptr theme(new Theme); QHash inis; for (const Resource &r : pe->all()) { const QString type = r.type.toUpper(); if (type == QLatin1String("TEXTFILE")) { inis.insert(r.name.toUpper(), r.data); } else if (type == QLatin1String("2")) { theme->m_bitmapData.insert(r.name.toUpper(), r.data); } else if (type == QLatin1String("COLORNAMES")) { theme->m_variants.colourNames = utf16List(r.data); } else if (type == QLatin1String("SIZENAMES")) { theme->m_variants.sizeNames = utf16List(r.data); } else if (type == QLatin1String("FILERESNAMES")) { for (const QString &name : utf16List(r.data)) theme->m_variants.fileNames.append(name.toUpper()); } } if (inis.isEmpty()) { if (error) *error = QStringLiteral("no TEXTFILE resources: not a classic (XP-era) visual style"); return nullptr; } theme->m_variants.iniNames = inis.keys(); theme->m_variants.iniNames.sort(); if (inis.contains(QStringLiteral("THEMES_INI"))) theme->m_variants.themesIni = decodeText(inis.value(QStringLiteral("THEMES_INI"))); QString chosen = variant.toUpper(); if (chosen.isEmpty()) { // XP's default: the first colour at the first size, FILERESNAMES[0]; // then any NORMAL* variant; then the largest INI (layout dwarfs // THEMES_INI). Same order as msstyles_parse.MsStyle.load. for (const QString &n : theme->m_variants.fileNames) { if (inis.contains(n)) { chosen = n; break; } } if (chosen.isEmpty()) { for (const QString &n : theme->m_variants.iniNames) { if (n.startsWith(QLatin1String("NORMAL"))) { chosen = n; break; } } } if (chosen.isEmpty()) { qsizetype largest = -1; for (auto it = inis.cbegin(); it != inis.cend(); ++it) { if (it.value().size() > largest) { largest = it.value().size(); chosen = it.key(); } } } } if (!inis.contains(chosen)) { if (error) *error = QStringLiteral("no layout INI named %1").arg(chosen); return nullptr; } theme->m_variant = chosen; theme->m_ini = Ini::parse(inis.value(chosen)); return theme; } namespace { bool isColourKey(const QString &property) { return property == QLatin1String("transparentcolor") || property == QLatin1String("glyphtransparentcolor"); } // An "R G B" value: three numbers and nothing else (not a font's "Tahoma, 8"). std::optional colourValue(const QString &value) { QList n; QString digits; const QString text = value + QLatin1Char(' '); for (const QChar c : text) { if (c.isDigit()) { digits += c; } else if (c.isLetter()) { return std::nullopt; } else if (!digits.isEmpty()) { n.append(digits.toInt()); digits.clear(); } } if (n.size() != 3) return std::nullopt; return qRgb(qBound(0, n[0], 255), qBound(0, n[1], 255), qBound(0, n[2], 255)); } } // namespace std::shared_ptr Theme::adjusted(const std::shared_ptr &base, const Adjustments &adjustments) { if (!base || adjustments.isIdentity()) return base; const TintSpec &spec = adjustments.tint; std::shared_ptr theme(new Theme); theme->m_variants = base->m_variants; theme->m_variant = base->m_variant; theme->m_bitmapData = base->m_bitmapData; // (implicitly shared) theme->m_untinted = base->m_untinted ? base->m_untinted : base; // (its decoded bitmaps, shared) theme->m_spec = spec; theme->m_adjustments = adjustments; // The colour properties, by their section's rule. (Bitmaps: the // renderer, frame by frame -- tinterFor.) QHash sections = base->m_ini.sections(); for (auto it = sections.begin(); it != sections.end(); ++it) { const SectionKey &key = it.key(); const bool sysMetrics = key.cls == QLatin1String("sysmetrics"); for (auto p = it.value().begin(); p != it.value().end(); ++p) { if (isColourKey(p.key()) || !(sysMetrics || p.key().endsWith(QLatin1String("color")))) continue; // ([SysMetrics] colours one by one: "sysmetrics.activecaption".) const auto tinter = sysMetrics ? theme->tinterFor(u"", key.cls, p.key(), u"") : theme->tinterFor(key.sub, key.cls, key.part, key.state); if (const auto c = colourValue(p.value()); c && tinter) { const QRgb out = tinter->apply(*c); p.value() = QStringLiteral("%1 %2 %3").arg(qRed(out)).arg(qGreen(out)).arg(qBlue(out)); } } } // Then the overrides, as set: over the tint. for (auto s = adjustments.overrides.sections.cbegin(); s != adjustments.overrides.sections.cend(); ++s) { Properties &props = sections[Ini::parseHeader(s.key())]; for (auto p = s.value().cbegin(); p != s.value().cend(); ++p) props.insert(p.key(), p.value()); } theme->m_ini = Ini::fromSections(std::move(sections)); return theme; } std::shared_ptr Theme::tinterFor(QStringView sub, QStringView cls, QStringView part, QStringView state) const { if (m_spec.rules.isEmpty()) return nullptr; const Tint t = m_spec.forSection(sub, cls, part, state).first; if (t.isIdentity()) return nullptr; QMutexLocker lock(&m_cacheLock); auto &slot = m_tinters[t.toString()]; if (!slot) slot = std::make_shared(t); return slot; } namespace { // Art that carries a neighbour's colours, and the neighbour: Luna's Start // button ends in the taskbar's blue. A section's own XPlasmaSharedEdge // property (an override) wins: another selector, or "none". struct SharedEdge { const char *sub, *cls, *part; const char *with; bool byActivity = false; // the neighbour's active or inactive state, as this frame's }; // (A close button keeps its red -- a signal colour -- but some styles paint // the caption's colour round it in the same bitmap: ambient's blue.) constexpr SharedEdge kSharedEdges[] = { {"start", "button", "pushbutton", "taskbar.backgroundbottom"}, {"", "window", "closebutton", "window.caption", true}, {"", "window", "smallclosebutton", "window.smallcaption", true}, }; double hueGap(double a, double b) { const double d = std::fmod(std::abs(a - b), 360.0); return d > 180 ? 360 - d : d; } } // namespace void Theme::tintFrame(QImage &frame, QStringView sub, QStringView cls, QStringView part, QStringView state, const QSet &keep) const { const auto own = tinterFor(sub, cls, part, state); QString with = property(cls, part, state, u"xplasmasharededge", sub).value_or(QString()).trimmed().toLower(); bool byActivity = false; if (with.isEmpty()) for (const SharedEdge &e : kSharedEdges) if (sub == QLatin1String(e.sub) && cls == QLatin1String(e.cls) && part == QLatin1String(e.part)) { with = QLatin1String(e.with); byActivity = e.byActivity; } const auto parsed = with.isEmpty() || with == QLatin1String("none") ? std::nullopt : TintSpec::parse(with + QLatin1String(": keep")); std::optional edge = parsed && parsed->rules.size() == 1 ? std::optional(parsed->rules.first()) : std::nullopt; if (edge && byActivity && edge->state.isEmpty()) edge->state = state.startsWith(QLatin1String("inactive")) ? QStringLiteral("inactive") : QStringLiteral("active"); const auto other = edge ? tinterFor(edge->sub, edge->cls, edge->part, edge->state) : nullptr; // Each part's own colour, as the style has it (untinted, as the art is). auto hint = [&](QStringView s, QStringView c, QStringView p, QStringView st) -> std::optional { const Theme &base = m_untinted ? *m_untinted : *this; const auto v = base.property(c, p, st, u"fillcolorhint", s); const auto rgb = v ? colourValue(*v) : std::nullopt; return rgb ? std::optional(toLch(*rgb)) : std::nullopt; }; const auto ownHint = edge ? hint(sub, cls, part, state) : std::nullopt; const auto otherHint = edge ? hint(edge->sub, edge->cls, edge->part, edge->state) : std::nullopt; // Only where the two differ, and their colours are far enough apart to // tell a pixel's (Luna Olive's olive button on an olive bar isn't). constexpr double kGrey = 10, kApart = 40; if (!other || own == other || !ownHint || !otherHint || ownHint->c < kGrey || otherHint->c < kGrey || hueGap(ownHint->h, otherHint->h) < kApart) { if (own) own->apply(frame, keep); return; } if (frame.format() != QImage::Format_ARGB32) frame = frame.convertToFormat(QImage::Format_ARGB32); for (int y = 0; y < frame.height(); ++y) { auto *row = reinterpret_cast(frame.scanLine(y)); for (int x = 0; x < frame.width(); ++x) { const QRgb c = row[x]; if (keep.contains(c & 0x00ffffffu)) continue; const QRgb mine = own ? own->apply(c) : c; const Lch lch = toLch(c); if (lch.c < kGrey) { // (no hue to tell by: the part's own) row[x] = mine; continue; } const double toOwn = hueGap(lch.h, ownHint->h), toOther = hueGap(lch.h, otherHint->h); // 0: the part's own colour; 1: the neighbour's; eased, so shades // of either stay wholly theirs and only what's between blends. // (Snapped within a quarter of the way: the eased blend alone // still moved Luna's close red by a unit.) const double t = toOwn / (toOwn + toOther + 1e-9); const double s = qBound(0.0, (t - 0.25) / 0.5, 1.0), w = s * s * (3 - 2 * s); const QRgb theirs = other->apply(c); auto mix = [w](int a, int b) { return qBound(0, qRound(a + (b - a) * w), 255); }; row[x] = qRgba(mix(qRed(mine), qRed(theirs)), mix(qGreen(mine), qGreen(theirs)), mix(qBlue(mine), qBlue(theirs)), qAlpha(c)); } } } QString Theme::resourceName(const QString &imageFile) { QString out = imageFile.toUpper(); for (QChar &c : out) { const ushort u = c.unicode(); if (!((u >= 'A' && u <= 'Z') || (u >= '0' && u <= '9'))) c = QLatin1Char('_'); } return out; } QStringList Theme::bitmapNames() const { QStringList names = m_bitmapData.keys(); names.sort(); return names; } Dib Theme::bitmap(const QString &imageFile) const { if (m_untinted) return m_untinted->bitmap(imageFile); const QString key = resourceName(imageFile); QMutexLocker lock(&m_cacheLock); auto cached = m_bitmaps.constFind(key); if (cached != m_bitmaps.cend()) return cached.value(); const Dib dib = decodeDib(m_bitmapData.value(key)); m_bitmaps.insert(key, dib); return dib; } } // namespace xpl