Aurore
A Switch-like interface for your app, on every screen
Loading...
Searching...
No Matches
theme.hpp
Go to the documentation of this file.
1/*
2 Copyright 2019-2020 natinusala
3 Copyright 2019 p-sam
4 Copyright 2026 JigSawFr
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17*/
18
19#pragma once
20
21#include <nanovg.h>
22
23#include <cstdint>
24#include <ctime>
25#include <initializer_list>
26#include <map>
27#include <memory>
28#include <string>
29#include <unordered_map>
30#include <vector>
31
32namespace aurore
33{
34
36enum class ThemeVariant
37{
38 LIGHT,
39 DARK
40};
41
45{
46 public:
48 ThemeValues(std::initializer_list<std::pair<std::string, NVGcolor>> list);
49
51 void addColor(const std::string&, NVGcolor color);
53 NVGcolor getColor(const std::string&);
55 bool hasColor(const std::string&) const;
56
58 void setOverlay(const std::unordered_map<std::string, NVGcolor>& overlay);
59
61 const std::unordered_map<std::string, NVGcolor>& getColors() const { return this->values; }
62 const std::unordered_map<std::string, NVGcolor>& getBaseColors() const { return this->base; }
63
64 private:
65 std::unordered_map<std::string, NVGcolor> base;
66 std::unordered_map<std::string, NVGcolor> overlay;
67 std::unordered_map<std::string, NVGcolor> values;
68};
69
71enum class BackgroundFit
72{
73 COVER,
74 CONTAIN,
75 TILE,
76 CENTER,
77};
78
81{
82 static constexpr float DEFAULT_DIM = 0.35f;
83
85 std::string image;
91 float surfaceOpacity = 1.0f;
92
93 bool hasImage() const { return !this->image.empty(); }
94};
95
98{
99 std::string image;
102 bool tint = false;
103
104 bool operator==(const ThemeIcon&) const = default;
105};
106
110{
111 std::string logo;
112 std::string preview;
113 std::map<std::string, ThemeIcon> icons;
114 std::map<std::string, std::string> images;
115
116 bool empty() const { return this->logo.empty() && this->preview.empty() && this->icons.empty() && this->images.empty(); }
117 bool operator==(const ThemePictures&) const = default;
118};
119
120struct ThemeImage;
121
122// ---------------------------------------------------------------------------
123// Decorations (docs/themes.md, Decorations): pictures over the surfaces of
124// a few areas, strips along the screen's edges, particles
125// ---------------------------------------------------------------------------
126
128enum class ThemeArea
129{
130 NONE,
131 HEADER,
132 SIDEBAR,
133 DIALOG,
134 TOP,
135 BOTTOM,
136};
137constexpr int THEME_AREA_COUNT = 5;
138
141{
142 std::string image;
144 float opacity = 1.0f;
147 float height = 0.0f;
148
149 bool hasImage() const { return !this->image.empty(); }
150};
151
153enum class ParticleKind
154{
155 NONE,
156 SNOW,
157 LEAVES,
158 PETALS,
159 EMBERS,
160 CONFETTI,
161 SPARKLES,
162 HEARTS,
163 BUBBLES,
164};
165
169{
170 BACK,
171 FRONT,
172};
173
176{
177 static constexpr float LOW = 0.3f, MEDIUM = 0.6f, HIGH = 1.0f;
178 static constexpr int MAX_COLORS = 5;
179
181 float density = LOW;
182 float speed = 1.0f;
185 std::vector<NVGcolor> colors;
186
188 bool enabled() const { return this->kind != ParticleKind::NONE && this->density > 0.0f; }
189};
190
193{
196
198 const ThemePicture& picture(ThemeArea area) const { return this->pictures[(int)area - 1]; }
199 bool hasPictures() const
200 {
201 for (const ThemePicture& picture : this->pictures)
202 if (picture.hasImage())
203 return true;
204 return false;
205 }
206};
207
210{
211 std::string path;
212 std::string id;
213 std::string name;
214 std::string author;
215 std::string version;
216 std::string description;
219 std::string backgroundImage;
222 std::string icon;
223
227 std::string counterpart;
228
231 bool light = false;
232 bool dark = false;
233
235 struct Preview
236 {
237 NVGcolor background;
238 NVGcolor surface;
239 NVGcolor text;
240 NVGcolor accent;
241 NVGcolor highlight;
242 };
245
253
258 {
259 return variant == ThemeVariant::LIGHT ? this->lightPictures : this->darkPictures;
260 }
261
266 {
267 return variant == ThemeVariant::LIGHT ? this->lightDecor : this->darkDecor;
268 }
269
271 std::vector<std::string> errors;
276 std::vector<std::string> warnings;
277
279 bool ok() const { return this->errors.empty(); }
281 ThemeVariant variant() const { return this->light && !this->dark ? ThemeVariant::LIGHT : ThemeVariant::DARK; }
283 bool styles(ThemeVariant variant) const { return variant == ThemeVariant::LIGHT ? this->light : this->dark; }
284};
285
297class Theme
298{
299 public:
303 NVGcolor operator[](const std::string& name);
304
306 void addColor(const std::string&, NVGcolor color);
308 NVGcolor getColor(const std::string& name);
310 bool hasColor(const std::string& name);
312 ThemeValues* getValues() { return this->values; }
313
319 static Theme& get(ThemeVariant variant);
320
344 static bool loadFile(const std::string& path, ThemeVariant* variant = nullptr);
345
350 static bool load(const std::string& text, ThemeVariant* variant = nullptr, const std::string& directory = "");
351
355 static void reset();
356
364 static void followCalendar();
365
370 static std::string seasonalTheme(std::time_t when);
371
375 static const std::string& getCurrentPath();
376
381 static bool styles(ThemeVariant variant);
382
388 static std::string getBackgroundImage();
389
396
403
415 static bool resolveAsset(const std::string& name, const std::string& directory, const std::string& root,
416 std::string* path, std::string* error);
417
426 static bool recheckAsset(const std::string& path, std::string* error = nullptr);
427
434
439 static void addIconNames(const std::vector<std::string>& names);
440
449 static void addImageSlot(const std::string& key, const std::string& fallback = "", int maxSide = 0);
450
454 static std::vector<std::string> getImageSlots();
455
465 static std::shared_ptr<ThemeImage> image(const std::string& key);
466
472 static std::shared_ptr<ThemeImage> logo();
473
479 static std::shared_ptr<ThemeImage> icon(const std::string& name, bool* tint = nullptr);
480
488 static void addSurfaceKey(const std::string& key);
489
497 static ThemeInfo inspect(const std::string& path, bool decodePictures = false);
498
507 static std::vector<ThemeInfo> listThemes(const std::string& directory, bool decodePictures = false);
508
512 static std::vector<ThemeInfo> builtinThemes();
513
518 static bool parseColor(const std::string& text, NVGcolor* color);
519
523 static float contrast(NVGcolor a, NVGcolor b);
524
530 static void addContrastCheck(const std::string& ink, const std::string& surface, float minimum = 4.5f);
531
539 static std::vector<std::string> checkContrast(ThemeVariant variant);
540
546 static std::vector<std::string> checkBackgroundContrast(ThemeVariant variant);
547
553 static uint64_t getGeneration();
554
555 private:
556 ThemeValues* values;
557};
558
559} // namespace aurore
Definition theme.hpp:45
void setOverlay(const std::unordered_map< std::string, NVGcolor > &overlay)
The theme file's colours, over the base (empty: the base alone).
const std::unordered_map< std::string, NVGcolor > & getColors() const
Every colour as drawn now, and the base alone.
Definition theme.hpp:61
void addColor(const std::string &, NVGcolor color)
A colour of the base: what the variant is without a theme file.
NVGcolor getColor(const std::string &)
A colour as drawn now; an unknown key is fatal (check with hasColor()).
bool hasColor(const std::string &) const
Whether the key exists.
const std::unordered_map< std::string, NVGcolor > & getBaseColors() const
Definition theme.hpp:62
ThemeValues(std::initializer_list< std::pair< std::string, NVGcolor > > list)
The base colours.
Definition theme.hpp:298
static void addSurfaceKey(const std::string &key)
ThemeValues * getValues()
The colours behind the handle.
Definition theme.hpp:312
static bool load(const std::string &text, ThemeVariant *variant=nullptr, const std::string &directory="")
static std::shared_ptr< ThemeImage > image(const std::string &key)
static void followCalendar()
static std::string seasonalTheme(std::time_t when)
static std::shared_ptr< ThemeImage > icon(const std::string &name, bool *tint=nullptr)
static std::vector< std::string > checkContrast(ThemeVariant variant)
static std::shared_ptr< ThemeImage > logo()
static Theme & getLightTheme()
The light variant's colours.
static void addIconNames(const std::vector< std::string > &names)
static std::vector< ThemeInfo > listThemes(const std::string &directory, bool decodePictures=false)
static bool loadFile(const std::string &path, ThemeVariant *variant=nullptr)
void addColor(const std::string &, NVGcolor color)
A colour of the base (see ThemeValues::addColor).
static ThemePictures getPictures(ThemeVariant variant)
static void addContrastCheck(const std::string &ink, const std::string &surface, float minimum=4.5f)
static float contrast(NVGcolor a, NVGcolor b)
static std::vector< std::string > getImageSlots()
static bool styles(ThemeVariant variant)
static Theme & get(ThemeVariant variant)
The colours of variant.
static ThemeInfo inspect(const std::string &path, bool decodePictures=false)
static void addImageSlot(const std::string &key, const std::string &fallback="", int maxSide=0)
static const std::string & getCurrentPath()
static std::string getBackgroundImage()
static std::vector< std::string > checkBackgroundContrast(ThemeVariant variant)
static bool recheckAsset(const std::string &path, std::string *error=nullptr)
NVGcolor getColor(const std::string &name)
The colour name as drawn now; an unknown key is fatal (check with hasColor()).
NVGcolor operator[](const std::string &name)
The colour name (getColor()).
static uint64_t getGeneration()
Theme(ThemeValues *values)
A handle on one variant's colours (not owned).
static ThemeDecor getDecor(ThemeVariant variant)
static ThemeBackground getBackground(ThemeVariant variant)
bool hasColor(const std::string &name)
Whether the key exists.
static Theme & getDarkTheme()
The dark variant's colours.
static bool parseColor(const std::string &text, NVGcolor *color)
static bool resolveAsset(const std::string &name, const std::string &directory, const std::string &root, std::string *path, std::string *error)
static void reset()
static std::vector< ThemeInfo > builtinThemes()
Definition actions.hpp:25
ThemeVariant
The two sets of colours every theme has: light and dark.
Definition theme.hpp:37
ParticleKind
The particles a theme may draw (particles in a theme file).
Definition theme.hpp:154
BackgroundFit
How a theme's background_image fills the window (background_fit)
Definition theme.hpp:72
@ CONTAIN
scaled to fit in it whole, centred, aurore/clear around it
@ COVER
scaled to cover it, centred, cut at the edges (the default)
@ TILE
repeated from the top left, one pixel per unit of the layout
constexpr int THEME_AREA_COUNT
HEADER to BOTTOM.
Definition theme.hpp:137
ThemeArea
What a decoration picture is drawn in (View::setThemeArea)
Definition theme.hpp:129
@ BOTTOM
decor.bottom: along its bottom edge
@ DIALOG
backdrop.dialog: dialogs, dropdowns, Cadran's sheets
@ TOP
decor.top: a strip along the top edge of the screen
@ SIDEBAR
backdrop.sidebar: the sidebar (Cadran's rail)
@ HEADER
backdrop.header: a screen's header, its whole width
ParticleLayer
Definition theme.hpp:169
Definition text.hpp:13
A variant's background, as a theme file sets it (docs/themes.md)
Definition theme.hpp:81
BackgroundFit fit
Definition theme.hpp:86
float surfaceOpacity
Definition theme.hpp:91
static constexpr float DEFAULT_DIM
Definition theme.hpp:82
float dim
background_dim: how much of aurore/background veils the image (0 to 1)
Definition theme.hpp:88
bool hasImage() const
Definition theme.hpp:93
std::string image
background_image: its path (Theme::resolveAsset), "" without one
Definition theme.hpp:85
A variant's decorations: one picture per area, and particles.
Definition theme.hpp:193
const ThemePicture & picture(ThemeArea area) const
The picture of area.
Definition theme.hpp:198
bool hasPictures() const
Definition theme.hpp:199
ThemePicture pictures[THEME_AREA_COUNT]
by ThemeArea, HEADER first
Definition theme.hpp:194
ThemeParticles particles
Definition theme.hpp:195
A Cadran icon a theme draws as a picture (icon.<name>, docs/themes.md)
Definition theme.hpp:98
std::string image
Definition theme.hpp:99
bool tint
Definition theme.hpp:102
bool operator==(const ThemeIcon &) const =default
Definition theme_image.hpp:49
A few colours to show it in a picker, per variant it styles.
Definition theme.hpp:236
NVGcolor accent
Definition theme.hpp:240
NVGcolor text
Definition theme.hpp:239
NVGcolor highlight
Definition theme.hpp:241
NVGcolor background
Definition theme.hpp:237
NVGcolor surface
Definition theme.hpp:238
A theme file as listThemes() or inspect() read it, without applying it.
Definition theme.hpp:210
const ThemeBackground & background(ThemeVariant variant) const
Definition theme.hpp:249
std::string icon
Definition theme.hpp:222
std::string author
its author
Definition theme.hpp:214
bool dark
Definition theme.hpp:232
std::string name
its name, else the id
Definition theme.hpp:213
std::string path
as given to loadFile(); "builtin:<id>" for a preset
Definition theme.hpp:211
std::string backgroundImage
Definition theme.hpp:219
bool ok() const
Whether it was read without errors.
Definition theme.hpp:279
std::string counterpart
Definition theme.hpp:227
std::vector< std::string > warnings
Definition theme.hpp:276
ThemePictures darkPictures
Definition theme.hpp:256
bool light
Definition theme.hpp:231
Preview darkPreview
Definition theme.hpp:244
std::string version
its version
Definition theme.hpp:215
const ThemePictures & pictures(ThemeVariant variant) const
Definition theme.hpp:257
std::string description
Definition theme.hpp:216
Preview lightPreview
Definition theme.hpp:243
ThemeDecor darkDecor
Definition theme.hpp:264
ThemeBackground lightBackground
Each variant's background (image, fit, dim, surface opacity)
Definition theme.hpp:247
ThemePictures lightPictures
Each variant's logo, icons, preview and image slots' pictures.
Definition theme.hpp:255
std::vector< std::string > errors
"line 4: #12 is not a colour"…: lines skipped, files not found.
Definition theme.hpp:271
bool styles(ThemeVariant variant) const
Whether it styles variant.
Definition theme.hpp:283
ThemeVariant variant() const
The variant to show it in when it styles one only.
Definition theme.hpp:281
ThemeDecor lightDecor
Each variant's decorations (backdrops, strips, particles)
Definition theme.hpp:263
const ThemeDecor & decor(ThemeVariant variant) const
Definition theme.hpp:265
ThemeBackground darkBackground
Definition theme.hpp:248
std::string id
the file's name without its extension
Definition theme.hpp:212
A variant's particles, as a theme file sets them (docs/themes.md).
Definition theme.hpp:176
static constexpr int MAX_COLORS
Definition theme.hpp:178
float speed
0.25 to 4
Definition theme.hpp:182
std::vector< NVGcolor > colors
particles_color: up to MAX_COLORS; empty: from the palette
Definition theme.hpp:185
ParticleKind kind
Definition theme.hpp:180
static constexpr float LOW
Definition theme.hpp:177
bool enabled() const
Whether any particle is drawn.
Definition theme.hpp:188
float density
0 to 1, of ThemeDecorations::MAX_PARTICLES
Definition theme.hpp:181
static constexpr float HIGH
Definition theme.hpp:177
static constexpr float MEDIUM
Definition theme.hpp:177
ParticleLayer layer
Definition theme.hpp:183
A picture of the decorations, as a theme file sets it.
Definition theme.hpp:141
bool hasImage() const
Definition theme.hpp:149
float opacity
Definition theme.hpp:144
float height
Definition theme.hpp:147
std::string image
its path (Theme::resolveAsset), "" without one
Definition theme.hpp:142
BackgroundFit fit
Definition theme.hpp:143
Definition theme.hpp:110
std::string logo
logo: the app's mark, "" for the app's own
Definition theme.hpp:111
bool operator==(const ThemePictures &) const =default
bool empty() const
Definition theme.hpp:116
std::map< std::string, std::string > images
image.<key>, by image slot
Definition theme.hpp:114
std::string preview
preview: the theme's thumbnail in a picker
Definition theme.hpp:112
std::map< std::string, ThemeIcon > icons
icon.<name>, by icon name
Definition theme.hpp:113