ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
ZMarkup.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include "Common.hpp"
12#include "Logger.hpp"
13#include "Layout.hpp"
14#include "Bindable.hpp"
15#include "Diagnostics.hpp"
16#include "ZMarkupDocs.hpp"
17
18#include <filesystem>
19#include <optional>
20#include <type_traits>
21#include <unordered_map>
22#include <unordered_set>
23
24namespace ZenitUI::ZMarkup
25{
29
34 struct Element
35 {
36 std::string tag;
37 std::vector<std::string> classes;
38 std::string id;
39 std::string ref;
40 std::string text;
41 std::vector<std::pair<std::string, std::string>> attrs;
42 std::vector<SourceSpan> attrSpans;
44 std::vector<ZStyleDiagnostic> parseErrors;
45 std::vector<Element> children;
46
48 std::string attr(std::string_view k, std::string_view d = "") const;
50 bool has(std::string_view k) const;
52 float attrFloat(std::string_view k, float d = 0.0f) const;
54 bool attrBool(std::string_view k, bool d = false) const;
55 };
56
61 class Parser
62 {
63 public:
64 explicit Parser(std::string_view src, std::string fileName = "")
65 : s(src), fileName_(std::move(fileName)) {}
66
69
70 private:
71 std::string_view s;
72 std::string fileName_;
73 size_t p{0};
74 std::uint32_t line_{1};
75 std::uint32_t col_{1};
76 std::vector<ZStyleDiagnostic> errors_;
77
78 bool eof() const { return p >= s.size(); }
79 char peek() const { return eof() ? '\0' : s[p]; }
80 char get();
81 SourceSpan spanAt(std::uint32_t line, std::uint32_t col, std::uint32_t length = 0) const
82 {
83 return SourceSpan{fileName_, line, col, length};
84 }
85
86 static bool isIdentStart(char c);
87 static bool isIdentChar(char c);
88
89 void skipWs();
90 void skipInlineWs();
91 void skipBlockComment();
92 std::string readIdent();
93 std::string readQuoted();
94 std::string readBareToken();
95 Element parseElement();
96 };
97
99 Element parse(std::string_view src, std::string fileName = "");
100
101 class Registry;
102 class UINode;
103 class WeakUINode;
104
110 {
111 std::string prefabName;
112 std::unordered_map<std::string, std::vector<const Element *>> buckets;
113 std::unordered_set<std::string> seenSlots;
114 std::unordered_set<std::string> consumedSlots;
115 };
116
122 {
123 bool ownState{false};
124 bool effectiveVisible{false};
125 std::string parentCondition;
126 std::vector<std::weak_ptr<Layout>> directNodes;
127 std::vector<std::string> childConditions;
128 std::vector<std::string> childEaches;
129 };
130
136 {
137 virtual ~IKeyedStore() = default;
138 };
139
140 struct BuildContext;
141
147 {
148 std::weak_ptr<Layout> anchor;
149 std::string prefabName;
150 std::string parentCondition;
151 std::vector<Element> fallbackAst;
152 std::vector<std::weak_ptr<Layout>> spawnedNodes;
153 std::shared_ptr<IKeyedStore> keyedStore;
154 bool showingFallback{false};
155 };
156
166 {
167 std::unordered_map<std::string, std::weak_ptr<Layout>> byId;
168 std::unordered_map<std::string, std::vector<std::weak_ptr<Layout>>> byRef;
169
170 std::vector<SlotFrame> slotStack;
171 std::vector<std::string> conditionStack;
172
173 std::unordered_map<std::string, ConditionBinding> conditions;
174 std::unordered_map<std::string, EachBinding> eaches;
175
176 // Type-erased Bindable<T>::Subscription handles (RAII cleanup on destroy).
177 std::unordered_map<std::string, std::shared_ptr<void>> conditionSubs;
178 std::unordered_map<std::string, std::shared_ptr<void>> eachSubs;
179
180 // Registry that built this context (weakly pinned when owned by a UIContext).
181 const Registry *registry{nullptr};
182 std::weak_ptr<const Registry> registryOwner;
183 bool usesOwnedRegistry{false};
184
185 bool attachedToRoot{false};
186
187 bool isConditionVisible(const std::string &condName) const;
188
196 const Registry *resolveRegistry(std::shared_ptr<const Registry> &keepAlive) const;
197 };
198
199 template <typename KeyT>
201 {
202 struct Entry
203 {
204 std::weak_ptr<Layout> root;
205 std::shared_ptr<BuildContext> ctx;
206 };
207 std::unordered_map<KeyT, Entry> entries;
208 };
209
214 class Registry : public std::enable_shared_from_this<Registry>
215 {
216 public:
218 using Factory = std::function<std::shared_ptr<Layout>(const Element &)>;
219
222
224 void reg(const std::string &tag, Factory f);
225
227 TagBuilder tag(std::string_view name);
228
230 void reg(TagDoc doc, Factory f);
231
233 [[nodiscard]] const std::vector<TagDoc> &tagDocs() const { return docs_; }
234
236 [[nodiscard]] const TagDoc *findTagDoc(std::string_view name) const;
237
244 [[nodiscard]] std::vector<ZStyleDiagnostic> validate(const Element &root) const;
245
247 bool hasTag(const std::string &tagName) const;
248
250 bool hasPrefab(const std::string &tagName) const;
251
253 void registerPrefab(const std::string &tagName, const std::string &markupSource);
254
259 bool registerPrefabFile(const std::string &tagName, const std::filesystem::path &path);
260
265 bool unregisterPrefab(const std::string &tagName);
266
269
271 std::vector<std::shared_ptr<Layout>> create_impl(
272 const Element &el, BuildContext &ctx, bool isPrefabRoot = false) const;
273
278 std::shared_ptr<Layout> create(const Element &el, BuildContext &ctx) const;
279
281 UINode build(std::string_view source, DiagnosticSink *sink = nullptr) const;
282
284 UINode loadMarkupFile(const std::filesystem::path &path, DiagnosticSink *sink = nullptr) const;
285
288 const std::string &tagName,
289 const std::vector<std::string> &extraClasses = {},
290 const std::string &id = "",
291 const std::string &ref = "") const;
292
293 private:
294 struct CreatedNode
295 {
296 std::shared_ptr<Layout> node;
297 bool ownedByStructural{false};
298 };
299
300 void bindContext(BuildContext &ctx) const;
301
302 std::vector<CreatedNode> create_nodes_impl(
303 const Element &el, BuildContext &ctx, bool isPrefabRoot = false) const;
304
305 std::vector<CreatedNode> expandPrefab(
306 const std::string &tagName,
307 const Element &prefabAst,
308 const std::vector<std::string> &extraClasses,
309 const std::string &id,
310 const std::string &ref,
311 const std::vector<std::pair<std::string, std::string>> &extraAttrs,
312 const std::vector<Element> &callerChildren,
313 BuildContext &ctx) const;
314
315 std::unordered_map<std::string, Factory> factories;
316 std::unordered_map<std::string, Element> prefabs;
317 std::vector<TagDoc> docs_;
318 };
319
322
325
330 class UINode
331 {
332 public:
333 UINode(std::shared_ptr<Layout> root, std::shared_ptr<BuildContext> ctx)
334 : root_(std::move(root)), ctx_(std::move(ctx)) {}
335
336 std::shared_ptr<Layout> root() const { return root_; }
337 std::shared_ptr<BuildContext> context() const { return ctx_; }
338
346
351 std::shared_ptr<Layout> find(const std::string &id) const;
352
354 template <typename T = Layout>
355 std::shared_ptr<T> find(const std::string &id) const
356 {
357 auto n = find(id);
358 return n ? std::static_pointer_cast<T>(n) : nullptr;
359 }
360
362 template <typename T = Layout>
363 std::shared_ptr<T> ref(const std::string &name) const
364 {
365 auto it = ctx_->byRef.find(name);
366 if (it == ctx_->byRef.end() || it->second.empty())
367 {
368 warnOnce("ref:" + name,
369 "UINode::ref('" + name + "'): no node with this ref");
370 return nullptr;
371 }
372 std::shared_ptr<Layout> firstLive;
373 size_t liveCount = 0;
374 for (const auto &wp : it->second)
375 {
376 if (auto sp = wp.lock())
377 {
378 if (!firstLive)
379 firstLive = sp;
380 ++liveCount;
381 }
382 }
383 if (!firstLive)
384 return nullptr;
385 if (liveCount > 1)
386 {
387 warnOnce("ref-ambig:" + name,
388 "UINode::ref('" + name + "'): " +
389 std::to_string(liveCount) +
390 " nodes match, returning the first. "
391 "Use refAll for the full list.");
392 }
393 return std::static_pointer_cast<T>(firstLive);
394 }
395
397 template <typename T = Layout>
398 std::vector<std::shared_ptr<T>> refAll(const std::string &name) const
399 {
400 auto it = ctx_->byRef.find(name);
401 if (it == ctx_->byRef.end() || it->second.empty())
402 {
403 logDebug("ZMarkup", "", 0, 0,
404 "UINode::refAll('" + name + "'): empty");
405 return {};
406 }
407 std::vector<std::shared_ptr<T>> out;
408 out.reserve(it->second.size());
409 for (const auto &wp : it->second)
410 {
411 if (auto sp = wp.lock())
412 out.push_back(std::static_pointer_cast<T>(sp));
413 }
414 return out;
415 }
416
417 // --- #id-based event helpers ---
418
419 UINode &onClick(const std::string &id, std::function<void()> cb);
420 UINode &onPress(const std::string &id, std::function<void()> cb);
421 UINode &onRelease(const std::string &id, std::function<void()> cb);
422 UINode &onHoverEnter(const std::string &id, std::function<void()> cb);
423 UINode &onHoverExit(const std::string &id, std::function<void()> cb);
424 UINode &onValueChanged(const std::string &id, std::function<void(float)> cb);
425 UINode &onToggle(const std::string &id, std::function<void(bool)> cb);
426
427 // --- @ref-based chainable helpers (§5.36) ---
428
429 UINode &setTextRef(const std::string &refName, const std::string &text);
430 UINode &setButtonTextRef(const std::string &refName, const std::string &text);
431 UINode &addClassRef(const std::string &refName, const std::string &cls);
432 UINode &removeClassRef(const std::string &refName, const std::string &cls);
433 UINode &toggleClassRef(const std::string &refName, const std::string &cls, bool active);
434 UINode &setEnabledRef(const std::string &refName, bool enabled);
435 UINode &setInteractiveRef(const std::string &refName, bool interactive);
436 UINode &setVisibleRef(const std::string &refName, bool visible);
437 UINode &onClickRef(const std::string &refName, std::function<void()> cb);
438 UINode &onPressRef(const std::string &refName, std::function<void()> cb);
439 UINode &onReleaseRef(const std::string &refName, std::function<void()> cb);
440 UINode &onHoverEnterRef(const std::string &refName, std::function<void()> cb);
441 UINode &onHoverExitRef(const std::string &refName, std::function<void()> cb);
442 UINode &onTextChangedRef(const std::string &refName, std::function<void(const std::string &)> cb);
443 UINode &onValueChangedRef(const std::string &refName, std::function<void(float)> cb);
444 UINode &onToggleRef(const std::string &refName, std::function<void(bool)> cb);
445
446 // --- Runtime structural If & Each (§5.37) ---
447
448 UINode &setCondition(const std::string &condRef, bool value);
449 UINode &bindCondition(const std::string &condRef,
450 Bindable<bool> &source,
451 bool fireImmediately = true);
452
453 template <typename Range, typename Fn>
454 UINode &syncEach(const std::string &eachRef, const Range &items, Fn &&binder)
455 {
456 auto it = ctx_->eaches.find(eachRef);
457 if (it == ctx_->eaches.end())
458 {
459 warnOnce("each:" + eachRef,
460 "UINode::syncEach('" + eachRef + "'): no Each with this ref");
461 return *this;
462 }
463
464 auto &binding = it->second;
465 auto anchor = binding.anchor.lock();
466 auto parent = anchor ? anchor->getParent() : nullptr;
467 if (!parent)
468 return *this;
469
470 std::shared_ptr<const Registry> regKeepAlive;
471 const Registry *reg = ctx_->resolveRegistry(regKeepAlive);
472 if (!reg)
473 return *this;
474
475 binding.keyedStore.reset();
476 clearEachNodes_(binding, *parent);
477
478 const bool effVis = ctx_->isConditionVisible(binding.parentCondition);
479 const bool isEmpty = (std::begin(items) == std::end(items));
480
481 if (isEmpty)
482 {
483 mountEachFallback_(*reg, binding, *parent, anchor.get(), effVis);
484 return *this;
485 }
486
487 binding.showingFallback = false;
488 size_t idx = 0;
489 for (const auto &elem : items)
490 {
491 UINode itemUi = reg->instantiate(binding.prefabName);
492 auto itemRoot = itemUi.root();
493 if (!itemRoot)
494 break;
495
496 invokeEachCallback_(binder, itemUi, elem, idx);
497 if (!effVis)
498 itemRoot->setVisible(false);
499
500 parent->insertChildBefore(anchor.get(), itemRoot);
501 binding.spawnedNodes.push_back(itemRoot);
502 ++idx;
503 }
504 return *this;
505 }
506
507 template <typename T, typename Fn>
508 UINode &bindEach(const std::string &eachRef,
509 Bindable<std::vector<T>> &source,
510 Fn binder,
511 bool fireImmediately = true)
512 {
513 if (ctx_->eaches.find(eachRef) == ctx_->eaches.end())
514 {
515 warnOnce("each:" + eachRef,
516 "UINode::bindEach('" + eachRef + "'): no Each with this ref");
517 return *this;
518 }
519
520 attachToRoot_();
521
522 std::weak_ptr<Layout> weakRoot = root_;
523 std::weak_ptr<BuildContext> weakCtx = ctx_;
524 auto sub = source.onChanged(
525 [weakRoot, weakCtx, eachRef, binder = std::move(binder)](const std::vector<T> &vec) mutable
526 {
527 auto r = weakRoot.lock();
528 auto c = weakCtx.lock();
529 if (!r || !c)
530 return;
531 UINode ui(std::move(r), std::move(c));
532 ui.syncEach(eachRef, vec, binder);
533 },
534 fireImmediately);
535
536 ctx_->eachSubs[eachRef] =
537 std::make_shared<typename Bindable<std::vector<T>>::Subscription>(std::move(sub));
538 return *this;
539 }
540
541 template <typename Range, typename KeyFn, typename OnCreateFn, typename OnUpdateFn>
542 UINode &syncEachKeyed(const std::string &eachRef,
543 const Range &items,
544 KeyFn &&keyFn,
545 OnCreateFn &&onCreate,
546 OnUpdateFn &&onUpdate)
547 {
548 auto it = ctx_->eaches.find(eachRef);
549 if (it == ctx_->eaches.end())
550 {
551 warnOnce("each:" + eachRef,
552 "UINode::syncEachKeyed('" + eachRef + "'): no Each with this ref");
553 return *this;
554 }
555
556 auto &binding = it->second;
557 auto anchor = binding.anchor.lock();
558 auto parent = anchor ? anchor->getParent() : nullptr;
559 if (!parent)
560 return *this;
561
562 std::shared_ptr<const Registry> regKeepAlive;
563 const Registry *reg = ctx_->resolveRegistry(regKeepAlive);
564 if (!reg)
565 return *this;
566
567 using ElemT = std::decay_t<decltype(*std::begin(items))>;
568 using KeyT = std::decay_t<std::invoke_result_t<KeyFn &, const ElemT &>>;
569
570 const bool effVis = ctx_->isConditionVisible(binding.parentCondition);
571 const bool isEmpty = (std::begin(items) == std::end(items));
572
573 if (isEmpty)
574 {
575 binding.keyedStore.reset();
576 clearEachNodes_(binding, *parent);
577 mountEachFallback_(*reg, binding, *parent, anchor.get(), effVis);
578 return *this;
579 }
580
581 if (binding.showingFallback)
582 clearEachNodes_(binding, *parent);
583
584 auto *store = dynamic_cast<KeyedStore<KeyT> *>(binding.keyedStore.get());
585 if (!store)
586 {
587 auto newStore = std::make_shared<KeyedStore<KeyT>>();
588 store = newStore.get();
589 binding.keyedStore = std::move(newStore);
590 }
591
592 std::unordered_map<KeyT, typename KeyedStore<KeyT>::Entry> nextEntries;
593 nextEntries.reserve(store->entries.size() + 4);
594 std::vector<std::shared_ptr<Layout>> nextNodes;
595
596 size_t idx = 0;
597 for (const auto &elem : items)
598 {
599 KeyT key = keyFn(elem);
600 if (nextEntries.count(key) > 0)
601 {
602 logWarn("ZMarkup", "", 0, 0,
603 "UINode::syncEachKeyed('" + eachRef + "'): duplicate key detected, skipping duplicate item");
604 ++idx;
605 continue;
606 }
607
608 auto existing = store->entries.find(key);
609 std::shared_ptr<Layout> existingRoot =
610 (existing != store->entries.end()) ? existing->second.root.lock() : nullptr;
611
612 if (existingRoot)
613 {
614 auto entry = std::move(existing->second);
615 UINode itemUi(existingRoot, entry.ctx);
616 invokeEachCallback_(onUpdate, itemUi, elem, idx);
617 if (!effVis)
618 existingRoot->setVisible(false);
619
620 nextNodes.push_back(std::move(existingRoot));
621 nextEntries.emplace(std::move(key), std::move(entry));
622 }
623 else
624 {
625 UINode itemUi = reg->instantiate(binding.prefabName);
626 auto itemRoot = itemUi.root();
627 if (!itemRoot)
628 break;
629
630 invokeEachCallback_(onCreate, itemUi, elem, idx);
631 invokeEachCallback_(onUpdate, itemUi, elem, idx);
632 if (!effVis)
633 itemRoot->setVisible(false);
634
635 nextEntries.emplace(std::move(key),
636 typename KeyedStore<KeyT>::Entry{itemRoot, itemUi.context()});
637 nextNodes.push_back(std::move(itemRoot));
638 }
639 ++idx;
640 }
641
642 bool sameOrder = (nextNodes.size() == binding.spawnedNodes.size());
643 if (sameOrder)
644 {
645 for (size_t i = 0; i < nextNodes.size(); ++i)
646 {
647 if (nextNodes[i].get() != binding.spawnedNodes[i].lock().get())
648 {
649 sameOrder = false;
650 break;
651 }
652 }
653 }
654
655 if (!sameOrder)
656 {
657 std::unordered_set<const Layout *> keepSet;
658 keepSet.reserve(nextNodes.size());
659 for (const auto &n : nextNodes)
660 keepSet.insert(n.get());
661
662 for (auto &oldWp : binding.spawnedNodes)
663 {
664 if (auto oldNode = oldWp.lock())
665 {
666 if (keepSet.find(oldNode.get()) == keepSet.end())
667 parent->removeChild(oldNode.get());
668 }
669 }
670
671 for (auto &newNode : nextNodes)
672 {
673 if (newNode->getParent().get() == parent.get())
674 parent->moveChildBefore(anchor.get(), newNode.get());
675 else
676 parent->insertChildBefore(anchor.get(), newNode);
677 }
678 }
679
680 store->entries = std::move(nextEntries);
681 binding.spawnedNodes.clear();
682 binding.spawnedNodes.reserve(nextNodes.size());
683 for (const auto &n : nextNodes)
684 binding.spawnedNodes.push_back(n);
685 binding.showingFallback = false;
686 return *this;
687 }
688
689 template <typename T, typename KeyFn, typename OnCreateFn, typename OnUpdateFn>
690 UINode &bindEachKeyed(const std::string &eachRef,
691 Bindable<std::vector<T>> &source,
692 KeyFn keyFn,
693 OnCreateFn onCreate,
694 OnUpdateFn onUpdate,
695 bool fireImmediately = true)
696 {
697 if (ctx_->eaches.find(eachRef) == ctx_->eaches.end())
698 {
699 warnOnce("each:" + eachRef,
700 "UINode::bindEachKeyed('" + eachRef + "'): no Each with this ref");
701 return *this;
702 }
703
704 attachToRoot_();
705
706 std::weak_ptr<Layout> weakRoot = root_;
707 std::weak_ptr<BuildContext> weakCtx = ctx_;
708 auto sub = source.onChanged(
709 [weakRoot, weakCtx, eachRef,
710 keyFn = std::move(keyFn),
711 onCreate = std::move(onCreate),
712 onUpdate = std::move(onUpdate)](const std::vector<T> &vec) mutable
713 {
714 auto r = weakRoot.lock();
715 auto c = weakCtx.lock();
716 if (!r || !c)
717 return;
718 UINode ui(std::move(r), std::move(c));
719 ui.syncEachKeyed(eachRef, vec, keyFn, onCreate, onUpdate);
720 },
721 fireImmediately);
722
723 ctx_->eachSubs[eachRef] =
724 std::make_shared<typename Bindable<std::vector<T>>::Subscription>(std::move(sub));
725 return *this;
726 }
727
728 private:
729 void attachToRoot_() const;
730 void warnOnce(const std::string &key, const std::string &msg) const;
731
732 static void propagateCondition_(BuildContext &ctx, const std::string &condName, bool parentEffective);
733 static void clearEachNodes_(EachBinding &binding, Layout &parent);
734 static void mountEachFallback_(const Registry &reg, EachBinding &binding, Layout &parent, const Layout *anchor, bool effVis);
735
736 template <typename Fn, typename Elem>
737 static void invokeEachCallback_(Fn &fn, UINode &itemUi, const Elem &elem, size_t idx)
738 {
739 if constexpr (std::is_invocable_v<Fn &, UINode &, const Elem &, size_t>)
740 fn(itemUi, elem, idx);
741 else if constexpr (std::is_invocable_v<Fn &, UINode &, const Elem &>)
742 fn(itemUi, elem);
743 else
744 fn(itemUi);
745 }
746
747 std::shared_ptr<Layout> root_;
748 std::shared_ptr<BuildContext> ctx_;
749 mutable std::unordered_set<std::string> warnedKeys_;
750 };
751
756 class WeakUINode
757 {
758 public:
759 WeakUINode() = default;
760 WeakUINode(std::weak_ptr<Layout> root, std::weak_ptr<BuildContext> ctx)
761 : root_(std::move(root)), ctx_(std::move(ctx)) {}
762
763 std::optional<UINode> lock() const;
764 bool expired() const;
765
766 private:
767 std::weak_ptr<Layout> root_;
768 std::weak_ptr<BuildContext> ctx_;
769 };
770
771 // --- Global registry entry points (backwards-compatible) ---
772
773 void registerPrefab(const std::string &tagName, const std::string &markupSource);
774 bool registerPrefabFile(const std::string &tagName, const std::filesystem::path &path);
775 bool hasPrefab(const std::string &tagName);
776
777 UINode build(std::string_view source, DiagnosticSink *sink = nullptr);
778 UINode loadMarkupFile(const std::filesystem::path &path, DiagnosticSink *sink = nullptr);
779 UINode instantiate(
780 const std::string &tagName,
781 const std::vector<std::string> &extraClasses = {},
782 const std::string &id = "",
783 const std::string &ref = "");
784
785 // --- Per-context / explicit-registry entry points (§5.43) ---
786
788 void registerPrefab(UIContext &ctx, const std::string &tagName, const std::string &markupSource);
790 bool registerPrefabFile(UIContext &ctx, const std::string &tagName, const std::filesystem::path &path);
792 bool hasPrefab(const UIContext &ctx, const std::string &tagName);
793
795 UINode build(std::string_view source, const Registry &registry, DiagnosticSink *sink = nullptr);
797 UINode build(std::string_view source, UIContext &ctx, DiagnosticSink *sink = nullptr);
798
800 UINode loadMarkupFile(const std::filesystem::path &path, const Registry &registry, DiagnosticSink *sink = nullptr);
802 UINode loadMarkupFile(const std::filesystem::path &path, UIContext &ctx, DiagnosticSink *sink = nullptr);
803
805 UINode instantiate(
806 const Registry &registry,
807 const std::string &tagName,
808 const std::vector<std::string> &extraClasses = {},
809 const std::string &id = "",
810 const std::string &ref = "");
811
813 UINode instantiate(
814 UIContext &ctx,
815 const std::string &tagName,
816 const std::vector<std::string> &extraClasses = {},
817 const std::string &id = "",
818 const std::string &ref = "");
819
821} // namespace ZenitUI::ZMarkup
Single-value observable used for one-way data binding between models and UI components.
Centralized standard library includes used throughout ZenitUI.
Structured diagnostics for the CSS parser and style resolver.
The node class: tree structure, layout pass, style resolution, input dispatch, render orchestration,...
Lightweight, replaceable logging facility used across ZenitUI.
Executable metadata, typed attribute accessor, and fluent TagBuilder for ZMarkup.
Observable single-value container.
Definition Bindable.hpp:44
Abstract destination for diagnostics.
Definition Diagnostics.hpp:127
Element parseRoot()
Parse the whole source as a single root element.
Tag → factory map plus the prefab table (either per-UIContext or standalone/global).
Definition ZMarkup.hpp:215
void registerPrefab(const std::string &tagName, const std::string &markupSource)
Register a prefab from inline markup source.
std::vector< std::shared_ptr< Layout > > create_impl(const Element &el, BuildContext &ctx, bool isPrefabRoot=false) const
Build one or more nodes from el.
std::shared_ptr< Layout > create(const Element &el, BuildContext &ctx) const
Convenience wrapper: build exactly one root node from el.
std::function< std::shared_ptr< Layout >(const Element &)> Factory
Signature for tag factories: Element → node.
Definition ZMarkup.hpp:218
const std::vector< TagDoc > & tagDocs() const
Metadata of every tag registered with a TagDoc, in registration order.
Definition ZMarkup.hpp:233
UINode loadMarkupFile(const std::filesystem::path &path, DiagnosticSink *sink=nullptr) const
Load, parse, and build a UINode from path using this registry.
bool registerPrefabFile(const std::string &tagName, const std::filesystem::path &path)
Register a prefab from a file.
bool hasPrefab(const std::string &tagName) const
true if a prefab is registered under tagName.
static Registry withBuiltins()
Create a standalone Registry pre-populated with all built-in widgets.
void reg(TagDoc doc, Factory f)
Register or replace a tag together with its TagDoc metadata.
void clearPrefabs()
Remove all registered prefabs (leaves tag factories intact).
std::vector< ZStyleDiagnostic > validate(const Element &root) const
Static validation of a parsed tree against registered metadata.
TagBuilder tag(std::string_view name)
Fluent typed registration builder (drives parsing, defaults, validation, and docs).
bool unregisterPrefab(const std::string &tagName)
Remove a previously registered prefab.
void reg(const std::string &tag, Factory f)
Register or replace a tag factory.
const TagDoc * findTagDoc(std::string_view name) const
Lookup metadata for name, or nullptr if unregistered/undocumented.
UINode instantiate(const std::string &tagName, const std::vector< std::string > &extraClasses={}, const std::string &id="", const std::string &ref="") const
Directly instantiate a registered prefab without invoking the parser.
bool hasTag(const std::string &tagName) const
true if a tag factory is registered under tagName.
UINode build(std::string_view source, DiagnosticSink *sink=nullptr) const
Parse and build a UINode from inline markup source using this registry.
Fluent builder returned by Registry::tag(name).
Definition ZMarkupDocs.hpp:192
Root handle returned by build / instantiate: root node + build context + wiring helpers.
Definition ZMarkup.hpp:331
std::shared_ptr< Layout > find(const std::string &id) const
Look up a node by its #id.
std::shared_ptr< T > find(const std::string &id) const
Typed find: casts the result to T.
Definition ZMarkup.hpp:355
WeakUINode weak() const
Return a non-owning WeakUINode safe to capture in child callbacks.
std::vector< std::shared_ptr< T > > refAll(const std::string &name) const
Return every live node sharing the given @ref.
Definition ZMarkup.hpp:398
std::shared_ptr< T > ref(const std::string &name) const
Look up a live node by @ref.
Definition ZMarkup.hpp:363
Non-owning handle to a UINode, safe to capture inside child callbacks.
Definition ZMarkup.hpp:757
Registry & getGlobalRegistry()
Return the global registry, initializing built-ins on first call.
void registerBuiltins(Registry &r)
Register the built-in widgets into r.
Element parse(std::string_view src, std::string fileName="")
Parse src into a single root Element.
Source location for a diagnostic.
Definition Diagnostics.hpp:41
Per-instance UI context: backends, theme, text engine, frame state, and routing.
Definition UIContext.hpp:725
Per-build accumulator for #id, @ref, and runtime structural bindings.
Definition ZMarkup.hpp:166
const Registry * resolveRegistry(std::shared_ptr< const Registry > &keepAlive) const
Resolve the active Registry for runtime structural operations (Each).
Runtime state for a structural If@ref block.
Definition ZMarkup.hpp:122
Runtime state for a structural Each@ref prefab="..." slice.
Definition ZMarkup.hpp:147
Parsed representation of one markup node.
Definition ZMarkup.hpp:35
std::string attr(std::string_view k, std::string_view d="") const
Return the value of attribute k, or d if absent.
std::vector< SourceSpan > attrSpans
Parallel to attrs (source location of each attribute key).
Definition ZMarkup.hpp:42
std::vector< ZStyleDiagnostic > parseErrors
Lexical/syntax diagnostics recorded while parsing.
Definition ZMarkup.hpp:44
float attrFloat(std::string_view k, float d=0.0f) const
Return attribute k parsed as float, or d on failure.
SourceSpan span
Source location of the element tag.
Definition ZMarkup.hpp:43
bool attrBool(std::string_view k, bool d=false) const
Return attribute k as bool ("true", "1", "yes").
bool has(std::string_view k) const
true if the element has attribute k (regardless of value).
Type-erased storage base for keyed Each reconciliation.
Definition ZMarkup.hpp:136
Definition ZMarkup.hpp:203
Definition ZMarkup.hpp:201
Per-prefab-invocation slot routing table.
Definition ZMarkup.hpp:110
Metadata of one ZMarkup tag (only tag-specific attributes live in attrs).
Definition ZMarkupDocs.hpp:67