SILICON
Loading...
Searching...
No Matches
theme.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <array>
20#include <optional>
21
22#include <QApplication>
23#include <QColor>
24#include <QJsonObject>
25#include <QPalette>
26#include <QStringView>
27
28namespace SILICON::ui::theme {
29
30enum class Mode { Light, Dark };
31
32// This is the palette's schema, not another palette. Values only live in palette.json.
58
59QStringView tokenName(ColorToken token);
60std::optional<ColorToken> tokenFromName(QStringView name);
61
63public:
64 static ColorTokens fromJson(const QJsonObject& object);
65 const QColor& operator[](ColorToken token) const;
66
67private:
68 std::array<QColor, static_cast<std::size_t>(ColorToken::Count)> values;
69};
70
71struct Theme {
73 QPalette palette() const;
74};
75
76const Theme& light();
77const Theme& dark();
78
79} // namespace SILICON::ui::theme
80
81namespace SILICON::ui {
82
83// GUI-thread service owned by QApplication. Colors reference immutable palettes;
84// views that store colors or pixmaps must refresh them on themeChanged().
85class ThemeEngine : public QObject {
86 Q_OBJECT
87public:
88 static ThemeEngine& instance();
89 static void apply(QApplication& app, theme::Mode mode);
90 static const theme::Theme& current();
91 static theme::Mode mode() { return instance().currentMode; }
92 static const QColor& getColor(theme::ColorToken token)
93 {
94 return current().colors[token];
95 }
96 static const QColor& getColor(const QString& key);
97
98signals:
100
101private:
102 explicit ThemeEngine(QObject* parent);
104};
105
106} // namespace SILICON::ui
static void apply(QApplication &app, theme::Mode mode)
Definition theme.cpp:169
static theme::Mode mode()
Definition theme.hpp:91
static const QColor & getColor(theme::ColorToken token)
Definition theme.hpp:92
static ThemeEngine & instance()
Definition theme.cpp:147
theme::Mode currentMode
Definition theme.hpp:103
ThemeEngine(QObject *parent)
Definition theme.cpp:145
static const theme::Theme & current()
Definition theme.cpp:164
const QColor & operator[](ColorToken token) const
Definition theme.cpp:96
std::array< QColor, static_cast< std::size_t >(ColorToken::Count)> values
Definition theme.hpp:68
static ColorTokens fromJson(const QJsonObject &object)
Definition theme.cpp:79
QStringView tokenName(ColorToken token)
Definition theme.cpp:65
const Theme & dark()
Definition theme.cpp:137
std::optional< ColorToken > tokenFromName(QStringView name)
Definition theme.cpp:70
const Theme & light()
Definition theme.cpp:133
QPalette palette() const
Definition theme.cpp:101