Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_switch.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4#include <portals/ui_controls.hpp>
5
6namespace portals::ui {
14
16namespace detail {
17template <class T>
18concept ToggleCaption = std::convertible_to<T, std::string_view> || std::same_as<std::remove_cvref_t<T>, TextValue>;
19
20template <ToggleCaption T> TextValue toggleCaption(T &&caption) {
21 if constexpr (std::same_as<std::remove_cvref_t<T>, TextValue>) {
22 return std::forward<T>(caption);
23 } else {
24 return TextValue(std::string_view(caption));
25 }
26}
27
28inline Description<Switch> toggleRecipe(TextValue caption, std::optional<ManagedState<bool>> external,
29 std::optional<bool> initial, SwitchOptions options) {
30 auto description =
31 Access::cold<Switch>([options](ManagedScene &scene, Node<Widget> parent,
32 MountContext &context) -> std::expected<Node<Switch>, ManagedError> {
33 const auto lifetime = Access::lifetime(scene);
34 const auto policy = scene.widthPolicy();
35
36 // Capture policy and lifetime before an optional user resolver can reenter.
37 auto text = options.resolver ? context.controlSettings->text->resolve(policy, options.resolver)
38 : context.controlSettings->text->resolve(policy);
39
40 if (lifetime.expired()) {
41 return std::unexpected(error(parent, TreeError::CLOSED));
42 }
43
44 if (!text) {
45 if (const auto *cause = std::get_if<TextError>(&text.error())) {
46 return std::unexpected(textError(parent, *cause));
47 }
48 auto failure = error(parent, TreeError::INVALID_ARGUMENT);
49 failure.resource = std::get<TextResourceError>(text.error());
50 return std::unexpected(failure);
51 }
52
53 auto chrome = SwitchChrome::from(options.offText, options.onText, policy);
54
55 if (!chrome) {
56 return std::unexpected(error(parent, chrome.error()));
57 }
58
59 return scene.emplace<Switch>(parent, std::move(*text), std::move(*chrome), ControlStyle{}, DEFAULT_CONTENT_POLICY,
60 context.controlSettings->readOnly.value_or(options.readOnly));
61 });
62 description = Access::controlArguments(
63 Access::configuredControl(std::move(description), "Switch"),
64 ControlSettings{.text = std::move(caption),
65 .state =
66 initial ? std::optional(*initial ? CheckState::CHECKED : CheckState::UNCHECKED) : std::nullopt});
68 std::move(description),
69 [external](ManagedScene &scene, Node<Switch> node,
70 const ControlSettings &settings) -> std::expected<void, ManagedError> {
71 if (settings.state == CheckState::MIXED || settings.cycle == CheckCycle::THREE_STATE ||
72 (external && settings.state)) {
73 return std::unexpected(error(node, TreeError::INVALID_ARGUMENT));
74 }
75
76 if (external) {
77 return scene.bind(Node<CheckBox>{node}, *external);
78 }
79
80 auto model = scene.state(settings.state.value_or(CheckState::UNCHECKED) == CheckState::CHECKED);
81
82 return model ? scene.bind(Node<CheckBox>{node}, *model) : std::unexpected(model.error());
83 });
84}
85} // namespace detail
86
88
94template <detail::ToggleCaption Caption>
95Description<Switch> toggle(Caption &&caption, bool initial, SwitchOptions options = {}) {
96 return detail::toggleRecipe(detail::toggleCaption(std::forward<Caption>(caption)), {}, initial, std::move(options));
97}
98
104template <detail::ToggleCaption Caption> Description<Switch> toggle(Caption &&caption, SwitchOptions options = {}) {
105 return detail::toggleRecipe(detail::toggleCaption(std::forward<Caption>(caption)), {}, {}, std::move(options));
106}
107
113template <detail::ToggleCaption Caption>
114Description<Switch> toggle(Caption &&caption, ManagedState<bool> model, SwitchOptions options = {}) {
115 return detail::toggleRecipe(detail::toggleCaption(std::forward<Caption>(caption)), model, std::nullopt,
116 std::move(options));
117}
118
124template <detail::ToggleCaption Caption>
125Description<Switch> toggle(Caption &&caption, std::expected<ManagedState<bool>, ManagedError> model,
126 SwitchOptions options = {}) {
127 if (model) {
128 return toggle(std::forward<Caption>(caption), *model, std::move(options));
129 }
130
131 return detail::Access::failed<Switch>(model.error());
132}
133
141template <detail::ToggleCaption Caption, class Model>
142 requires(std::same_as<std::remove_cvref_t<Model>, bool> ||
143 std::same_as<std::remove_cvref_t<Model>, ManagedState<bool>> ||
144 std::same_as<std::remove_cvref_t<Model>, std::expected<ManagedState<bool>, ManagedError>>)
145Description<Switch> toggle(Ref<Switch> reference, Caption &&caption, Model model, SwitchOptions options = {}) {
146 return toggle(std::forward<Caption>(caption), std::move(model), std::move(options)).reference(std::move(reference));
147}
148
154template <detail::ToggleCaption Caption>
155Description<Switch> toggle(Ref<Switch> reference, Caption &&caption, SwitchOptions options = {}) {
156 return toggle(std::forward<Caption>(caption), std::move(options)).reference(std::move(reference));
157}
158} // namespace portals::ui
static std::expected< SwitchChrome, TreeError > from(std::string_view off, std::string_view on, WidthPolicy policy={})
Validate same-width single-line off/on decorations under the receiving policy.
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.
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::function< std::expected< std::string, TextResourceError >(std::string_view)> KeyTextResolver
Same exact resolver type as ui::TextResolver; used only during checked normalization.
Definition key_binding.hpp:57
@ 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
@ THREE_STATE
Unchecked, checked, mixed, then unchecked.
Definition controls.hpp:275
constexpr std::string_view DEFAULT_SWITCH_OFF_TEXT
Plain off decoration.
Definition controls.hpp:354
@ 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
constexpr std::string_view DEFAULT_SWITCH_ON_TEXT
Plain on decoration.
Definition controls.hpp:355
Cold per-Switch configuration; never retained by unrelated controls.
Definition ui_switch.hpp:8
bool readOnly
Block user writes while retaining focus and programmatic bool access.
Definition ui_switch.hpp:9
std::string onText
Owning same-width single-line decoration.
Definition ui_switch.hpp:11
std::string offText
Owning same-width single-line decoration.
Definition ui_switch.hpp:10
KeyTextResolver resolver
Optional caption resource resolver, called only during mount.
Definition ui_switch.hpp:12
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 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 Description< T > failed(ManagedError failure)
Definition ui_description.hpp:3424
Stack-owned transaction bookkeeping, discarded after one description mount.
Definition ui_recipe_context.hpp:554