Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_combo_box.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/combo_box.hpp>
6#include <portals/ui_scene_pipeline.hpp>
7#include <portals/ui_suggestions.hpp>
8
9namespace portals::ui {
12 ~ComboBoxConfig() = default;
14 .options = {.limits = {4096, 4096, 4096,
15 8192}}};
17 std::size_t pageRows{32};
18 std::int32_t popupRows{8};
19 std::int32_t popupWidth{60};
20 bool searchable{true};
21 std::optional<std::uint64_t> initial;
22 std::function<void(SearchError)> failed;
23 std::size_t browseScratchBytes{8 * 1024 *
24 1024};
25};
26
29 bool enabled;
30
35 template <std::derived_from<ComboBox> T>
37 return std::move(description).fuzzy(modifier.enabled);
38 }
39};
40
44inline ChoiceSearchModifier fuzzy(bool enabled = true) {
45 return {enabled};
46}
47
49inline ChoiceSearchModifier prefix() {
50 return {false};
51}
52
53namespace detail {
55Description<ComboBox> comboBoxRecipe(std::shared_ptr<portals::detail::FindModelSource>, ComboBoxConfig);
56Description<DropDown> dropDownRecipe(std::shared_ptr<portals::detail::FindModelSource>, ComboBoxConfig);
57std::shared_ptr<portals::detail::FindModelSource> choiceStrings(std::initializer_list<std::string_view>);
58std::expected<void, ManagedError> choiceSelectHandler(const std::shared_ptr<RefTarget> &,
59 std::function<void(ChoiceSelection)>, std::source_location);
60
61template <class Projection> struct ChoiceProjection {
62 Projection projection;
63 std::size_t maxBytes{4096};
64
65 template <class T>
66 requires(std::same_as<std::remove_cvref_t<std::invoke_result_t<const Projection &, const T &>>, std::string> ||
67 std::same_as<std::invoke_result_t<const Projection &, const T &>, std::string_view> ||
68 (std::is_lvalue_reference_v<std::invoke_result_t<const Projection &, const T &>> &&
69 std::convertible_to<std::invoke_result_t<const Projection &, const T &>, std::string_view>))
70 auto operator()(const T &row) const {
71 using Result = std::invoke_result_t<const Projection &, const T &>;
72 if constexpr (std::same_as<std::remove_cvref_t<Result>, std::string> && !std::is_lvalue_reference_v<Result>) {
73 auto text = std::invoke(projection, row);
74 if (text.size() > maxBytes) {
75 throw portals::detail::ProjectionLimit{};
76 }
77 return text;
78 } else {
79 return std::string_view(std::invoke(projection, row));
80 }
81 }
82};
83
84template <class Projection, class T>
85concept ChoiceProjectionFor = std::is_nothrow_move_constructible_v<Projection> &&
86 (std::same_as<std::invoke_result_t<const Projection &, const T &>, std::string_view> ||
87 std::same_as<std::invoke_result_t<const Projection &, const T &>, std::string>);
88
89template <class T, class Projection>
90 requires ChoiceProjectionFor<Projection, T>
91auto choiceSource(ManagedModel<T> source, Projection projection) {
92 return std::make_shared<FindModelSourceOf<T, Projection>>(std::move(source), std::move(projection));
93}
94
95template <class T, class Projection>
96 requires ChoiceProjectionFor<Projection, T>
97auto choiceSource(std::shared_ptr<const ModelSnapshot<T>> source, Projection projection) {
98 return std::make_shared<FindModelSourceOf<T, Projection>>(std::move(source), std::move(projection));
99}
100
101template <class T, class Projection>
102 requires ChoiceProjectionFor<Projection, T>
103auto choiceSource(std::expected<RowCollection<T>, TreeError> source, Projection projection) {
104 return std::make_shared<FindCollectionSourceOf<T, Projection>>(std::move(source), std::move(projection));
105}
106
107template <class T, class Projection>
108 requires ChoiceProjectionFor<Projection, T>
109auto choiceSource(RowCollection<T> source, Projection projection) {
110 return choiceSource(std::expected<RowCollection<T>, TreeError>(std::move(source)), std::move(projection));
111}
112
113template <class Source, class Projection>
114concept ChoiceSource = requires(Source source, Projection projection) {
115 choiceSource(std::move(source), ChoiceProjection<Projection>{std::move(projection)});
116};
118} // namespace detail
119
128template <class Source, class Projection>
129 requires detail::ChoiceSource<Source, Projection>
130Description<ComboBox> comboBox(Source source, Projection projection, ComboBoxConfig config = {}) {
131 auto binding = detail::choiceSource(
132 std::move(source), detail::ChoiceProjection<Projection>{std::move(projection), config.search.maxRowBytes});
133 return detail::comboBoxRecipe(std::move(binding), std::move(config));
134}
135
137template <class Source, class Projection>
138 requires detail::ChoiceSource<Source, Projection>
139Description<DropDown> dropDown(Source source, Projection projection, ComboBoxConfig config = {}) {
140 auto binding = detail::choiceSource(
141 std::move(source), detail::ChoiceProjection<Projection>{std::move(projection), config.search.maxRowBytes});
142 return detail::dropDownRecipe(std::move(binding), std::move(config));
143}
144
149inline Description<ComboBox> comboBox(std::initializer_list<std::string_view> values, ComboBoxConfig config = {}) {
150 return detail::comboBoxRecipe(detail::choiceStrings(values), std::move(config));
151}
152
154inline Description<DropDown> dropDown(std::initializer_list<std::string_view> values, ComboBoxConfig config = {}) {
155 return detail::dropDownRecipe(detail::choiceStrings(values), std::move(config));
156}
157
162template <class Source, class Projection>
163 requires detail::ChoiceSource<Source, Projection>
164Description<ComboBox> comboBox(Ref<ComboBox> reference, Source source, Projection projection,
165 ComboBoxConfig config = {}) {
166 return comboBox(std::move(source), std::move(projection), std::move(config)).reference(std::move(reference));
167}
168
170template <class Source, class Projection>
171 requires detail::ChoiceSource<Source, Projection>
172Description<DropDown> dropDown(Ref<DropDown> reference, Source source, Projection projection,
173 ComboBoxConfig config = {}) {
174 return dropDown(std::move(source), std::move(projection), std::move(config)).reference(std::move(reference));
175}
176
180inline Description<ComboBox> comboBox(Ref<ComboBox> reference, std::initializer_list<std::string_view> values,
181 ComboBoxConfig config = {}) {
182 return comboBox(values, std::move(config)).reference(std::move(reference));
183}
184
186inline Description<DropDown> dropDown(Ref<DropDown> reference, std::initializer_list<std::string_view> values,
187 ComboBoxConfig config = {}) {
188 return dropDown(values, std::move(config)).reference(std::move(reference));
189}
190
196template <std::derived_from<ComboBox> T, class Callback = detail::QuitCommand>
197 requires portals::detail::ManagedAction<Callback, ChoiceSelection>
198auto onSelect(Ref<T> reference, Callback callback, std::source_location origin = std::source_location::current()) {
199 detail::commandOrigin(callback, origin);
200 return detail::SceneStage{[registration = [reference = std::move(reference), callback = std::move(callback),
201 origin](ManagedScene &scene) mutable -> std::expected<void, ManagedError> {
202 auto adapter = detail::Access::choiceHandler(scene, reference.node(), std::move(callback), origin);
203
204 return detail::choiceSelectHandler(detail::NamedAccess::target(reference), std::move(adapter), origin);
205 },
206 origin](Scene &owner) mutable {
207 detail::SceneAccess::onEditing(owner, registration, origin);
208 }};
209}
210} // 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
Single-use lazy owner of one Application and ManagedScene; no second runtime. Construct and operate o...
Definition ui_scene.hpp:253
auto reference(ui::Ref< T > value)
Definition ui_static.hpp:572
SearchError
Search/provider errors, separate from scheduler and UI delivery errors.
Definition search.hpp:12
@ PREFIX
Exact normalized prefix of the complete label or a whitespace token.
Definition search.hpp:25
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
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
Configurable finite source, index, query and result budgets.
Definition search.hpp:30
Owning choice matching stage, preserving every worker budget.
Definition ui_combo_box.hpp:28
bool enabled
True selects FUZZY_PREFIX; false selects PREFIX.
Definition ui_combo_box.hpp:29
friend Description< T > operator|(Description< T > &&description, ChoiceSearchModifier modifier)
Definition ui_combo_box.hpp:36
Independent finite browse, popup and indexed-completion settings.
Definition ui_combo_box.hpp:11
bool searchable
DropDown popup offers a separate ordinary query editor.
Definition ui_combo_box.hpp:20
std::size_t pageRows
Positive browse page cap, at most256.
Definition ui_combo_box.hpp:17
std::size_t browseScratchBytes
Transient projection pins/range metadata; output has row/text caps.
Definition ui_combo_box.hpp:23
std::function< void(SearchError)> failed
Newest provider failure; draft and accepted value survive.
Definition ui_combo_box.hpp:22
std::optional< std::uint64_t > initial
Optional source ordinal resolved asynchronously after mount.
Definition ui_combo_box.hpp:21
LineEditConfig editor
Normal editor modes, validation and history; protected display is rejected.
Definition ui_combo_box.hpp:13
std::int32_t popupWidth
Positive maximum panel width in terminal cells.
Definition ui_combo_box.hpp:19
SearchOptions search
Finite search budgets; PREFIX by default.
Definition ui_combo_box.hpp:16
std::int32_t popupRows
Positive visible list height in terminal rows.
Definition ui_combo_box.hpp:18
Owning optional editor customization and confirmation policy.
Definition ui_description.hpp:34
static std::shared_ptr< RefTarget > target(const DynamicRef &reference)
Definition ui_ref.hpp:2704
static void onEditing(Scene &owner, Callback &callback, std::source_location origin)
Definition ui_scene.hpp:1289
Owning minimal pipe adapter forwarding to the same facade method.
Definition ui_scene_pipeline.hpp:29