4#include <portals/ui_controls.hpp>
18concept ToggleCaption = std::convertible_to<T, std::string_view> || std::same_as<std::remove_cvref_t<T>,
TextValue>;
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);
24 return TextValue(std::string_view(caption));
28inline Description<Switch> toggleRecipe(TextValue caption, std::optional<ManagedState<bool>> external,
29 std::optional<bool> initial, SwitchOptions options) {
31 Access::cold<Switch>([options](ManagedScene &scene, Node<Widget> parent,
32 MountContext &context) -> std::expected<Node<Switch>, ManagedError> {
34 const auto policy = scene.widthPolicy();
37 auto text = options.resolver ? context.controlSettings->text->resolve(policy, options.resolver)
38 : context.controlSettings->text->resolve(policy);
40 if (lifetime.expired()) {
45 if (
const auto *cause = std::get_if<TextError>(&
text.error())) {
46 return std::unexpected(textError(parent, *cause));
49 failure.resource = std::get<TextResourceError>(
text.error());
50 return std::unexpected(failure);
56 return std::unexpected(error(parent, chrome.error()));
59 return scene.emplace<Switch>(parent, std::move(*text), std::move(*chrome), ControlStyle{},
DEFAULT_CONTENT_POLICY,
60 context.controlSettings->readOnly.value_or(options.readOnly));
64 ControlSettings{.text = std::move(caption),
68 std::move(description),
69 [external](ManagedScene &scene, Node<Switch> node,
70 const ControlSettings &settings) -> std::expected<void, ManagedError> {
72 (external && settings.state)) {
77 return scene.bind(Node<CheckBox>{node}, *external);
82 return model ? scene.bind(Node<CheckBox>{node}, *model) : std::unexpected(model.error());
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));
104template <detail::ToggleCaption Caption> Description<Switch> toggle(Caption &&caption, SwitchOptions options = {}) {
105 return detail::toggleRecipe(detail::toggleCaption(std::forward<Caption>(caption)), {}, {}, std::move(options));
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,
124template <detail::ToggleCaption Caption>
125Description<Switch> toggle(Caption &&caption, std::expected<ManagedState<bool>, ManagedError> model,
126 SwitchOptions options = {}) {
128 return toggle(std::forward<Caption>(caption), *model, std::move(options));
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));
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));
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