Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_password_controls.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/ui_description.hpp>
6
7namespace portals::ui {
9using PasswordRef = Ref<Password>;
10
15 std::function<std::expected<PasswordValue, PasswordFailure>()>
17 std::function<std::expected<void, TreeError>(PasswordResult)> result{};
18 std::shared_ptr<Clipboard> clipboard{};
19 std::function<bool(SecretView)> validate{};
20};
21
23namespace detail {
24inline std::expected<PasswordOptions, ManagedError> passwordOptions(const ControlSettings &settings,
25 PasswordOptions options, WidthPolicy policy,
26 const TextResolver &resolver, Node<Widget> parent) {
27 const auto apply = [](auto &target, const auto &value) {
28 if (value) {
29 target = *value;
30 }
31 };
32
33 options.widthPolicy = policy;
34 options.limits = controlTextLimits(settings, options.limits);
35 apply(options.editMode, settings.editMode);
36 apply(options.readOnly, settings.readOnly);
37 apply(options.pastePolicy, settings.pastePolicy);
38 apply(options.securePolicy.maxBytes, settings.securePolicy.maxBytes);
39 apply(options.securePolicy.storage, settings.securePolicy.storage);
40 apply(options.securePolicy.lockPages, settings.securePolicy.lockPages);
41 apply(options.securePolicy.excludeFromDumps, settings.securePolicy.excludeFromDumps);
42 apply(options.securePolicy.encryptWhileIdle, settings.securePolicy.encryptWhileIdle);
43
44 if (settings.displayMode) {
45 if (*settings.displayMode == LineEditDisplayMode::NORMAL) {
46 return std::unexpected(error(parent, TreeError::INVALID_ARGUMENT));
47 }
48
51 }
52 if (settings.text && !settings.text->isEmptyLiteral()) {
53 return std::unexpected(error(parent, TreeError::INVALID_ARGUMENT));
54 }
55 const bool validMode = options.editMode == EditMode::IMMEDIATE || options.editMode == EditMode::ON_COMPLETION ||
57 const bool validDisplay =
59 const bool validPaste =
61 if (!validMode || !validDisplay || !validPaste ||
64 return std::unexpected(error(parent, TreeError::INVALID_ARGUMENT));
65 }
66
67 if (settings.placeholder) {
68 auto text = resolver ? settings.placeholder->resolve(policy, resolver, options.limits)
69 : settings.placeholder->resolve(policy, options.limits);
70
71 if (!text) {
72 auto failure = error(parent, TreeError::INVALID_ARGUMENT);
73
74 if (const auto *cause = std::get_if<TextError>(&text.error())) {
75 failure.text = *cause;
76 } else {
77 failure.resource = std::get<TextResourceError>(text.error());
78 }
79
80 return std::unexpected(std::move(failure));
81 }
82
83 options.placeholder.emplace(text->bytes());
84 }
85
86 return options;
87}
88
89inline Description<Password> passwordRecipe(std::optional<ManagedState<PasswordValue>> external, PasswordConfig config,
90 bool inherit, TextResolver resolver = {}) {
91 auto model = std::make_shared<ManagedState<PasswordValue>>(external.value_or(ManagedState<PasswordValue>{}));
92 auto description =
93 Access::cold<Password>([options = config.options, policy = config.policy, initial = std::move(config.initial),
94 resolver = std::move(resolver), model, external = external.has_value()](
95 ManagedScene &scene, Node<Widget> parent,
96 MountContext &context) mutable -> std::expected<Node<Password>, ManagedError> {
97 const ControlSettings empty;
98 const auto &settings = context.controlSettings ? *context.controlSettings : empty;
99 auto effective = passwordOptions(settings, options, scene.widthPolicy(), resolver, parent);
100
101 if (!effective) {
102 return std::unexpected(effective.error());
103 }
104 if (external && initial) {
105 return std::unexpected(error(parent, TreeError::INVALID_ARGUMENT));
106 }
107 if (!external) {
108 std::expected<PasswordValue, PasswordFailure> value =
109 initial ? initial()
110 : std::expected<PasswordValue, PasswordFailure>(PasswordValue::create({}, effective->securePolicy));
111
112 if (!value) {
113 return std::unexpected(
114 portals::detail::passwordError(ManagedPhase::BIND, parent.id(), std::move(value.error())));
115 }
116
117 auto created = scene.state(std::move(*value));
118
119 if (!created) {
120 return std::unexpected(created.error());
121 }
122
123 *model = *created;
124 }
125
126 return scene.emplace<Password>(parent, *effective, policy);
127 });
128
130 Access::configuredControl(std::move(description), "Password", inherit),
131 [model, result = std::move(config.result), clipboard = std::move(config.clipboard),
132 validate = std::move(config.validate)](ManagedScene &scene, Node<Password> node, const ControlSettings &) mutable {
133 return scene.bind(node, *model, std::move(result), std::move(clipboard), std::move(validate));
134 });
135}
136} // namespace detail
137
139
141inline Description<Password> password() {
142 return detail::passwordRecipe(std::nullopt, {}, true);
143}
144
146inline Description<Password> password(PasswordConfig config) {
147 return detail::passwordRecipe(std::nullopt, std::move(config), false);
148}
149
151inline Description<Password> password(ManagedState<PasswordValue> model) {
152 return detail::passwordRecipe(model, {}, true);
153}
154
158inline Description<Password> password(ManagedState<PasswordValue> model, PasswordConfig config) {
159 return detail::passwordRecipe(model, std::move(config), false);
160}
161
163inline Description<Password> password(PasswordRef reference) {
164 return password().reference(std::move(reference));
165}
166
168inline Description<Password> password(PasswordRef reference, PasswordConfig config) {
169 return password(std::move(config)).reference(std::move(reference));
170}
171
173inline Description<Password> password(PasswordRef reference, ManagedState<PasswordValue> model) {
174 return password(model).reference(std::move(reference));
175}
176
180inline Description<Password> password(PasswordRef reference, ManagedState<PasswordValue> model, PasswordConfig config) {
181 return password(model, std::move(config)).reference(std::move(reference));
182}
183
186 bool enabled{true};
187
191 template <class T>
192 requires(std::same_as<T, LineEdit> || std::same_as<T, Password>)
193 friend Description<T> operator|(Description<T> &&description, NoEchoModifier modifier) {
194 return std::move(description).noEcho(modifier.enabled);
195 }
196};
197
199constexpr NoEchoModifier noEcho(bool enabled = true) noexcept {
200 return {enabled};
201}
202} // namespace portals::ui
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
Synchronous read-only byte borrow, valid only during its callback.
Definition secure_string.hpp:195
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
@ NORMAL
Render the draft using its actual grapheme widths; copying and cutting retain normal behavior.
Definition line_edit.hpp:35
@ NO_ECHO
Render no value or selection, with a fixed-origin caret and intrinsic width one; suppress copy/cut.
Definition line_edit.hpp:37
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
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
std::variant< TreeError, TextError, PasswordInputError, SecureStringFailure, PasswordValueAccessFailure< SecureStringFailure >, PasswordValueAccessFailure< TextError >, PasswordValueAccessFailure< std::monostate > > PasswordFailure
Owning original causes, including independently retained callback/cleanup diagnostics.
Definition password.hpp:42
@ NO_ECHO
No value/selection glyphs, intrinsic width one and fixed-origin caret.
Definition password.hpp:13
@ PASSWORD
One ASCII '*' per grapheme; exposes the grapheme count.
Definition password.hpp:12
@ BIND
Register a managed callback, binding or presentation adapter.
Definition managed_diagnostics.hpp:51
constexpr std::size_t MAX_SECRET_BYTES
Hard upper bound for an individual secret, in bytes.
Definition secure_string.hpp:27
@ DISABLED
Consume paste without reading the service or inspecting supplied bytes.
Definition password.hpp:17
@ ENABLED
Accept bounded external plaintext; wipe the owned service payload after copying.
Definition password.hpp:18
@ NATIVE_GUARDED
Dedicated native RW payload surrounded by inaccessible pages.
Definition secure_string.hpp:32
@ INVALID_ARGUMENT
An option, enum value or operation argument is invalid.
Definition widget_types.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 protected-editor policy; no plaintext history, copying or cutting exists.
Definition password.hpp:22
SecureStringOptions securePolicy
Mandatory native encryption and bounded protected storage.
Definition password.hpp:33
std::optional< std::string > placeholder
Public, nonsecret placeholder; never populated from a secret.
Definition password.hpp:32
PasswordDisplayMode displayMode
NO_ECHO reveals no count through layout.
Definition password.hpp:31
PasswordPastePolicy pastePolicy
Clipboard paste requires explicit opt-in.
Definition password.hpp:30
TextLimits limits
Whole UTF-8 value bounds; edits are never truncated.
Definition password.hpp:23
bool readOnly
Block user edits; explicit setValue remains available.
Definition password.hpp:29
WidthPolicy widthPolicy
Unicode measurement policy; masks always use ASCII '*'.
Definition password.hpp:24
EditMode editMode
Publication boundary shared by input and setValue.
Definition password.hpp:28
Secret-free editing outcome; a commit owns only protected immutable snapshots.
Definition password.hpp:60
std::size_t maxBytes
Maximum application bytes, from zero through MAX_SECRET_BYTES.
Definition secure_string.hpp:40
bool lockPages
Require native resident page locking for payload and any key page before copying input.
Definition secure_string.hpp:44
bool excludeFromDumps
Require Linux core-dump exclusion for payload and any key page; unsupported on Windows/WASM.
Definition secure_string.hpp:46
SecureStringStorage storage
Mandatory allocation policy; no implicit platform fallback.
Definition secure_string.hpp:42
bool encryptWhileIdle
Require native idle encryption; true by default, unsupported with WIPE_ONLY or on WASM.
Definition secure_string.hpp:48
Owning independent policies for width and height.
Definition layout.hpp:35
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42
Owning cold presentation modifier; ordinary LineEdit storage remains plaintext.
Definition ui_password_controls.hpp:185
bool enabled
False restores each editor type's normal presentation.
Definition ui_password_controls.hpp:186
Optional protected-editor customization; ordinary forms need only password(ref).
Definition ui_password_controls.hpp:12
std::shared_ptr< Clipboard > clipboard
Service used only by explicit ENABLED paste policy.
Definition ui_password_controls.hpp:18
SizePolicy policy
Owning external layout constraints.
Definition ui_password_controls.hpp:14
std::function< std::expected< PasswordValue, PasswordFailure >()> initial
Checked owning provider, called once at mount.
Definition ui_password_controls.hpp:16
std::function< std::expected< void, TreeError >(PasswordResult)> result
Optional secret-free result observer.
Definition ui_password_controls.hpp:17
std::function< bool(SecretView)> validate
Synchronous scoped publication predicate; false retains the draft.
Definition ui_password_controls.hpp:19
PasswordOptions options
Native editor policy; the scene supplies its Unicode width policy.
Definition ui_password_controls.hpp:13
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 > 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
Stack-owned transaction bookkeeping, discarded after one description mount.
Definition ui_recipe_context.hpp:554