SILICON
Toggle main menu visibility
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
28
namespace
SILICON::ui::theme
{
29
30
enum class
Mode
{
Light
,
Dark
};
31
32
// This is the palette's schema, not another palette. Values only live in palette.json.
33
enum class
ColorToken
{
34
Orange
,
35
Lorange
,
36
Blue
,
37
Green
,
38
Violet
,
39
Internal
,
40
Ink
,
41
Background
,
42
Surface
,
43
SurfaceRaised
,
44
Grid
,
45
Grey
,
46
Disabled
,
47
Selected
,
48
Hover
,
49
MenuHover
,
50
Error
,
51
ErrorHover
,
52
ErrorFill
,
53
SignalLow
,
54
OnAccent
,
55
OnViolet
,
56
Count
57
};
58
59
QStringView
tokenName
(
ColorToken
token);
60
std::optional<ColorToken>
tokenFromName
(QStringView name);
61
62
class
ColorTokens
{
63
public
:
64
static
ColorTokens
fromJson
(
const
QJsonObject&
object
);
65
const
QColor&
operator[]
(
ColorToken
token)
const
;
66
67
private
:
68
std::array<QColor, static_cast<std::size_t>(
ColorToken::Count
)>
values
;
69
};
70
71
struct
Theme
{
72
ColorTokens
colors
;
73
QPalette
palette
()
const
;
74
};
75
76
const
Theme
&
light
();
77
const
Theme
&
dark
();
78
79
}
// namespace SILICON::ui::theme
80
81
namespace
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().
85
class
ThemeEngine
:
public
QObject {
86
Q_OBJECT
87
public
:
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
98
signals:
99
void
themeChanged
();
100
101
private
:
102
explicit
ThemeEngine
(QObject* parent);
103
theme::Mode
currentMode
=
theme::Mode::Light
;
104
};
105
106
}
// 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::currentMode
theme::Mode currentMode
Definition
theme.hpp:103
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::ThemeEngine::themeChanged
void themeChanged()
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::Light
@ Light
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::Lorange
@ Lorange
Definition
theme.hpp:35
SILICON::ui::theme::ColorToken::OnViolet
@ OnViolet
Definition
theme.hpp:55
SILICON::ui::theme::ColorToken::ErrorHover
@ ErrorHover
Definition
theme.hpp:51
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::Error
@ Error
Definition
theme.hpp:50
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::OnAccent
@ OnAccent
Definition
theme.hpp:54
SILICON::ui::theme::ColorToken::Background
@ Background
Definition
theme.hpp:41
SILICON::ui::theme::ColorToken::Surface
@ Surface
Definition
theme.hpp:42
SILICON::ui::theme::ColorToken::Internal
@ Internal
Definition
theme.hpp:39
SILICON::ui::theme::ColorToken::Disabled
@ Disabled
Definition
theme.hpp:46
SILICON::ui::theme::ColorToken::ErrorFill
@ ErrorFill
Definition
theme.hpp:52
SILICON::ui::theme::ColorToken::Grey
@ Grey
Definition
theme.hpp:45
SILICON::ui::theme::ColorToken::Green
@ Green
Definition
theme.hpp:37
SILICON::ui::theme::ColorToken::MenuHover
@ MenuHover
Definition
theme.hpp:49
SILICON::ui::theme::ColorToken::Count
@ Count
Definition
theme.hpp:56
SILICON::ui::theme::ColorToken::Hover
@ Hover
Definition
theme.hpp:48
SILICON::ui::theme::ColorToken::SignalLow
@ SignalLow
Definition
theme.hpp:53
SILICON::ui::theme::light
const Theme & light()
Definition
theme.cpp:133
SILICON::ui
Definition
actionGroups.hpp:14
SILICON::ui::ColorToken
ColorToken
Definition
theme.hpp:33
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
src
ui
shell
theme.hpp
Generated by
1.17.0