ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
Debug.hpp
Go to the documentation of this file.
1#pragma once
2
23
24#include "Layout.hpp"
25#include "Theme.hpp"
26#include "Unit.hpp"
27#include "CoreTypes.hpp"
28
29#ifdef ZENITUI_DEBUG
30
31#include <iostream>
32#include <sstream>
33#include <iomanip>
34#include <string>
35#include <vector>
36#include <cmath>
37
38namespace ZenitUI::Debug
39{
40
44
46 inline const char *unitName(Unit u)
47 {
48 switch (u)
49 {
50 case Unit::Auto:
51 return "auto";
52 case Unit::Number:
53 return "";
54 case Unit::Pixel:
55 return "px";
56 case Unit::Percent:
57 return "%";
58 case Unit::VW:
59 return "vw";
60 case Unit::VH:
61 return "vh";
62 case Unit::PW:
63 return "pw";
64 case Unit::PH:
65 return "ph";
66 }
67 return "?";
68 }
69
73 inline std::string valueToString(const Value &v)
74 {
75 if (v.isAuto())
76 return "auto";
77 if (v.getOp() != ValueOp::None)
78 {
79 auto opName = [](ValueOp op) -> const char *
80 {
81 switch (op)
82 {
83 case ValueOp::Add: return "add";
84 case ValueOp::Sub: return "sub";
85 case ValueOp::Mul: return "mul";
86 case ValueOp::Div: return "div";
87 case ValueOp::Min: return "min";
88 case ValueOp::Max: return "max";
89 case ValueOp::Clamp: return "clamp";
90 case ValueOp::Round: return "round";
91 case ValueOp::Floor: return "floor";
92 case ValueOp::Ceil: return "ceil";
93 case ValueOp::Mod: return "mod";
94 default: return "calc";
95 }
96 };
97 std::ostringstream os;
98 os << opName(v.getOp()) << "(";
99 if (const auto *args = v.astArgs())
100 {
101 for (size_t i = 0; i < args->size(); ++i)
102 {
103 if (i > 0)
104 os << ", ";
105 os << valueToString((*args)[i]);
106 }
107 }
108 os << ")";
109 return os.str();
110 }
111 std::ostringstream os;
112 const size_t n = v.termCount();
113 for (size_t i = 0; i < n; ++i)
114 {
115 const auto t = v.termAt(i);
116 if (i == 0)
117 {
118 if (t.coeff < 0.0f)
119 os << "-";
120 os << std::abs(t.coeff) << unitName(t.unit);
121 }
122 else
123 {
124 os << (t.coeff < 0.0f ? " - " : " + ");
125 os << std::abs(t.coeff) << unitName(t.unit);
126 }
127 }
128 return os.str();
129 }
130
131 inline std::string toString(float v)
132 {
133 std::ostringstream os;
134 os << v;
135 return os.str();
136 }
137 inline std::string toString(const std::string &s) { return s.empty() ? "\"\"" : s; }
138 inline std::string toString(const Value &v) { return valueToString(v); }
139
140 inline std::string toString(const Color &c)
141 {
142 std::ostringstream os;
143 os << "#" << std::hex << std::setfill('0')
144 << std::setw(2) << (int)c.r
145 << std::setw(2) << (int)c.g
146 << std::setw(2) << (int)c.b
147 << std::setw(2) << (int)c.a
148 << std::dec;
149 return os.str();
150 }
151
152 inline std::string toString(const Spacing &s)
153 {
154 return "[" + valueToString(s.top) + " " + valueToString(s.right) + " " + valueToString(s.bottom) + " " + valueToString(s.left) + "]";
155 }
156
157 inline std::string toString(const FontStack &stack)
158 {
159 if (stack.empty())
160 return "\"\"";
161 std::string out = "[";
162 for (size_t i = 0; i < stack.size(); ++i)
163 {
164 if (i)
165 out += ", ";
166 out += stack[i];
167 }
168 out += "]";
169 return out;
170 }
171
172 inline std::string toString(Align a)
173 {
174 switch (a)
175 {
176 case Align::Auto:
177 return "auto";
178 case Align::Start:
179 return "start";
180 case Align::Center:
181 return "center";
182 case Align::End:
183 return "end";
184 case Align::Stretch:
185 return "stretch";
186 }
187 return "?";
188 }
189
190 inline std::string toString(Justify j)
191 {
192 switch (j)
193 {
194 case Justify::Start:
195 return "start";
196 case Justify::Center:
197 return "center";
198 case Justify::End:
199 return "end";
201 return "space-between";
202 }
203 return "?";
204 }
205
206 inline std::string toString(Position p)
207 {
208 switch (p)
209 {
210 case Position::Static:
211 return "static";
212 case Position::Relative:
213 return "relative";
214 case Position::Absolute:
215 return "absolute";
216 }
217 return "?";
218 }
219
220 inline std::string toString(Direction d)
221 {
222 switch (d)
223 {
224 case Direction::Auto:
225 return "auto";
226 case Direction::Row:
227 return "row";
228 case Direction::Column:
229 return "column";
230 }
231 return "?";
232 }
233
234 inline std::string toString(CursorShape c)
235 {
236 switch (c)
237 {
239 return "auto";
241 return "default";
243 return "pointer";
245 return "text";
247 return "move";
249 return "grab";
251 return "grabbing";
253 return "ew-resize";
255 return "ns-resize";
257 return "nesw-resize";
259 return "nwse-resize";
261 return "not-allowed";
263 return "crosshair";
265 return "wait";
266 }
267 return "auto";
268 }
269
270 inline std::string toString(TextDirection d)
271 {
272 switch (d)
273 {
275 return "auto";
277 return "ltr";
279 return "rtl";
280 }
281 return "auto";
282 }
283
284 inline std::string toString(Overflow o)
285 {
286 switch (o)
287 {
288 case Overflow::Visible:
289 return "visible";
290 case Overflow::Hidden:
291 return "hidden";
292 case Overflow::Scroll:
293 return "scroll";
294 case Overflow::Auto:
295 return "auto";
296 }
297 return "?";
298 }
299
300 inline std::string toString(WhiteSpace w)
301 {
302 switch (w)
303 {
304 case WhiteSpace::Normal:
305 return "normal";
306 case WhiteSpace::Nowrap:
307 return "nowrap";
308 }
309 return "unknown";
310 }
311
312 inline std::string toString(TextOverflow t)
313 {
314 switch (t)
315 {
316 case TextOverflow::Visible:
317 return "visible";
318 case TextOverflow::Ellipsis:
319 return "ellipsis";
320 }
321 return "unknown";
322 }
323
324 inline std::string toString(TransitionFunction f)
325 {
326 switch (f)
327 {
328 case TransitionFunction::Linear:
329 return "linear";
330 case TransitionFunction::EaseInQuad:
331 return "ease-in-quad";
332 case TransitionFunction::EaseOutQuad:
333 return "ease-out-quad";
334 case TransitionFunction::EaseInOutQuad:
335 return "ease-in-out-quad";
336 case TransitionFunction::EaseInCubic:
337 return "ease-in-cubic";
338 case TransitionFunction::EaseOutCubic:
339 return "ease-out-cubic";
340 case TransitionFunction::EaseInOutCubic:
341 return "ease-in-out-cubic";
342 case TransitionFunction::EaseInBack:
343 return "ease-in-back";
344 case TransitionFunction::EaseOutBack:
345 return "ease-out-back";
346 case TransitionFunction::EaseOutElastic:
347 return "ease-out-elastic";
348 case TransitionFunction::EaseOutBounce:
349 return "ease-out-bounce";
350 }
351 return "?";
352 }
353
354 inline std::string toString(const ZIndex &z)
355 {
356 return z.isAuto ? "auto" : std::to_string(z.value);
357 }
358
359 inline std::string toString(const TextureRef &t)
360 {
361 std::ostringstream os;
362 os << t.name;
363 if (t.isNineSlice())
364 os << " " << t.left << " " << t.top << " " << t.right << " " << t.bottom;
365 return os.str();
366 }
367
368 inline std::string toString(const FilterRef &f)
369 {
370 if (f.args.empty())
371 return f.name;
372 std::string s = f.name + "(";
373 for (size_t i = 0; i < f.args.size(); ++i)
374 {
375 if (i > 0)
376 s += ", ";
377 s += f.args[i];
378 }
379 s += ")";
380 return s;
381 }
382
383 inline std::string toString(const SharedVector<FilterRef> &fs)
384 {
385 return toString(fs.asVector());
386 }
387
388 inline std::string toString(const BoxShadow &b)
389 {
390 if (!b.enabled)
391 return "none";
392 std::ostringstream os;
393 os << valueToString(b.x) << " " << valueToString(b.y)
394 << " " << valueToString(b.blur) << " " << toString(b.color);
395 return os.str();
396 }
397
398 inline std::string toString(const GridTrack &t)
399 {
400 switch (t.kind)
401 {
403 return "auto";
405 return toString(t.size);
407 return toString(t.fr) + "fr";
408 }
409 return "auto";
410 }
411
412 inline std::string toString(const SharedVector<GridTrack> &ts)
413 {
414 return toString(ts.asVector());
415 }
416
417 inline std::string toString(const GridPlacement &p)
418 {
419 if (p.isAuto())
420 return (p.span <= 1) ? "auto" : ("span " + std::to_string(p.span));
421 if (p.span <= 1)
422 return std::to_string(p.start);
423 return std::to_string(p.start) + " / span " + std::to_string(p.span);
424 }
425
429 inline std::string styleToString(const Style &s)
430 {
431 std::ostringstream os;
432 bool first = true;
433 auto sep = [&]()
434 { if (!first) os << ", "; first = false; };
435
436#define X(T, name, def) \
437 if (s.name.is_set) \
438 { \
439 sep(); \
440 os << #name "=" << toString(s.name.value); \
441 }
442 BUBBLE_STYLE_PROPS(X)
443#undef X
444
445 if (s.transitions.is_set)
446 {
447 sep();
448 os << "transitions(" << s.transitions.value.size() << ")";
449 }
450 if (s.animations.is_set)
451 {
452 sep();
453 os << "animations(" << s.animations.value.size() << ")";
454 }
455 return os.str();
456 }
457
461 inline std::string computedStyleToString(const ComputedStyle &cs)
462 {
463 std::ostringstream os;
464 os << "size=" << valueToString(cs.width) << "x" << valueToString(cs.height);
465 os << " bg=" << toString(cs.background);
466 os << " color=" << toString(cs.color);
467 if (!cs.font.empty())
468 os << " font=" << cs.font;
469 os << " fs=" << valueToString(cs.fontSize);
470 os << " padding=" << toString(cs.padding);
471 os << " margin=" << toString(cs.margin);
472 os << " radius=" << valueToString(cs.radius);
473 if (cs.borderWidth != Spacing())
474 {
475 os << " borderW=" << toString(cs.borderWidth);
476 os << " borderC=" << toString(cs.borderColor);
477 }
478 if (cs.opacity != 1.0f)
479 os << " opacity=" << cs.opacity;
480 if (cs.position != Position::Static)
481 os << " pos=" << toString(cs.position);
482 if (!cs.zIndex.isAuto)
483 os << " zIndex=" << cs.zIndex.value;
484 if (cs.overflowX != Overflow::Visible)
485 os << " overflow-x=" << toString(cs.overflowX);
486 if (cs.overflowY != Overflow::Visible)
487 os << " overflow-y=" << toString(cs.overflowY);
488 if (!cs.left.isAuto())
489 os << " left=" << valueToString(cs.left);
490 if (!cs.top.isAuto())
491 os << " top=" << valueToString(cs.top);
492 if (!cs.right.isAuto())
493 os << " right=" << valueToString(cs.right);
494 if (!cs.bottom.isAuto())
495 os << " bottom=" << valueToString(cs.bottom);
496 if (!cs.translateX.isAuto() || !cs.translateY.isAuto())
497 os << " translate=(" << valueToString(cs.translateX) << "," << valueToString(cs.translateY) << ")";
498 if (cs.scale != 1.0f)
499 os << " scale=" << cs.scale;
500 if (cs.rotation != 0.0f)
501 os << " rotation=" << cs.rotation;
502 if (!cs.customProps.empty())
503 {
504 os << " vars{";
505 bool first = true;
506 for (const auto &[k, v] : cs.customProps)
507 {
508 if (!first)
509 os << ", ";
510 os << k << "=" << v;
511 first = false;
512 }
513 os << "}";
514 }
515 return os.str();
516 }
517
519 inline std::string selectorToString(const ThemeRule &r)
520 {
521 std::ostringstream os;
522 for (size_t i = 0; i < r.chain.size(); ++i)
523 {
524 if (i > 0)
525 os << " ";
526 const auto &ss = r.chain[i];
527
528 auto printComponent = [&](SimpleSelector::Kind kind,
529 const std::string &name)
530 {
531 if (kind == SimpleSelector::Kind::Class)
532 os << ".";
533 else if (kind == SimpleSelector::Kind::Id)
534 os << "#";
535 os << name;
536 };
537
538 printComponent(ss.kind, ss.name);
539 for (const auto &[kind, name] : ss.extras)
540 printComponent(kind, name);
541
542 if (ss.requireHover)
543 os << ":hover";
544 if (ss.requirePressed)
545 os << ":pressed";
546 if (ss.requireFocus)
547 os << ":focus";
548 if (ss.requireDisabled)
549 os << ":disabled";
550 if (ss.requireChecked)
551 os << ":checked";
552 }
553 if (!r.part.empty())
554 os << "::" << r.part;
555 return os.str();
556 }
557
559 inline std::string stateFlagsToString(const Layout &node)
560 {
561 std::string s;
562 if (node.isHoveredState())
563 s += "H";
564 if (node.isPressedState())
565 s += "P";
566 if (node.isFocusedState())
567 s += "F";
568 if (node.isCheckedState())
569 s += "C";
570 if (node.isDisabledState())
571 s += "D";
572 return s.empty() ? "-" : s;
573 }
574
578 inline void dumpStyle(const Layout &node, std::ostream &os = std::cerr)
579 {
580 os << "=== Node ===\n";
581 os << " tag : " << node.getStyleTag() << "\n";
582 if (!node.nodeId.empty())
583 os << " id : #" << node.nodeId << "\n";
584 if (!node.ref.empty())
585 os << " ref : @" << node.ref << "\n";
586 if (!node.getStyleClasses().empty())
587 {
588 os << " classes :";
589 for (const auto &c : node.getStyleClasses())
590 os << " ." << c;
591 os << "\n";
592 }
593 os << " state : " << stateFlagsToString(node) << "\n";
594 const auto &r = node.getRect();
595 os << " rect : (" << r.x << ", " << r.y << " " << r.width << "x" << r.height << ")\n";
596 os << " style : " << computedStyleToString(node.getStyle()) << "\n";
597
598 auto matches = node.getMatchingRules();
599 os << " matches : " << matches.size() << " rule(s)\n";
600 for (const auto *rule : matches)
601 {
602 os << "spec=" << rule->specificity.ids << "."
603 << rule->specificity.classes << "."
604 << rule->specificity.tags << "] "
605 << selectorToString(*rule) << "\n";
606 os << " -> { " << styleToString(rule->style) << " }\n";
607 }
608 }
609
614 inline void dumpTree(const Layout &node, std::ostream &os = std::cerr,
615 int maxDepth = -1, int depth = 0)
616 {
617 std::string indent(depth * 2, ' ');
618 os << indent << "- ";
619 if (!node.nodeId.empty())
620 os << "#" << node.nodeId << " ";
621 if (!node.ref.empty())
622 os << "@" << node.ref << " ";
623 os << node.getStyleTag();
624 for (const auto &c : node.getStyleClasses())
625 os << "." << c;
626
627 const auto &r = node.getRect();
628 os << " [" << r.width << "x" << r.height << " @ " << r.x << "," << r.y << "]";
629
630 std::string st = stateFlagsToString(node);
631 if (st != "-")
632 os << " {" << st << "}";
633 os << "\n";
634
635 if (maxDepth >= 0 && depth >= maxDepth)
636 return;
637 for (const auto &c : node.children)
638 dumpTree(*c, os, maxDepth, depth + 1);
639 }
640
645 inline void dumpTheme(const Theme &theme, std::ostream &os = std::cerr)
646 {
647 os << "=== Theme ===\n";
648 os << "Rules : " << theme.rules.size() << "\n";
649 os << "Keyframes: " << theme.keyframes.size() << "\n";
650
651 if (!theme.keyframes.empty())
652 {
653 os << "\n--- Keyframes ---\n";
654 for (const auto &[name, kf] : theme.keyframes)
655 {
656 os << " " << name << " (" << kf.keyframes.size() << " frames)\n";
657 }
658 }
659
660 os << "\n--- Rules ---\n";
661 for (size_t i = 0; i < theme.rules.size(); ++i)
662 {
663 const auto &r = theme.rules[i];
664 os << " [" << std::setw(3) << i << "] spec=" << std::setw(6) << r.specificity.ids << "."
665 << r.specificity.classes << "."
666 << r.specificity.tags << " "
667 << selectorToString(r) << "\n";
668 os << " { " << styleToString(r.style) << " }\n";
669 }
670 os << "\n";
671 }
672
676 inline void dumpParts(const Layout &node, std::ostream &os = std::cerr)
677 {
678 static const char *candidates[] = {
679 "track", "fill", "knob", "box", "mark", "cursor"};
680 os << "=== Parts of " << node.getStyleTag() << " ===\n";
681 const auto &theme = node.theme();
682 for (auto *name : candidates)
683 {
684 int matchCount = 0;
685 for (const auto &r : theme.rules)
686 {
687 if (r.part != name)
688 continue;
689 if (ruleMatches(r, &node, node.viewportSize()))
690 matchCount++;
691 }
692 if (matchCount > 0)
693 {
694 os << " ::" << name << " (" << matchCount << " matching rule(s))\n";
695 }
696 }
697 }
698
702 inline std::string renderStatsToString(const RenderStats &s)
703 {
704 std::ostringstream os;
705 os << "targets[created=" << s.targetsCreated
706 << " destroyed=" << s.targetsDestroyed
707 << " live=" << s.liveTargets
708 << "] pushTarget=" << s.pushTargetCalls
709 << " passes=" << s.filterPasses;
710 return os.str();
711 }
712
716 inline void dumpRenderStats(const RenderStats &s, std::ostream &os = std::cerr)
717 {
718 os << "[RenderStats] " << renderStatsToString(s) << "\n";
719 }
720
724 inline void dumpRenderStats(const UIContext &ctx, std::ostream &os = std::cerr)
725 {
726 dumpRenderStats(ctx.renderStats, os);
727 }
729} // namespace ZenitUI::Debug
730
731#else // !ZENITUI_DEBUG
732
733#include <iostream>
734
735namespace ZenitUI::Debug
736{
737
738 // No-op fallbacks. Kept as inline functions so the symbols always exist.
739 inline void dumpStyle(const Layout &, std::ostream & = std::cerr) {}
740 inline void dumpTree(const Layout &, std::ostream & = std::cerr, int = -1, int = 0) {}
741 inline void dumpTheme(const Theme &, std::ostream & = std::cerr) {}
742 inline void dumpParts(const Layout &, std::ostream & = std::cerr) {}
743 inline std::string renderStatsToString(const RenderStats &) { return {}; }
744 inline void dumpRenderStats(const RenderStats &, std::ostream & = std::cerr) {}
745 inline void dumpRenderStats(const UIContext &, std::ostream & = std::cerr) {}
746
747} // namespace ZenitUI::Debug
748
749#endif // ZENITUI_DEBUG
Small value types shared by the layout, rendering and input layers.
The node class: tree structure, layout pass, style resolution, input dispatch, render orchestration,...
Global stylesheet storage and rule matching.
Length units and the compact 8-byte tagged Value expression representation.
TransitionFunction
Timing function applied between two keyframe values.
Definition Easing.hpp:28
Justify
Main-axis distribution for flex containers.
Definition CoreTypes.hpp:462
Align
Cross-axis alignment for flex items and content.
Definition CoreTypes.hpp:452
bool ruleMatches(const ThemeRule &r, const Layout *node, Vec2 viewport)
true if node matches the full rule selector chain ( , >, +, ~, :not()).
Definition Layout.hpp:1615
TextDirection
Inline / layout writing direction for Bidi text and RTL layout mirroring (ยง6.2).
Definition UIEnums.hpp:83
CursorShape
Standard OS cursor shapes requested via ZStyle cursor: and IPlatform::setCursor.
Definition UIEnums.hpp:61
@ Center
Center the whole set.
Definition CoreTypes.hpp:464
@ End
Pack at the trailing edge.
Definition CoreTypes.hpp:465
@ Start
Pack at the leading edge.
Definition CoreTypes.hpp:463
@ SpaceBetween
Distribute leftover space between items.
Definition CoreTypes.hpp:466
@ Auto
Defer to the parent's alignItems.
Definition CoreTypes.hpp:453
@ Center
Center within the available space.
Definition CoreTypes.hpp:455
@ End
Pack at the trailing edge.
Definition CoreTypes.hpp:456
@ Start
Pack at the leading edge.
Definition CoreTypes.hpp:454
@ Stretch
Stretch to fill the cross axis.
Definition CoreTypes.hpp:457
@ Auto
Infer paragraph direction from first strong Bidi character (fallback LTR).
Definition UIEnums.hpp:84
@ LTR
Explicit left-to-right direction.
Definition UIEnums.hpp:85
@ RTL
Explicit right-to-left direction.
Definition UIEnums.hpp:86
@ Auto
Inherit or fall back to Default.
Definition UIEnums.hpp:62
@ Crosshair
Precision crosshair (crosshair).
Definition UIEnums.hpp:74
@ Wait
Busy / loading indicator (wait, progress).
Definition UIEnums.hpp:75
@ ResizeEW
Horizontal resize (ew-resize, col-resize).
Definition UIEnums.hpp:69
@ ResizeNS
Vertical resize (ns-resize, row-resize).
Definition UIEnums.hpp:70
@ Pointer
Pointing hand (buttons, links, interactive controls).
Definition UIEnums.hpp:64
@ ResizeNESW
Diagonal NE-SW resize (nesw-resize).
Definition UIEnums.hpp:71
@ Move
Four-way move cursor.
Definition UIEnums.hpp:66
@ Grabbing
Closed hand (active drag).
Definition UIEnums.hpp:68
@ Default
Standard arrow cursor.
Definition UIEnums.hpp:63
@ ResizeNWSE
Diagonal NW-SE resize (nwse-resize).
Definition UIEnums.hpp:72
@ Text
I-beam (editable text fields).
Definition UIEnums.hpp:65
@ Grab
Open hand (draggable item).
Definition UIEnums.hpp:67
@ NotAllowed
Forbidden / disabled action (not-allowed).
Definition UIEnums.hpp:73
TextOverflow
CSS text-overflow modes supported by the text renderer.
Definition Style.hpp:595
Overflow
CSS overflow modes for one axis.
Definition Style.hpp:579
WhiteSpace
CSS white-space values relevant to text layout.
Definition Style.hpp:588
Position
CSS-style position modes supported by the layout system.
Definition Style.hpp:552
@ Auto
Size to content.
Definition Style.hpp:729
@ Flex
Flexible fraction (fr).
Definition Style.hpp:731
@ Fixed
Explicit length (px, %, vw, vh, calc(...)).
Definition Style.hpp:730
Kind
Definition Style.hpp:634