HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
style_sheet.hpp
1// Copyright Take Vos 2023.
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 "../utility/module.hpp"
8#include "../color/module.hpp"
9#include "../font/module.hpp"
10#include "../text/module.hpp"
11#include "../i18n/module.hpp"
12#include "theme_mode.hpp"
13#include "theme_state.hpp"
14#include "theme_length.hpp"
15#include "theme_model.hpp"
16#include <string>
17#include <vector>
18#include <variant>
19
20namespace hi { inline namespace v1 {
21
22
23enum class style_sheet_value_mask {
24 pixels = 0b0000'0001,
25 dips = 0b0000'0010,
26 em_quads = 0b0000'0100,
27 color = 0b0000'1000,
28 font_family_id = 0b0001'0000,
29 font_weight = 0b0010'0000,
30 font_style = 0b0100'0000,
31
32 length = pixels | dips | em_quads,
33};
34
35[[nodiscard]] constexpr style_sheet_value_mask
36operator|(style_sheet_value_mask const& lhs, style_sheet_value_mask const& rhs) noexcept
37{
38 return static_cast<style_sheet_value_mask>(to_underlying(lhs) | to_underlying(rhs));
39}
40
41[[nodiscard]] constexpr style_sheet_value_mask
42operator&(style_sheet_value_mask const& lhs, style_sheet_value_mask const& rhs) noexcept
43{
44 return static_cast<style_sheet_value_mask>(to_underlying(lhs) & to_underlying(rhs));
45}
46
47[[nodiscard]] constexpr bool to_bool(style_sheet_value_mask const& rhs) noexcept
48{
49 return static_cast<bool>(to_underlying(rhs));
50}
51
53 std::variant<hi::pixels, hi::dips, hi::em_quads, hi::color, font_family_id, font_weight, font_style> {
54 using super = std::variant<hi::pixels, hi::dips, hi::em_quads, hi::color, font_family_id, font_weight, font_style>;
55 using super::super;
56
57 constexpr style_sheet_value(theme_length length) noexcept :
58 super([&] {
59 if (auto pixels_ptr = std::get_if<hi::pixels>(&length)) {
60 return super{*pixels_ptr};
61 } else if (auto dips_ptr = std::get_if<hi::dips>(&length)) {
62 return super{*dips_ptr};
63 } else if (auto em_quads_ptr = std::get_if<hi::em_quads>(&length)) {
64 return super{*em_quads_ptr};
65 } else {
67 }
68 }())
69 {
70 }
71};
72
75 std::vector<bool> is_child;
76
77 [[nodiscard]] constexpr std::string get_path_as_string() const noexcept
78 {
79 hi_assert(not path.empty());
80 auto r = path[0];
81
82 hi_assert(path.size() == is_child.size() + 1);
83
84 for (auto i = 0_uz; i != is_child.size(); ++i) {
85 if (is_child[i]) {
86 r += ">";
87 } else {
88 r += " ";
89 }
90 r += path[i + 1];
91 }
92
93 return r;
94 }
95};
96
97struct style_sheet_selector : std::vector<style_sheet_pattern> {};
98
99enum class style_sheet_declaration_name {
100 background_color,
101 border_bottom_left_radius,
102 border_bottom_right_radius,
103 border_color,
104 border_top_left_radius,
105 border_top_right_radius,
106 border_width,
107 caret_color_primary,
108 caret_color_secondary,
109 fill_color,
110 font_color,
111 font_family,
112 font_size,
114 font_weight,
115 height,
116 margin_bottom,
117 margin_left,
118 margin_right,
119 margin_top,
120 selection_color,
121 spacing_horizontal,
122 spacing_vertical,
123 width,
124};
125
126// clang-format off
127constexpr auto style_sheet_declaration_name_metadata = enum_metadata{
128 style_sheet_declaration_name::background_color, "background-color",
129 style_sheet_declaration_name::border_bottom_left_radius, "border-bottom-left-radius",
130 style_sheet_declaration_name::border_bottom_right_radius, "border-bottom-right-radius",
131 style_sheet_declaration_name::border_color, "border-color",
132 style_sheet_declaration_name::border_top_left_radius, "border-top-left-radius",
133 style_sheet_declaration_name::border_top_right_radius, "border-top-right-radius",
134 style_sheet_declaration_name::border_width, "border-width",
135 style_sheet_declaration_name::caret_color_primary, "caret-color-primary",
136 style_sheet_declaration_name::caret_color_secondary, "caret-color-secondary",
137 style_sheet_declaration_name::fill_color, "fill-color",
138 style_sheet_declaration_name::font_color, "font-color",
139 style_sheet_declaration_name::font_family, "font-family",
140 style_sheet_declaration_name::font_size, "font-size",
141 style_sheet_declaration_name::font_style, "font-style",
142 style_sheet_declaration_name::font_weight, "font-weight",
143 style_sheet_declaration_name::height, "height",
144 style_sheet_declaration_name::margin_bottom, "margin-bottom",
145 style_sheet_declaration_name::margin_left, "margin-left",
146 style_sheet_declaration_name::margin_right, "margin-right",
147 style_sheet_declaration_name::margin_top, "margin-top",
148 style_sheet_declaration_name::selection_color, "selection-color",
149 style_sheet_declaration_name::spacing_horizontal, "spacing-horizontal",
150 style_sheet_declaration_name::spacing_vertical, "spacing-vertical",
151 style_sheet_declaration_name::width, "width",
152};
153// clang-format on
154
155// clang-format off
156constexpr auto style_sheet_declaration_name_value_mask_metadata = enum_metadata{
157 style_sheet_declaration_name::background_color, style_sheet_value_mask::color,
158 style_sheet_declaration_name::border_bottom_left_radius, style_sheet_value_mask::length,
159 style_sheet_declaration_name::border_bottom_right_radius, style_sheet_value_mask::length,
160 style_sheet_declaration_name::border_color, style_sheet_value_mask::color,
161 style_sheet_declaration_name::border_top_left_radius, style_sheet_value_mask::length,
162 style_sheet_declaration_name::border_top_right_radius, style_sheet_value_mask::length,
163 style_sheet_declaration_name::border_width, style_sheet_value_mask::length,
164 style_sheet_declaration_name::caret_color_primary, style_sheet_value_mask::color,
165 style_sheet_declaration_name::caret_color_secondary, style_sheet_value_mask::color,
166 style_sheet_declaration_name::fill_color, style_sheet_value_mask::color,
167 style_sheet_declaration_name::font_color, style_sheet_value_mask::color,
168 style_sheet_declaration_name::font_family, style_sheet_value_mask::font_family_id,
169 style_sheet_declaration_name::font_size, style_sheet_value_mask::dips,
170 style_sheet_declaration_name::font_style, style_sheet_value_mask::font_style,
171 style_sheet_declaration_name::font_weight, style_sheet_value_mask::font_weight,
172 style_sheet_declaration_name::height, style_sheet_value_mask::length,
173 style_sheet_declaration_name::margin_bottom, style_sheet_value_mask::length,
174 style_sheet_declaration_name::margin_left, style_sheet_value_mask::length,
175 style_sheet_declaration_name::margin_right, style_sheet_value_mask::length,
176 style_sheet_declaration_name::margin_top, style_sheet_value_mask::length,
177 style_sheet_declaration_name::selection_color, style_sheet_value_mask::color,
178 style_sheet_declaration_name::spacing_horizontal, style_sheet_value_mask::length,
179 style_sheet_declaration_name::spacing_vertical, style_sheet_value_mask::length,
180 style_sheet_declaration_name::width, style_sheet_value_mask::length,
181};
182// clang-format on
183
185 style_sheet_declaration_name name;
186 style_sheet_value value;
187 bool important = false;
188};
189
191 style_sheet_selector selector;
192 theme_state state = {};
193 theme_state_mask state_mask = {};
194 text_phrasing_mask phrasing_mask = {};
195 language_tag language = {};
196
198
199 [[nodiscard]] constexpr size_t size() const noexcept
200 {
201 return declarations.size();
202 }
203
204 [[nodiscard]] constexpr style_sheet_declaration const& operator[](size_t i) const noexcept
205 {
206 return declarations[i];
207 }
208
209 [[nodiscard]] constexpr std::string get_selector_as_string() const noexcept
210 {
211 hi_assert(not selector.empty());
212
213 auto r = selector[0].get_path_as_string();
214 for (auto i = 1_uz; i != selector.size(); ++i) {
215 r += ',';
216 r += selector[i].get_path_as_string();
217 }
218
219 return r;
220 }
221};
222
224 std::string name;
225 theme_mode mode;
226
229
230 [[nodiscard]] constexpr size_t size() const noexcept
231 {
232 return rule_sets.size();
233 }
234
235 [[nodiscard]] style_sheet_rule_set const& operator[](size_t i) const noexcept
236 {
237 return rule_sets[i];
238 }
239
242 void activate() const noexcept
243 {
244 }
245};
246
247}} // namespace hi::v1
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:199
#define hi_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:279
DOXYGEN BUG.
Definition algorithm.hpp:13
font_weight
Definition font_weight.hpp:16
geometry/margins.hpp
Definition cache.hpp:11
unit< em_length_tag, double > em_quads
Em-quad: A font's line-height.
Definition units.hpp:200
unit< px_length_tag, double > pixels
A physical pixel on a display.
Definition units.hpp:196
unit< si_length_tag, double, std::ratio< 254, 960 '000 >::type > dips
Device Independent Pixels: 1/96 inch.
Definition units.hpp:192
font_style
The different styles a font-family comes with.
Definition font_style.hpp:21
Definition style_sheet.hpp:53
Definition style_sheet.hpp:73
Definition style_sheet.hpp:97
Definition style_sheet.hpp:184
Definition style_sheet.hpp:190
Definition style_sheet.hpp:223
void activate() const noexcept
Activate style sheet as the current theme.
Definition style_sheet.hpp:242
Definition theme_length.hpp:12
T empty(T... args)
T size(T... args)