#include "ExampleHost.hpp"
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <memory>
#include <string>
#include <vector>
using namespace ZenitUI;
static constexpr const char *kStyle = R"zs(
:root {
font: inter;
font-size: 13px;
letter-spacing: 0px;
color: #E5E7EB;
}
.app {
width: 100%;
height: 100%;
background: #0B0E14;
padding: 18px;
gap: 12px;
}
.header {
width: 100%;
background: #141824;
border-width: 1px;
border-color: #252D40;
radius: 12px;
padding: 14px 16px;
gap: 10px;
}
.title {
font: inter-bold;
font-size: 20px;
color: #F8FAFC;
}
.muted {
font-size: 12px;
color: #94A3B8;
}
.stat-pill {
padding: 4px 10px;
radius: 999px;
background: #1B2233;
border-width: 1px;
border-color: #2E3852;
color: #7DD3FC;
font-size: 12px;
}
TextInput.search {
flex: 1;
min-width: 180px;
background: #0F131D;
border-width: 1px;
border-color: #2B3347;
radius: 8px;
padding: 7px 10px;
color: #F8FAFC;
}
TextInput.search:focus {
border-color: #38BDF8;
}
Button.chip-btn {
padding: 6px 11px;
radius: 7px;
background: #1B2130;
border-width: 1px;
border-color: #2C354A;
color: #CBD5E1;
font-size: 12px;
cursor: pointer;
transition-time: 0.1;
}
Button.chip-btn:hover {
border-color: #475569;
color: #FFFFFF;
}
Button.chip-btn.active {
background: #1E3A5F;
border-color: #38BDF8;
color: #7DD3FC;
}
.list-frame {
width: 100%;
flex: 1;
min-height: 0px;
background: #111520;
border-width: 1px;
border-color: #252D40;
radius: 12px;
overflow: hidden;
}
.col-header {
width: 100%;
height: 32px;
background: #161C2B;
border-bottom: 1px;
border-color: #252D40;
padding: 0px 14px;
gap: 12px;
align-items: center;
}
VirtualList.log-list {
width: 100%;
flex: 1;
min-height: 0px;
overflow-x: hidden;
overflow-y: auto;
}
.log-row {
width: 100%;
height: 38px;
padding: 0px 14px;
gap: 12px;
align-items: center;
background: #111520;
border-bottom: 1px;
border-color: #1A2030;
cursor: pointer;
}
.log-row.odd {
background: #141926;
}
.log-row:hover {
background: #1C2336;
}
.log-row.selected {
background: #1B3152;
border-color: #2563EB;
}
.col-seq {
width: 68px;
color: #64748B;
font-size: 12px;
}
.col-sev {
width: 62px;
padding: 2px 8px;
radius: 999px;
font-size: 11px;
font: inter-semibold;
text-align: center;
background: #1E293B;
color: #7DD3FC;
}
.col-sev.sev-warn {
background: #422006;
color: #FBBF24;
}
.col-sev.sev-err {
background: #450A0A;
color: #F87171;
}
.col-svc {
width: 110px;
color: #93C5FD;
font-size: 12px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.col-msg {
flex: 1;
min-width: 0px;
color: #E2E8F0;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.col-ts {
width: 82px;
color: #64748B;
font-size: 12px;
text-align: end;
}
.inspector {
width: 100%;
background: #141824;
border-width: 1px;
border-color: #252D40;
border-left: 3px;
border-color: #38BDF8;
radius: 10px;
padding: 10px 14px;
gap: 2px;
}
)zs";
static constexpr const char *kLogRowPrefab = R"zm(
HStack.log-row {
Text.col-seq@seq ""
Text.col-sev@sev ""
Text.col-svc@svc ""
Text.col-msg@msg ""
Text.col-ts@ts ""
}
)zm";
static constexpr const char *kMarkup = R"zm(
VStack.app {
VStack.header {
HStack width="100%" justify="space-between" align-items="center" wrap="true" gap="8px" {
VStack gap="2px" {
Text.title "Virtualized Log Viewer (50,000 Rows)"
Text.muted "UI::VirtualList recycles a fixed pool of ZMarkup prefab nodes while scrolling."
}
HStack gap="6px" wrap="true" align-items="center" {
Text.stat-pill@statVisible "Visible: 0-0 / 50000"
Text.stat-pill@statPool "Active: 0 | Pooled: 0"
Text.stat-pill@statAlloc "Total Allocated: 0"
}
}
HStack width="100%" gap="8px" wrap="true" align-items="center" {
TextInput.search@search placeholder="Filter message or service (e.g. shader, atlas, timeout)..."
Button.chip-btn.active@fAll "All"
Button.chip-btn@fInfo "INFO"
Button.chip-btn@fWarn "WARN"
Button.chip-btn@fErr "ERROR"
Button.chip-btn@jumpTop "Top"
Button.chip-btn@jumpMid "Mid"
Button.chip-btn@jumpEnd "Bottom"
}
}
VStack.list-frame {
HStack.col-header {
Text.col-seq "SEQ"
Text.muted width="62px" "LEVEL"
Text.muted width="110px" "SERVICE"
Text.muted flex="1" min-width="0px" "MESSAGE"
Text.col-ts "TIME"
}
VirtualList.log-list@logList prefab="LogRow" items="50000" row-height="38"
}
VStack.inspector {
Text.muted@selHeader "Click any row to inspect its full log entry"
Text@selDetail wrap="true" "No row selected."
}
}
)zm";
namespace
{
struct LogEntry
{
int seq{0};
int severity{0};
const char *service{""};
std::string message;
std::string timestamp;
};
struct ViewerState
{
std::vector<LogEntry> entries;
std::vector<int> filteredIndices;
int severityFilter{-1};
std::string query;
int selectedSeq{-1};
int totalAllocated{0};
};
std::string toLowerCopy(std::string_view sv)
{
std::string out(sv);
for (char &c : out)
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
return out;
}
const char *severityName(int sev)
{
switch (sev)
{
case 2:
return "ERROR";
case 1:
return "WARN";
default:
return "INFO";
}
}
void populateLogs(std::vector<LogEntry> &out, int count)
{
static constexpr const char *kServices[] = {
"Renderer", "TextEngine", "Layout", "TargetPool", "AssetIO", "Input"};
static constexpr const char *kInfoMsgs[] = {
"Committed indexed geometry batch to GPU stream ring",
"Glyph atlas page upload completed with zero stalls",
"Resolved responsive flex pass within 0.14 ms budget",
"Reused bucketed off-screen render target from TargetPool",
"Loaded shader pipeline variant for active backend",
"Dispatched pointer hit-test across portal overlay stack"};
static constexpr const char *kWarnMsgs[] = {
"Glyph atlas utilization exceeded 85%, preparing new page",
"Style cache miss triggered by viewport resize event",
"TargetPool trimmed 3 idle FBO allocations after grace window",
"IME composition commit arrived after focus transition"};
static constexpr const char *kErrMsgs[] = {
"Shader uniform lookup missed on custom filter pass",
"AssetIO timeout while reading remote texture stream",
"Nested off-screen FBO exceeded maximum 4096px dimension clamp"};
out.reserve(static_cast<size_t>(count));
for (int i = 0; i < count; ++i)
{
LogEntry e;
e.seq = i + 1;
const int r = (i * 2654435761u) % 100u;
if (r < 8)
e.severity = 2;
else if (r < 28)
e.severity = 1;
else
e.severity = 0;
e.service = kServices[i % 6];
if (e.severity == 2)
e.message = std::string(kErrMsgs[i % 3]) + " [job #" + std::to_string((i * 7) % 997) + "]";
else if (e.severity == 1)
e.message = std::string(kWarnMsgs[i % 4]) + " [frame " + std::to_string(i) + "]";
else
e.message = std::string(kInfoMsgs[i % 6]) + " [batch " + std::to_string(i % 64) + "]";
const int totalSec = (i / 12) % 86400;
char buf[16];
std::snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
totalSec / 3600, (totalSec / 60) % 60, totalSec % 60);
e.timestamp = buf;
out.push_back(std::move(e));
}
}
}
int main()
{
cfg.
title =
"ZenitUI 08 — VirtualList (50,000 Rows)";
std::weak_ptr<UI::VirtualList> listW;
std::weak_ptr<UI::Text> statVisibleW;
std::weak_ptr<UI::Text> statPoolW;
std::weak_ptr<UI::Text> statAllocW;
std::shared_ptr<ViewerState> state;
return ZenitExample::runApp(
cfg,
ZenitExample::loadDefaultFonts(ctx);
ZMarkup::registerPrefab(ctx, "LogRow", kLogRowPrefab);
state = std::make_shared<ViewerState>();
populateLogs(state->entries, 50000);
state->filteredIndices.resize(state->entries.size());
for (size_t i = 0; i < state->entries.size(); ++i)
state->filteredIndices[i] = static_cast<int>(i);
auto ui = ZMarkup::build(kMarkup, ctx);
ui.root()->retainObject(state);
listW = list;
statVisibleW = ui.ref<
UI::Text>(
"statVisible");
statPoolW = ui.ref<
UI::Text>(
"statPool");
statAllocW = ui.ref<
UI::Text>(
"statAlloc");
auto updateInspector = [state, uiWeak](const LogEntry &e) {
if (auto locked = uiWeak.lock())
{
locked->setTextRef("selHeader",
"Selected #" + std::to_string(e.seq) + " | " +
severityName(e.severity) + " | " +
e.service + " | " + e.timestamp);
locked->setTextRef("selDetail", e.message);
}
};
state->filteredIndices.clear();
const std::string q = toLowerCopy(state->query);
for (size_t i = 0; i < state->entries.size(); ++i)
{
const auto &e = state->entries[i];
if (state->severityFilter >= 0 && e.severity != state->severityFilter)
continue;
if (!q.empty())
{
if (toLowerCopy(e.message).find(q) == std::string::npos &&
toLowerCopy(e.service).find(q) == std::string::npos)
continue;
}
state->filteredIndices.push_back(static_cast<int>(i));
}
if (auto l = listW.lock())
{
l->setItemCount(static_cast<int>(state->filteredIndices.size()));
l->scrollToRow(0);
l->refresh();
}
if (auto locked = uiWeak.lock())
{
locked->toggleClassRef("fAll", "active", state->severityFilter == -1);
locked->toggleClassRef("fInfo", "active", state->severityFilter == 0);
locked->toggleClassRef("fWarn", "active", state->severityFilter == 1);
locked->toggleClassRef("fErr", "active", state->severityFilter == 2);
}
};
if (list)
{
list->setBufferRows(3);
++state->totalAllocated;
};
list->onBindRowNode = [state, listW, updateInspector](
int rowIdx,
ZMarkup::UINode &row) {
if (rowIdx < 0 || static_cast<size_t>(rowIdx) >= state->filteredIndices.size())
return;
const LogEntry &e = state->entries[static_cast<size_t>(state->filteredIndices[static_cast<size_t>(rowIdx)])];
row.setTextRef("seq", "#" + std::to_string(e.seq));
row.setTextRef("sev", severityName(e.severity));
row.toggleClassRef("sev", "sev-warn", e.severity == 1);
row.toggleClassRef("sev", "sev-err", e.severity == 2);
row.setTextRef("svc", e.service);
row.setTextRef("msg", e.message);
row.setTextRef("ts", e.timestamp);
auto root = row.root();
if ((rowIdx % 2) == 1)
root->addClass("odd");
else
root->removeClass("odd");
if (e.seq == state->selectedSeq)
root->addClass("selected");
else
root->removeClass("selected");
root->setInteractive(true);
root->onClick = [state, seq = e.seq, listW, updateInspector]() {
state->selectedSeq = seq;
updateInspector(state->entries[static_cast<size_t>(seq - 1)]);
if (auto l = listW.lock())
l->refresh();
};
};
list->setItemCount(static_cast<int>(state->filteredIndices.size()));
list->refresh();
}
ui.onTextChangedRef(
"search", [state,
applyFilter](
const std::string &txt) {
state->query = txt;
});
state->severityFilter = -1;
});
state->severityFilter = 0;
});
state->severityFilter = 1;
});
state->severityFilter = 2;
});
ui.onClickRef("jumpTop", [listW]() {
if (auto l = listW.lock())
l->scrollToRow(0);
});
ui.onClickRef("jumpMid", [listW, state]() {
if (auto l = listW.lock(); l && !state->filteredIndices.empty())
l->scrollToRow(static_cast<int>(state->filteredIndices.size() / 2));
});
ui.onClickRef("jumpEnd", [listW, state]() {
if (auto l = listW.lock(); l && !state->filteredIndices.empty())
l->scrollToRow(static_cast<int>(state->filteredIndices.size()) - 1);
});
return ui.root();
},
auto l = listW.lock();
if (!l || !state)
return;
const int first = l->getFirstVisibleRow();
const int last = l->getLastVisibleRow();
const int total = l->getItemCount();
if (auto sv = statVisibleW.lock())
{
if (first < 0 || total == 0)
sv->setText("Visible: none / 0");
else
sv->setText("Visible: " + std::to_string(first + 1) + "-" +
std::to_string(last + 1) + " / " + std::to_string(total));
}
if (auto sp = statPoolW.lock())
{
sp->setText("Active: " + std::to_string(l->activeRowCount()) +
" | Pooled: " + std::to_string(l->pooledRowCount()));
}
if (auto sa = statAllocW.lock())
sa->setText("Total Allocated: " + std::to_string(state->totalAllocated));
});
}
Unified window, frame-loop, and subsystem host for interchangeable backends.
Definition Backend.hpp:49
A read-only text node.
Definition Text.hpp:32
A fixed-height virtualized list that recycles ZMarkup prefab rows.
Definition VirtualList.hpp:46
Root handle returned by build / instantiate: root node + build context + wiring helpers.
Definition ZMarkup.hpp:331
Non-owning handle to a UINode, safe to capture inside child callbacks.
Definition ZMarkup.hpp:757
void applyFilter(const FilterDef &def, const FilterContext &ctx)
Execute one filter against the given context.
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
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