Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_controls.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/detail/ui_value_text.hpp>
6#include <portals/ui_construction.hpp>
7#include <portals/ui_model.hpp>
8
9namespace portals::ui {
10
18template <class T> Description<T> control(std::string_view text, ControlStyle style, SizePolicy policy) {
19 auto description =
20 detail::Access::cold<T>([style, policy](ManagedScene &scene, Node<Widget> parent, detail::MountContext &context) {
21 auto value = context.controlSettings->text->resolve(scene.widthPolicy());
22
23 if (!value) {
24 auto failure = detail::error(parent, TreeError::INVALID_ARGUMENT);
25 if (const auto *textError = std::get_if<TextError>(&value.error())) {
26 failure = detail::textError(parent, *textError);
27 } else {
28 failure.resource = std::get<TextResourceError>(value.error());
29 }
30
31 return std::expected<Node<T>, ManagedError>(std::unexpected(failure));
32 }
33
34 if constexpr (std::same_as<T, Button>) {
35 return scene.emplace<T>(parent, std::move(*value), style, policy,
37 } else {
38 return scene.emplace<T>(parent, std::move(*value), style, policy);
39 }
40 });
43 ControlSettings{.text = TextValue(text)});
44}
45
51inline Description<Label> label(std::string_view text, ControlStyle style = {},
53 return control<Label>(text, style, policy);
54}
55
56namespace detail {
58
63template <class Control>
64Description<Control> valueTextControl(ValueText source, ControlStyle style = {},
65 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
66 auto description =
67 detail::Access::cold<Control>([source = std::move(source), style, policy](
68 ManagedScene &scene, Node<Widget> parent,
69 detail::MountContext &context) -> std::expected<Node<Control>, ManagedError> {
70 auto empty = GraphemeText::from("", scene.widthPolicy());
71 if (!empty) {
72 return std::unexpected(detail::textError(parent, empty.error()));
73 }
74
75 auto node = [&] {
76 if constexpr (std::same_as<Control, Button>) {
77 return scene.emplace<Control>(parent, std::move(*empty), style, policy,
78 context.controlSettings->chrome.value_or(ButtonChrome::BRACKETS));
79 } else {
80 return scene.emplace<Control>(parent, std::move(*empty), style, policy);
81 }
82 }();
83 if (!node) {
84 return std::unexpected(node.error());
85 }
86
87 detail::ModelAccess::keep(scene, *node, context);
88
89 auto bound = detail::ValueTextAccess::bind(scene, *node, source, context);
90
91 return bound ? node : std::unexpected(bound.error());
92 });
93
94 return detail::Access::configuredControl(std::move(description), std::same_as<Control, Label> ? "Label" : "Button");
95}
96
98} // namespace detail
99
105template <class Source>
106 requires std::same_as<Source, ValueText>
107Description<Label> label(Source source, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
108 return detail::valueTextControl<Label>(std::move(source), style, policy);
109}
110
111namespace detail {
113template <class T>
114concept ValueCaption =
115 std::formattable<std::remove_cvref_t<T>, char> || std::same_as<std::remove_cvref_t<T>, GraphemeText>;
116
117template <class Projection, class T>
118concept ValueCaptionProjection =
119 std::copy_constructible<std::decay_t<Projection>> && std::invocable<Projection &, const T &> &&
120 (!portals::detail::IS_UNSUPPORTED_DERIVED_EXPECTED<
121 std::remove_cvref_t<std::invoke_result_t<Projection &, const T &>>>) &&
122 ValueCaption<portals::detail::DerivedOutput<Projection, T>>;
123
124template <class Control, std::equality_comparable T, class Projection>
125Description<Control> valueControl(Model<T> source, Projection projection, ControlStyle style, SizePolicy policy) {
126 auto description =
127 detail::Access::cold<Control>([source = std::move(source), projection = std::move(projection), style, policy](
128 ManagedScene &scene, Node<Widget> parent,
129 detail::MountContext &context) -> std::expected<Node<Control>, ManagedError> {
130 auto empty = GraphemeText::from("", scene.widthPolicy());
131 if (!empty) {
132 return std::unexpected(detail::textError(parent, empty.error()));
133 }
134
135 auto node = [&] {
136 if constexpr (std::same_as<Control, Button>) {
137 return scene.emplace<Control>(parent, std::move(*empty), style, policy,
138 context.controlSettings->chrome.value_or(ButtonChrome::BRACKETS));
139 } else {
140 return scene.emplace<Control>(parent, std::move(*empty), style, policy);
141 }
142 }();
143 if (!node) {
144 return std::unexpected(node.error());
145 }
146
147 detail::ModelAccess::keep(scene, *node, context);
148
149 const auto owner = detail::Access::lifetime(scene);
150 auto retained = std::make_shared<Projection>(std::move(projection));
151 if (owner.expired()) {
152 return std::unexpected(detail::error(*node, TreeError::CLOSED));
153 }
154
155 auto bound = detail::ValueModelAccess::bindCaption(scene, *node, source, retained, context);
156
157 return bound ? node : std::unexpected(bound.error());
158 });
159
160 return detail::Access::configuredControl(std::move(description), std::same_as<Control, Label> ? "Label" : "Button");
161}
162
164} // namespace detail
165
173template <std::equality_comparable T>
174 requires std::copy_constructible<T> && detail::ValueCaption<T>
175Description<Label> label(Model<T> source, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
176 return detail::valueControl<Label>(std::move(source), std::identity{}, style, policy);
177}
178
184template <std::equality_comparable T>
185 requires std::copy_constructible<T> && detail::ValueCaption<T>
186Description<Label> label(ModelValue<T> value, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
187 return label(detail::ValueModelAccess::root(value), style, policy);
188}
189
200template <std::equality_comparable T, class Projection>
201 requires std::copy_constructible<T> && detail::ValueCaptionProjection<Projection, T>
202Description<Label> label(Model<T> source, Projection projection, ControlStyle style = {},
203 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
204 return detail::valueControl<Label>(std::move(source), std::move(projection), style, policy);
205}
206
214template <std::equality_comparable T, class Member>
215 requires std::copy_constructible<T> && detail::ValueCaption<Member>
216Description<Label> label(ModelField<T, Member> field, ControlStyle style = {},
217 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
218 return label(detail::ValueModelAccess::root(field), detail::ValueModelAccess::member(field), style, policy);
219}
220
226template <class Source>
227 requires std::same_as<Source, ValueText>
228Description<Button> button(Source source, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
229 return detail::valueTextControl<Button>(std::move(source), style, policy);
230}
231
237template <std::equality_comparable T>
238 requires std::copy_constructible<T> && detail::ValueCaption<T>
239Description<Button> button(Model<T> source, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
240 return detail::valueControl<Button>(std::move(source), std::identity{}, style, policy);
241}
242
248template <std::equality_comparable T>
249 requires std::copy_constructible<T> && detail::ValueCaption<T>
250Description<Button> button(ModelValue<T> value, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
251 return button(detail::ValueModelAccess::root(value), style, policy);
252}
253
259template <std::equality_comparable T, class Projection>
260 requires std::copy_constructible<T> && detail::ValueCaptionProjection<Projection, T>
261Description<Button> button(Model<T> source, Projection projection, ControlStyle style = {},
262 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
263 return detail::valueControl<Button>(std::move(source), std::move(projection), style, policy);
264}
265
271template <std::equality_comparable T, class Member>
272 requires std::copy_constructible<T> && detail::ValueCaption<Member>
273Description<Button> button(ModelField<T, Member> field, ControlStyle style = {},
274 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
275 return button(detail::ValueModelAccess::root(field), detail::ValueModelAccess::member(field), style, policy);
276}
277
278namespace detail {
288template <class T>
289Description<T> textControl(TextValue text, ControlStyle style, SizePolicy policy, TextResolver resolver,
291 auto arguments = ControlSettings{.text = std::move(text)};
292 auto description = Access::cold<T>(
293 [style, policy, chrome, resolver = std::move(resolver)](
294 ManagedScene &scene, Node<Widget> parent, MountContext &context) -> std::expected<Node<T>, ManagedError> {
295 const auto &caption = *context.controlSettings->text;
296 auto value = resolver ? caption.resolve(scene.widthPolicy(), resolver) : caption.resolve(scene.widthPolicy());
297
298 if (!value) {
299 if (const auto *failure = std::get_if<TextError>(&value.error())) {
300 return std::unexpected(textError(parent, *failure));
301 }
302
303 auto failure = error(parent, TreeError::INVALID_ARGUMENT);
304
305 failure.resource = std::get<TextResourceError>(value.error());
306
307 return std::unexpected(failure);
308 }
309
310 if constexpr (std::same_as<T, Button>) {
311 return scene.emplace<T>(parent, std::move(*value), style, policy,
312 context.controlSettings->chrome.value_or(chrome));
313 } else {
314 return scene.emplace<T>(parent, std::move(*value), style, policy);
315 }
316 });
318 std::move(arguments));
319}
321template <class T>
323 std::same_as<std::remove_cvref_t<T>, TextSource> || std::same_as<std::remove_cvref_t<T>, TextProperty>;
324
325template <class T> Description<T> liveCaption(TextSource source, ControlStyle style, SizePolicy policy) {
326 auto description = Access::cold<T>(
327 [source = std::move(source), style, policy](ManagedScene &scene, Node<Widget> parent,
328 MountContext &context) -> std::expected<Node<T>, ManagedError> {
329 auto admitted = Access::check(scene, parent);
330 if (!admitted) {
331 return std::unexpected(admitted.error());
332 }
333 auto graph = std::make_shared<Access::ColdGraph>();
334 graph->scene = &scene;
335 context.rollback.push_back([graph] {
336 graph->retire();
337 });
338 graph->pending = true;
339 Access::beginCold(scene);
340 auto empty = GraphemeText::from("", scene.widthPolicy());
341 if (!empty) {
342 return std::unexpected(textError(parent, empty.error()));
343 }
344 auto node = [&] {
345 if constexpr (std::same_as<T, Button>) {
346 return scene.emplace<T>(parent, std::move(*empty), style, policy,
347 context.controlSettings->chrome.value_or(ButtonChrome::BRACKETS));
348 } else {
349 return scene.emplace<T>(parent, std::move(*empty), style, policy);
350 }
351 }();
352 if (!node) {
353 return node;
354 }
355 if (context.root == Node<Widget>{}) {
356 context.root = *node;
357 }
358 context.captions.push_back(
359 {*node, Access::captionIdentity(source), false, [&scene, &context, node = *node, source, graph] {
360 return Access::completeCaption(scene, node, source, context, graph);
361 }});
362 context.finalize.push_back([&context] {
363 return Access::finishCaptions(context);
364 });
365 return node;
366 });
367 return Access::configuredControl(std::move(description), Access::controlType<T>());
368}
369
370template <CaptionSource Source> TextSource captionSource(Source &&source) {
371 if constexpr (std::same_as<std::remove_cvref_t<Source>, TextProperty>) {
372 return source.source();
373 } else {
374 return std::forward<Source>(source);
375 }
376}
377} // namespace detail
378
385template <detail::CaptionSource Source>
386Description<Label> label(Source &&source, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
387 return detail::liveCaption<Label>(detail::captionSource(std::forward<Source>(source)), style, policy);
388}
389
396template <detail::CaptionSource Source>
397Description<Button> button(Source &&source, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
398 return detail::liveCaption<Button>(detail::captionSource(std::forward<Source>(source)), style, policy);
399}
400
407inline Description<Label> label(TextValue text, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY,
408 TextResolver resolver = {}) {
409 return detail::textControl<Label>(std::move(text), style, policy, std::move(resolver));
410}
411
418template <class State>
419 requires(std::same_as<std::remove_cvref_t<State>, ManagedState<std::string>> ||
420 std::same_as<std::remove_cvref_t<State>, ManagedSource<std::string>>)
421inline Description<Label> label(State &&model, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
422 return control<Label>("", style, policy).setup([model](ManagedScene &scene, Node<Label> node) {
423 return scene.bindText(node, model);
424 });
425}
426
433inline Description<Button> button(TextValue text, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY,
434 TextResolver resolver = {}) {
435 return detail::textControl<Button>(std::move(text), style, policy, std::move(resolver));
436}
437
444template <class State>
445 requires(std::same_as<std::remove_cvref_t<State>, ManagedState<std::string>> ||
446 std::same_as<std::remove_cvref_t<State>, ManagedSource<std::string>>)
447inline Description<Button> button(State &&model, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
448 return control<Button>("", style, policy).setup([model](ManagedScene &scene, Node<Button> node) {
449 return scene.bindText(node, model);
450 });
451}
452
461template <class Caption, class Callback>
462 requires((std::same_as<std::remove_cvref_t<Caption>, TextValue> ||
463 std::same_as<std::remove_cvref_t<Caption>, ManagedState<std::string>> ||
464 std::same_as<std::remove_cvref_t<Caption>, ManagedSource<std::string>> ||
465 portals::detail::IS_LIVE_TEXT_SOURCE<std::remove_cvref_t<Caption>> ||
466 std::same_as<std::remove_cvref_t<Caption>, ValueText> || detail::CaptionSource<Caption>) &&
467 portals::detail::ManagedAction<Callback, ClickEvent>)
468inline Description<Button> button(Caption &&text, Callback action, ControlStyle style = {},
469 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
470 return button(std::forward<Caption>(text), style, policy).onClick(std::move(action));
471}
472
479inline Description<Button> button(std::string_view text, std::function<std::expected<void, TreeError>()> action,
480 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
481 return control<Button>(text, style, policy)
482 .setup([action = std::move(action)](ManagedScene &scene, Node<Button> node) mutable {
483 return scene.onClick(node, std::move(action));
484 });
485}
486
494template <class Callback>
495 requires portals::detail::ManagedAction<Callback, ClickEvent>
496inline Description<Button> button(std::string_view text, Callback action, ControlStyle style = {},
497 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
498 return control<Button>(text, style, policy)
499 .setup([action = std::move(action)](ManagedScene &scene, Node<Button> node) mutable {
500 return scene.onClick(node, std::move(action));
501 });
502}
503
509template <class Action>
510 requires std::same_as<Action, ActionRef>
511inline Description<Button> button(Action action, ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
512 return button(TextValue(""), style, policy).onClick(std::move(action));
513}
514
520template <class Action>
521 requires std::same_as<Action, ActionRef>
522inline Description<Button> button(Ref<Button> reference, Action action, ControlStyle style = {},
523 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
524 return button(std::move(action), style, policy).reference(std::move(reference));
525}
526
534inline Description<CheckBox> checkbox(std::string_view text, ManagedState<bool> model,
535 std::function<std::expected<void, TreeError>(CheckBoxResult)> changed = {},
536 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
538 detail::Access::controlArguments(control<CheckBox>(text, style, policy),
539 {.text = TextValue(text), .cycle = CheckCycle::TWO_STATE}),
540 [model,
541 changed = std::move(changed)](ManagedScene &scene, Node<CheckBox> node,
542 const ControlSettings &settings) mutable -> std::expected<void, ManagedError> {
543 if (settings.state == CheckState::MIXED || settings.cycle == CheckCycle::THREE_STATE) {
544 return std::unexpected(ManagedError{ManagedPhase::BIND, node.id(), TreeError::INVALID_ARGUMENT, {}, {}});
545 }
546
547 return scene.bind(node, model, std::move(changed));
548 });
549}
550
558inline Description<CheckBox> checkbox(std::string_view text, bool initial,
559 std::function<std::expected<void, TreeError>(CheckBoxResult)> changed = {},
560 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
562 detail::Access::controlArguments(control<CheckBox>(text, style, policy),
563 {.text = TextValue(text),
564 .state = initial ? CheckState::CHECKED : CheckState::UNCHECKED,
565 .cycle = CheckCycle::TWO_STATE}),
566 [initial, changed = std::move(changed)](ManagedScene &scene, Node<CheckBox> node,
567 const ControlSettings &settings) mutable {
568 if (settings.state == CheckState::MIXED || settings.cycle == CheckCycle::THREE_STATE) {
569 return std::expected<void, ManagedError>(std::unexpected(detail::error(node, TreeError::INVALID_ARGUMENT)));
570 }
571 return scene
572 .state(settings.state.value_or(initial ? CheckState::CHECKED : CheckState::UNCHECKED) == CheckState::CHECKED)
573 .and_then([&scene, node, &changed](ManagedState<bool> model) {
574 return scene.bind(node, model, std::move(changed));
575 });
576 });
577}
578
587inline Description<CheckBox> checkbox(std::string_view text, ManagedState<CheckState> model,
588 std::function<std::expected<void, TreeError>(CheckStateResult)> changed = {},
589 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY,
590 CheckBoxOptions options = {}) {
592 control<CheckBox>(text, style, policy),
593 [model, changed = std::move(changed), options](ManagedScene &scene, Node<CheckBox> node,
594 const ControlSettings &settings) mutable {
595 options.cycle = settings.cycle.value_or(options.cycle);
596 return scene.bind(node, model, std::move(changed), options);
597 });
598}
599
608inline Description<CheckBox> checkbox(std::string_view text, CheckState initial,
609 std::function<std::expected<void, TreeError>(CheckStateResult)> changed = {},
610 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY,
611 CheckBoxOptions options = {}) {
613 detail::Access::controlArguments(control<CheckBox>(text, style, policy),
614 {.text = TextValue(text), .state = initial}),
615 [initial, changed = std::move(changed), options](ManagedScene &scene, Node<CheckBox> node,
616 const ControlSettings &settings) mutable {
617 options.cycle = settings.cycle.value_or(options.cycle);
618 return scene.state(settings.state.value_or(initial))
619 .and_then([&scene, node, &changed, options](ManagedState<CheckState> model) {
620 return scene.bind(node, model, std::move(changed), options);
621 });
622 });
623}
624
633inline Description<ThreeState> threeState(std::string_view text, ManagedState<CheckState> model,
634 std::function<std::expected<void, TreeError>(CheckStateResult)> changed = {},
635 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
636 return detail::Access::intrinsicStyle(control<ThreeState>(text, style, policy),
637 StyleTarget{.type = "ThreeState", .control = true, .baseTypes = {"CheckBox"}})
638 .setup([model, changed = std::move(changed)](ManagedScene &scene, Node<ThreeState> node) mutable {
639 return scene.bind(Node<CheckBox>{node}, model, std::move(changed),
640 CheckBoxOptions{.cycle = CheckCycle::THREE_STATE});
641 });
642}
643
652inline Description<ThreeState> threeState(std::string_view text, CheckState initial,
653 std::function<std::expected<void, TreeError>(CheckStateResult)> changed = {},
654 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
657 detail::Access::intrinsicStyle(control<ThreeState>(text, style, policy),
658 StyleTarget{.type = "ThreeState", .control = true, .baseTypes = {"CheckBox"}}),
659 {.text = TextValue(text), .state = initial}),
660 [initial, changed = std::move(changed)](ManagedScene &scene, Node<ThreeState> node,
661 const ControlSettings &settings) mutable {
662 return scene.state(settings.state.value_or(initial))
663 .and_then([&scene, node, &changed](ManagedState<CheckState> model) {
664 return scene.bind(Node<CheckBox>{node}, model, std::move(changed),
665 CheckBoxOptions{.cycle = CheckCycle::THREE_STATE});
666 });
667 });
668}
669
671inline Description<CheckBox> checkbox(std::string_view text) {
672 return detail::Access::controlArguments(checkbox(text, CheckState::UNCHECKED), {.text = TextValue(text)});
673}
674
676inline Description<ThreeState> threeState(std::string_view text) {
677 return detail::Access::controlArguments(threeState(text, CheckState::UNCHECKED), {.text = TextValue(text)});
678}
679
690template <class Options>
691 requires std::same_as<std::remove_cvref_t<Options>, CheckBoxOptions>
692inline Description<CheckBox> checkbox(std::string_view text, CheckState initial, Options &&options,
693 std::function<std::expected<void, TreeError>(CheckStateResult)> changed = {},
694 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
696 checkbox(text, initial, std::move(changed), style, policy, std::forward<Options>(options)),
697 {.text = TextValue(text), .state = initial, .cycle = options.cycle});
698}
699
710template <class Options>
711 requires std::same_as<std::remove_cvref_t<Options>, CheckBoxOptions>
712inline Description<CheckBox> checkbox(std::string_view text, ManagedState<CheckState> model, Options &&options,
713 std::function<std::expected<void, TreeError>(CheckStateResult)> changed = {},
714 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
716 checkbox(text, model, std::move(changed), style, policy, std::forward<Options>(options)),
717 {.text = TextValue(text), .cycle = options.cycle});
718}
719
720namespace detail {
729inline Description<LineEdit> lineEditRecipe(std::optional<ManagedState<std::string>> external, TextValue initial,
730 LineEditConfig config, bool inherit, TextResolver resolver = {}) {
731 auto model = std::make_shared<ManagedState<std::string>>(external.value_or(ManagedState<std::string>{}));
732 auto description =
733 Access::cold<LineEdit>([initial = std::move(initial), options = config.options, policy = config.policy,
734 resolver = std::move(resolver), model, external = external.has_value()](
735 ManagedScene &scene, Node<Widget> parent,
736 MountContext &context) mutable -> std::expected<Node<LineEdit>, ManagedError> {
737 const ControlSettings empty;
738 const auto &settings = context.controlSettings ? *context.controlSettings : empty;
739 auto effective = controlLineEditOptions(settings, options, scene.widthPolicy(), resolver, parent);
740 if (!effective) {
741 return std::unexpected(effective.error());
742 }
743 auto placeholder = lineEditPlaceholder(*effective, parent);
744 if (!placeholder) {
745 return std::unexpected(placeholder.error());
746 }
747 if (!external) {
748 const auto &text = settings.text ? *settings.text : initial;
749 auto resolved = resolver ? text.resolve(scene.widthPolicy(), resolver, effective->limits)
750 : text.resolve(scene.widthPolicy(), effective->limits);
751 if (!resolved) {
752 auto failure = error(parent, TreeError::INVALID_ARGUMENT);
753 if (const auto *textError = std::get_if<TextError>(&resolved.error())) {
754 failure.text = *textError;
755 // Initial draft admission remains a binding failure when effective
756 // limits reject it before an editor or model has been allocated.
757 switch (textError->kind) {
762 failure.phase = ManagedPhase::BIND;
763 break;
764 default:
765 break;
766 }
767 } else {
768 failure.resource = std::get<TextResourceError>(resolved.error());
769 }
770 return std::unexpected(failure);
771 }
772 auto state = scene.state(std::string(resolved->bytes()));
773 if (!state) {
774 return std::unexpected(state.error());
775 }
776 *model = *state;
777 }
778 return scene.emplace<LineEdit>(parent, *effective, policy, std::move(*placeholder));
779 });
781 Access::configuredControl(std::move(description), "LineEdit", inherit),
782 [model, result = std::move(config.result), clipboard = std::move(config.clipboard),
783 validate = std::move(config.validate)](ManagedScene &scene, Node<LineEdit> node, const ControlSettings &) mutable {
784 return scene.bind(node, *model, std::move(result), std::move(clipboard), std::move(validate));
785 });
786}
787} // namespace detail
788
793 return detail::lineEditRecipe(model, TextValue(""), std::move(config), false);
794}
795
798 return detail::lineEditRecipe(model, TextValue(""), {}, true);
799}
800
804inline Description<LineEdit> lineEdit(TextValue initial, LineEditConfig config, TextResolver resolver = {}) {
805 return detail::lineEditRecipe(std::nullopt, std::move(initial), std::move(config), false, std::move(resolver));
806}
807
809inline Description<LineEdit> lineEdit(TextValue initial) {
810 auto arguments = ControlSettings{.text = initial};
811 return detail::Access::controlArguments(detail::lineEditRecipe(std::nullopt, std::move(initial), {}, true),
812 std::move(arguments));
813}
814
818inline Description<LineEdit> lineEdit(std::string_view initial, LineEditConfig config) {
819 return lineEdit(TextValue(initial), std::move(config));
820}
821
823inline Description<LineEdit> lineEdit(std::string_view initial) {
824 return lineEdit(TextValue(initial));
825}
826
828inline Description<LineEdit> lineEdit() {
829 return detail::lineEditRecipe(std::nullopt, TextValue(""), {}, true);
830}
831
837template <class Initial, class Settings>
838 requires std::same_as<std::remove_cvref_t<Settings>, ControlSettings> &&
839 requires(Initial &&initial) { lineEdit(std::forward<Initial>(initial)); }
840inline Description<LineEdit> lineEdit(Initial &&initial, Settings &&patch) {
841 return lineEdit(std::forward<Initial>(initial)).settings(std::forward<Settings>(patch));
842}
843
848template <class Settings>
849 requires std::same_as<std::remove_cvref_t<Settings>, ControlSettings>
850inline Description<LineEdit> lineEdit(TextValue initial, Settings &&patch, TextResolver resolver) {
851 auto arguments = ControlSettings{.text = initial};
852 return detail::Access::controlArguments(
853 detail::lineEditRecipe(std::nullopt, std::move(initial), {}, true, std::move(resolver)),
854 std::move(arguments))
855 .settings(std::forward<Settings>(patch));
856}
857
862inline Description<LineEdit> lineEdit(Ref<LineEdit> reference, TextValue initial, LineEditConfig config,
863 TextResolver resolver = {}) {
864 return lineEdit(std::move(initial), std::move(config), std::move(resolver)).reference(std::move(reference));
865}
866
870inline Description<LineEdit> lineEdit(Ref<LineEdit> reference, TextValue initial) {
871 return lineEdit(std::move(initial)).reference(std::move(reference));
872}
873
877inline Description<LineEdit> lineEdit(Ref<LineEdit> reference, std::string_view initial, LineEditConfig config) {
878 return lineEdit(initial, std::move(config)).reference(std::move(reference));
879}
880
884inline Description<LineEdit> lineEdit(Ref<LineEdit> reference, std::string_view initial) {
885 return lineEdit(initial).reference(std::move(reference));
886}
887
889inline Description<LineEdit> lineEdit(Ref<LineEdit> reference) {
890 return lineEdit().reference(std::move(reference));
891}
892
897template <class Initial, class Settings>
898 requires std::same_as<std::remove_cvref_t<Settings>, ControlSettings> &&
899 requires(Initial &&initial) { lineEdit(std::forward<Initial>(initial)); }
900inline Description<LineEdit> lineEdit(Ref<LineEdit> reference, Initial &&initial, Settings &&patch) {
901 return lineEdit(std::forward<Initial>(initial), std::forward<Settings>(patch)).reference(std::move(reference));
902}
903
907inline Description<Button> button(std::string_view text) {
908 return control<Button>(text, {}, DEFAULT_CONTENT_POLICY);
909}
910
917inline Description<Label> label(Ref<Label> reference, std::string_view text, ControlStyle style = {},
918 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
919 return label(text, style, policy).reference(std::move(reference));
920}
921
929template <std::equality_comparable T>
930 requires std::copy_constructible<T> && detail::ValueCaption<T>
931Description<Label> label(Ref<Label> reference, Model<T> source, ControlStyle style = {},
932 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
933 return label(std::move(source), style, policy).reference(std::move(reference));
934}
935
941template <std::equality_comparable T>
942 requires std::copy_constructible<T> && detail::ValueCaption<T>
943Description<Label> label(Ref<Label> reference, ModelValue<T> value, ControlStyle style = {},
944 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
945 return label(std::move(value), style, policy).reference(std::move(reference));
946}
947
957template <std::equality_comparable T, class Projection>
958 requires std::copy_constructible<T> && detail::ValueCaptionProjection<Projection, T>
959Description<Label> label(Ref<Label> reference, Model<T> source, Projection projection, ControlStyle style = {},
960 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
961 return label(std::move(source), std::move(projection), style, policy).reference(std::move(reference));
962}
963
970template <std::equality_comparable T, class Projection>
971 requires std::copy_constructible<T> && detail::ValueCaptionProjection<Projection, T>
972Description<Button> button(Ref<Button> reference, Model<T> source, Projection projection, ControlStyle style = {},
973 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
974 return button(std::move(source), std::move(projection), style, policy).reference(std::move(reference));
975}
976
985template <std::equality_comparable T, class Member>
986 requires std::copy_constructible<T> && detail::ValueCaption<Member>
987Description<Label> label(Ref<Label> reference, ModelField<T, Member> field, ControlStyle style = {},
988 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
989 return label(std::move(field), style, policy).reference(std::move(reference));
990}
991
998inline Description<Button> button(Ref<Button> reference, std::string_view text, ControlStyle style = {},
999 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
1000 return control<Button>(text, style, policy).reference(std::move(reference));
1001}
1002
1010template <class Caption>
1011 requires(std::same_as<std::remove_cvref_t<Caption>, TextValue> ||
1012 std::same_as<std::remove_cvref_t<Caption>, ManagedState<std::string>> ||
1013 std::same_as<std::remove_cvref_t<Caption>, ManagedSource<std::string>> ||
1014 portals::detail::IS_LIVE_TEXT_SOURCE<std::remove_cvref_t<Caption>> ||
1015 std::same_as<std::remove_cvref_t<Caption>, ValueText> || detail::CaptionSource<Caption>)
1016inline Description<Label> label(Ref<Label> reference, Caption &&text, ControlStyle style = {},
1017 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
1018 return label(std::forward<Caption>(text), style, policy).reference(std::move(reference));
1019}
1020
1028template <class Caption>
1029 requires(std::same_as<std::remove_cvref_t<Caption>, TextValue> ||
1030 std::same_as<std::remove_cvref_t<Caption>, ManagedState<std::string>> ||
1031 std::same_as<std::remove_cvref_t<Caption>, ManagedSource<std::string>> ||
1032 portals::detail::IS_LIVE_TEXT_SOURCE<std::remove_cvref_t<Caption>> ||
1033 std::same_as<std::remove_cvref_t<Caption>, ValueText> || detail::CaptionSource<Caption>)
1034inline Description<Button> button(Ref<Button> reference, Caption &&text, ControlStyle style = {},
1035 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
1036 return button(std::forward<Caption>(text), style, policy).reference(std::move(reference));
1037}
1038
1048template <class Caption, class Callback>
1049 requires((std::same_as<std::remove_cvref_t<Caption>, TextValue> ||
1050 std::same_as<std::remove_cvref_t<Caption>, ManagedState<std::string>> ||
1051 std::same_as<std::remove_cvref_t<Caption>, ManagedSource<std::string>> ||
1052 portals::detail::IS_LIVE_TEXT_SOURCE<std::remove_cvref_t<Caption>> ||
1053 std::same_as<std::remove_cvref_t<Caption>, ValueText> || detail::CaptionSource<Caption>) &&
1055inline Description<Button> button(Ref<Button> reference, Caption &&text, Callback action, ControlStyle style = {},
1056 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
1057 return button(std::forward<Caption>(text), std::move(action), style, policy).reference(std::move(reference));
1058}
1059
1068template <class Callback>
1070Description<Button> button(Ref<Button> reference, std::string_view text, Callback action, ControlStyle style = {},
1071 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
1072 return button(text, std::move(action), style, policy).reference(std::move(reference));
1073}
1074
1083inline Description<CheckBox> checkbox(Ref<CheckBox> reference, std::string_view text, ManagedState<bool> model,
1084 std::function<std::expected<void, TreeError>(CheckBoxResult)> changed = {},
1085 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
1086 return checkbox(text, model, std::move(changed), style, policy).reference(std::move(reference));
1087}
1088
1098inline Description<CheckBox> checkbox(Ref<CheckBox> reference, std::string_view text, ManagedState<CheckState> model,
1099 std::function<std::expected<void, TreeError>(CheckStateResult)> changed = {},
1100 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY,
1101 CheckBoxOptions options = {}) {
1102 return checkbox(text, model, std::move(changed), style, policy, options).reference(std::move(reference));
1103}
1104
1113inline Description<ThreeState> threeState(Ref<ThreeState> reference, std::string_view text, CheckState initial,
1114 std::function<std::expected<void, TreeError>(CheckStateResult)> changed = {},
1115 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
1116 return threeState(text, initial, std::move(changed), style, policy).reference(std::move(reference));
1117}
1118
1122inline Description<ThreeState> threeState(Ref<ThreeState> reference, std::string_view text) {
1123 return threeState(text).reference(std::move(reference));
1124}
1125
1129inline Description<CheckBox> checkbox(Ref<CheckBox> reference, std::string_view text) {
1130 return checkbox(text).reference(std::move(reference));
1131}
1132
1141inline Description<ThreeState> threeState(Ref<ThreeState> reference, std::string_view text,
1142 ManagedState<CheckState> model,
1143 std::function<std::expected<void, TreeError>(CheckStateResult)> changed = {},
1144 ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY) {
1145 return threeState(text, model, std::move(changed), style, policy).reference(std::move(reference));
1146}
1147
1153inline Description<LineEdit> lineEdit(Ref<LineEdit> reference, ManagedState<std::string> model, LineEditConfig config) {
1154 return lineEdit(model, std::move(config)).reference(std::move(reference));
1155}
1156
1160inline Description<LineEdit> lineEdit(Ref<LineEdit> reference, ManagedState<std::string> model) {
1161 return lineEdit(model).reference(std::move(reference));
1162}
1163
1171template <class T>
1172Description<T> control(Ref<T> reference, std::string_view text, ControlStyle style = {},
1173 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
1174 return control<T>(text, style, policy).reference(std::move(reference));
1175}
1176
1179 bool enabled;
1180
1186 template <class T>
1187 requires(std::same_as<T, LineEdit> || std::same_as<T, Password> || std::same_as<T, ScrollArea> ||
1188 std::same_as<T, ScrollBar>)
1189 friend Description<T> operator|(Description<T> &&description, ReadOnlyModifier modifier) {
1190 return std::move(description).readOnly(modifier.enabled);
1191 }
1192};
1193
1197inline ReadOnlyModifier readOnly(bool enabled = true) {
1198 return {enabled};
1199}
1200
1206inline Description<Button> plainButton(std::string_view text, ControlStyle style = {},
1207 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
1208 return detail::textControl<Button>(TextValue(text), style, policy, {}, ButtonChrome::NONE);
1209}
1210
1218template <class Callback>
1220Description<Button> plainButton(std::string_view text, Callback action, ControlStyle style = {},
1221 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
1222 return plainButton(text, style, policy).onClick(std::move(action));
1223}
1224
1231inline Description<Button> plainButton(Ref<Button> reference, std::string_view text, ControlStyle style = {},
1232 SizePolicy policy = DEFAULT_CONTENT_POLICY) {
1233 return plainButton(text, style, policy).reference(std::move(reference));
1234}
1235
1240 return std::move(description).threeState();
1241 }
1242};
1243
1245inline ThreeStateModifier threeState() {
1246 return {};
1247}
1248
1249} // namespace portals::ui
static std::expected< GraphemeText, TextError > from(std::string_view bytes, WidthPolicy policy={}, TextLimits limits={})
Validate, segment and measure a single logical UTF-8 buffer.
Additive lifecycle owner over one existing Application subtree. UI affine and nonmovable....
Definition managed.hpp:530
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
Owning literal bytes or a resource key, measured only at resolution.
Definition text_value.hpp:42
constexpr TextValue(const TextValue &other)=default
Copy expression bytes and kind into independent storage.
One owning callback and its metadata; construct before checked set admission. Captures and metadata c...
Definition ui_action.hpp:75
Single-use owning construction recipe; no mounted widget ownership.
Definition ui_description.hpp:48
Description && style(StyleTarget target) &&
Definition ui_description.hpp:1288
Description()=default
Construct an empty recipe; mounting it returns INVALID_ARGUMENT.
friend Description< LineEdit > lineEdit(ManagedState< std::string >, LineEditConfig)
Definition ui_controls.hpp:792
friend Description< U > control(std::string_view, ControlStyle, SizePolicy)
Construct a standard text-control recipe with the scene's width policy.
Definition ui_controls.hpp:18
Description && value(double value) &&
Definition ui_description.hpp:290
Owning field descriptor over one scalar root; snapshots never retain a borrowed member reference.
Definition ui_value_model.hpp:520
Weak scalar property observation; assignment publishes a value without rebinding the descriptor.
Definition ui_value_model.hpp:412
Owning cold model declaration; copies share one single-scene registration. Construction does not read...
Definition ui_model.hpp:129
Typed Widget observation; explicit composite specializations reuse their existing weak facades.
Definition ui_ref.hpp:761
Copyable observing text property; copies own their resolver and last-operation result....
Definition ui_ref.hpp:585
Copyable read-only source observing one caption, visible draft or accepted editor model....
Definition ui_ref.hpp:302
Owning cold formatted text with explicit scalar dependencies, separate from its input roots....
Definition ui_value_text.hpp:37
A compact action optionally receives owning event parameters.
Definition managed.hpp:239
Exact deferred caption inputs; literals and bare braces keep their existing overloads.
Definition ui_controls.hpp:322
ui::ValueText text(std::format_string< const ui::detail::TextArgumentValue< Args > &... > pattern, Args &&...args)
Declare live formatted text over explicit scalar roots, fields or value properties.
Definition ui_value_text.hpp:567
std::expected< PropertyUpdate< CheckState >, TreeError > CheckStateResult
Exact typed Property set receipt; successful commits can reject notifications.
Definition controls.hpp:477
CheckState
Explicit checkbox value; MIXED describes an aggregate or an opt-in user choice.
Definition controls.hpp:266
@ MIXED
Partial selection, displayed independently of color.
Definition controls.hpp:269
@ CHECKED
Complete selection.
Definition controls.hpp:268
@ UNCHECKED
No selection.
Definition controls.hpp:267
constexpr SizePolicy DEFAULT_CONTENT_POLICY
Intrinsic content on both axes; explicit policies remain authoritative.
Definition layout.hpp:44
@ BIND
Register a managed callback, binding or presentation adapter.
Definition managed_diagnostics.hpp:51
@ TWO_STATE
Unchecked/checked toggle; mixed activates to checked.
Definition controls.hpp:274
@ THREE_STATE
Unchecked, checked, mixed, then unchecked.
Definition controls.hpp:275
ButtonChrome
Button caption decoration; action routing and identity remain unchanged.
Definition controls.hpp:155
@ BRACKETS
ASCII bracket contour spanning the allocated button width.
Definition controls.hpp:156
@ INPUT_LIMIT
Input length exceeds its configured/representation limit.
Definition text.hpp:73
@ CLUSTER_LIMIT
One whole cluster exceeds its byte limit.
Definition text.hpp:75
@ GRAPHEME_LIMIT
The number of clusters exceeds its limit.
Definition text.hpp:77
@ CELL_LIMIT
Cell accumulation exceeds its configured/int32_t limit.
Definition text.hpp:79
std::expected< PropertyUpdate< bool >, TreeError > CheckBoxResult
Exact Property set outcome; errors mean no commit, successful receipts can reject notifications.
Definition controls.hpp:474
@ CLOSED
The owning tree or source is closed.
Definition widget_types.hpp:31
@ INVALID_ARGUMENT
An option, enum value or operation argument is invalid.
Definition widget_types.hpp:27
Owning paint values; no theme engine or behavior is encoded here.
Definition controls.hpp:24
Original error plus independent runner/restoration and cleanup failures.
Definition managed_diagnostics.hpp:62
Owning independent policies for width and height.
Definition layout.hpp:35
Owning sparse literals only; never contains IDs, models, handlers or live history.
Definition ui_control_context.hpp:391
std::optional< TextValue > text
Caption/value expression, resolved by receiving scene.
Definition ui_control_context.hpp:394
std::optional< ButtonChrome > chrome
Button decoration; absence preserves the receiving factory's chrome.
Definition ui_control_context.hpp:412
Owning optional editor customization and confirmation policy.
Definition ui_description.hpp:34
Owning existing LineEdit/scroll read-only policy stage.
Definition ui_controls.hpp:1178
bool enabled
User-mutation policy; programmatic commands retain their existing behavior.
Definition ui_controls.hpp:1179
Sparse checkbox cycle stage preserving the factory's backing model.
Definition ui_controls.hpp:1237
friend Description< CheckBox > operator|(Description< CheckBox > &&description, ThreeStateModifier)
Definition ui_controls.hpp:1239
static Description< T > controlSetup(Description< T > description, Callback callback)
Attach binding setup after node creation within the mount transaction.
Definition ui_description.hpp:3460
static Description< T > controlArguments(Description< T > description, ControlSettings arguments)
Retain explicit constructor properties above inherited defaults.
Definition ui_description.hpp:3417
static Description< T > intrinsicStyle(Description< T > description, StyleTarget identity)
Definition ui_description.hpp:3471
static std::weak_ptr< const void > lifetime(ManagedScene &scene)
Definition ui_recipe_access.hpp:539
static Description< T > configuredControl(Description< T > description, std::string_view type, bool inherit=true)
Attach a builtin configuration identity to an owning recipe.
Definition ui_description.hpp:3409
static std::expected< void, ManagedError > check(ManagedScene &scene, Node< Widget > node)
Verify a created node remains accessible before publishing references.
Definition ui_recipe_access.hpp:1394
static constexpr std::string_view controlType()
Identify builtin recipe types without allocating runtime metadata.
Definition ui_recipe_access.hpp:808
Stack-owned transaction bookkeeping, discarded after one description mount.
Definition ui_recipe_context.hpp:554
const ControlSettings * controlSettings
Constructor-only borrowed settings; restored before setup/children.
Definition ui_recipe_context.hpp:562