ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
03_selectors_theme/main.cpp

Example from examples/03_selectors_theme/.

Example from examples/03_selectors_theme/.

#include "ExampleHost.hpp"
#include <memory>
#include <string>
using namespace ZenitUI;
static constexpr const char *kDarkPalette = R"zs(
:root {
--bg: #0E1118;
--surface: #171C28;
--surface-alt: #1F2636;
--border: #2C354A;
--text: #E8ECF4;
--muted: #94A3B8;
--accent: #38BDF8;
--accent-fg: #082F49;
}
)zs";
static constexpr const char *kLightPalette = R"zs(
:root {
--bg: #F1F5F9;
--surface: #FFFFFF;
--surface-alt: #E2E8F0;
--border: #CBD5E1;
--text: #0F172A;
--muted: #475569;
--accent: #2563EB;
--accent-fg: #FFFFFF;
}
)zs";
static constexpr const char *kForestPalette = R"zs(
:root {
--bg: #0B1410;
--surface: #12211A;
--surface-alt: #1A3026;
--border: #274738;
--text: #ECFDF5;
--muted: #86EFAC;
--accent: #10B981;
--accent-fg: #022C22;
}
)zs";
static constexpr const char *kRules = R"zs(
:root {
font: inter;
font-size: 14px;
letter-spacing: 0px;
color: var(--text);
}
.page {
width: 100%;
height: 100%;
background: var(--bg);
padding: 28px;
overflow-x: hidden;
overflow-y: auto;
}
.title {
font: inter-bold;
font-size: 24px;
color: var(--text);
}
.card-title {
font: inter-semibold;
font-size: 15px;
color: var(--text);
}
.muted {
color: var(--muted);
font-size: 13px;
}
.card {
width: 100%;
background: var(--surface);
border-width: 1px;
border-color: var(--border);
radius: 12px;
padding: 18px;
gap: 12px;
}
.toolbar {
width: 100%;
gap: 10px;
wrap: true;
}
/* Direct child combinator (>) + universal (*) */
.toolbar > * {
padding: 8px 14px;
radius: 8px;
border-width: 1px;
border-color: var(--border);
transition-time: 0.12;
}
/* :not() excludes .primary and :disabled */
.toolbar > Button:not(.primary, :disabled) {
background: var(--surface-alt);
color: var(--text);
}
.toolbar > Button:not(.primary, :disabled):hover {
border-color: var(--accent);
}
.toolbar > Button.primary {
background: var(--accent);
border-color: var(--accent);
color: var(--accent-fg);
}
.toolbar > Button:disabled {
background: var(--surface);
color: var(--muted);
opacity: 0.45;
cursor: not-allowed;
}
.check-row {
width: 100%;
gap: 10px;
align-items: center;
}
/* Adjacent sibling (+): style the label right next to a checked Checkbox */
.check-row > Text {
flex: 1;
min-width: 0px;
color: var(--muted);
}
.check-row > Checkbox:checked + Text {
color: var(--accent);
font: inter-semibold;
}
/* General sibling (~): highlight every sibling badge after .marker */
.sibling-row {
width: 100%;
gap: 8px;
wrap: true;
align-items: center;
}
.sibling-row > .pill {
padding: 6px 12px;
radius: 999px;
background: var(--surface-alt);
color: var(--muted);
font-size: 12px;
}
.sibling-row > .marker ~ .pill {
background: var(--accent);
color: var(--accent-fg);
}
)zs";
static constexpr const char *kMarkup = R"zm(
ScrollView.page {
VStack width="100%" max-width="820px" margin="auto" gap="18px" {
VStack width="100%" gap="4px" {
Text.title wrap="true" "ZStyle Selectors & Live CSS Variables"
Text.muted wrap="true" "Switch palettes at runtime and inspect >, +, ~, *, and :not() selectors."
}
VStack.card {
Text.card-title wrap="true" "1. Live Theme Switching (:root --variables + ctx.invalidateStyles())"
HStack.toolbar {
Button.primary@dark "Dark Theme"
Button@light "Light Theme"
Button@forest "Forest Theme"
Button@disabledBtn "Disabled (:not excluded)"
}
}
VStack.card {
Text.card-title wrap="true" "2. Adjacent Sibling Combinator (Checkbox:checked + Text)"
Text.muted wrap="true" "Toggle the checkboxes below — the label style updates purely in ZStyle, with zero C++ callbacks."
HStack.check-row {
Checkbox checked="true"
Text wrap="true" "Enable live shadows (matches Checkbox:checked + Text)"
}
HStack.check-row {
Checkbox checked="false"
Text wrap="true" "Enable wireframe debug overlay"
}
}
VStack.card {
Text.card-title wrap="true" "3. General Sibling Combinator (.marker ~ .pill)"
Text.muted wrap="true" "Only pills appearing after the Marker pill receive the accent highlight."
HStack.sibling-row {
Text.pill "Before 1"
Text.pill "Before 2"
Text.pill.marker "Marker"
Text.pill "After 1 (~)"
Text.pill "After 2 (~)"
Text.pill "After 3 (~)"
}
}
}
}
)zm";
int main()
{
cfg.title = "ZenitUI 03 — Selectors & Themes";
cfg.width = 920;
cfg.height = 640;
cfg.minWidth = 400;
cfg.minHeight = 420;
return ZenitExample::runApp(cfg, [](UIContext &ctx, IBackend &) -> std::shared_ptr<Layout> {
ZenitExample::loadDefaultFonts(ctx);
auto applyPalette = [&ctx](const char *palette) {
ctx.theme.clear();
};
applyPalette(kDarkPalette);
auto ui = ZMarkup::build(kMarkup, ctx);
ui.setEnabledRef("disabledBtn", false);
auto bDark = std::weak_ptr<Layout>(ui.ref("dark"));
auto bLight = std::weak_ptr<Layout>(ui.ref("light"));
auto bForest = std::weak_ptr<Layout>(ui.ref("forest"));
auto setActive = [bDark, bLight, bForest](int idx) {
std::weak_ptr<Layout> arr[] = {bDark, bLight, bForest};
for (int i = 0; i < 3; ++i)
{
if (auto b = arr[i].lock())
{
if (i == idx)
b->addClass("primary");
else
b->removeClass("primary");
}
}
};
ui.onClickRef("dark", [applyPalette, setActive]() {
applyPalette(kDarkPalette);
setActive(0);
});
ui.onClickRef("light", [applyPalette, setActive]() {
applyPalette(kLightPalette);
setActive(1);
});
ui.onClickRef("forest", [applyPalette, setActive]() {
applyPalette(kForestPalette);
setActive(2);
});
return ui.root();
});
}
Unified window, frame-loop, and subsystem host for interchangeable backends.
Definition Backend.hpp:49
void clear()
Remove every rule, keyframe, and root declaration.
Definition Theme.hpp:242
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
void invalidateStyles()
Force style-cache invalidation across the tree.
Definition UIContext.hpp:928
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