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

Example from examples/10_overlays_cursors/.

Example from examples/10_overlays_cursors/.

#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;
}
.root {
width: 100%;
height: 100%;
background: #0D1017;
}
.page {
width: 100%;
height: 100%;
padding: 24px;
overflow-x: hidden;
overflow-y: auto;
}
.shell {
width: 100%;
max-width: 820px;
margin: auto;
gap: 18px;
}
.title {
font: inter-bold;
font-size: 24px;
color: #F8FAFC;
}
.section-title {
font: inter-semibold;
font-size: 15px;
color: #F1F5F9;
}
.muted {
font-size: 13px;
color: #94A3B8;
}
.card {
width: 100%;
background: #151924;
border-width: 1px;
border-color: #262E42;
radius: 12px;
padding: 18px;
gap: 12px;
}
.btn-row {
width: 100%;
gap: 10px;
wrap: true;
align-items: center;
}
Button.btn {
padding: 9px 15px;
radius: 8px;
background: #2563EB;
color: #FFFFFF;
cursor: pointer;
transition-time: 0.12;
}
Button.btn:hover {
background: #3B82F6;
}
Button.btn-ghost {
padding: 9px 15px;
radius: 8px;
background: #1F2638;
border-width: 1px;
border-color: #334155;
color: #E2E8F0;
cursor: pointer;
transition-time: 0.12;
}
Button.btn-ghost:hover {
background: #2A344D;
border-color: #38BDF8;
}
Button.btn-danger {
padding: 9px 15px;
radius: 8px;
background: #B91C1C;
color: #FFFFFF;
cursor: pointer;
transition-time: 0.12;
}
Button.btn-danger:hover {
background: #DC2626;
}
.status-bar {
width: 100%;
padding: 11px 14px;
radius: 8px;
background: #10141F;
border-left: 3px;
border-color: #38BDF8;
color: #CBD5E1;
font-size: 13px;
}
/* Modal content panel */
.modal-content {
width: 440px;
max-width: 90vw;
background: #171C28;
border-width: 1px;
border-color: #334155;
radius: 14px;
padding: 22px;
gap: 14px;
}
/* Popup & Tooltip portal styling */
Popup {
background: #181E2C;
border-width: 1px;
border-color: #334155;
radius: 10px;
padding: 8px;
}
Popup Button {
padding: 7px 12px;
radius: 6px;
background: transparent;
color: #E2E8F0;
cursor: pointer;
align-items: start;
}
Popup Button:hover {
background: #253047;
color: #FFFFFF;
}
.popover-body {
width: 260px;
padding: 8px;
gap: 8px;
}
Tooltip {
background: #0F172A;
border-width: 1px;
border-color: #38BDF8;
radius: 6px;
padding: 6px 10px;
color: #F8FAFC;
font-size: 12px;
}
/* System cursor gallery */
.cursor-grid {
width: 100%;
gap: 10px;
wrap: true;
}
.cursor-tile {
flex: 1;
min-width: 135px;
padding: 12px 14px;
radius: 9px;
background: #10141F;
border-width: 1px;
border-color: #252D40;
align-items: center;
gap: 3px;
transition-time: 0.1;
}
.cursor-tile:hover {
border-color: #38BDF8;
background: #161E30;
}
.cur-default { cursor: default; }
.cur-pointer { cursor: pointer; }
.cur-text { cursor: text; }
.cur-move { cursor: move; }
.cur-grab { cursor: grab; }
.cur-grabbing { cursor: grabbing; }
.cur-ew { cursor: ew-resize; }
.cur-ns { cursor: ns-resize; }
.cur-nesw { cursor: nesw-resize; }
.cur-nwse { cursor: nwse-resize; }
.cur-not-allowed { cursor: not-allowed; }
.cur-crosshair { cursor: crosshair; }
.cur-wait { cursor: wait; }
)zs";
static constexpr const char *kMarkup = R"zm(
Stack.root {
ScrollView.page {
VStack.shell {
VStack width="100%" gap="4px" {
Text.title wrap="true" "Portal Overlays & System Cursors"
Text.muted wrap="true" "Unclipped Modal, Popup, ContextMenu, hover Tooltips, and native OS cursor shapes."
}
VStack.card {
Text.section-title "1. Portal Overlays (Escape or outside click dismisses)"
Text.muted wrap="true" "Hover any button for 250 ms to inspect its portal Tooltip."
HStack.btn-row {
Button.btn@openModal "Open Modal Dialog"
Button.btn-ghost@openBelow "Popup Below"
Button.btn-ghost@openAbove "Popup Above"
Button.btn-ghost@openMenu "Context Menu"
Button.btn-ghost@openAtCursor "Popup at Pointer"
}
Text.status-bar@status wrap="true" "Ready — interact with any overlay trigger above."
}
VStack.card {
Text.section-title "2. Native System Cursors (CSS cursor:)"
Text.muted wrap="true" "Hover each tile to verify IPlatform::setCursor() deduplicated cursor switching."
HStack.cursor-grid {
VStack.cursor-tile.cur-default { Text "default" Text.muted "Arrow" }
VStack.cursor-tile.cur-pointer { Text "pointer" Text.muted "Hand" }
VStack.cursor-tile.cur-text { Text "text" Text.muted "I-Beam" }
VStack.cursor-tile.cur-move { Text "move" Text.muted "All-Dir" }
VStack.cursor-tile.cur-grab { Text "grab" Text.muted "Hand (fallback)" }
VStack.cursor-tile.cur-grabbing { Text "grabbing" Text.muted "Move (fallback)" }
VStack.cursor-tile.cur-ew { Text "ew-resize" Text.muted "Horizontal" }
VStack.cursor-tile.cur-ns { Text "ns-resize" Text.muted "Vertical" }
VStack.cursor-tile.cur-nesw { Text "nesw-resize" Text.muted "Diag NE-SW" }
VStack.cursor-tile.cur-nwse { Text "nwse-resize" Text.muted "Diag NW-SE" }
VStack.cursor-tile.cur-not-allowed { Text "not-allowed" Text.muted "Forbidden" }
VStack.cursor-tile.cur-crosshair { Text "crosshair" Text.muted "Precision" }
VStack.cursor-tile.cur-wait { Text "wait" Text.muted "Busy (SDL3)" }
}
}
}
}
Modal@confirmModal width="100%" height="100%" {
Text.section-title "Deploy Release 1.0.0?"
Text.muted wrap="true" "This Modal renders a full-screen backdrop, blocks background input, saves/restores focus, and closes on Escape."
HStack width="100%" justify="end" gap="10px" {
Button.btn-ghost@modalCancel "Cancel"
Button.btn-danger@modalConfirm "Confirm Deploy"
}
}
}
)zm";
int main()
{
cfg.title = "ZenitUI 10 — Overlays & System Cursors";
cfg.width = 920;
cfg.height = 680;
cfg.minWidth = 460;
cfg.minHeight = 460;
return ZenitExample::runApp(cfg, [](UIContext &ctx, IBackend &) -> std::shared_ptr<Layout> {
ZenitExample::loadDefaultFonts(ctx);
auto ui = ZMarkup::build(kMarkup, ctx);
ZMarkup::WeakUINode uiWeak = ui.weak();
auto setStatus = [uiWeak](const std::string &msg) {
if (auto locked = uiWeak.lock())
locked->setTextRef("status", msg);
};
// 1. Modal dialog
auto modal = ui.ref<UI::Modal>("confirmModal");
std::weak_ptr<UI::Modal> modalW = modal;
if (modal)
{
modal->onShow = [setStatus]() { setStatus("Modal opened (press Escape or click Cancel/Confirm)."); };
modal->onHide = [setStatus]() { setStatus("Modal closed and previous focus restored."); };
}
ui.onClickRef("openModal", [modalW]() {
if (auto m = modalW.lock())
m->show();
});
ui.onClickRef("modalCancel", [modalW]() {
if (auto m = modalW.lock())
m->hide();
});
ui.onClickRef("modalConfirm", [modalW, setStatus]() {
if (auto m = modalW.lock())
m->hide();
setStatus("Release 1.0.0 confirmed via Modal.");
});
// 2. Anchored Popover Popup
auto popover = UI::Popup::create();
std::weak_ptr<UI::Popup> popoverW = popover;
auto popBody = UI::VStack();
popBody->cls("popover-body");
auto popTitle = UI::Label("Anchored Portal Popup");
popTitle->cls("section-title");
auto popDesc = UI::Label("Automatically flips above/below when near a viewport edge and closes on outside click or Escape.");
popDesc->cls("muted");
popDesc->setWrap(true);
auto popClose = UI::Btn("Close Popover");
popClose->cls("btn");
popClose->onClick = [popoverW]() {
if (auto p = popoverW.lock())
p->close();
};
popBody->addChild(popTitle);
popBody->addChild(popDesc);
popBody->addChild(popClose);
popover->setContent(popBody);
popover->onClose = [setStatus]() { setStatus("Anchored Popup dismissed."); };
ui.root()->addChild(popover);
std::weak_ptr<Layout> btnBelowW = ui.ref("openBelow");
std::weak_ptr<Layout> btnAboveW = ui.ref("openAbove");
ui.onClickRef("openBelow", [popoverW, btnBelowW, setStatus]() {
if (auto p = popoverW.lock())
if (auto a = btnBelowW.lock())
{
p->openBelow(a);
setStatus("Popup opened below anchor.");
}
});
ui.onClickRef("openAbove", [popoverW, btnAboveW, setStatus]() {
if (auto p = popoverW.lock())
if (auto a = btnAboveW.lock())
{
p->openAbove(a);
setStatus("Popup opened above anchor (auto-flips if clipped).");
}
});
ui.onClickRef("openAtCursor", [&ctx, popoverW, setStatus]() {
if (auto p = popoverW.lock())
{
p->openAt(ctx.pointer.pos);
setStatus("Popup opened at pointer coordinates.");
}
});
// 3. ContextMenu factory
auto ctxMenu = UI::ContextMenu({
{"Duplicate Item", [setStatus]() { setStatus("ContextMenu action: Duplicate Item"); }},
{"Copy Path", [setStatus]() { setStatus("ContextMenu action: Copy Path"); }},
{"Inspect Layout", [setStatus]() { setStatus("ContextMenu action: Inspect Layout"); }},
{"Archive", [setStatus]() { setStatus("ContextMenu action: Archive"); }},
});
std::weak_ptr<UI::Popup> ctxMenuW = ctxMenu;
ui.root()->addChild(ctxMenu);
std::weak_ptr<Layout> btnMenuW = ui.ref("openMenu");
ui.onClickRef("openMenu", [ctxMenuW, btnMenuW, setStatus]() {
if (auto m = ctxMenuW.lock())
if (auto a = btnMenuW.lock())
{
m->openBelow(a);
setStatus("ContextMenu opened below button.");
}
});
// 4. Hover-delayed portal Tooltips
if (auto n = ui.ref("openModal"))
UI::Tooltip::attach(n, "Opens a blocking full-screen Modal dialog", 0.25f);
if (auto n = ui.ref("openBelow"))
UI::Tooltip::attach(n, "Anchors a portal Popup below this button", 0.25f);
if (auto n = ui.ref("openAbove"))
UI::Tooltip::attach(n, "Anchors a portal Popup above this button", 0.25f);
if (auto n = ui.ref("openMenu"))
UI::Tooltip::attach(n, "Opens a UI::ContextMenu portal list", 0.25f);
if (auto n = ui.ref("openAtCursor"))
UI::Tooltip::attach(n, "Opens the Popup at ctx.pointer.pos", 0.25f);
return ui.root();
});
}
Unified window, frame-loop, and subsystem host for interchangeable backends.
Definition Backend.hpp:49
static std::shared_ptr< Popup > create(Args &&...args)
Definition Layout.hpp:1410
A backdrop + content overlay.
Definition Modal.hpp:32
static std::shared_ptr< Tooltip > attach(std::shared_ptr< Layout > owner, std::string text, float showDelay=0.4f)
Attach a tooltip to owner.
Non-owning handle to a UINode, safe to capture inside child callbacks.
Definition ZMarkup.hpp:757
std::shared_ptr< Menu > ContextMenu(std::vector< MenuEntry > entries)
Convenience factory: create a structured context Menu from MenuEntry items.
std::shared_ptr< Text > Label(std::string t)
Convenience factory: a Text node.
Definition Text.hpp:88
std::shared_ptr< Layout > VStack(std::initializer_list< std::shared_ptr< Layout > > children={})
Create a vertical-flow container (LayoutType::Vertical).
Definition Layouts.hpp:27
std::shared_ptr< Button > Btn(std::string text, std::function< void()> cb=nullptr)
Convenience factory: a Button with a Text label and a click handler.
Definition Button.hpp:98
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