5#include <portals/ui_model_edit.hpp>
11namespace portals::ui {
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);
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);
27 constexpr auto SIGN_AND_ROUNDING_BYTES = 3;
28 constexpr auto EXPONENT_PUNCTUATION_BYTES = 4;
29 constexpr auto CONVERSION_BYTES =
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{};
35 if constexpr (std::floating_point<Value>) {
36 if (!std::isfinite(value)) {
40 const auto converted = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value);
42 if (converted.ec != std::errc{}) {
46 return std::expected<std::string, TreeError>(std::in_place, buffer.data(),
47 static_cast<std::size_t
>(converted.ptr - buffer.data()));
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());
60 const auto converted = std::from_chars(
text.data(),
text.data() +
text.size(), value);
62 if (converted.ec != std::errc{} || converted.ptr !=
text.data() +
text.size()) {
65 if constexpr (std::floating_point<Value>) {
66 if (!std::isfinite(value)) {
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>});
125 rule_ = std::move(rule);
134 auto caption = std::move(config_.captionRecipe);
136 config_ = std::move(config);
138 config_.captionRecipe = std::move(caption);
167 codec_ = std::make_shared<const EditTextCodec<Value>>(std::move(
codec));
187 template <
class Caption>
void captionSource(Caption source) {
188 config_.captionRecipe = [source = std::move(source)](
ControlStyle style) {
189 return ui::label(source, style);
193 Description<FormField> map(
const EditSession<Row> &session)
const {
194 auto field = key_.empty() ? session.field(member_) : session.field(key_, member_);
197 if constexpr (detail::DefaultFormScalar<Value> && !std::same_as<Value, std::string>) {
199 codec = detail::defaultFormCodec<Value>();
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_);
209 if (
codec && !detail::configureEditTextCodec(field,
codec)) {
210 return detail::boundFormField(caption_, EditField<Row, Value>{}, config_).
reference(reference_);
215 field.editorValidate_ = std::make_shared<
const std::function<bool(
const Value &)>>(rule_);
219 FormFieldOptions
options{.help = config_.help,
220 .required = config_.required,
221 .requiredMessage = config_.requiredMessage,
222 .appearance = config_.appearance};
224 return detail::Access::formField(caption_, factory_(field, config_.editor.options.readOnly), std::move(
options),
225 config_.captionRecipe)
228 if constexpr (std::same_as<Value, std::string>) {
231 return detail::boundFormField(caption_, std::move(field), config_).reference(reference_);
239 return detail::boundFormField(caption_, std::move(field), config_).reference(reference_);
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_;
270template <
class Caption,
class Row,
class Value>
271 requires(!std::convertible_to<Caption, std::string_view>) &&
requires(Caption caption) { ui::label(caption); }
275 result.captionSource(std::move(caption));
282template <
class Row,
class Value>
283Description<FormField> formChild(
const EditSession<Row> &session,
const FormFieldMapping<Row, Value> &mapping) {
284 return mapping.map(session);
287template <
class Row, DescriptionInput Child> Child &&formChild(
const EditSession<Row> &, Child &&child) {
288 return std::forward<Child>(child);
302template <
class Row,
class... Mappings>
304 return vbox(LinearOptions{.gap = 1}, detail::formChild(session, std::forward<Mappings>(mappings))...);
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
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
static Description< Widget > erase(Description< T > description)
Erase only the observer type while preserving one mount transaction.
Definition ui_description.hpp:3534