#include "ExampleHost.hpp"
#include <cmath>
#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: #0E1016;
padding: 24px;
overflow-x: hidden;
overflow-y: auto;
}
.form-card {
width: 100%;
max-width: 680px;
margin: auto;
background: #161923;
border-width: 1px;
border-color: #272D3D;
radius: 14px;
padding: 26px;
gap: 16px;
}
.heading {
font: inter-bold;
font-size: 22px;
color: #F8FAFC;
}
.label {
font: inter-semibold;
font-size: 13px;
color: #CBD5E1;
}
.hint {
font-size: 12px;
color: #94A3B8;
}
TextInput, TextArea {
width: 100%;
background: #10131C;
border-width: 1px;
border-color: #2B3245;
radius: 8px;
padding: 10px 12px;
color: #F8FAFC;
}
TextInput:focus, TextArea:focus {
border-color: #38BDF8;
}
Dropdown {
width: 100%;
}
.two-col {
width: 100%;
gap: 16px;
wrap: true;
align-items: start;
}
.col {
flex: 1;
min-width: 220px;
gap: 6px;
}
.options-row {
width: 100%;
gap: 12px;
wrap: true;
justify: space-between;
align-items: center;
}
.divider {
width: 100%;
height: 1px;
background: #252B3B;
}
Button.submit {
padding: 10px 22px;
radius: 8px;
background: #2563EB;
color: #FFFFFF;
cursor: pointer;
transition-time: 0.12;
}
Button.submit:hover {
background: #3B82F6;
}
Button.submit:disabled {
background: #1E2433;
color: #64748B;
cursor: not-allowed;
}
Button.secondary {
padding: 10px 16px;
radius: 8px;
background: #1F2433;
border-width: 1px;
border-color: #323A4F;
color: #CBD5E1;
cursor: pointer;
}
.summary {
width: 100%;
padding: 12px 14px;
radius: 8px;
background: #111520;
border-left: 3px;
border-color: #38BDF8;
color: #CBD5E1;
font-size: 13px;
}
)zs";
static constexpr const char *kMarkup = R"zm(
ScrollView.page {
VStack.form-card {
VStack width="100%" gap="4px" {
Text.heading wrap="true" "Create Project Workspace"
Text.hint wrap="true" "Built-in widgets with live validation, keyboard navigation, clipboard & IME support."
}
VStack width="100%" gap="6px" {
Text.label "Project Name *"
TextInput@projectName placeholder="e.g. ZenitEditor"
}
VStack width="100%" gap="6px" {
Text.label "Description (multi-line TextArea)"
TextArea@description rows="4" placeholder="Write project notes, paste text, or press Enter for new lines..."
}
HStack.two-col {
VStack.col {
Text.label "Target Profile"
Dropdown@profile options="Desktop Tool,2D Game HUD,Embedded Kiosk" selected="0"
}
VStack.col {
HStack width="100%" justify="space-between" align-items="center" gap="8px" {
Text.label "Worker Threads Budget"
Text.hint@budgetPct "50%"
}
Slider@budget width="100%" value="0.5"
ProgressBar@budgetBar width="100%" height="8px" value="0.5"
}
}
Stack.divider {}
HStack.options-row {
HStack gap="10px" align-items="center" {
Checkbox@strictMode checked="true"
Text "Enable strict ZStyle diagnostics"
}
HStack gap="10px" align-items="center" {
Text.hint "Telemetry"
Toggle@telemetry checked="false"
}
}
HStack width="100%" justify="end" wrap="true" gap="10px" {
Button.secondary@clearBtn "Clear"
Button.submit@submitBtn "Create Workspace"
}
Text.summary@summary wrap="true" "Enter a project name (at least 3 characters) to enable creation."
}
}
)zm";
int main()
{
cfg.
title =
"ZenitUI 04 — Widgets & Forms";
return ZenitExample::runApp(cfg, [](
UIContext &ctx,
IBackend &) -> std::shared_ptr<Layout> {
ZenitExample::loadDefaultFonts(ctx);
auto ui = ZMarkup::build(kMarkup, ctx);
auto nameInput = std::weak_ptr<UI::TextInput>(ui.ref<
UI::TextInput>(
"projectName"));
auto descInput = std::weak_ptr<UI::TextArea>(ui.ref<
UI::TextArea>(
"description"));
auto budgetBar = std::weak_ptr<UI::ProgressBar>(ui.ref<
UI::ProgressBar>(
"budgetBar"));
auto budgetPct = std::weak_ptr<UI::Text>(ui.ref<
UI::Text>(
"budgetPct"));
auto strictCb = std::weak_ptr<UI::Checkbox>(ui.ref<
UI::Checkbox>(
"strictMode"));
auto teleTg = std::weak_ptr<UI::Toggle>(ui.ref<
UI::Toggle>(
"telemetry"));
auto submitBtn = std::weak_ptr<Layout>(ui.ref("submitBtn"));
auto summaryLbl = std::weak_ptr<UI::Text>(ui.ref<
UI::Text>(
"summary"));
auto validate = [nameInput, submitBtn]() {
bool ok = false;
if (auto in = nameInput.lock())
ok = in->getText().size() >= 3;
if (auto btn = submitBtn.lock())
btn->setEnabled(ok);
};
validate();
ui.onTextChangedRef("projectName", [validate](const std::string &) { validate(); });
ui.onValueChangedRef("budget", [budgetBar, budgetPct](float v) {
if (auto bar = budgetBar.lock())
bar->setValue(v);
if (auto lbl = budgetPct.lock())
lbl->setText(std::to_string(static_cast<int>(std::lround(v * 100.0f))) + "%");
});
auto doSubmit = [nameInput, descInput, strictCb, teleTg, summaryLbl]() {
std::string name = nameInput.lock() ? nameInput.lock()->getText() : "";
if (name.size() < 3)
return;
size_t descLen = descInput.lock() ? descInput.lock()->getText().size() : 0;
bool strict = strictCb.lock() ? strictCb.lock()->getChecked() : false;
bool tele = teleTg.lock() ? teleTg.lock()->getChecked() : false;
if (auto s = summaryLbl.lock())
{
s->setText("Created '" + name + "' — notes: " + std::to_string(descLen) +
" bytes, strict=" + (strict ? "on" : "off") +
", telemetry=" + (tele ? "on" : "off") + ".");
}
};
if (auto in = nameInput.lock())
in->onSubmit = [doSubmit](const std::string &) { doSubmit(); };
ui.onClickRef("submitBtn", doSubmit);
ui.onClickRef("clearBtn", [nameInput, descInput, summaryLbl, validate]() {
if (auto in = nameInput.lock())
in->setText("");
if (auto d = descInput.lock())
d->setText("");
validate();
if (auto s = summaryLbl.lock())
s->setText("Form cleared.");
});
return ui.root();
});
}
Unified window, frame-loop, and subsystem host for interchangeable backends.
Definition Backend.hpp:49
A checkbox with a custom-painted check mark.
Definition Checkbox.hpp:29
A value-in-[0, 1] progress indicator.
Definition ProgressBar.hpp:29
A multi-line UTF-8 text editor.
Definition TextArea.hpp:28
A single-line text editor.
Definition TextInput.hpp:27
A read-only text node.
Definition Text.hpp:32
A toggle switch.
Definition Toggle.hpp:29
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