Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_scene_recipes.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/ui_mount.hpp>
6#include <portals/ui_scene.hpp>
7
8namespace portals::ui {
9
10namespace detail {
14template <class T> struct IsSceneRecipe : IsDescription<T> {};
15
19template <class T> struct IsSceneRecipe<std::expected<Description<T>, ManagedError>> : std::true_type {};
20
24template <class T> struct IsAppResult : IsSceneRecipe<T> {};
25
27template <> struct IsAppResult<Scene> : std::true_type {};
28
30template <> struct IsAppResult<std::expected<Scene, ManagedError>> : std::true_type {};
31
35template <class Factory>
36concept AppFactory = std::invocable<Factory &> && !std::is_reference_v<std::invoke_result_t<Factory &>> &&
38
42template <class Factory>
43concept SceneFactory =
44 (std::invocable<Factory &, ManagedScene &> && !std::is_reference_v<std::invoke_result_t<Factory &, ManagedScene &>> &&
46 (!std::invocable<Factory &, ManagedScene &> && std::invocable<Factory &, ManagedScene &, Application &> &&
47 !std::is_reference_v<std::invoke_result_t<Factory &, ManagedScene &, Application &>> &&
48 IsSceneRecipe<std::remove_cvref_t<std::invoke_result_t<Factory &, ManagedScene &, Application &>>>::value);
49
53template <class Setup>
54concept AppSetup = requires(Setup &setup, Scene &owner) {
55 { setup(owner) } -> std::same_as<void>;
56} || requires(Setup &setup, Scene &owner) {
57 { setup(owner) } -> std::same_as<std::expected<void, ManagedError>>;
58};
59
60} // namespace detail
61
68template <class T>
69Scene scene(Description<T> &&description, SceneOptions options = {},
70 std::source_location origin = std::source_location::current()) {
72 [description = std::move(description),
73 controls = options.controls](ManagedScene &owner) mutable -> std::expected<void, ManagedError> {
74 auto mounted = mount(owner, std::move(description), controls);
75
76 return mounted ? std::expected<void, ManagedError>{} : std::unexpected(mounted.error());
77 },
78 options, origin);
79}
80
93template <detail::SceneFactory Factory>
94Scene scene(Factory factory, SceneOptions options = {}, std::source_location origin = std::source_location::current()) {
95 return detail::SceneAccess::create(
96 [factory = std::move(factory),
97 controls = options.controls](ManagedScene &owner) mutable -> std::expected<void, ManagedError> {
98 auto effectiveControls = controls;
99 auto installed = detail::ControlContextAccess::installFallbackSchema(effectiveControls);
100
101 if (!installed) {
102 return std::unexpected(detail::controlContextError(installed.error(), owner.root()));
103 }
104
105 if (detail::ControlContextAccess::hasUserConfiguration(effectiveControls)) {
106 for (const auto name : {"Border", "Gauge", "Spinner"}) {
107 installed = detail::ControlContextAccess::installPrimitiveSchema(effectiveControls, name);
108
109 if (!installed) {
110 return std::unexpected(detail::controlContextError(installed.error(), owner.root()));
111 }
112 }
113
114 for (const auto name : {"ScrollArea", "ScrollBar", "LogView"}) {
115 installed = detail::ControlContextAccess::installScrollSchema(effectiveControls, name);
116
117 if (!installed) {
118 return std::unexpected(detail::controlContextError(installed.error(), owner.root()));
119 }
120 }
121 for (const auto name : {"HelpView", "KeyHints"}) {
122 installed = detail::ControlContextAccess::installKeyHelpSchema(effectiveControls, name);
123 if (!installed) {
124 return std::unexpected(detail::controlContextError(installed.error(), owner.root()));
125 }
126 }
127 for (const auto name : {"VirtualList", "VirtualTable", "Chart"}) {
128 installed = detail::ControlContextAccess::installVirtualViewSchema(effectiveControls, name);
129
130 if (!installed) {
131 return std::unexpected(detail::controlContextError(installed.error(), owner.root()));
132 }
133 }
134
135 for (const auto name : {"OverlayHost", "Dialog"}) {
136 installed = detail::ControlContextAccess::installDialogSchema(effectiveControls, name);
137
138 if (!installed) {
139 return std::unexpected(detail::controlContextError(installed.error(), owner.root()));
140 }
141 }
142
143 installed = detail::ControlContextAccess::installNotificationSchema(effectiveControls);
144
145 if (!installed) {
146 return std::unexpected(detail::controlContextError(installed.error(), owner.root()));
147 }
148
149 installed = detail::ControlContextAccess::installThemeCatalogSchema(effectiveControls);
150
151 if (!installed) {
152 return std::unexpected(detail::controlContextError(installed.error(), owner.root()));
153 }
154
155 installed = detail::ControlContextAccess::installMessageSchema(effectiveControls);
156
157 if (!installed) {
158 return std::unexpected(detail::controlContextError(installed.error(), owner.root()));
159 }
160
161 installed = detail::ControlContextAccess::installDataStateSchema(effectiveControls);
162
163 if (!installed) {
164 return std::unexpected(detail::controlContextError(installed.error(), owner.root()));
165 }
166
167 installed = detail::ControlContextAccess::installOverlaySchema(effectiveControls);
168 if (!installed) {
169 return std::unexpected(detail::controlContextError(installed.error(), owner.root()));
170 }
171 }
172 auto prepared = effectiveControls.prepared();
173 if (!prepared) {
174 return std::unexpected(detail::controlContextError(prepared.error(), owner.root()));
175 }
176 auto recipe = [&] {
177 if constexpr (std::invocable<Factory &, ManagedScene &>) {
178 return std::invoke(factory, owner);
179 } else {
180 return std::invoke(factory, owner, detail::SceneAccess::application(owner));
181 }
182 }();
183
184 if constexpr (detail::IsDescription<decltype(recipe)>::value) {
185 auto mounted = mount(owner, std::move(recipe), *prepared);
186
187 return mounted ? std::expected<void, ManagedError>{} : std::unexpected(mounted.error());
188 } else {
189 if (!recipe) {
190 return std::unexpected(recipe.error());
191 }
192
193 auto mounted = mount(owner, std::move(*recipe), *prepared);
194
195 return mounted ? std::expected<void, ManagedError>{} : std::unexpected(mounted.error());
196 }
197 },
198 options, origin);
199}
200
201} // namespace portals::ui
Single-use lazy owner of one Application and ManagedScene; no second runtime. Construct and operate o...
Definition ui_scene.hpp:253
Admit only no-argument factories returning owning supported app results.
Definition ui_scene_recipes.hpp:36
Admit exact stationary setup results.
Definition ui_scene_recipes.hpp:54
Select the legacy factory signature before considering an Application borrow.
Definition ui_scene_recipes.hpp:43
Admit owning app results without widening managed root-recipe factories.
Definition ui_scene_recipes.hpp:24
Recognize an owning recipe after cv/ref normalization.
Definition ui_recipe_context.hpp:35
Admit only owning descriptions or their exact managed checked result.
Definition ui_scene_recipes.hpp:14
static Scene create(portals::detail::MoveOnlyFunction< std::expected< void, ManagedError >(ManagedScene &)> recipe, SceneOptions options, std::source_location origin)
Definition ui_scene.hpp:1445