Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_model_find.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/detail/find_model_source.hpp>
6#include <portals/ui_find_bar.hpp>
7
8namespace portals::ui {
10namespace detail {
11template <class T, class Projection> struct FindModelSourceOf : portals::detail::FindModelSource {
12 FindModelSourceOf(ManagedModel<T> model, Projection projection)
13 : model_(std::move(model)), projection_(std::make_shared<const Projection>(std::move(projection))) {
14 }
15
16 FindModelSourceOf(std::shared_ptr<const ModelSnapshot<T>> source, Projection projection)
17 : initial_(std::move(source)), projection_(std::make_shared<const Projection>(std::move(projection))),
18 owning_(true) {
19 }
20
21 std::expected<void, ManagedError> attach(ManagedScene &scene) override {
22 if (owning_) {
23 return attachSnapshot(scene, std::move(initial_));
24 }
25
26 auto valid = portals::detail::ManagedModelAccess::validate(model_, scene.root().id());
27
28 return valid ? std::expected<void, ManagedError>{}
29 : std::unexpected(ManagedError{ManagedPhase::STATE, scene.root().id(), valid.error(), {}, {}});
30 }
31
32 std::expected<std::shared_ptr<const portals::detail::FindWorkerSource>, TreeError>
33 capture(ManagedScene &scene) override {
34 auto snapshot = portals::detail::ManagedModelAccess::snapshot(model_, scene.root().id());
35
36 if (!snapshot) {
37 return std::unexpected(snapshot.error());
38 }
39
40 if (!worker_ || worker_->pin != *snapshot) {
41 worker_ = std::make_shared<const portals::detail::FindWorkerSourceOf<T, Projection>>(*snapshot, projection_);
42 }
43
44 return worker_;
45 }
46
47protected:
48 std::expected<void, ManagedError> attachSnapshot(ManagedScene &scene,
49 std::shared_ptr<const ModelSnapshot<T>> snapshot) {
50 auto model = scene.model<T>(std::move(snapshot));
51
52 if (!model) {
53 return std::unexpected(model.error());
54 }
55
56 model_ = *model;
57
58 return {};
59 }
60
61private:
62 ManagedModel<T> model_;
63 std::shared_ptr<const ModelSnapshot<T>> initial_;
64 std::shared_ptr<const Projection> projection_;
65 std::shared_ptr<const portals::detail::FindWorkerSource> worker_;
66 bool owning_{};
67};
68
69template <class T, class Projection> struct FindCollectionSourceOf final : FindModelSourceOf<T, Projection> {
70 FindCollectionSourceOf(std::expected<RowCollection<T>, TreeError> rows, Projection projection)
71 : FindModelSourceOf<T, Projection>(ManagedModel<T>{}, std::move(projection)), rows_(std::move(rows)) {
72 }
73
74 std::expected<void, ManagedError> attach(ManagedScene &scene) override {
75 if (!rows_) {
76 return std::unexpected(ManagedError{ManagedPhase::STATE, scene.root().id(), rows_.error(), {}, {}});
77 }
78
79 // Reuse the existing immutable collection adapter; model creation stays in the mount collector.
80 auto snapshot = std::make_shared<const portals::detail::CollectionSnapshot<T>>(*rows_, 1);
81
82 return this->attachSnapshot(scene, std::move(snapshot));
83 }
84
85private:
86 std::expected<RowCollection<T>, TreeError> rows_;
87};
88
89template <class Projection, class T>
90concept FindProjection = std::is_nothrow_move_constructible_v<Projection> &&
91 std::same_as<std::invoke_result_t<const Projection &, const T &>, std::string_view>;
92
93inline Description<FindBar> modelFindBar(std::shared_ptr<portals::detail::FindModelSource> binding,
94 std::string_view initial, FindBarConfig config) {
95 if (initial.size() > config.editor.options.limits.max_input_bytes ||
96 config.editor.options.limits.max_input_bytes > FindLimits::MAX_QUERY_BYTES ||
97 !portals::detail::validFindSearchOptions(config.search)) {
98 return Access::cold<FindBar>(
99 [](ManagedScene &, Node<Widget> parent, MountContext &) -> std::expected<Node<FindBar>, ManagedError> {
100 return std::unexpected(ManagedError{ManagedPhase::TEXT, parent.id(), TreeError::INVALID_ARGUMENT, {}, {}});
101 });
102 }
103
104 return Access::findBar({}, std::string(initial), std::move(config), std::move(binding));
105}
106} // namespace detail
107
109
124template <class T, class Projection>
125 requires detail::FindProjection<Projection, T>
126Description<FindBar> findBar(ManagedModel<T> model, Projection projection, std::string_view initialQuery = {},
127 FindBarConfig config = {}) {
128 return detail::modelFindBar(
129 std::make_shared<detail::FindModelSourceOf<T, Projection>>(std::move(model), std::move(projection)), initialQuery,
130 std::move(config));
131}
132
142template <class T, class Projection>
143 requires detail::FindProjection<Projection, T>
144Description<FindBar> findBar(std::shared_ptr<const ModelSnapshot<T>> source, Projection projection,
145 std::string_view initialQuery = {}, FindBarConfig config = {}) {
146 return detail::modelFindBar(
147 std::make_shared<detail::FindModelSourceOf<T, Projection>>(std::move(source), std::move(projection)), initialQuery,
148 std::move(config));
149}
150
160template <std::copy_constructible T, class Projection>
161 requires std::equality_comparable<T> && detail::FindProjection<Projection, T>
162Description<FindBar> findBar(std::expected<RowCollection<T>, TreeError> rows, Projection projection,
163 std::string_view initialQuery = {}, FindBarConfig config = {}) {
164 return detail::modelFindBar(
165 std::make_shared<detail::FindCollectionSourceOf<T, Projection>>(std::move(rows), std::move(projection)),
166 initialQuery, std::move(config));
167}
168
178template <std::copy_constructible T, class Projection>
179 requires std::equality_comparable<T> && detail::FindProjection<Projection, T>
180Description<FindBar> findBar(RowCollection<T> rows, Projection projection, std::string_view initialQuery = {},
181 FindBarConfig config = {}) {
182 return findBar(std::expected<RowCollection<T>, TreeError>(std::move(rows)), std::move(projection), initialQuery,
183 std::move(config));
184}
185
195template <class Source, class Projection>
196 requires requires(Source source, Projection projection, std::string_view query, FindBarConfig config) {
197 portals::ui::findBar(std::move(source), std::move(projection), query, std::move(config));
198 }
199Description<FindBar> findBar(Ref<FindBar> reference, Source source, Projection projection,
200 std::string_view initialQuery = {}, FindBarConfig config = {}) {
201 return findBar(std::move(source), std::move(projection), initialQuery, std::move(config))
202 .reference(std::move(reference));
203}
204} // namespace portals::ui
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
@ TEXT
Read or mutate a fluent caption/editor draft through a checked observing reference.
Definition managed_diagnostics.hpp:57
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
@ INVALID_ARGUMENT
An option, enum value or operation argument is invalid.
Definition widget_types.hpp:27
static constexpr std::size_t MAX_QUERY_BYTES
Hard synchronous query ceiling.
Definition find_bar.hpp:34
Owning bounded literal search configuration; the normal editor owns query admission/history.
Definition ui_find_bar.hpp:11
Stack-owned transaction bookkeeping, discarded after one description mount.
Definition ui_recipe_context.hpp:554