Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
model_edit.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <limits>
6#include <portals/form_field.hpp>
7#include <portals/managed_navigation.hpp>
8#include <portals/model_edit.hpp>
9
10namespace portals::detail {
12struct ManagedModelSource;
13
15struct EditTextDraft {
16 explicit EditTextDraft(GraphemeText value, const LineEditOptions &options)
17 : text(std::move(value)),
18 history(std::make_unique<TextHistory>(options.history, options.widthPolicy, options.limits)),
19 limits(options.limits) {
20 }
21
22 GraphemeText text;
23 std::unique_ptr<GraphemeText> mask;
24 std::unique_ptr<TextHistory> history;
25 TextLimits limits;
26 std::size_t cursor{};
27 std::size_t anchor{};
28 std::int32_t scroll{};
29 std::uint64_t revision{};
30 std::uint64_t generation{};
31 bool initialized{};
32 bool dirty{};
33};
34
36struct EditStorageAccess {
37 static std::expected<std::size_t, TreeError> draftBytes(const GraphemeText &text, const GraphemeText *mask,
38 std::size_t history) noexcept {
39 auto total = sizeof(EditTextDraft) + sizeof(TextHistory);
40 auto add = [&](std::size_t bytes) {
41 if (bytes > std::numeric_limits<std::size_t>::max() - total) {
42 return false;
43 }
44 total += bytes;
45 return true;
46 };
47 auto payload = [&](const GraphemeText &value) {
48 return value.graphemes_.capacity() <= std::numeric_limits<std::size_t>::max() / sizeof(Grapheme) &&
49 add(value.bytes_.capacity()) && add(1) && add(value.graphemes_.capacity() * sizeof(Grapheme));
50 };
51 if (!payload(text) || (mask && (!add(sizeof(GraphemeText)) || !payload(*mask))) || !add(history)) {
52 return std::unexpected(TreeError::NODE_LIMIT);
53 }
54 return total;
55 }
56};
57
58// Preserve a typed scalar conversion error across the existing bool validation callback.
59struct EditTextFailure {
60 TreeError error;
61};
62
64struct EditTextBinding {
65 Node<LineEdit> receiver;
66 EditTextDraft *displayed{};
67 EditTextDraft *prepared{};
68 void *displayedEntry{};
69 void *preparedEntry{};
70 LineEditStatus publicationStatus{LineEditStatus::FAILED};
71 bool validatedByEditor{};
72 bool asynchronous{};
73 const void *definition{};
74 bool readOnly{};
75 bool writable{};
76 bool retentionRefused{};
77 bool closed{};
78 std::function<std::expected<std::shared_ptr<const FieldValidation>, TreeError>()> validation;
79 std::function<std::expected<void, TreeError>(std::shared_ptr<const FieldValidation>)> displayValidation;
80 std::function<std::expected<void, TreeError>()> preserveValidation;
81 std::function<std::expected<bool, TreeError>(std::string_view)> checkCandidate;
82 std::function<std::expected<bool, TreeError>(std::string_view)> validateBeforeParse;
83 std::function<std::expected<bool, TreeError>(std::string_view)> validateForCommit;
84 std::function<std::expected<void, TreeError>(const LineEditOptions &)> initialize;
85 std::function<std::expected<void, TreeError>()> prepare;
86 std::function<std::expected<void, TreeError>(const NavigationPosition &, const std::shared_ptr<ManagedModelSource> &)>
87 preflight;
88 std::function<std::expected<std::shared_ptr<const PropertyChange<std::string>>, TreeError>()> snapshot;
89 std::function<std::expected<std::shared_ptr<const PropertyChange<std::string>>, TreeError>()> cancelSnapshot;
90 std::function<std::expected<std::optional<PropertyUpdate<std::string>>, TreeError>(const std::string &)> publish;
91 std::function<std::expected<void, TreeError>(std::size_t, bool)> admit;
92 std::function<void()> accepted;
93 std::function<std::expected<Subscription, TreeError>(Node<LineEdit>, std::function<void()>)> observe;
94 std::function<std::expected<LineEditResult, TreeError>(bool)> complete;
95 std::function<std::expected<LineEditResult, TreeError>(const std::string &)> setText;
96};
97
98struct EditNavigationParticipant {
99 virtual ~EditNavigationParticipant() = default;
100 virtual std::expected<void, TreeError> beforeTransition(const NavigationPosition *,
101 const std::shared_ptr<ManagedModelSource> &) = 0;
102 virtual void closed() noexcept = 0;
103};
104
106} // namespace portals::detail
std::expected< void, ProcessContextError > initialize(int argc, const char *const *argv)
Capture argv and the environment once, before any concurrent foreign environment mutation....
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
LineEditStatus
Observable editing or service outcome; rejection preserves the draft.
Definition line_edit.hpp:81
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26