#include "ExampleHost.hpp"
#include <memory>
#include <string>
using namespace ZenitUI;
static constexpr const char *kStyle = R"zs(
:root {
font: inter;
font-size: 14px;
letter-spacing: 0px;
color: #E5E7EB;
}
.page {
width: 100%;
height: 100%;
background: #0D1017;
padding: 24px;
overflow-x: hidden;
overflow-y: auto;
}
.shell {
width: 100%;
max-width: 780px;
margin: auto;
gap: 18px;
}
.title {
font: inter-bold;
font-size: 24px;
color: #F8FAFC;
}
.muted {
font-size: 13px;
color: #94A3B8;
}
.section-card {
width: 100%;
background: #151924;
border-width: 1px;
border-color: #262E42;
radius: 12px;
overflow: hidden;
}
.section-header {
width: 100%;
padding: 14px 18px;
background: #191F2E;
border-bottom: 1px;
border-color: #262E42;
align-items: center;
}
.section-title {
font: inter-semibold;
font-size: 15px;
color: #F1F5F9;
}
.section-body {
width: 100%;
padding: 18px;
gap: 12px;
}
.section-footer {
width: 100%;
padding: 12px 18px;
background: #111520;
border-top: 1px;
border-color: #232A3C;
align-items: center;
}
.metrics-row {
width: 100%;
gap: 12px;
wrap: true;
}
.metric-tile {
flex: 1;
min-width: 170px;
padding: 14px;
radius: 10px;
background: #10141F;
border-width: 1px;
border-color: #252D40;
gap: 4px;
}
.metric-tile.accent {
border-color: #2563EB;
background: #121C30;
}
.metric-num {
font: inter-bold;
font-size: 22px;
color: #38BDF8;
}
.badge {
padding: 3px 10px;
radius: 999px;
background: #1E293B;
color: #7DD3FC;
font-size: 12px;
}
Button.btn {
padding: 8px 14px;
radius: 8px;
background: #2563EB;
color: #FFFFFF;
cursor: pointer;
transition-time: 0.12;
}
Button.btn:hover {
background: #3B82F6;
}
Button.btn-warn {
padding: 8px 14px;
radius: 8px;
background: #B45309;
color: #FFFBEB;
cursor: pointer;
transition-time: 0.12;
}
Button.btn-warn:hover {
background: #D97706;
}
Button.btn-ghost {
padding: 8px 14px;
radius: 8px;
background: #1F2638;
border-width: 1px;
border-color: #334155;
color: #CBD5E1;
cursor: pointer;
}
Button.btn-ghost:hover {
background: #2A344D;
}
.feed-entry {
width: 100%;
padding: 12px 14px;
radius: 8px;
background: #10141F;
border-width: 1px;
border-color: #252D40;
border-left: 3px;
border-color: #38BDF8;
justify: space-between;
align-items: center;
gap: 12px;
}
.feed-entry.warn {
border-color: #F59E0B;
background: #1C1810;
}
Button.dismiss {
padding: 4px 10px;
radius: 6px;
background: #232A3C;
color: #CBD5E1;
cursor: pointer;
}
Button.dismiss:hover {
background: #DC2626;
color: #FFFFFF;
}
)zs";
static constexpr const char *kSectionCardPrefab = R"zm(
VStack.section-card {
HStack.section-header {
Slot name="header" {
Text.section-title "Untitled Section"
}
}
VStack.section-body {
Slot {
Text.muted "No body content provided."
}
}
HStack.section-footer {
Slot name="footer" {
Text.muted wrap="true" "Default fallback footer (caller omitted slot=\"footer\")."
}
}
}
)zm";
static constexpr const char *kMetricTilePrefab = R"zm(
VStack.metric-tile {
Slot {}
}
)zm";
static constexpr const char *kFeedEntryPrefab = R"zm(
HStack.feed-entry {
VStack flex="1" min-width="0px" gap="2px" {
Text@title wrap="true" ""
Text.muted@meta wrap="true" ""
}
Button.dismiss@dismiss "x"
}
)zm";
static constexpr const char *kMarkup = R"zm(
ScrollView.page {
VStack.shell {
VStack width="100%" gap="4px" {
Text.title wrap="true" "Prefabs, Named Slots & Direct Instantiation"
Text.muted wrap="true" "Components registered in ctx.markupRegistry() with named slots, fallbacks, and zero-parse runtime spawning."
}
SectionCard {
HStack slot="header" width="100%" justify="space-between" align-items="center" wrap="true" gap="8px" {
Text.section-title "System Overview (All 3 Slots Provided)"
Text.badge "Named Slots"
}
HStack.metrics-row {
MetricTile.accent {
Text.muted "REGISTERED PREFABS"
Text.metric-num "3"
}
MetricTile {
Text.muted "SPAWNED ENTRIES"
Text.metric-num@spawnedCount "0"
}
MetricTile {
Text.muted "ACTIVE IN FEED"
Text.metric-num@activeCount "0"
}
}
HStack slot="footer" width="100%" justify="end" wrap="true" gap="10px" {
Button.btn@addInfo "+ Spawn Info"
Button.btn-warn@addWarn "+ Spawn Warning"
Button.btn-ghost@clearFeed "Clear Feed"
}
}
SectionCard {
HStack slot="header" width="100%" justify="space-between" align-items="center" wrap="true" gap="8px" {
Text.section-title "Live Event Feed (Instantiated from C++)"
Text.badge "Fallback Footer"
}
VStack@feed width="100%" gap="8px" {}
}
}
}
)zm";
int main()
{
cfg.
title =
"ZenitUI 06 — Prefabs & Named Slots";
return ZenitExample::runApp(cfg, [](
UIContext &ctx,
IBackend &) -> std::shared_ptr<Layout> {
ZenitExample::loadDefaultFonts(ctx);
reg.registerPrefab("MetricTile", kMetricTilePrefab);
reg.registerPrefab("FeedEntry", kFeedEntryPrefab);
auto ui = ZMarkup::build(kMarkup, ctx);
struct FeedState
{
int spawned{0};
int active{0};
};
auto state = std::make_shared<FeedState>();
std::weak_ptr<Layout> feedW = ui.ref("feed");
std::weak_ptr<UI::Text> spawnedLbl = ui.ref<
UI::Text>(
"spawnedCount");
std::weak_ptr<UI::Text> activeLbl = ui.
ref<
UI::Text>(
"activeCount");
auto refreshCounts = [state, spawnedLbl, activeLbl]() {
if (auto s = spawnedLbl.lock())
s->setText(std::to_string(state->spawned));
if (auto a = activeLbl.lock())
a->setText(std::to_string(state->active));
};
auto spawnEntry = [&ctx, state, feedW, refreshCounts](bool isWarning,
const std::string &headline,
const std::string &detail) {
auto feed = feedW.lock();
if (!feed)
return;
++state->spawned;
++state->active;
entry.
setTextRef(
"title",
"#" + std::to_string(state->spawned) +
" — " + headline);
entry.setTextRef("meta", detail);
if (isWarning)
entry.
root()->addClass(
"warn");
std::weak_ptr<Layout> entryW = entry.root();
auto dismissed = std::make_shared<bool>(false);
entry.onClickRef("dismiss", [entryW, state, dismissed, refreshCounts]() {
if (*dismissed)
return;
*dismissed = true;
if (auto n = entryW.lock())
n->removeFromParent();
if (state->active > 0)
--state->active;
refreshCounts();
});
feed->addChild(entry.root());
refreshCounts();
};
ui.onClickRef("addInfo", [spawnEntry]() {
spawnEntry(false, "Asset pipeline ready", "Instantiated directly from cached FeedEntry prefab AST.");
});
ui.onClickRef("addWarn", [spawnEntry]() {
spawnEntry(true, "High frame budget warning", "Styled with .warn class merged onto the prefab root.");
});
ui.onClickRef("clearFeed", [feedW, state, refreshCounts]() {
if (auto f = feedW.lock())
f->removeAllChildren();
state->active = 0;
refreshCounts();
});
spawnEntry(false, "Welcome to ZenitUI 1.0 Prefabs", "Click 'x' on any row to dismiss it safely.");
spawnEntry(true, "Slot fallback active below", "Notice the bottom card uses the prefab's default footer.");
return ui.root();
});
}
Unified window, frame-loop, and subsystem host for interchangeable backends.
Definition Backend.hpp:49
std::string ref
@ref for UINode::ref / refAll lookup.
Definition Layout.hpp:943
A read-only text node.
Definition Text.hpp:32
void registerPrefab(const std::string &tagName, const std::string &markupSource)
Register a prefab from inline markup source.
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 loadStyleString(std::string_view src, Theme &theme, const std::string &fileName="<string>", DiagnosticSink *sink=nullptr, bool strict=false, const Registry *registry=nullptr)
Parse src and apply it to theme.
Per-instance UI context: backends, theme, text engine, frame state, and routing.
Definition UIContext.hpp:725
ZMarkup::Registry & markupRegistry()
Return this context's isolated ZMarkup::Registry, lazily initialized with built-in widgets.
Theme theme
Per-context stylesheet (rules, keyframes, and :root style).
Definition UIContext.hpp:911
Backend-agnostic window and graphics-context creation parameters.
Definition Backend.hpp:31