SILICON
Toggle main menu visibility
Loading...
Searching...
No Matches
theme.cpp
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
#include "
theme.hpp
"
18
19
#include <stdexcept>
20
21
#include <QEvent>
22
#include <QFile>
23
#include <QJsonDocument>
24
#include <QPointer>
25
#include <QRegularExpression>
26
#include <QThread>
27
#include <QWidget>
28
29
namespace
SILICON::ui::theme
{
30
namespace
{
31
// Array indices follow ColorToken; JSON and SVG annotations use these names.
32
constexpr
std::array names{u
"SILICON_ORANGE"
, u
"SILICON_LORANGE"
,
33
u
"SILICON_BLUE"
, u
"SILICON_GREEN"
,
34
u
"SILICON_VIOLET"
, u
"SILICON_INTERNAL"
,
35
u
"SILICON_INK"
, u
"SILICON_BACKGROUND"
,
36
u
"SILICON_SURFACE"
, u
"SILICON_SURFACE_RAISED"
,
37
u
"SILICON_GRID"
, u
"SILICON_GREY"
,
38
u
"SILICON_DISABLED"
, u
"SILICON_SELECTED"
,
39
u
"SILICON_HOVER"
, u
"SILICON_MENU_HOVER"
,
40
u
"SILICON_ERROR"
, u
"SILICON_ERROR_HOVER"
,
41
u
"SILICON_ERROR_FILL"
, u
"SILICON_SIGNAL_LOW"
,
42
u
"SILICON_ON_ACCENT"
, u
"SILICON_ON_VIOLET"
};
43
static_assert
(names.size() ==
static_cast<
std::size_t
>
(
ColorToken::Count
));
44
45
const
std::array<Theme, 2>& themes()
46
{
47
// Validate both immutable palettes once, before either becomes available.
48
static
const
auto
result = [] {
49
QFile file(
":/palette.json"
);
50
if
(!file.open(QFile::ReadOnly))
51
throw
std::runtime_error(
"Cannot read shared palette"
);
52
QJsonParseError error;
53
const
auto
doc = QJsonDocument::fromJson(file.readAll(), &error);
54
if
(error.error != QJsonParseError::NoError || !doc.isObject()
55
|| doc.object().keys() != QStringList({
"dark"
,
"light"
}))
56
throw
std::runtime_error(
"Shared palette must contain exactly light and dark"
);
57
return
std::array{
58
Theme
{.colors =
ColorTokens::fromJson
(doc.object().value(
"light"
).toObject())},
59
Theme
{.colors =
ColorTokens::fromJson
(doc.object().value(
"dark"
).toObject())}};
60
}();
61
return
result;
62
}
63
}
// namespace
64
65
QStringView
tokenName
(
ColorToken
token)
66
{
67
return
names.at(
static_cast<
std::size_t
>
(token));
68
}
69
70
std::optional<ColorToken>
tokenFromName
(QStringView name)
71
{
72
for
(std::size_t i = 0; i < names.size(); ++i) {
73
if
(name == QStringView(names[i]))
74
return
static_cast<
ColorToken
>
(i);
75
}
76
return
std::nullopt;
77
}
78
79
ColorTokens
ColorTokens::fromJson
(
const
QJsonObject&
object
)
80
{
81
if
(
object
.size() !=
static_cast<
qsizetype
>
(names.size()))
82
throw
std::runtime_error(
"Missing or unknown shared palette token"
);
83
const
QRegularExpression hex(
"^#[0-9a-fA-F]{6}$"
);
84
ColorTokens
result;
85
for
(std::size_t i = 0; i < names.size(); ++i) {
86
const
auto
name = QStringView(names[i]).toString();
87
const
auto
value =
object
.value(name);
88
if
(!value.isString() || !hex.match(value.toString()).hasMatch())
89
throw
std::runtime_error(
"Missing or invalid shared palette token: "
90
+ name.toStdString());
91
result.
values
[i] = QColor(value.toString());
92
}
93
return
result;
94
}
95
96
const
QColor&
ColorTokens::operator[]
(
ColorToken
token)
const
97
{
98
return
values
.at(
static_cast<
std::size_t
>
(token));
99
}
100
101
QPalette
Theme::palette
()
const
102
{
103
QPalette p;
104
const
auto
set = [&](QPalette::ColorRole role,
ColorToken
token) {
105
p.setColor(QPalette::All, role,
colors
[token]);
106
};
107
set(QPalette::Window,
ColorToken::Background
);
108
set(QPalette::WindowText,
ColorToken::Ink
);
109
set(QPalette::Base,
ColorToken::Surface
);
110
set(QPalette::AlternateBase,
ColorToken::SurfaceRaised
);
111
set(QPalette::Text,
ColorToken::Ink
);
112
set(QPalette::Button,
ColorToken::Surface
);
113
set(QPalette::ButtonText,
ColorToken::Ink
);
114
set(QPalette::BrightText,
ColorToken::Orange
);
115
set(QPalette::Highlight,
ColorToken::Selected
);
116
set(QPalette::HighlightedText,
ColorToken::Ink
);
117
set(QPalette::Link,
ColorToken::Blue
);
118
set(QPalette::LinkVisited,
ColorToken::Violet
);
119
set(QPalette::PlaceholderText,
ColorToken::Grey
);
120
set(QPalette::Light,
ColorToken::SurfaceRaised
);
121
set(QPalette::Midlight,
ColorToken::SurfaceRaised
);
122
set(QPalette::Mid,
ColorToken::Grid
);
123
set(QPalette::Dark,
ColorToken::Grey
);
124
set(QPalette::Shadow,
ColorToken::Ink
);
125
set(QPalette::ToolTipBase,
ColorToken::SurfaceRaised
);
126
set(QPalette::ToolTipText,
ColorToken::Ink
);
127
for
(
auto
role : {QPalette::WindowText, QPalette::Text, QPalette::ButtonText,
128
QPalette::HighlightedText})
129
p.setColor(QPalette::Disabled, role,
colors
[
ColorToken::Disabled
]);
130
return
p;
131
}
132
133
const
Theme
&
light
()
134
{
135
return
themes()[0];
136
}
137
const
Theme
&
dark
()
138
{
139
return
themes()[1];
140
}
141
}
// namespace SILICON::ui::theme
142
143
namespace
SILICON::ui
{
144
145
ThemeEngine::ThemeEngine
(QObject* parent) : QObject(parent) {}
146
147
ThemeEngine
&
ThemeEngine::instance
()
148
{
149
Q_ASSERT(qApp && QThread::currentThread() == qApp->thread());
150
static
QPointer<ThemeEngine> engine;
151
if
(!engine)
152
engine =
new
ThemeEngine
(qApp);
153
return
*engine;
154
}
155
156
const
QColor&
ThemeEngine::getColor
(
const
QString& key)
157
{
158
const
auto
token =
theme::tokenFromName
(key);
159
if
(!token)
160
throw
std::invalid_argument(
"Unknown theme token: "
+ key.toStdString());
161
return
getColor
(*token);
162
}
163
164
const
theme::Theme
&
ThemeEngine::current
()
165
{
166
return
mode
() ==
theme::Mode::Dark
?
theme::dark
() :
theme::light
();
167
}
168
169
void
ThemeEngine::apply
(QApplication& app,
theme::Mode
mode
)
170
{
171
auto
& engine =
instance
();
172
const
auto
palette =
173
(
mode
==
theme::Mode::Dark
?
theme::dark
() :
theme::light
()).palette();
174
if
(engine.currentMode ==
mode
&& app.palette() == palette)
175
return
;
176
engine.currentMode =
mode
;
177
app.setPalette(palette);
178
// Deliver palette changes before observers refresh their palette-backed views.
179
QCoreApplication::sendPostedEvents(
nullptr
, QEvent::ApplicationPaletteChange);
180
emit engine.themeChanged();
181
for
(
auto
* window : QApplication::topLevelWidgets())
182
window->update();
183
}
184
185
}
// namespace SILICON::ui
SILICON::ui::ThemeEngine::apply
static void apply(QApplication &app, theme::Mode mode)
Definition
theme.cpp:169
SILICON::ui::ThemeEngine::mode
static theme::Mode mode()
Definition
theme.hpp:91
SILICON::ui::ThemeEngine::getColor
static const QColor & getColor(theme::ColorToken token)
Definition
theme.hpp:92
SILICON::ui::ThemeEngine::instance
static ThemeEngine & instance()
Definition
theme.cpp:147
SILICON::ui::ThemeEngine::ThemeEngine
ThemeEngine(QObject *parent)
Definition
theme.cpp:145
SILICON::ui::ThemeEngine::current
static const theme::Theme & current()
Definition
theme.cpp:164
SILICON::ui::theme::ColorTokens
Definition
theme.hpp:62
SILICON::ui::theme::ColorTokens::operator[]
const QColor & operator[](ColorToken token) const
Definition
theme.cpp:96
SILICON::ui::theme::ColorTokens::values
std::array< QColor, static_cast< std::size_t >(ColorToken::Count)> values
Definition
theme.hpp:68
SILICON::ui::theme::ColorTokens::fromJson
static ColorTokens fromJson(const QJsonObject &object)
Definition
theme.cpp:79
SILICON::ui::theme
Definition
theme.cpp:29
SILICON::ui::theme::tokenName
QStringView tokenName(ColorToken token)
Definition
theme.cpp:65
SILICON::ui::theme::Mode
Mode
Definition
theme.hpp:30
SILICON::ui::theme::Mode::Dark
@ Dark
Definition
theme.hpp:30
SILICON::ui::theme::dark
const Theme & dark()
Definition
theme.cpp:137
SILICON::ui::theme::tokenFromName
std::optional< ColorToken > tokenFromName(QStringView name)
Definition
theme.cpp:70
SILICON::ui::theme::ColorToken
ColorToken
Definition
theme.hpp:33
SILICON::ui::theme::ColorToken::Ink
@ Ink
Definition
theme.hpp:40
SILICON::ui::theme::ColorToken::Grid
@ Grid
Definition
theme.hpp:44
SILICON::ui::theme::ColorToken::SurfaceRaised
@ SurfaceRaised
Definition
theme.hpp:43
SILICON::ui::theme::ColorToken::Violet
@ Violet
Definition
theme.hpp:38
SILICON::ui::theme::ColorToken::Orange
@ Orange
Definition
theme.hpp:34
SILICON::ui::theme::ColorToken::Selected
@ Selected
Definition
theme.hpp:47
SILICON::ui::theme::ColorToken::Blue
@ Blue
Definition
theme.hpp:36
SILICON::ui::theme::ColorToken::Background
@ Background
Definition
theme.hpp:41
SILICON::ui::theme::ColorToken::Surface
@ Surface
Definition
theme.hpp:42
SILICON::ui::theme::ColorToken::Disabled
@ Disabled
Definition
theme.hpp:46
SILICON::ui::theme::ColorToken::Grey
@ Grey
Definition
theme.hpp:45
SILICON::ui::theme::ColorToken::Count
@ Count
Definition
theme.hpp:56
SILICON::ui::theme::light
const Theme & light()
Definition
theme.cpp:133
SILICON::ui
Definition
actionGroups.hpp:14
SILICON::ui::theme::Theme
Definition
theme.hpp:71
SILICON::ui::theme::Theme::palette
QPalette palette() const
Definition
theme.cpp:101
SILICON::ui::theme::Theme::colors
ColorTokens colors
Definition
theme.hpp:72
theme.hpp
src
ui
shell
theme.cpp
Generated by
1.17.0