ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
Theme.hpp
Go to the documentation of this file.
1#pragma once
2
17
18#include "Common.hpp"
19#include "Style.hpp"
20#include "AnimPrimitives.hpp"
21#include "Media.hpp"
22
23namespace ZenitUI
24{
28
33 {
34 int ids{0};
35 int classes{0};
36 int tags{0};
37
38 bool operator<(const Specificity &o) const
39 {
40 if (ids != o.ids)
41 return ids < o.ids;
42 if (classes != o.classes)
43 return classes < o.classes;
44 return tags < o.tags;
45 }
46
47 bool operator==(const Specificity &o) const
48 {
49 return ids == o.ids && classes == o.classes && tags == o.tags;
50 }
51 bool operator!=(const Specificity &o) const
52 {
53 return !(*this == o);
54 }
55 };
56
57 inline Specificity computeNegatedItemSpecificity(const SimpleSelector::NegatedItem &item)
58 {
60 if (item.kind == SimpleSelector::Kind::Id)
61 s.ids++;
62 else if (item.kind == SimpleSelector::Kind::Class)
63 s.classes++;
64 else if (item.kind == SimpleSelector::Kind::Tag)
65 s.tags++;
66
67 for (const auto &[kind, name] : item.extras)
68 {
69 (void)name;
70 if (kind == SimpleSelector::Kind::Id)
71 s.ids++;
72 else if (kind == SimpleSelector::Kind::Class)
73 s.classes++;
74 else if (kind == SimpleSelector::Kind::Tag)
75 s.tags++;
76 }
77
78 if (item.requireHover)
79 s.classes++;
80 if (item.requirePressed)
81 s.classes++;
82 if (item.requireFocus)
83 s.classes++;
84 if (item.requireDisabled)
85 s.classes++;
86 if (item.requireChecked)
87 s.classes++;
88 return s;
89 }
90
92 inline Specificity computeSpecificity(const std::vector<SimpleSelector> &chain)
93 {
95 for (const auto &sel : chain)
96 {
97 if (sel.kind == SimpleSelector::Kind::Id)
98 s.ids++;
99 else if (sel.kind == SimpleSelector::Kind::Class)
100 s.classes++;
101 else if (sel.kind == SimpleSelector::Kind::Tag)
102 s.tags++;
103
104 for (const auto &[kind, name] : sel.extras)
105 {
106 (void)name;
107 if (kind == SimpleSelector::Kind::Id)
108 s.ids++;
109 else if (kind == SimpleSelector::Kind::Class)
110 s.classes++;
111 else if (kind == SimpleSelector::Kind::Tag)
112 s.tags++;
113 }
114
115 if (sel.requireHover)
116 s.classes++;
117 if (sel.requirePressed)
118 s.classes++;
119 if (sel.requireFocus)
120 s.classes++;
121 if (sel.requireDisabled)
122 s.classes++;
123 if (sel.requireChecked)
124 s.classes++;
125
126 for (const auto &neg : sel.negations)
127 {
128 Specificity best;
129 for (const auto &item : neg.items)
130 {
131 Specificity cur = computeNegatedItemSpecificity(item);
132 if (best < cur)
133 best = cur;
134 }
135 s.ids += best.ids;
136 s.classes += best.classes;
137 s.tags += best.tags;
138 }
139 }
140 return s;
141 }
142
148 {
149 std::vector<SimpleSelector> chain;
150 std::string part;
151 Style style;
152 Specificity specificity;
153 int order{0};
154 std::optional<MediaQuery> media;
155 };
156
163 class Theme
164 {
165 public:
168 {
170 std::string name;
171
172 bool operator==(const SymbolKey &o) const noexcept
173 {
174 return kind == o.kind && name == o.name;
175 }
176 };
177
180 {
181 size_t operator()(const SymbolKey &k) const noexcept
182 {
183 return std::hash<std::string>{}(k.name) ^
184 (static_cast<size_t>(k.kind) * 0x9e3779b97f4a7c15ULL);
185 }
186 };
187
188 std::vector<ThemeRule> rules;
189 Style root;
190 std::unordered_map<std::string, KeyframeAnimation> keyframes;
191 std::unordered_map<std::string, std::vector<size_t>> rulesByTag;
192 std::vector<size_t> rulesNoTag;
193 std::unordered_map<SymbolKey, std::vector<uint32_t>, SymbolKeyHash> rulesByLastSymbol;
194 bool hasSiblingCombinators{false};
195 uint32_t version{0};
196
201 {
202 r.specificity = computeSpecificity(r.chain);
203 r.order = (int)rules.size();
204
205 for (size_t i = 1; i < r.chain.size(); ++i)
206 {
207 if (r.chain[i].combinator == SelectorCombinator::AdjacentSibling ||
208 r.chain[i].combinator == SelectorCombinator::GeneralSibling)
209 {
210 hasSiblingCombinators = true;
211 break;
212 }
213 }
214
215 if (r.part.empty() && !r.chain.empty())
216 {
217 const auto &back = r.chain.back();
218 if (back.kind == SimpleSelector::Kind::Tag)
219 rulesByTag[back.name].push_back((size_t)r.order);
220 else
221 rulesNoTag.push_back((size_t)r.order);
222
223 rulesByLastSymbol[SymbolKey{back.kind, back.name}]
224 .push_back((uint32_t)r.order);
225 for (const auto &[kind, name] : back.extras)
226 rulesByLastSymbol[SymbolKey{kind, name}]
227 .push_back((uint32_t)r.order);
228 }
229
230 rules.push_back(std::move(r));
231 ++version;
232 }
233
236 {
237 keyframes[anim.name] = anim;
238 ++version;
239 }
240
242 void clear()
243 {
244 rules.clear();
245 keyframes.clear();
246 root = Style{};
247 rulesByTag.clear();
248 rulesNoTag.clear();
249 rulesByLastSymbol.clear();
250 ++version;
251 hasSiblingCombinators = false;
252 }
253 };
254
256}
Low-level animation types: imperative tracks, CSS keyframes, and the per-instance active animation st...
Centralized standard library includes used throughout ZenitUI.
@media query representation and evaluation.
The Style / ComputedStyle pair and all style-related value types.
Per-context stylesheet: rules, keyframes, and root style.
Definition Theme.hpp:164
void addRule(ThemeRule r)
Insert a rule into the theme.
Definition Theme.hpp:200
void addKeyframes(const KeyframeAnimation &anim)
Insert or replace a keyframe animation and bump the version.
Definition Theme.hpp:235
void clear()
Remove every rule, keyframe, and root declaration.
Definition Theme.hpp:242
Specificity computeSpecificity(const std::vector< SimpleSelector > &chain)
Compute the specificity of a full selector chain.
Definition Theme.hpp:92
@ GeneralSibling
A ~ B (any preceding sibling).
Definition Style.hpp:625
@ AdjacentSibling
A + B (immediately preceding sibling).
Definition Style.hpp:624
A named list of keyframes, keyed by name in Theme::keyframes.
Definition AnimPrimitives.hpp:115
Single compound argument inside a :not(...) pseudo-class.
Definition Style.hpp:643
Kind
Definition Style.hpp:634
CSS-style specificity triple used to sort matching rules.
Definition Theme.hpp:33
Sparse style: every property is Opt<T>, unset by default.
Definition Style.hpp:932
A single selector-plus-declarations entry in the stylesheet.
Definition Theme.hpp:148
Hash for SymbolKey.
Definition Theme.hpp:180
Key for rulesByLastSymbol: the last selector component.
Definition Theme.hpp:168