Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_static.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/ui_scene_resource.hpp>
6
7#include <array>
8
10namespace portals::ui::ct {
15template <std::size_t N> struct Literal {
16 std::array<char, N> bytes{};
17
20 consteval Literal(const char (&value)[N]) {
21 static_assert(N > 0, "Static scene text requires a terminated array");
22
23 std::copy_n(value, N, bytes.begin());
24
25 if (bytes.back() != '\0') {
26 throw "Static scene text requires a terminated array";
27 }
28 }
29
31 constexpr std::string_view view() const noexcept {
32 return {bytes.data(), N - 1};
33 }
34};
35
36namespace detail {
37// MSVC 19.44 cannot materialize an immediate class result containing optional's union.
38// Keep its value and presence separately until the runtime-facing style is requested.
45 bool bold;
46 bool italic;
47 bool underline;
48
50 constexpr explicit PackedCellStyle(CellStyle style)
51 : foreground(style.foreground.value_or(RgbColor{})), background(style.background.value_or(RgbColor{})),
52 hasForeground(style.foreground.has_value()), hasBackground(style.background.has_value()), bold(style.bold),
53 italic(style.italic), underline(style.underline) {
54 }
55
57 constexpr CellStyle value() const {
58 CellStyle result{.foreground = {}, .background = {}, .bold = bold, .italic = italic, .underline = underline};
59
60 if (hasForeground) {
61 result.foreground = foreground;
62 }
63
64 if (hasBackground) {
65 result.background = background;
66 }
67
68 return result;
69 }
70};
71
77
79 constexpr explicit PackedControlStyle(ControlStyle style)
80 : normal(style.normal), focused(style.focused), disabled(style.disabled) {
81 }
82
84 constexpr ControlStyle value() const {
85 return {normal.value(), focused.value(), disabled.value()};
86 }
87};
88
89// Named non-constexpr boundaries give both MSVC and GCC useful rejection diagnostics.
91void staticSceneInvalidLinearOptions();
93void staticSceneInvalidSizePolicy();
95void staticSceneInvalidEditMode();
96
100constexpr bool validAxis(AxisSize axis) {
101 return axis.minimum >= 0 && axis.maximum >= axis.minimum && axis.preferred >= INTRINSIC_EXTENT && axis.weight > 0 &&
102 (axis.mode == SizeMode::AUTOMATIC || axis.mode == SizeMode::FIXED || axis.mode == SizeMode::FLEX ||
103 axis.mode == SizeMode::CONTENT);
104}
105
109constexpr bool validAlignment(LayoutAlign alignment) {
110 return alignment == LayoutAlign::START || alignment == LayoutAlign::CENTER || alignment == LayoutAlign::END ||
111 alignment == LayoutAlign::STRETCH;
112}
113
117constexpr bool validLinear(LinearOptions options) {
118 return options.gap >= 0 && options.padding.left >= 0 && options.padding.top >= 0 && options.padding.right >= 0 &&
119 options.padding.bottom >= 0 && validAlignment(options.main_align) && validAlignment(options.cross_align) &&
121}
122
123struct Access;
124} // namespace detail
125
133template <class T, Literal Id, std::size_t N, class... Children> class Element {
134public:
135 using WidgetType = T;
136 static constexpr auto ID = Id;
137 static constexpr std::size_t NODE_COUNT = 1 + (Children::NODE_COUNT + ... + 0);
138
140 constexpr std::string_view text() const noexcept {
141 return text_.view();
142 }
143
145 constexpr ControlStyle style() const {
146 return style_.value();
147 }
148
150 constexpr SizePolicy sizePolicy() const {
151 return policy_;
152 }
153
155 constexpr LinearOptions linearOptions() const {
156 return options_;
157 }
158
160 constexpr EditMode editMode() const {
161 return mode_;
162 }
163
165 constexpr const std::tuple<Children...> &children() const noexcept {
166 return children_;
167 }
168
169private:
170 friend struct detail::Access;
171
172 consteval Element(Literal<N> text, ControlStyle style, SizePolicy policy, LinearOptions options, EditMode mode,
173 Children... children)
174 : text_(text), style_(style), policy_(policy), options_(options), mode_(mode), children_(children...) {
175 if (!detail::validAxis(policy.width) || !detail::validAxis(policy.height)) {
176 detail::staticSceneInvalidSizePolicy();
177 }
178
179 if (!detail::validLinear(options)) {
180 detail::staticSceneInvalidLinearOptions();
181 }
182
183 if (mode != EditMode::IMMEDIATE && mode != EditMode::ON_COMPLETION && mode != EditMode::EXPLICIT) {
184 detail::staticSceneInvalidEditMode();
185 }
186 }
187
188 Literal<N> text_;
189 detail::PackedControlStyle style_;
190 SizePolicy policy_;
191 LinearOptions options_;
192 EditMode mode_;
193 std::tuple<Children...> children_;
194};
195
196namespace detail {
200template <class T> struct IsElement : std::false_type {};
201
208template <class T, Literal Id, std::size_t N, class... Children>
209struct IsElement<Element<T, Id, N, Children...>> : std::true_type {};
210
214template <class T>
216
220template <StaticElement Node> consteval auto identities() {
221 std::array<std::string_view, Node::NODE_COUNT> result{};
222 std::size_t index{};
223
224 result[index++] = Node::ID.view();
225 if constexpr (Node::NODE_COUNT > 1) {
226 []<class... Children>(std::tuple<Children...> *, auto &values, auto &offset) {
227 (
228 [&] {
229 for (auto id : identities<Children>()) {
230 values[offset++] = id;
231 }
232 }(),
233 ...);
234 }(static_cast<std::remove_cvref_t<decltype(std::declval<Node>().children())> *>(nullptr), result, index);
235 }
236
237 return result;
238}
239
243template <StaticElement Node> consteval bool uniqueIds() {
244 constexpr auto IDS = identities<Node>();
245
246 for (std::size_t i = 0; i < IDS.size(); ++i) {
247 for (std::size_t j = i + 1; j < IDS.size(); ++j) {
248 if (!IDS[i].empty() && IDS[i] == IDS[j]) {
249 return false;
250 }
251 }
252 }
253
254 return true;
255}
256
261template <Literal Id, StaticElement Node> consteval std::size_t idCount() {
262 constexpr auto IDS = identities<Node>();
263
264 return Id.view().empty() ? 0 : std::ranges::count(IDS, Id.view());
265}
266} // namespace detail
267
271template <detail::StaticElement Root> class StaticScene {
272public:
274 constexpr const Root &root() const noexcept {
275 return root_;
276 }
277
278private:
279 friend struct detail::Access;
280
281 consteval explicit StaticScene(Root root) : root_(root) {
282 static_assert(detail::uniqueIds<Root>(), "Static scene duplicate nonempty ID");
283 }
284
285 Root root_;
286};
287
288namespace detail {
290struct Access {
303 template <class T, Literal Id, std::size_t N, StaticElement... Children>
304 static consteval auto element(const char (&text)[N], ControlStyle style, SizePolicy policy, LinearOptions options,
305 EditMode mode, Children... children) {
306 return Element<T, Id, N, Children...>(Literal(text), style, policy, options, mode, children...);
307 }
308
313 template <StaticElement Root> static consteval auto scene(Root root) {
314 return StaticScene<Root>(root);
315 }
316};
317} // namespace detail
318
326template <Literal Id = "", std::size_t N>
327consteval auto label(const char (&text)[N], ControlStyle style = ControlStyle{},
329 return detail::Access::element<Label, Id>(text, style, policy, {}, EditMode::IMMEDIATE);
330}
331
339template <Literal Id = "", std::size_t N>
340consteval auto button(const char (&text)[N], ControlStyle style = ControlStyle{},
342 return detail::Access::element<Button, Id>(text, style, policy, {}, EditMode::IMMEDIATE);
343}
344
353template <Literal Id = "", std::size_t N>
354consteval auto lineEdit(const char (&initial)[N], EditMode mode = EditMode::IMMEDIATE,
356 return detail::Access::element<LineEdit, Id>(initial, style, policy, {}, mode);
357}
358
365template <Literal Id = "", detail::StaticElement... Children>
366consteval auto row(LinearOptions options, Children... children) {
367 return detail::Access::element<Row, Id>("", {}, {}, options, EditMode::IMMEDIATE, children...);
368}
369
375template <Literal Id = "", detail::StaticElement... Children> consteval auto row(Children... children) {
376 return row<Id>(LinearOptions{}, children...);
377}
378
385template <Literal Id = "", detail::StaticElement... Children>
386consteval auto column(LinearOptions options, Children... children) {
387 return detail::Access::element<Column, Id>("", {}, {}, options, EditMode::IMMEDIATE, children...);
388}
389
395template <Literal Id = "", detail::StaticElement... Children> consteval auto column(Children... children) {
396 return column<Id>(LinearOptions{}, children...);
397}
398
403template <detail::StaticElement Root> consteval auto scene(Root root) {
404 return detail::Access::scene(root);
405}
406
407namespace detail {
409enum class AdapterKind {
410 REFERENCE,
411 CLICK,
412 TEXT_BINDING
413};
414
419template <Literal Id, class T> struct Reference {
420 static constexpr auto ID = Id;
421 static constexpr auto KIND = AdapterKind::REFERENCE;
423
428 template <class U> auto apply(Description<U> recipe) {
429 static_assert(std::same_as<T, U>, "Static scene reference widget type mismatch");
430
431 if constexpr (std::same_as<T, U>) {
432 return std::move(recipe).reference(std::move(value));
433 }
434 }
435};
436
441template <Literal Id, class Callback> struct Click {
442 static constexpr auto ID = Id;
443 static constexpr auto KIND = AdapterKind::CLICK;
444 Callback value;
445
450 template <class T> auto apply(Description<T> recipe) {
451 static_assert(std::same_as<T, Button>, "Static scene onClick requires Button");
452
453 if constexpr (std::same_as<T, Button>) {
454 return std::move(recipe).onClick(std::move(value));
455 }
456 }
457};
458
462template <Literal Id> struct TextBinding {
463 static constexpr auto ID = Id;
464 static constexpr auto KIND = AdapterKind::TEXT_BINDING;
466};
467
471template <class T> struct IsAdapter : std::false_type {};
472
476template <Literal Id, class T> struct IsAdapter<Reference<Id, T>> : std::true_type {};
477
481template <Literal Id, class Callback> struct IsAdapter<Click<Id, Callback>> : std::true_type {};
482
484template <Literal Id> struct IsAdapter<TextBinding<Id>> : std::true_type {};
485
489template <class T>
491
495template <RuntimeAdapter... Adapters> consteval bool uniqueAdapters() {
496 constexpr std::array<std::string_view, sizeof...(Adapters)> IDS{Adapters::ID.view()...};
497 constexpr std::array<AdapterKind, sizeof...(Adapters)> KINDS{Adapters::KIND...};
498
499 for (std::size_t i = 0; i < IDS.size(); ++i) {
500 for (std::size_t j = i + 1; j < IDS.size(); ++j) {
501 if (IDS[i] == IDS[j] && KINDS[i] == KINDS[j]) {
502 return false;
503 }
504 }
505 }
506
507 return true;
508}
509
516template <StaticElement Node, RuntimeAdapter... Adapters> auto describeNode(const Node &node, Adapters &...adapters) {
517 using T = typename Node::WidgetType;
518
519 auto recipe = [&]() -> Description<T> {
520 if constexpr (std::same_as<T, Row> || std::same_as<T, Column>) {
521 return std::apply(
522 [&](const auto &...children) {
523 return ui::linear<T>(node.linearOptions(), describeNode(children, adapters...)...);
524 },
525 node.children());
526 } else if constexpr (std::same_as<T, LineEdit>) {
527 LineEditConfig config;
528 std::optional<ManagedState<std::string>> state;
529
530 config.options.editMode = node.editMode();
531 config.options.style = node.style();
532 config.policy = node.sizePolicy();
533 (
534 [&] {
535 if constexpr (Adapters::KIND == AdapterKind::TEXT_BINDING && Adapters::ID.view() == Node::ID.view()) {
536 state = adapters.value;
537 }
538 }(),
539 ...);
540
541 return state ? ui::lineEdit(*state, std::move(config)) : ui::lineEdit(node.text(), std::move(config));
542 } else {
543 return ui::control<T>(node.text(), node.style(), node.sizePolicy());
544 }
545 }();
546
547 if constexpr (!Node::ID.view().empty()) {
548 recipe = std::move(recipe).id(Node::ID.view());
549 }
550
551 (
552 [&] {
553 if constexpr (Adapters::ID.view() == Node::ID.view()) {
554 if constexpr (Adapters::KIND == AdapterKind::TEXT_BINDING) {
555 static_assert(std::same_as<T, LineEdit>, "Static scene bind requires LineEdit");
556 } else {
557 recipe = adapters.apply(std::move(recipe));
558 }
559 }
560 }(),
561 ...);
562
563 return recipe;
564}
565} // namespace detail
566
572template <Literal Id, class T> auto reference(ui::Ref<T> value) {
573 return detail::Reference<Id, T>{std::move(value)};
574}
575
581template <Literal Id, class Callback>
583auto onClick(Callback value) {
584 return detail::Click<Id, Callback>{std::move(value)};
585}
586
591template <Literal Id> auto bind(ManagedState<std::string> value) {
592 return detail::TextBinding<Id>{value};
593}
594
604template <detail::StaticElement Root, detail::RuntimeAdapter... Adapters>
605auto describe(const StaticScene<Root> &value, Adapters... adapters) {
606 static_assert(((detail::idCount<Adapters::ID, Root>() == 1) && ...),
607 "Static scene runtime adapter requires a known nonempty ID");
608 static_assert(detail::uniqueAdapters<Adapters...>(), "Static scene duplicate runtime adapter kind and ID");
609
610 return detail::describeNode(value.root(), adapters...);
611}
612} // namespace portals::ui::ct
Weak UI handle to a scene-owned Property, never an owning alias. Copies and destruction of the handle...
Definition managed.hpp:367
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
Single-use owning construction recipe; no mounted widget ownership.
Definition ui_description.hpp:48
Typed Widget observation; explicit composite specializations reuse their existing weak facades.
Definition ui_ref.hpp:761
Immutable-through-accessors static node; no live widgets, models, slots or callbacks.
Definition ui_static.hpp:133
static constexpr std::size_t NODE_COUNT
Complete subtree node count.
Definition ui_static.hpp:137
constexpr SizePolicy sizePolicy() const
Definition ui_static.hpp:150
constexpr std::string_view text() const noexcept
Definition ui_static.hpp:140
constexpr const std::tuple< Children... > & children() const noexcept
Definition ui_static.hpp:165
T WidgetType
Existing widget type instantiated only at runtime mount.
Definition ui_static.hpp:135
static constexpr auto ID
Owning static identity; empty means unnamed.
Definition ui_static.hpp:136
constexpr LinearOptions linearOptions() const
Definition ui_static.hpp:155
constexpr ControlStyle style() const
Definition ui_static.hpp:145
constexpr EditMode editMode() const
Definition ui_static.hpp:160
Validated static scene value; runtime recipes and mounted ownership remain separate.
Definition ui_static.hpp:271
constexpr const Root & root() const noexcept
Definition ui_static.hpp:274
A compact action optionally receives owning event parameters.
Definition managed.hpp:239
Admit exact factory-produced runtime adapter types.
Definition ui_static.hpp:490
Admit only static elements produced by this header's factories.
Definition ui_static.hpp:215
Compile-time descriptions translated into the existing owning UI recipes at runtime.
Definition ui_static.hpp:10
consteval auto column(LinearOptions options, Children... children)
Definition ui_static.hpp:386
consteval auto lineEdit(const char(&initial)[N], EditMode mode=EditMode::IMMEDIATE, ControlStyle style=ControlStyle{}, SizePolicy policy=DEFAULT_EDITOR_POLICY)
Definition ui_static.hpp:354
auto reference(ui::Ref< T > value)
Definition ui_static.hpp:572
auto onClick(Callback value)
Definition ui_static.hpp:583
auto bind(ManagedState< std::string > value)
Definition ui_static.hpp:591
consteval auto row(LinearOptions options, Children... children)
Definition ui_static.hpp:366
auto describe(const StaticScene< Root > &value, Adapters... adapters)
Definition ui_static.hpp:605
consteval auto label(const char(&text)[N], ControlStyle style=ControlStyle{}, SizePolicy policy=DEFAULT_CONTENT_POLICY)
Definition ui_static.hpp:327
consteval auto button(const char(&text)[N], ControlStyle style=ControlStyle{}, SizePolicy policy=DEFAULT_CONTENT_POLICY)
Definition ui_static.hpp:340
consteval auto scene(Root root)
Definition ui_static.hpp:403
constexpr SizePolicy DEFAULT_EDITOR_POLICY
Single-line editors retain one cell even during shortage; text length is not a flex basis.
Definition layout.hpp:50
@ AUTOMATIC
Use the preferred/intrinsic extent; shrink towards minimum when space is scarce.
Definition layout.hpp:13
@ CONTENT
Prefer intrinsic size, shrink towards minimum, and never grow to consume spare cells.
Definition layout.hpp:16
@ FIXED
Keep the bounded preferred/intrinsic extent even when it overflows the available space.
Definition layout.hpp:14
@ FLEX
Share spare main-axis cells by weight; allow shrinking towards minimum.
Definition layout.hpp:15
constexpr SizePolicy DEFAULT_CONTENT_POLICY
Intrinsic content on both axes; explicit policies remain authoritative.
Definition layout.hpp:44
constexpr std::int32_t INTRINSIC_EXTENT
Sentinel requesting the widget's intrinsic measurement.
Definition layout.hpp:20
@ CLIP
Preserve hard minima and fixed extents; clip content that does not fit the viewport.
Definition layout.hpp:181
LayoutAlign
Alignment; center assigns floor(free/2) before the child/group.
Definition layout.hpp:172
@ STRETCH
Fill available cross-axis space within size bounds; acts as START on the linear main axis.
Definition layout.hpp:176
@ START
Place at the leading edge of the available space.
Definition layout.hpp:173
@ END
Place at the trailing edge of the available space.
Definition layout.hpp:175
@ CENTER
Center within spare space, rounding the leading offset down to whole cells.
Definition layout.hpp:174
EditMode
When an accepted editing candidate is validated and published to its model.
Definition line_edit.hpp:27
@ ON_COMPLETION
Keep a draft until Enter, programmatic activation or focus loss.
Definition line_edit.hpp:29
@ EXPLICIT
Keep a draft until Enter or programmatic activation; focus loss preserves it.
Definition line_edit.hpp:30
@ IMMEDIATE
Publish each semantic-valid text change; invalid intermediate text remains visible.
Definition line_edit.hpp:28
Owning per-axis bounds; preferred=-1 or CONTENT uses intrinsic measurement.
Definition layout.hpp:23
Small owning paint value, independent of themes and text metrics.
Definition cell_buffer.hpp:35
std::optional< RgbColor > foreground
Foreground RGB, or the terminal default when absent.
Definition cell_buffer.hpp:37
std::optional< RgbColor > background
Background RGB, or the terminal default when absent.
Definition cell_buffer.hpp:39
Owning paint values; no theme engine or behavior is encoded here.
Definition controls.hpp:24
std::int32_t bottom
Trailing rows.
Definition layout.hpp:189
std::int32_t left
Leading columns.
Definition layout.hpp:186
std::int32_t right
Trailing columns.
Definition layout.hpp:188
std::int32_t top
Leading rows.
Definition layout.hpp:187
ControlStyle style
Normal, focused and disabled appearance.
Definition line_edit.hpp:178
EditMode editMode
Confirmation policy shared by input, paste and fluent text edits.
Definition line_edit.hpp:181
Owning linear container options.
Definition layout.hpp:193
LayoutPadding padding
Insets, retained during shortage.
Definition layout.hpp:195
std::int32_t gap
Cells between visible children only.
Definition layout.hpp:194
LayoutAlign cross_align
Child alignment across the linear direction.
Definition layout.hpp:197
LayoutAlign main_align
Group alignment after allocation; stretch is start on this axis.
Definition layout.hpp:196
LayoutOverflow overflow
Explicit shortage policy.
Definition layout.hpp:198
Owning RGB color; terminal encoding belongs to the output adapter.
Definition cell_buffer.hpp:20
Owning independent policies for width and height.
Definition layout.hpp:35
AxisSize width
Horizontal policy.
Definition layout.hpp:36
AxisSize height
Vertical policy.
Definition layout.hpp:37
consteval Literal(const char(&value)[N])
Definition ui_static.hpp:20
Owning optional editor customization and confirmation policy.
Definition ui_description.hpp:34
LineEditOptions options
Editor limits and appearances; scene width policy overrides options.widthPolicy.
Definition ui_description.hpp:36
SizePolicy policy
Owning layout constraints validated by the existing layout engine.
Definition ui_description.hpp:37
Owning fixed-size bytes usable as a structural template argument.
Definition ui_static.hpp:15
std::array< char, N > bytes
Owned bytes, including the terminator.
Definition ui_static.hpp:16
constexpr std::string_view view() const noexcept
Definition ui_static.hpp:31
consteval Literal(const char(&value)[N])
Definition ui_static.hpp:20
Internal construction bridge; factory values cannot bypass constant validation.
Definition ui_static.hpp:290
static consteval auto scene(Root root)
Definition ui_static.hpp:313
static consteval auto element(const char(&text)[N], ControlStyle style, SizePolicy policy, LinearOptions options, EditMode mode, Children... children)
Definition ui_static.hpp:304
Runtime callback storage; captures transfer to the existing managed binding.
Definition ui_static.hpp:441
static constexpr auto ID
Required static target identity.
Definition ui_static.hpp:442
auto apply(Description< T > recipe)
Definition ui_static.hpp:450
Callback value
Owning activation action, never evaluated during constant construction.
Definition ui_static.hpp:444
static constexpr auto KIND
Duplicate checking category.
Definition ui_static.hpp:443
Admit only this header's explicit runtime adapter values.
Definition ui_static.hpp:471
Recognize exact factory-produced static elements.
Definition ui_static.hpp:200
Union-free owning snapshot of one compile-time cell appearance.
Definition ui_static.hpp:40
bool bold
Explicit bold appearance.
Definition ui_static.hpp:45
constexpr CellStyle value() const
Definition ui_static.hpp:57
bool hasForeground
Whether the foreground is an explicit RGB value.
Definition ui_static.hpp:43
RgbColor foreground
Foreground value; ignored when absent.
Definition ui_static.hpp:41
constexpr PackedCellStyle(CellStyle style)
Definition ui_static.hpp:50
bool underline
Explicit underline appearance.
Definition ui_static.hpp:47
RgbColor background
Background value; ignored when absent.
Definition ui_static.hpp:42
bool italic
Explicit italic appearance.
Definition ui_static.hpp:46
bool hasBackground
Whether the background is an explicit RGB value.
Definition ui_static.hpp:44
constexpr ControlStyle value() const
Definition ui_static.hpp:84
PackedCellStyle disabled
Disabled appearance.
Definition ui_static.hpp:76
PackedCellStyle focused
Focused appearance.
Definition ui_static.hpp:75
PackedCellStyle normal
Ordinary appearance.
Definition ui_static.hpp:74
constexpr PackedControlStyle(ControlStyle style)
Definition ui_static.hpp:79
Runtime reference storage, excluded from constant construction.
Definition ui_static.hpp:419
auto apply(Description< U > recipe)
Definition ui_static.hpp:428
ui::Ref< T > value
Shared observing slot; owns neither widget nor scene.
Definition ui_static.hpp:422
static constexpr auto ID
Required static target identity.
Definition ui_static.hpp:420
static constexpr auto KIND
Duplicate checking category.
Definition ui_static.hpp:421
Runtime model observation; no alternate property owner or reactive system.
Definition ui_static.hpp:462
static constexpr auto ID
Required static target identity.
Definition ui_static.hpp:463
ManagedState< std::string > value
Observation belonging to the receiving ManagedScene.
Definition ui_static.hpp:465
static constexpr auto KIND
Duplicate checking category.
Definition ui_static.hpp:464