Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_model_form.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/ui_model_edit.hpp>
6
7#include <array>
8#include <charconv>
9#include <cmath>
10
11namespace portals::ui {
12namespace detail {
14template <class T>
15concept DefaultFormScalar =
16 std::same_as<T, std::string> || (((std::integral<T> || std::floating_point<T>) && !std::same_as<T, bool>) &&
17 requires(T value, char *output, const char *input) {
18 std::to_chars(output, output, value);
19 std::from_chars(input, input, value);
20 });
21
22template <DefaultFormScalar Value> std::expected<std::string, TreeError> formatFormScalar(const Value &value) {
23 if constexpr (std::same_as<Value, std::string>) {
24 return std::expected<std::string, TreeError>(std::in_place, value);
25 } else {
26 // General floating-point notation has a bounded mantissa and an int-sized decimal exponent.
27 constexpr auto SIGN_AND_ROUNDING_BYTES = 3;
28 constexpr auto EXPONENT_PUNCTUATION_BYTES = 4;
29 constexpr auto CONVERSION_BYTES =
30 std::integral<Value>
31 ? std::numeric_limits<Value>::digits10 + SIGN_AND_ROUNDING_BYTES
32 : std::numeric_limits<Value>::max_digits10 + std::numeric_limits<int>::digits10 + EXPONENT_PUNCTUATION_BYTES;
33 std::array<char, CONVERSION_BYTES> buffer{};
34
35 if constexpr (std::floating_point<Value>) {
36 if (!std::isfinite(value)) {
37 return std::unexpected(TreeError::INVALID_ARGUMENT);
38 }
39 }
40 const auto converted = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value);
41
42 if (converted.ec != std::errc{}) {
43 return std::unexpected(TreeError::INVALID_ARGUMENT);
44 }
45
46 return std::expected<std::string, TreeError>(std::in_place, buffer.data(),
47 static_cast<std::size_t>(converted.ptr - buffer.data()));
48 }
49}
50
51template <DefaultFormScalar Value> std::expected<Value, TreeError> parseFormScalar(std::string_view text) {
52 if constexpr (std::same_as<Value, std::string>) {
53 return std::expected<Value, TreeError>(std::in_place, text.data(), text.size());
54 } else {
55 if (text.empty()) {
56 return std::unexpected(TreeError::INVALID_ARGUMENT);
57 }
58 Value value{};
59
60 const auto converted = std::from_chars(text.data(), text.data() + text.size(), value);
61
62 if (converted.ec != std::errc{} || converted.ptr != text.data() + text.size()) {
63 return std::unexpected(TreeError::INVALID_ARGUMENT);
64 }
65 if constexpr (std::floating_point<Value>) {
66 if (!std::isfinite(value)) {
67 return std::unexpected(TreeError::INVALID_ARGUMENT);
68 }
69 }
70
71 return value;
72 }
73}
74
75template <DefaultFormScalar Value> std::shared_ptr<const EditTextCodec<Value>> defaultFormCodec() {
76 static const auto CODEC = std::make_shared<const EditTextCodec<Value>>(
77 EditTextCodec<Value>{.format = formatFormScalar<Value>, .parse = parseFormScalar<Value>});
78
79 return CODEC;
80}
81
83} // namespace detail
84
92template <class Row, class Value> class FormFieldMapping {
93public:
95 ~FormFieldMapping() = default;
96
100 FormFieldMapping(std::string_view caption, Value Row::*member) : caption_(caption), member_(member) {
101 }
102
106 FormFieldMapping &required(bool enabled = true) {
107 config_.required = enabled;
108
109 return *this;
110 }
111
115 FormFieldMapping &readOnly(bool enabled = true) {
116 config_.editor.options.readOnly = enabled;
117
118 return *this;
119 }
120
124 FormFieldMapping &validate(std::function<bool(const Value &)> rule) {
125 rule_ = std::move(rule);
126
127 return *this;
128 }
129
134 auto caption = std::move(config_.captionRecipe);
135
136 config_ = std::move(config);
137 if (caption) {
138 config_.captionRecipe = std::move(caption);
139 }
140
141 return *this;
142 }
143
148 reference_ = std::move(reference);
149
150 return *this;
151 }
152
156 FormFieldMapping &key(std::string_view key) {
157 key_ = key;
158
159 return *this;
160 }
161
167 codec_ = std::make_shared<const EditTextCodec<Value>>(std::move(codec));
168
169 return *this;
170 }
171
178 template <class Factory> FormFieldMapping &editor(Factory factory) {
179 factory_ = [factory = std::move(factory)](EditField<Row, Value> field, bool readOnly) mutable {
180 return detail::Access::erase(std::invoke(factory, std::move(field), readOnly));
181 };
182
183 return *this;
184 }
185
187 template <class Caption> void captionSource(Caption source) {
188 config_.captionRecipe = [source = std::move(source)](ControlStyle style) {
189 return ui::label(source, style);
190 };
191 }
192
193 Description<FormField> map(const EditSession<Row> &session) const {
194 auto field = key_.empty() ? session.field(member_) : session.field(key_, member_);
195 auto codec = codec_;
196
197 if constexpr (detail::DefaultFormScalar<Value> && !std::same_as<Value, std::string>) {
198 if (!codec) {
199 codec = detail::defaultFormCodec<Value>();
200 }
201 }
202
203 if (!factory_) {
204 if constexpr (std::same_as<Value, std::string>) {
205 if (!codec && field.definition_ && field.definition_->textCodec) {
206 return detail::boundFormField(caption_, EditField<Row, Value>{}, config_).reference(reference_);
207 }
208 }
209 if (codec && !detail::configureEditTextCodec(field, codec)) {
210 return detail::boundFormField(caption_, EditField<Row, Value>{}, config_).reference(reference_);
211 }
212 }
213
214 if (rule_) {
215 field.editorValidate_ = std::make_shared<const std::function<bool(const Value &)>>(rule_);
216 }
217
218 if (factory_) {
219 FormFieldOptions options{.help = config_.help,
220 .required = config_.required,
221 .requiredMessage = config_.requiredMessage,
222 .appearance = config_.appearance};
223
224 return detail::Access::formField(caption_, factory_(field, config_.editor.options.readOnly), std::move(options),
225 config_.captionRecipe)
226 .reference(reference_);
227 }
228 if constexpr (std::same_as<Value, std::string>) {
229 if (!codec) {
230 // Preserve the native string path and its pre-copy retention admission.
231 return detail::boundFormField(caption_, std::move(field), config_).reference(reference_);
232 }
233 }
234
235 if (!codec) {
236 field = {};
237 }
238
239 return detail::boundFormField(caption_, std::move(field), config_).reference(reference_);
240 }
241
243
244private:
245 std::string caption_;
246 Value Row::*member_{};
247 std::string key_{"", 0};
248 FormFieldConfig config_;
249 Ref<FormField> reference_;
250 std::function<bool(const Value &)> rule_;
251 std::shared_ptr<const EditTextCodec<Value>> codec_;
252 std::function<Description<Widget>(EditField<Row, Value>, bool)> factory_;
253};
254
259template <class Row, class Value> FormFieldMapping<Row, Value> field(std::string_view caption, Value Row::*member) {
260 return FormFieldMapping<Row, Value>(caption, member);
261}
262
270template <class Caption, class Row, class Value>
271 requires(!std::convertible_to<Caption, std::string_view>) && requires(Caption caption) { ui::label(caption); }
272FormFieldMapping<Row, Value> field(Caption caption, Value Row::*member) {
273 FormFieldMapping<Row, Value> result("", member);
274
275 result.captionSource(std::move(caption));
276
277 return result;
278}
279
280namespace detail {
282template <class Row, class Value>
283Description<FormField> formChild(const EditSession<Row> &session, const FormFieldMapping<Row, Value> &mapping) {
284 return mapping.map(session);
285}
286
287template <class Row, DescriptionInput Child> Child &&formChild(const EditSession<Row> &, Child &&child) {
288 return std::forward<Child>(child);
289}
290
292} // namespace detail
293
302template <class Row, class... Mappings>
303Description<Column> form(const EditSession<Row> &session, Mappings &&...mappings) {
304 return vbox(LinearOptions{.gap = 1}, detail::formChild(session, std::forward<Mappings>(mappings))...);
305}
306} // namespace portals::ui
Lay visible children from left to right.
Definition layout.hpp:250
Single-use owning construction recipe; no mounted widget ownership.
Definition ui_description.hpp:48
UI-thread-affine typed field capability with exact-target retained inspection and discard.
Definition ui_model_edit.hpp:139
Owning UI-thread-affine current-record editing descriptor; no runtime is created before mount....
Definition ui_model_edit.hpp:48
Owning declaration of one current-record field; no widget or edit runtime is created here.
Definition ui_model_form.hpp:92
FormFieldMapping & editor(Factory factory)
Definition ui_model_form.hpp:178
FormFieldMapping & key(std::string_view key)
Definition ui_model_form.hpp:156
FormFieldMapping & codec(EditTextCodec< Value > codec)
Definition ui_model_form.hpp:166
~FormFieldMapping()=default
Keep owning configuration transfers fallible before mount.
FormFieldMapping(std::string_view caption, Value Row::*member)
Definition ui_model_form.hpp:100
FormFieldMapping & required(bool enabled=true)
Definition ui_model_form.hpp:106
FormFieldMapping & options(FormFieldConfig config)
Definition ui_model_form.hpp:133
FormFieldMapping & validate(std::function< bool(const Value &)> rule)
Definition ui_model_form.hpp:124
FormFieldMapping & reference(Ref< FormField > reference)
Definition ui_model_form.hpp:147
FormFieldMapping & readOnly(bool enabled=true)
Definition ui_model_form.hpp:115
Typed Widget observation; explicit composite specializations reuse their existing weak facades.
Definition ui_ref.hpp:761
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
@ 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
Owning scalar conversion used by the existing current-record text editor.
Definition ui_model_edit.hpp:21
Standard text editor configuration; validation observes the normal publication boundaries.
Definition ui_form_field.hpp:26
static Description< Widget > erase(Description< T > description)
Erase only the observer type while preserving one mount transaction.
Definition ui_description.hpp:3534