diff --git a/src/apptheme.cpp b/src/apptheme.cpp index 8d61bc6..4222510 100644 --- a/src/apptheme.cpp +++ b/src/apptheme.cpp @@ -19,6 +19,21 @@ static QColor contrastInk(const QColor &color) { .0722 * linear(color.blueF()); return luminance > .179 ? QColor("#111111") : QColor("#ffffff"); } +QList> readThemeColors(const QString &path) { + QList> colors; + QFile file(path); + if (path.isEmpty() || !file.open(QIODevice::ReadOnly)) + return colors; + // Quoted or bare hex with an optional note. Quotes must pair; comments need no gap. + static const QRegularExpression entry( + R"re(^\s*([a-z_]+)\s*=\s*(["']?)(#[0-9a-fA-F]{6})\2\s*(?:#.*)?\s*$)re"); + while (!file.atEnd()) { + const auto match = entry.match(QString::fromUtf8(file.readLine())); + if (match.hasMatch()) + colors.append({match.captured(1), match.captured(3)}); + } + return colors; +} AppTheme::AppTheme(QObject *parent) : AppTheme(qEnvironmentVariable("XDG_STATE_HOME", QDir::homePath() + "/.local/state") + "/omarchy/current", @@ -35,17 +50,8 @@ AppTheme::AppTheme(const QString ¤tDirectory, QObject *parent) void AppTheme::reload() { const QString theme = m_currentDirectory + "/theme", path = theme + "/colors.toml"; QMap values; - QFile file(path); - if (file.open(QIODevice::ReadOnly)) { - // Quoted or bare hex with an optional note. Quotes must pair; comments need no gap. - const QRegularExpression entry( - R"re(^\s*([a-z_]+)\s*=\s*(["']?)(#[0-9a-fA-F]{6})\2\s*(?:#.*)?\s*$)re"); - while (!file.atEnd()) { - const auto match = entry.match(QString::fromUtf8(file.readLine())); - if (match.hasMatch()) - values[match.captured(1)] = QColor(match.captured(3)); - } - } + for (const auto &[key, value] : readThemeColors(path)) + values[key] = QColor(value); const QColor bg = values.value("background", QColor("#1a1b26")); const QColor fg = values.value("foreground", QColor("#c0caf5")); const QColor accent = values.value("accent", QColor("#7aa2f7")); diff --git a/src/apptheme.h b/src/apptheme.h index c5080eb..5b4e517 100644 --- a/src/apptheme.h +++ b/src/apptheme.h @@ -1,9 +1,15 @@ #pragma once #include +#include #include +#include #include #include +// The hex colors in an Omarchy colors.toml in file order, as written, quoted +// or bare, with or without a trailing note. +QList> readThemeColors(const QString &path); + class AppTheme : public QObject { Q_OBJECT Q_PROPERTY(QVariantMap colors READ colors NOTIFY changed) diff --git a/src/deck.cpp b/src/deck.cpp index ce55a64..79daf5c 100644 --- a/src/deck.cpp +++ b/src/deck.cpp @@ -1,4 +1,5 @@ #include "deck.h" +#include "apptheme.h" #include "budget.h" #include "animationexport.h" #include "filedialog.h" @@ -553,16 +554,8 @@ QVariantMap Deck::palette() const { {"background", "#1a1b26"}, {"foreground", "#c0caf5"}, {"accent", "#7aa2f7"}, {"green", "#9ece6a"}, {"red", "#f7768e"}, {"yellow", "#e0af68"}, {"magenta", "#bb9af7"}, {"cyan", "#7dcfff"}, {"dark_foreground", "#787c99"}}; - QFile f(m_themes.value(themeName())); - if (f.open(QIODevice::ReadOnly)) { - QRegularExpression re("^([a-z_]+)\\s*=\\s*\"(#[0-9a-fA-F]{6})\"", - QRegularExpression::MultilineOption); - auto matches = re.globalMatch(QString::fromUtf8(f.readAll())); - while (matches.hasNext()) { - auto m = matches.next(); - colors[m.captured(1)] = m.captured(2); - } - } + for (const auto &[key, value] : readThemeColors(m_themes.value(themeName()))) + colors[key] = value; for (const auto &key : colors.keys()) { QString v = scalar(m_parsed.header, "color_" + key); if (QColor(v).isValid()) @@ -581,16 +574,8 @@ void Deck::chooseTheme(const QString &name) { // Remove old palette before reading the newly selected installed theme. header.remove( QRegularExpression("^color_[a-z_]+:[^\\n]*\\n", QRegularExpression::MultilineOption)); - QFile file(m_themes.value(name)); - if (file.open(QIODevice::ReadOnly)) { - QRegularExpression re("^([a-z_]+)\\s*=\\s*\"(#[0-9a-fA-F]{6})\"", - QRegularExpression::MultilineOption); - auto matches = re.globalMatch(QString::fromUtf8(file.readAll())); - while (matches.hasNext()) { - auto m = matches.next(); - header = setScalar(header, "color_" + m.captured(1), m.captured(2)); - } - } + for (const auto &[key, value] : readThemeColors(m_themes.value(name))) + header = setScalar(header, "color_" + key, value); replaceHeader(header); } QStringList Deck::mediaPositions() const { return ::mediaPositions(); } diff --git a/tests/tests.cpp b/tests/tests.cpp index 15ece15..13c7bb5 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -204,6 +204,28 @@ static void write(const QString &path, const QString &content) { qPrintable(value)); } } + void slidesReadThemeColorFormats() { + QTemporaryDir omarchy; + const bool wasSet = qEnvironmentVariableIsSet("OMARCHY_PATH"); + const QByteArray previous = qgetenv("OMARCHY_PATH"); + const auto restore = qScopeGuard([&] { + if (wasSet) qputenv("OMARCHY_PATH", previous); + else qunsetenv("OMARCHY_PATH"); + }); + qputenv("OMARCHY_PATH", omarchy.path().toUtf8()); + QVERIFY(QDir().mkpath(omarchy.filePath("themes/hype-formats"))); + write(omarchy.filePath("themes/hype-formats/colors.toml"), + "background = #101020\nforeground = '#eeeeee' # note\naccent = \"#7788ff\"\n"); + Deck named; + named.editSource("---\ntheme: hype-formats\n---\n\n# One\n"); + QCOMPARE(named.background(), QColor("#101020")); + QCOMPARE(named.foreground(), QColor("#eeeeee")); + Deck chosen; + chosen.chooseTheme("hype-formats"); + QVERIFY(chosen.source().contains("color_background: \"#101020\"")); + QVERIFY(chosen.source().contains("color_foreground: \"#eeeeee\"")); + QCOMPARE(chosen.accent(), QColor("#7788ff")); + } void followsDesktopTheme() { QTemporaryDir files; const QString current = files.path() + "/current";