#include "ExampleHost.hpp"
#include <cmath>
#include <memory>
#include <optional>
#include <string>
#include <vector>
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: 720px;
margin: auto;
gap: 16px;
}
.title {
font: inter-bold;
font-size: 24px;
color: #F8FAFC;
}
.muted {
font-size: 13px;
color: #94A3B8;
}
.card {
width: 100%;
background: #151924;
border-width: 1px;
border-color: #262E42;
radius: 12px;
padding: 20px;
gap: 14px;
}
.section-title {
font: inter-semibold;
font-size: 15px;
color: #F1F5F9;
}
.channel {
width: 100%;
gap: 8px;
}
.pct {
font: inter-semibold;
font-size: 13px;
color: #38BDF8;
}
.btn-row {
width: 100%;
gap: 10px;
wrap: true;
align-items: center;
}
Button.preset {
padding: 8px 14px;
radius: 8px;
background: #1F2638;
border-width: 1px;
border-color: #334155;
color: #E2E8F0;
cursor: pointer;
transition-time: 0.12;
}
Button.preset:hover {
background: #2A344D;
border-color: #38BDF8;
}
Button.accent {
padding: 8px 14px;
radius: 8px;
background: #2563EB;
border-width: 1px;
border-color: #3B82F6;
color: #FFFFFF;
cursor: pointer;
transition-time: 0.12;
}
Button.accent:hover {
background: #3B82F6;
}
.summary {
width: 100%;
padding: 12px 14px;
radius: 8px;
background: #10141F;
border-left: 3px;
border-color: #38BDF8;
color: #CBD5E1;
font-size: 13px;
}
)zs";
static constexpr const char *kMarkup = R"zm(
ScrollView.page {
VStack.shell {
VStack width="100%" gap="4px" {
Text.title wrap="true" "Audio Mixer — Reactive Data Binding"
Text.muted wrap="true" "One Bindable<T> model drives multiple widgets via bindTo() and RAII Subscriptions."
}
VStack.card {
HStack width="100%" justify="space-between" align-items="center" wrap="true" gap="10px" {
Text.section-title "Presets (Model-Only Writes)"
HStack gap="10px" align-items="center" {
Text.muted "Mute Output"
Toggle@muteToggle checked="false"
}
}
HStack.btn-row {
Button.preset@presetBalanced "Balanced"
Button.preset@presetCinema "Cinematic"
Button.preset@presetNight "Night Mode"
Button.preset@presetMute "Mute All"
}
}
VStack.card {
HStack width="100%" justify="space-between" align-items="center" wrap="true" gap="8px" {
Text.section-title "Mixer Channels"
Button.accent@toggleSub "Disconnect Master Meter"
}
VStack.channel {
HStack width="100%" justify="space-between" align-items="center" {
Text "Master Output"
Text.pct@masterPct "80%"
}
Slider@masterSlider width="100%" value="0.8"
ProgressBar@masterBar width="100%" height="8px" value="0.8"
}
VStack.channel {
HStack width="100%" justify="space-between" align-items="center" {
Text "Music Bus"
Text.pct@musicPct "65%"
}
Slider@musicSlider width="100%" value="0.65"
ProgressBar@musicBar width="100%" height="8px" value="0.65"
}
VStack.channel {
HStack width="100%" justify="space-between" align-items="center" {
Text "Sound Effects Bus"
Text.pct@sfxPct "90%"
}
Slider@sfxSlider width="100%" value="0.9"
ProgressBar@sfxBar width="100%" height="8px" value="0.9"
}
}
Text.summary@summary wrap="true" ""
}
}
)zm";
namespace
{
struct AudioModel
{
std::vector<std::shared_ptr<void>> subs;
std::optional<Bindable<float>::Subscription> masterMeterSub;
template <typename Sub>
void keepSub(Sub &&sub)
{
subs.push_back(std::make_shared<std::decay_t<Sub>>(std::forward<Sub>(sub)));
}
};
std::string fmtPct(float v)
{
return std::to_string(static_cast<int>(std::lround(v * 100.0f))) + "%";
}
}
int main()
{
cfg.
title =
"ZenitUI 05 — Data Binding (Bindable<T>)";
return ZenitExample::runApp(cfg, [](
UIContext &ctx,
IBackend &) -> std::shared_ptr<Layout> {
ZenitExample::loadDefaultFonts(ctx);
auto ui = ZMarkup::build(kMarkup, ctx);
auto model = std::make_shared<AudioModel>();
ui.root()->retainObject(model);
auto masterSlider = ui.ref<
UI::Slider>(
"masterSlider");
auto recomputeSummary = [m = model.get()]() {
const float effMaster = m->muted.get() ? 0.0f : m->master.get();
const float effMusic = effMaster * m->music.get();
const float effSfx = effMaster * m->sfx.get();
std::string s = m->muted.get() ? "[MUTED] " : "[LIVE] ";
s += "Effective output — Master: " + fmtPct(effMaster) +
" | Music: " + fmtPct(effMusic) +
" | SFX: " + fmtPct(effSfx) +
" | Master meter binding: " + (m->masterMeterSub.has_value() ? "connected" : "disconnected (RAII reset)");
m->summary.set(std::move(s));
};
std::weak_ptr<UI::Slider> sliderW,
std::weak_ptr<UI::Text> pctW) {
model->keepSub(prop.
onChanged([sliderW, pctW, recomputeSummary](
float v) {
if (auto s = sliderW.lock())
s->setValue(v, false);
if (auto p = pctW.lock())
p->setText(fmtPct(v));
recomputeSummary();
}, true));
};
bindChannel(model->master, masterSlider, masterPct);
bindChannel(model->music, musicSlider, musicPct);
bindChannel(model->sfx, sfxSlider, sfxPct);
std::weak_ptr<UI::Toggle> muteW = muteToggle;
model->keepSub(model->muted.onChanged([muteW, recomputeSummary](bool isMuted) {
if (auto t = muteW.lock())
t->setChecked(isMuted);
recomputeSummary();
}, true));
ui.onValueChangedRef("masterSlider", [m = model.get()](float v) { m->master.set(v); });
ui.onValueChangedRef("musicSlider", [m = model.get()](float v) { m->music.set(v); });
ui.onValueChangedRef("sfxSlider", [m = model.get()](float v) { m->sfx.set(v); });
ui.onToggleRef("muteToggle", [m = model.get()](bool on) { m->muted.set(on); });
ui.onClickRef("presetBalanced", [m = model.get()]() {
m->muted.set(false);
m->master.set(0.80f);
m->music.set(0.65f);
m->sfx.set(0.90f);
});
ui.onClickRef("presetCinema", [m = model.get()]() {
m->muted.set(false);
m->master.set(1.00f);
m->music.set(0.95f);
m->sfx.set(0.85f);
});
ui.onClickRef("presetNight", [m = model.get()]() {
m->muted.set(false);
m->master.set(0.45f);
m->music.set(0.40f);
m->sfx.set(0.35f);
});
ui.onClickRef("presetMute", [m = model.get()]() {
m->muted.set(true);
});
std::weak_ptr<UI::ProgressBar> masterBarW = masterBar;
std::weak_ptr<UI::Button> toggleBtnW = toggleSubBtn;
ui.onClickRef("toggleSub", [m = model.get(), masterBarW, toggleBtnW, recomputeSummary]() {
if (m->masterMeterSub.has_value())
{
m->masterMeterSub.reset();
if (auto b = toggleBtnW.lock())
b->setText("Reconnect Master Meter");
}
else if (auto bar = masterBarW.lock())
{
m->masterMeterSub.emplace(m->master.bindTo(bar, &UI::ProgressBar::setValue, true));
if (auto b = toggleBtnW.lock())
b->setText("Disconnect Master Meter");
}
recomputeSummary();
});
return ui.root();
});
}
Observable single-value container.
Definition Bindable.hpp:44
Subscription onChanged(std::function< void(const T &)> cb, bool fireImmediately=false)
Register a callback fired on value changes.
Definition Bindable.hpp:176
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 value-in-[0, 1] progress indicator.
Definition ProgressBar.hpp:29
void setValue(float v)
Set the value, clamped to [0, 1].
A [0, 1] slider with keyboard and pointer control.
Definition Slider.hpp:29
A read-only text node.
Definition Text.hpp:32
void setText(std::string new_text)
Replace the content.
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