Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_mount.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/ui_layout.hpp>
6
7namespace portals::ui {
8namespace detail {
9template <class T>
10std::expected<Node<T>, ManagedError> mountAt(ManagedScene &, Node<Widget>, Description<T> &&, ControlContext);
11}
12
22template <class T>
23std::expected<Node<T>, ManagedError> detail::mountAt(ManagedScene &scene, Node<Widget> mountParent,
24 Description<T> &&description, ControlContext controls) try {
25 auto admitted = detail::Access::check(scene, mountParent);
26 if (!admitted) {
27 auto consumed = std::move(description);
28 return std::unexpected(admitted.error());
29 }
30
31 if (detail::Access::needsGridContext(description)) {
33
34 if (!installed) {
35 auto consumed = std::move(description);
36 return std::unexpected(detail::controlContextError(installed.error(), scene.root()));
37 }
38 }
39
41 for (const auto name : {"Border", "Gauge", "Spinner"}) {
42 auto installed = detail::ControlContextAccess::installPrimitiveSchema(controls, name);
43
44 if (!installed) {
45 auto consumed = std::move(description);
46
47 return std::unexpected(detail::controlContextError(installed.error(), scene.root()));
48 }
49 }
50
51 for (const auto name : {"ScrollArea", "ScrollBar", "LogView"}) {
52 auto installed = detail::ControlContextAccess::installScrollSchema(controls, name);
53
54 if (!installed) {
55 auto consumed = std::move(description);
56
57 return std::unexpected(detail::controlContextError(installed.error(), scene.root()));
58 }
59 }
60 for (const auto name : {"HelpView", "KeyHints"}) {
61 auto installed = detail::ControlContextAccess::installKeyHelpSchema(controls, name);
62 if (!installed) {
63 auto consumed = std::move(description);
64 return std::unexpected(detail::controlContextError(installed.error(), scene.root()));
65 }
66 }
67 for (const auto name : {"VirtualList", "VirtualTable", "Chart"}) {
68 auto installed = detail::ControlContextAccess::installVirtualViewSchema(controls, name);
69
70 if (!installed) {
71 auto consumed = std::move(description);
72 return std::unexpected(detail::controlContextError(installed.error(), scene.root()));
73 }
74 }
75
76 for (const auto name : {"OverlayHost", "Dialog"}) {
77 auto installed = detail::ControlContextAccess::installDialogSchema(controls, name);
78
79 if (!installed) {
80 auto consumed = std::move(description);
81
82 return std::unexpected(detail::controlContextError(installed.error(), scene.root()));
83 }
84 }
85
86 auto notificationInstalled = detail::ControlContextAccess::installNotificationSchema(controls);
87
88 if (!notificationInstalled) {
89 auto consumed = std::move(description);
90
91 return std::unexpected(detail::controlContextError(notificationInstalled.error(), scene.root()));
92 }
93
94 auto selectorInstalled = detail::ControlContextAccess::installThemeCatalogSchema(controls);
95
96 if (!selectorInstalled) {
97 auto consumed = std::move(description);
98 return std::unexpected(detail::controlContextError(selectorInstalled.error(), scene.root()));
99 }
100
101 auto messageInstalled = detail::ControlContextAccess::installMessageSchema(controls);
102
103 if (!messageInstalled) {
104 auto consumed = std::move(description);
105
106 return std::unexpected(detail::controlContextError(messageInstalled.error(), scene.root()));
107 }
108
109 auto dataStateInstalled = detail::ControlContextAccess::installDataStateSchema(controls);
110
111 if (!dataStateInstalled) {
112 auto consumed = std::move(description);
113
114 return std::unexpected(detail::controlContextError(dataStateInstalled.error(), scene.root()));
115 }
116
118 if (!installed) {
119 auto consumed = std::move(description);
120 return std::unexpected(detail::controlContextError(installed.error(), scene.root()));
121 }
122 }
123
124 auto prepared = detail::Access::defersControlValidation(description)
125 ? std::expected<ControlContext, ControlContextError>(controls)
126 : controls.prepared();
127 if (!prepared) {
128 auto consumed = std::move(description);
129 return std::unexpected(detail::controlContextError(prepared.error(), scene.root()));
130 }
131
132 struct Mounting {
133 ManagedScene &scene;
134
135 ~Mounting() {
136 detail::Access::endMount(scene);
137 }
138 } guard{scene};
139
141 detail::MountContext context;
142 context.controls = std::move(*prepared);
143 std::expected<Node<T>, ManagedError> result =
144 std::unexpected(detail::error(scene.root(), TreeError::INVALID_ARGUMENT));
145
146 try {
147 Node<OverlayHost> automaticHost;
148
149 auto needsHost = detail::Access::prepareChoiceHost(description, context.controls);
150 if (!needsHost) {
151 result = std::unexpected(needsHost.error());
152 } else if (*needsHost) {
153 const AxisSize fill{.preferred = 0, .mode = SizeMode::FLEX};
154 auto host = scene.emplace<OverlayHost>(mountParent, OverlayHostOptions{}, SizePolicy{fill, fill});
155
156 if (!host) {
157 result = std::unexpected(host.error());
158 } else {
159 automaticHost = *host;
160 context.root = *host;
161 result = detail::Access::mount(std::move(description), scene, *host, context);
162
163 if (result) {
164 const auto body = context.mountedOuter != Node<Widget>{} ? context.mountedOuter : Node<Widget>(*result);
165 auto bound = scene.overlayHost(*host, body);
166
167 if (!bound) {
168 result = std::unexpected(bound.error());
169 }
170 }
171 }
172 } else {
173 result = detail::Access::mount(std::move(description), scene, mountParent, context);
174 }
175
176 if (result) {
177 for (auto &finalize : context.finalize) {
178 auto completed = finalize();
179 if (!completed) {
180 result = std::unexpected(completed.error());
181 break;
182 }
183 }
184 }
185 if (result) {
186 for (auto node : context.nodes) {
187 auto live = detail::Access::check(scene, node);
188
189 if (!live) {
190 result = std::unexpected(live.error());
191
192 break;
193 }
194 }
195 }
196 } catch (const std::bad_alloc &) {
197 result = std::unexpected(detail::error(context.root, TreeError::NO_MEMORY));
198 } catch (...) {
199 result = std::unexpected(detail::error(context.root, TreeError::CALLBACK_FAILED));
200 }
201
202 if (!result && context.root != Node<Widget>{}) {
203 auto removed = scene.remove(context.root);
204
205 if (!removed) {
206 if (const auto *cause = std::get_if<TreeError>(&removed.error().cause)) {
207 if (*cause != TreeError::STALE_NODE && *cause != TreeError::CLOSED) {
208 result.error().cleanup = *cause;
209 }
210 }
211 }
212 }
213
214 if (result) {
215 context.committed = true;
216 for (auto &publish : context.publish) {
217 publish();
218 }
219 }
220
221 return result;
222} catch (const std::bad_alloc &) {
223 // Transaction storage is constructed before any node exists; preserve the checked allocation boundary too.
224 auto consumed = std::move(description);
225
226 return std::unexpected(detail::error(scene.root(), TreeError::NO_MEMORY));
227}
228
229template <class T>
230std::expected<Node<T>, ManagedError> mount(ManagedScene &scene, Description<T> &&description, ControlContext controls) {
231 return detail::mountAt(scene, scene.root(), std::move(description), std::move(controls));
232}
233
237template <class T> std::expected<Node<T>, ManagedError> mount(ManagedScene &scene, Description<T> &&description) {
238 return mount(scene, std::move(description), ControlContext{});
239}
240
241} // namespace portals::ui
Node()=default
Empty observer; checked access fails.
Cheap owning immutable snapshot of named definitions and defaults. Const snapshot reads are concurren...
Definition ui_control_context.hpp:632
Single-use owning construction recipe; no mounted widget ownership.
Definition ui_description.hpp:48
auto live(ManagedScene &scene, const S &description)
Definition style_static.hpp:1703
@ FLEX
Share spare main-axis cells by weight; allow shrinking towards minimum.
Definition layout.hpp:15
@ CLOSED
The owning tree or source is closed.
Definition widget_types.hpp:31
@ CALLBACK_FAILED
A user extension or callback threw.
Definition widget_types.hpp:37
@ STALE_NODE
The observed node or model consumer is absent or retired.
Definition widget_types.hpp:30
@ INVALID_ARGUMENT
An option, enum value or operation argument is invalid.
Definition widget_types.hpp:27
@ NO_MEMORY
Allocation failed inside the operation boundary.
Definition widget_types.hpp:33
static std::expected< Node< T >, ManagedError > mount(Description< T > &&description, ManagedScene &scene, Node< Widget > parent, MountContext &context)
Mount a root or child inside the same transaction.
Definition ui_description.hpp:3560
static void beginMount(ManagedScene &scene) noexcept
Definition ui_recipe_access.hpp:523
static std::expected< void, ManagedError > check(ManagedScene &scene, Node< Widget > node)
Verify a created node remains accessible before publishing references.
Definition ui_recipe_access.hpp:1394
static bool defersControlValidation(const Description< T > &description)
Definition ui_description.hpp:3149
static bool needsGridContext(const Description< T > &description)
Definition ui_description.hpp:3086
static std::expected< void, ControlContextError > installFallbackSchema(ControlContext &context) noexcept
static std::expected< void, ControlContextError > installDialogSchema(ControlContext &context, std::string_view name) noexcept
static std::expected< void, ControlContextError > installNotificationSchema(ControlContext &context) noexcept
static std::expected< void, ControlContextError > installDataStateSchema(ControlContext &context) noexcept
static std::expected< void, ControlContextError > installOverlaySchema(ControlContext &context) noexcept
static std::expected< void, ControlContextError > installScrollSchema(ControlContext &context, std::string_view name) noexcept
static bool hasUserConfiguration(const ControlContext &context) noexcept
static std::expected< void, ControlContextError > installThemeCatalogSchema(ControlContext &context, bool recipient=false) noexcept
static std::expected< void, ControlContextError > installPrimitiveSchema(ControlContext &context, std::string_view name) noexcept
static std::expected< void, ControlContextError > installKeyHelpSchema(ControlContext &context, std::string_view name) noexcept
static std::expected< void, ControlContextError > installMessageSchema(ControlContext &context) noexcept
static std::expected< void, ControlContextError > installVirtualViewSchema(ControlContext &context, std::string_view name) noexcept
Stack-owned transaction bookkeeping, discarded after one description mount.
Definition ui_recipe_context.hpp:554
ControlContext controls
Immutable owning defaults for this mount transaction.
Definition ui_recipe_context.hpp:555