Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/apptheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ static QColor contrastInk(const QColor &color) {
.0722 * linear(color.blueF());
return luminance > .179 ? QColor("#111111") : QColor("#ffffff");
}
QList<QPair<QString, QString>> readThemeColors(const QString &path) {
QList<QPair<QString, QString>> 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",
Expand All @@ -35,17 +50,8 @@ AppTheme::AppTheme(const QString &currentDirectory, QObject *parent)
void AppTheme::reload() {
const QString theme = m_currentDirectory + "/theme", path = theme + "/colors.toml";
QMap<QString, QColor> 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"));
Expand Down
6 changes: 6 additions & 0 deletions src/apptheme.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#pragma once
#include <QFileSystemWatcher>
#include <QList>
#include <QObject>
#include <QPair>
#include <QTimer>
#include <QVariantMap>

// The hex colors in an Omarchy colors.toml in file order, as written, quoted
// or bare, with or without a trailing note.
QList<QPair<QString, QString>> readThemeColors(const QString &path);

class AppTheme : public QObject {
Q_OBJECT
Q_PROPERTY(QVariantMap colors READ colors NOTIFY changed)
Expand Down
25 changes: 5 additions & 20 deletions src/deck.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "deck.h"
#include "apptheme.h"
#include "budget.h"
#include "animationexport.h"
#include "filedialog.h"
Expand Down Expand Up @@ -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())
Expand All @@ -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(); }
Expand Down
22 changes: 22 additions & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down