5#include <portals/detail/find_model_source.hpp>
6#include <portals/ui_find_bar.hpp>
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))) {
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))),
21 std::expected<void, ManagedError> attach(ManagedScene &scene)
override {
23 return attachSnapshot(scene, std::move(initial_));
26 auto valid = portals::detail::ManagedModelAccess::validate(model_, scene.root().id());
28 return valid ? std::expected<void, ManagedError>{}
29 : std::unexpected(ManagedError{ManagedPhase::STATE, scene.root().id(), valid.error(), {}, {}});
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());
37 return std::unexpected(snapshot.error());
40 if (!worker_ || worker_->pin != *snapshot) {
41 worker_ = std::make_shared<const portals::detail::FindWorkerSourceOf<T, Projection>>(*snapshot, projection_);
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));
53 return std::unexpected(model.error());
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_;
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)) {
74 std::expected<void, ManagedError> attach(ManagedScene &scene)
override {
76 return std::unexpected(ManagedError{ManagedPhase::STATE, scene.root().id(), rows_.error(), {}, {}});
80 auto snapshot = std::make_shared<const portals::detail::CollectionSnapshot<T>>(*rows_, 1);
82 return this->attachSnapshot(scene, std::move(snapshot));
86 std::expected<RowCollection<T>,
TreeError> rows_;
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>;
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 ||
97 !portals::detail::validFindSearchOptions(config.search)) {
98 return Access::cold<FindBar>(
99 [](ManagedScene &, Node<Widget> parent,
MountContext &) -> std::expected<Node<FindBar>, ManagedError> {
104 return Access::findBar({}, std::string(initial), std::move(config), std::move(binding));
124template <
class T,
class Projection>
125 requires detail::FindProjection<Projection, T>
126Description<FindBar> findBar(ManagedModel<T> model, Projection projection, std::string_view initialQuery = {},
128 return detail::modelFindBar(
129 std::make_shared<detail::FindModelSourceOf<T, Projection>>(std::move(model), std::move(projection)), initialQuery,
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,
160template <std::copy_constructible T,
class Projection>
161 requires std::equality_comparable<T> && detail::FindProjection<Projection, T>
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));
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 = {},
182 return findBar(std::expected<RowCollection<T>,
TreeError>(std::move(rows)), std::move(projection), initialQuery,
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));
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));
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