HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
theme.hpp
1// Copyright Take Vos 2020-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../settings/settings.hpp"
8#include "../text/text.hpp"
9#include "../utility/utility.hpp"
10#include "../color/color.hpp"
11#include "../geometry/geometry.hpp"
12#include "../codec/codec.hpp"
13#include "../macros.hpp"
14#include <array>
15#include <filesystem>
16#include <string>
17#include <vector>
18
19hi_export_module(hikogui.GUI : theme);
20
21hi_export namespace hi::inline v1 {
22
23class theme {
24public:
27 float dpi = 72;
28
31 float scale = 1.0f;
32
33 std::string name;
34 theme_mode mode = theme_mode::light;
35
36 theme() noexcept = default;
37 theme(theme const&) noexcept = default;
38 theme(theme&&) noexcept = default;
39 theme& operator=(theme const&) noexcept = default;
40 theme& operator=(theme&&) noexcept = default;
41
44 theme(std::filesystem::path const& path)
45 {
46 try {
47 hi_log_info("Parsing theme at {}", path.string());
48 auto const data = parse_JSON(path);
49 parse(data);
50 } catch (std::exception const& e) {
51 throw io_error(std::format("{}: Could not load theme.\n{}", path.string(), e.what()));
52 }
53 }
54
57 template<typename T = hi::margins>
58 [[nodiscard]] constexpr T margin() const noexcept
59 {
60 if constexpr (std::is_same_v<T, hi::margins>) {
61 return hi::margins{_margin};
62 } else if constexpr (std::is_same_v<T, float>) {
63 return _margin;
64 } else {
65 hi_static_not_implemented();
66 }
67 }
68
71 [[nodiscard]] constexpr float border_width() const noexcept
72 {
73 return _border_width;
74 }
75
78 template<typename T = hi::corner_radii>
79 [[nodiscard]] constexpr T rounding_radius() const noexcept
80 {
81 if constexpr (std::is_same_v<T, hi::corner_radii>) {
82 return T{_rounding_radius};
83 } else if constexpr (std::is_same_v<T, float>) {
84 return _rounding_radius;
85 } else {
86 hi_static_not_implemented();
87 }
88 }
89
92 [[nodiscard]] constexpr float size() const noexcept
93 {
94 return _size;
95 }
96
99 [[nodiscard]] constexpr float large_size() const noexcept
100 {
101 return _large_size;
102 }
103
106 [[nodiscard]] constexpr float icon_size() const noexcept
107 {
108 return _icon_size;
109 }
110
113 [[nodiscard]] constexpr float large_icon_size() const noexcept
114 {
115 return _large_icon_size;
116 }
117
120 [[nodiscard]] constexpr float label_icon_size() const noexcept
121 {
122 return _label_icon_size;
123 }
124
127 [[nodiscard]] constexpr float baseline_adjustment() const noexcept
128 {
129 return _baseline_adjustment;
130 }
131
142 [[nodiscard]] theme transform(float new_dpi) const noexcept
143 {
144 auto r = *this;
145
146 hi_assert(new_dpi != 0.0f);
147 hi_assert(dpi != 0.0f);
148 hi_assert(scale != 0.0f);
149
150 auto delta_scale = new_dpi / dpi;
151 r.dpi = new_dpi;
152 r.scale = delta_scale * scale;
153
154 // Scale each size, and round so that everything will stay aligned on pixel boundaries.
155 r._margin = std::round(delta_scale * _margin);
156 r._border_width = std::round(delta_scale * _border_width);
157 r._rounding_radius = std::round(delta_scale * _rounding_radius);
158 r._size = std::round(delta_scale * _size);
159 r._large_size = std::round(delta_scale * _large_size);
160 r._icon_size = std::round(delta_scale * _icon_size);
161 r._large_icon_size = std::round(delta_scale * _large_icon_size);
162 r._label_icon_size = std::round(delta_scale * _label_icon_size);
163 // Cap height is not rounded, since the text-shaper will align the text to sub-pixel boundaries.
164 r._baseline_adjustment = std::round(delta_scale * _baseline_adjustment);
165
166 return r;
167 }
168
169 [[nodiscard]] hi::color color(hi::semantic_color original_color, ssize_t nesting_level = 0) const noexcept
170 {
171 auto const& shades = _colors[std::to_underlying(original_color)];
172 hi_assert(not shades.empty());
173
174 nesting_level = std::max(ssize_t{0}, nesting_level);
175 return shades[nesting_level % ssize(shades)];
176 }
177
178 [[nodiscard]] hi::color color(hi::color original_color, ssize_t nesting_level = 0) const noexcept
179 {
180 if (original_color.is_semantic()) {
181 return color(static_cast<semantic_color>(original_color), nesting_level);
182 } else {
183 return original_color;
184 }
185 }
186
187 [[nodiscard]] hi::text_style text_style(semantic_text_style theme_color) const noexcept
188 {
189 return _text_styles[std::to_underlying(theme_color)];
190 }
191
192 [[nodiscard]] hi::text_style text_style(hi::text_style original_style) const noexcept
193 {
194 if (original_style.is_semantic()) {
195 return text_style(static_cast<semantic_text_style>(original_style));
196 } else {
197 return original_style;
198 }
199 }
200
201private:
204 float _margin = 5.0f;
205
208 float _border_width = 1.0f;
209
212 float _rounding_radius = 4.0f;
213
216 float _size = 11.0f;
217
220 float _large_size = 19.0f;
221
224 float _icon_size = 8.0f;
225
228 float _large_icon_size = 23.0f;
229
232 float _label_icon_size = 15.0f;
233
236 float _baseline_adjustment = 9.0f;
237
238 std::array<std::vector<hi::color>, semantic_color_metadata.size()> _colors;
239 std::array<hi::text_style, semantic_text_style_metadata.size()> _text_styles;
240
241 [[nodiscard]] float parse_float(datum const& data, char const *object_name)
242 {
243 if (!data.contains(object_name)) {
244 throw parse_error(std::format("Missing '{}'", object_name));
245 }
246
247 auto const object = data[object_name];
248 if (auto f = get_if<double>(object)) {
249 return static_cast<float>(*f);
250 } else if (auto ll = get_if<long long>(object)) {
251 return static_cast<float>(*ll);
252 } else {
253 throw parse_error(
254 std::format("'{}' attribute must be a floating point number, got {}.", object_name, object.type_name()));
255 }
256 }
257
258 [[nodiscard]] long long parse_long_long(datum const& data, char const *object_name)
259 {
260 if (!data.contains(object_name)) {
261 throw parse_error(std::format("Missing '{}'", object_name));
262 }
263
264 auto const object = data[object_name];
265 if (auto f = get_if<long long>(object)) {
266 return static_cast<long long>(*f);
267 } else {
268 throw parse_error(std::format("'{}' attribute must be a integer, got {}.", object_name, object.type_name()));
269 }
270 }
271
272 [[nodiscard]] int parse_int(datum const& data, char const *object_name)
273 {
274 auto const value = parse_long_long(data, object_name);
276 throw parse_error(std::format("'{}' attribute is out of range, got {}.", object_name, value));
277 }
278 return narrow_cast<int>(value);
279 }
280
281 [[nodiscard]] bool parse_bool(datum const& data, char const *object_name)
282 {
283 if (!data.contains(object_name)) {
284 throw parse_error(std::format("Missing '{}'", object_name));
285 }
286
287 auto const object = data[object_name];
288 if (!holds_alternative<bool>(object)) {
289 throw parse_error(std::format("'{}' attribute must be a boolean, got {}.", object_name, object.type_name()));
290 }
291
292 return to_bool(object);
293 }
294
295 [[nodiscard]] std::string parse_string(datum const& data, char const *object_name)
296 {
297 // Extract name
298 if (!data.contains(object_name)) {
299 throw parse_error(std::format("Missing '{}'", object_name));
300 }
301 auto const object = data[object_name];
302 if (!holds_alternative<std::string>(object)) {
303 throw parse_error(std::format("'{}' attribute must be a string, got {}.", object_name, object.type_name()));
304 }
305 return static_cast<std::string>(object);
306 }
307
308 [[nodiscard]] hi::color parse_color_value(datum const& data)
309 {
310 if (holds_alternative<datum::vector_type>(data)) {
311 if (data.size() != 3 && data.size() != 4) {
312 throw parse_error(std::format("Expect 3 or 4 values for a color, got {}.", data));
313 }
314 auto const r = data[0];
315 auto const g = data[1];
316 auto const b = data[2];
317 auto const a = data.size() == 4 ? data[3] : (holds_alternative<long long>(r) ? datum{255} : datum{1.0});
318
319 if (holds_alternative<long long>(r) and holds_alternative<long long>(g) and holds_alternative<long long>(b) and
320 holds_alternative<long long>(a)) {
321 auto const r_ = get<long long>(r);
322 auto const g_ = get<long long>(g);
323 auto const b_ = get<long long>(b);
324 auto const a_ = get<long long>(a);
325
326 hi_check(r_ >= 0 and r_ <= 255, "integer red-color value not within 0 and 255");
327 hi_check(g_ >= 0 and g_ <= 255, "integer green-color value not within 0 and 255");
328 hi_check(b_ >= 0 and b_ <= 255, "integer blue-color value not within 0 and 255");
329 hi_check(a_ >= 0 and a_ <= 255, "integer alpha-color value not within 0 and 255");
330
331 return color_from_sRGB(
332 static_cast<uint8_t>(r_), static_cast<uint8_t>(g_), static_cast<uint8_t>(b_), static_cast<uint8_t>(a_));
333
334 } else if (
335 holds_alternative<double>(r) and holds_alternative<double>(g) and holds_alternative<double>(b) and
336 holds_alternative<double>(a)) {
337 auto const r_ = static_cast<float>(get<double>(r));
338 auto const g_ = static_cast<float>(get<double>(g));
339 auto const b_ = static_cast<float>(get<double>(b));
340 auto const a_ = static_cast<float>(get<double>(a));
341
342 return hi::color(r_, g_, b_, a_);
343
344 } else {
345 throw parse_error(std::format("Expect all integers or all floating point numbers in a color, got {}.", data));
346 }
347
348 } else if (auto const *color_name = get_if<std::string>(data)) {
349 auto const color_name_ = to_lower(*color_name);
350 if (color_name_.starts_with("#")) {
351 return color_from_sRGB(color_name_);
352
353 } else {
354 throw parse_error(std::format("Unable to parse color, got {}.", data));
355 }
356 } else {
357 throw parse_error(std::format("Unable to parse color, got {}.", data));
358 }
359 }
360
361 [[nodiscard]] hi::color parse_color(datum const& data, char const *object_name)
362 {
363 if (!data.contains(object_name)) {
364 throw parse_error(std::format("Missing color '{}'", object_name));
365 }
366
367 auto const color_object = data[object_name];
368
369 try {
370 return parse_color_value(color_object);
371 } catch (parse_error const&) {
372 if (auto s = get_if<std::string>(color_object)) {
374 } else {
375 throw;
376 }
377 }
378 }
379
380 [[nodiscard]] std::vector<hi::color> parse_color_list(datum const& data, char const *object_name)
381 {
382 // Extract name
383 if (!data.contains(object_name)) {
384 throw parse_error(std::format("Missing color list '{}'", object_name));
385 }
386
387 auto const color_list_object = data[object_name];
388 if (holds_alternative<datum::vector_type>(color_list_object) and not color_list_object.empty() and
389 holds_alternative<datum::vector_type>(color_list_object[0])) {
390 auto r = std::vector<hi::color>{};
391 ssize_t i = 0;
392 for (auto const& color : color_list_object) {
393 try {
394 r.push_back(parse_color_value(color));
395 } catch (parse_error const& e) {
396 throw parse_error(
397 std::format("Could not parse {}nd entry of color list '{}'\n{}", i + 1, object_name, e.what()));
398 }
399 }
400 return r;
401
402 } else {
403 try {
404 return {parse_color_value(data[object_name])};
405 } catch (parse_error const& e) {
406 throw parse_error(std::format("Could not parse color '{}'\n{}", object_name, e.what()));
407 }
408 }
409 }
410
411 [[nodiscard]] hi::text_style parse_text_style_value(datum const& data)
412 {
413 if (!holds_alternative<datum::map_type>(data)) {
414 throw parse_error(std::format("Expect a text-style to be an object, got '{}'", data));
415 }
416
417 auto const family_id = find_font_family(parse_string(data, "family"));
418 auto const font_size = parse_float(data, "size");
419
420 auto variant = font_variant{};
421 if (data.contains("weight")) {
422 variant.set_weight(parse_font_weight(data, "weight"));
423 } else {
424 variant.set_weight(font_weight::regular);
425 }
426
427 if (data.contains("italic")) {
428 variant.set_style(parse_bool(data, "italic") ? font_style::italic : font_style::normal);
429 } else {
430 variant.set_style(font_style::normal);
431 }
432
433 // resolve semantic color.
434 auto const color = this->color(parse_color(data, "color"), 0);
435
436 auto sub_styles = std::vector<text_sub_style>{};
437 sub_styles.emplace_back(
438 phrasing_mask::all, iso_639{}, iso_15924{}, family_id, variant, font_size, color, text_decoration{});
439 return hi::text_style(sub_styles);
440 }
441
442 [[nodiscard]] font_weight parse_font_weight(datum const& data, char const *object_name)
443 {
444 if (!data.contains(object_name)) {
445 throw parse_error(std::format("Missing '{}'", object_name));
446 }
447
448 auto const object = data[object_name];
449 if (auto i = get_if<long long>(object)) {
450 return font_weight_from_int(*i);
451 } else if (auto s = get_if<std::string>(object)) {
452 return font_weight_from_string(*s);
453 } else {
454 throw parse_error(std::format("Unable to parse font weight, got {}.", object.type_name()));
455 }
456 }
457
458 [[nodiscard]] hi::text_style parse_text_style(datum const& data, char const *object_name)
459 {
460 // Extract name
461 if (!data.contains(object_name)) {
462 throw parse_error(std::format("Missing text-style '{}'", object_name));
463 }
464
465 auto const textStyleObject = data[object_name];
466 try {
467 return parse_text_style_value(textStyleObject);
468 } catch (parse_error const& e) {
469 throw parse_error(std::format("Could not parse text-style '{}'\n{}", object_name, e.what()));
470 }
471 }
472
473 void parse(datum const& data)
474 {
475 hi_assert(holds_alternative<datum::map_type>(data));
476
477 name = parse_string(data, "name");
478
479 auto const mode_name = to_lower(parse_string(data, "mode"));
480 if (mode_name == "light") {
481 mode = theme_mode::light;
482 } else if (mode_name == "dark") {
483 mode = theme_mode::dark;
484 } else {
485 throw parse_error(std::format("Attribute 'mode' must be \"light\" or \"dark\", got \"{}\".", mode_name));
486 }
487
488 std::get<std::to_underlying(semantic_color::blue)>(_colors) = parse_color_list(data, "blue");
489 std::get<std::to_underlying(semantic_color::green)>(_colors) = parse_color_list(data, "green");
490 std::get<std::to_underlying(semantic_color::indigo)>(_colors) = parse_color_list(data, "indigo");
491 std::get<std::to_underlying(semantic_color::orange)>(_colors) = parse_color_list(data, "orange");
492 std::get<std::to_underlying(semantic_color::pink)>(_colors) = parse_color_list(data, "pink");
493 std::get<std::to_underlying(semantic_color::purple)>(_colors) = parse_color_list(data, "purple");
494 std::get<std::to_underlying(semantic_color::red)>(_colors) = parse_color_list(data, "red");
495 std::get<std::to_underlying(semantic_color::teal)>(_colors) = parse_color_list(data, "teal");
496 std::get<std::to_underlying(semantic_color::yellow)>(_colors) = parse_color_list(data, "yellow");
497
498 std::get<std::to_underlying(semantic_color::gray)>(_colors) = parse_color_list(data, "gray");
499 std::get<std::to_underlying(semantic_color::gray2)>(_colors) = parse_color_list(data, "gray2");
500 std::get<std::to_underlying(semantic_color::gray3)>(_colors) = parse_color_list(data, "gray3");
501 std::get<std::to_underlying(semantic_color::gray4)>(_colors) = parse_color_list(data, "gray4");
502 std::get<std::to_underlying(semantic_color::gray5)>(_colors) = parse_color_list(data, "gray5");
503 std::get<std::to_underlying(semantic_color::gray6)>(_colors) = parse_color_list(data, "gray6");
504
505 std::get<std::to_underlying(semantic_color::foreground)>(_colors) = parse_color_list(data, "foreground-color");
506 std::get<std::to_underlying(semantic_color::border)>(_colors) = parse_color_list(data, "border-color");
507 std::get<std::to_underlying(semantic_color::fill)>(_colors) = parse_color_list(data, "fill-color");
508 std::get<std::to_underlying(semantic_color::accent)>(_colors) = parse_color_list(data, "accent-color");
509 std::get<std::to_underlying(semantic_color::text_select)>(_colors) = parse_color_list(data, "text-select-color");
510 std::get<std::to_underlying(semantic_color::primary_cursor)>(_colors) = parse_color_list(data, "primary-cursor-color");
511 std::get<std::to_underlying(semantic_color::secondary_cursor)>(_colors) =
512 parse_color_list(data, "secondary-cursor-color");
513
514 std::get<std::to_underlying(semantic_text_style::label)>(_text_styles) = parse_text_style(data, "label-style");
515 std::get<std::to_underlying(semantic_text_style::small_label)>(_text_styles) =
516 parse_text_style(data, "small-label-style");
517 std::get<std::to_underlying(semantic_text_style::warning)>(_text_styles) = parse_text_style(data, "warning-label-style");
518 std::get<std::to_underlying(semantic_text_style::error)>(_text_styles) = parse_text_style(data, "error-label-style");
519 std::get<std::to_underlying(semantic_text_style::help)>(_text_styles) = parse_text_style(data, "help-label-style");
520 std::get<std::to_underlying(semantic_text_style::placeholder)>(_text_styles) =
521 parse_text_style(data, "placeholder-label-style");
522 std::get<std::to_underlying(semantic_text_style::link)>(_text_styles) = parse_text_style(data, "link-label-style");
523
524 _margin = narrow_cast<float>(parse_int(data, "margin"));
525 _border_width = narrow_cast<float>(parse_int(data, "border-width"));
526 _rounding_radius = narrow_cast<float>(parse_int(data, "rounding-radius"));
527 _size = narrow_cast<float>(parse_int(data, "size"));
528 _large_size = narrow_cast<float>(parse_int(data, "large-size"));
529 _icon_size = narrow_cast<float>(parse_int(data, "icon-size"));
530 _large_icon_size = narrow_cast<float>(parse_int(data, "large-icon-size"));
531 _label_icon_size = narrow_cast<float>(parse_int(data, "label-icon-size"));
532
533 _baseline_adjustment = std::ceil(std::get<std::to_underlying(semantic_text_style::label)>(_text_styles)->cap_height());
534 }
535
536 [[nodiscard]] friend std::string to_string(theme const& rhs) noexcept
537 {
538 return std::format("{}:{}", rhs.name, rhs.mode);
539 }
540
541 friend std::ostream& operator<<(std::ostream& lhs, theme const& rhs)
542 {
543 return lhs << to_string(rhs);
544 }
545};
546
547} // namespace hi::inline v1
Defined the color type.
hi_inline semantic_color semantic_color_from_string(std::string_view str)
Convert a string to a semantic color.
Definition semantic_color.hpp:94
semantic_color
Semantic colors.
Definition semantic_color.hpp:25
hi_inline color color_from_sRGB(float r, float g, float b, float a) noexcept
Convert gama corrected sRGB color to the linear color.
Definition sRGB.hpp:149
STL namespace.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
constexpr font_weight font_weight_from_int(numeric_integral auto rhs)
Convert a font weight value between 50 and 1000 to a font weight.
Definition font_weight.hpp:63
font_weight
Definition font_weight.hpp:21
hi_export hi_inline font_family_id find_font_family(std::string const &family_name) noexcept
Find font family id.
Definition font_book.hpp:403
text_decoration
Describes how a grapheme should be underlined when rendering the text.
Definition text_decoration.hpp:24
std::ptrdiff_t ssize_t
Signed size/index into an array.
Definition misc.hpp:32
font_style
The different styles a font-family comes with.
Definition font_style.hpp:27
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
This is a RGBA floating point color.
Definition color_intf.hpp:49
The left, bottom, right and top margins.
Definition margins.hpp:25
Definition theme.hpp:23
constexpr float large_size() const noexcept
The size of large widgets.
Definition theme.hpp:99
constexpr float baseline_adjustment() const noexcept
The amount the base-line needs to be moved downwards when a label is aligned to top.
Definition theme.hpp:127
constexpr float label_icon_size() const noexcept
Size of icons being hi_inline with a label's text.
Definition theme.hpp:120
constexpr T rounding_radius() const noexcept
The rounding radius of boxes with rounded corners.
Definition theme.hpp:79
constexpr float large_icon_size() const noexcept
Size of icons representing the length of am average word of a label's text.
Definition theme.hpp:113
constexpr float border_width() const noexcept
The line-width of a border.
Definition theme.hpp:71
constexpr float size() const noexcept
The size of small square widgets.
Definition theme.hpp:92
constexpr float icon_size() const noexcept
Size of icons inside a widget.
Definition theme.hpp:106
constexpr T margin() const noexcept
Distance between widgets and between widgets and the border of the container.
Definition theme.hpp:58
theme transform(float new_dpi) const noexcept
Create a transformed copy of the theme.
Definition theme.hpp:142
T ceil(T... args)
T emplace_back(T... args)
T max(T... args)
T round(T... args)
T to_string(T... args)
T what(T... args)