Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_action.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/managed.hpp>
6
7namespace portals::ui {
11 ~ActionMetadata() = default;
12 std::string caption = std::string(0, '\0');
13 std::string shortcut = std::string(0, '\0');
14 std::string help = std::string(0, '\0');
15 bool enabled{true};
16 bool visible{true};
17 bool checked{};
18 std::string exclusiveGroup = std::string(0, '\0');
19};
20
23 std::size_t maxActions{256};
24 std::size_t maxPresenters{1024};
25 std::size_t maxMetadataBytes{65536};
26 std::size_t maxKeyBytes{256};
27};
28
34
35class ActionRef;
36
37namespace detail {
39struct QuitCommand;
40struct ActionStore;
41struct ActionRecord;
42struct MountContext;
43struct MenuRegistration;
44
46struct ActionAccess {
47 static EditorCommandResult finish(ManagedScene &, std::expected<void, TreeError>);
48 static EditorCommandResult finish(ManagedScene &, std::expected<void, ManagedError>);
50 static std::expected<void, ManagedError> attach(ManagedScene &, const ActionRef &);
51 static std::expected<void, ManagedError> button(ManagedScene &, Node<Button>, const ActionRef &);
52 static std::expected<void, ManagedError> menu(ManagedScene &, Node<Menu>, MountContext &);
53 static void popupLayer(ManagedScene &, Node<Dialog>, Node<Menu>);
54 static std::expected<void, ManagedError> preparePopup(ManagedScene &, Node<Menu>);
55 static std::expected<void, ManagedError> stageMenu(ManagedScene &, MenuRegistration &);
56 static std::expected<void, ManagedError> validateMenu(ManagedScene &, const MenuRegistration &);
57 static void rollbackMenu(ManagedScene &, const MenuRegistration &);
58 static EventReply routeMenu(ManagedScene &, Node<Menu>, Menu &, const RoutedEvent &);
59 static std::expected<void, TreeError> prepare(ManagedScene &);
60 static void retire(ManagedScene &) noexcept;
61 static EditorCommandResult invoke(const ActionRef &, ActionInvocation, std::source_location);
62 static EditorCommandResult report(const ActionRef &, std::expected<void, TreeError>, std::source_location);
63 static std::expected<void, TreeError> live(const ActionStore &);
64 static std::expected<void, TreeError> dirty(const ActionStore &);
65 static std::expected<void, TreeError> write(const ActionStore &);
66 static std::shared_ptr<ActionStore> retain(const ActionRef &);
67};
68
70} // namespace detail
71
75class Action {
76public:
77 using Result = std::variant<std::expected<void, TreeError>, std::expected<void, ManagedError>,
79 using Callback = std::function<Result(const ActionInvocation &)>;
80
84 template <class CallbackType>
85 requires(std::copy_constructible<CallbackType> && portals::detail::ManagedAction<CallbackType, ActionInvocation>)
87 : metadata(std::move(metadata)),
88 callback([callback = std::move(callback)]([[maybe_unused]] const ActionInvocation &invocation) mutable -> Result {
89 const auto invoke = [&]() -> decltype(auto) {
90 if constexpr (std::invocable<CallbackType &, const ActionInvocation &>) {
91 return callback(invocation);
92 } else {
93 return callback();
94 }
95 };
96
97 if constexpr (std::same_as<decltype(invoke()), void>) {
98 invoke();
99
100 return std::expected<void, TreeError>{};
101 } else if constexpr (std::same_as<decltype(invoke()), EditorCommandResult> ||
102 std::same_as<decltype(invoke()), std::expected<void, ManagedError>>) {
103 return invoke();
104 } else {
105 auto result = invoke();
106
107 return result ? std::expected<void, TreeError>{} : std::unexpected(result.error());
108 }
109 }) {
110 }
111
114};
115
118public:
120 ActionRef() = default;
121
123 std::expected<std::shared_ptr<const ActionMetadata>, TreeError> snapshot() const;
128 EditorCommandResult set(ActionMetadata metadata, std::source_location origin = std::source_location::current()) const;
133 EditorCommandResult rebind(Action action, std::source_location origin = std::source_location::current()) const;
135 EditorCommandResult remove(std::source_location origin = std::source_location::current()) const;
141 std::source_location origin = std::source_location::current()) const;
146
147private:
148 friend class ActionSet;
149 friend struct detail::ActionAccess;
150 std::expected<std::shared_ptr<detail::ActionRecord>, TreeError> lock() const;
151 std::weak_ptr<detail::ActionStore> store_;
152 std::weak_ptr<detail::ActionRecord> record_;
153 std::thread::id owner_{};
154 std::optional<TreeError> failure_;
155};
156
163public:
165 explicit ActionSet(ActionSetOptions options = {});
167 ActionSet(ActionSet &&other) noexcept;
169 ActionSet &operator=(ActionSet &&other) noexcept;
170 ActionSet(const ActionSet &) = delete;
171 ActionSet &operator=(const ActionSet &) = delete;
174
179 std::expected<ActionRef, TreeError> add(std::string_view key, Action action);
183 ActionRef action(std::string_view key, Action action);
184
188 template <class Callback = detail::QuitCommand>
189 requires(std::copy_constructible<Callback> && portals::detail::ManagedAction<Callback, ActionInvocation>)
190 ActionRef action(std::string_view key, std::string_view caption, Callback callback) {
191 try {
192 ActionMetadata metadata;
193
194 metadata.caption = caption;
195
196 return action(key, Action{std::move(metadata), std::move(callback)});
197 } catch (const std::bad_alloc &) {
198 return failed(TreeError::NO_MEMORY);
199 } catch (...) {
200 return failed(TreeError::CALLBACK_FAILED);
201 }
202 }
203
207 template <class Callback = detail::QuitCommand>
208 requires(std::copy_constructible<Callback> && portals::detail::ManagedAction<Callback, ActionInvocation>)
209 std::expected<ActionRef, TreeError> add(std::string_view key, std::string_view caption, Callback callback) {
210 try {
211 ActionMetadata metadata;
212
213 metadata.caption = caption;
214
215 return add(key, Action{std::move(metadata), std::move(callback)});
216 } catch (const std::bad_alloc &) {
217 return std::unexpected(TreeError::NO_MEMORY);
218 } catch (...) {
219 return std::unexpected(TreeError::CALLBACK_FAILED);
220 }
221 }
222
223private:
224 ActionRef failed(TreeError failure) const;
225 std::shared_ptr<detail::ActionStore> store_;
226};
227
228namespace detail {
230
231template <class Callback> auto retainAction(const Callback &callback) {
232 if constexpr (std::same_as<Callback, ActionRef>) {
233 return ActionAccess::retain(callback);
234 } else {
235 return std::monostate{};
236 }
237}
238
240} // namespace detail
241} // namespace portals::ui
Owning checked editor command, preserving exactly its collector reporting receipt....
Definition editor_command.hpp:23
Additive lifecycle owner over one existing Application subtree. UI affine and nonmovable....
Definition managed.hpp:530
Initially visible action-backed command list, without popup or submenu behavior. Actions own callback...
Definition menu.hpp:21
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
Weak UI-affine action identity; owner retirement never leaves an executable callback.
Definition ui_action.hpp:117
EditorCommandResult rebind(Action action, std::source_location origin=std::source_location::current()) const
ActionRef()=default
Empty weak identity; operations return CLOSED.
EditorCommandResult remove(std::source_location origin=std::source_location::current()) const
EditorCommandResult operator()(const ClickEvent &event) const
EditorCommandResult invoke(ActionInvocation invocation={}, std::source_location origin=std::source_location::current()) const
EditorCommandResult set(ActionMetadata metadata, std::source_location origin=std::source_location::current()) const
std::expected< std::shared_ptr< const ActionMetadata >, TreeError > snapshot() const
EditorCommandResult operator()(const KeyEvent &event) const
std::expected< ActionRef, TreeError > add(std::string_view key, std::string_view caption, Callback callback)
Definition ui_action.hpp:209
ActionSet & operator=(ActionSet &&other) noexcept
ActionSet(ActionSetOptions options={})
ActionRef action(std::string_view key, Action action)
ActionRef action(std::string_view key, std::string_view caption, Callback callback)
Definition ui_action.hpp:190
~ActionSet()
Release cold declarations on UI; attached scenes/recipes retain their own ownership.
std::expected< ActionRef, TreeError > add(std::string_view key, Action action)
ActionSet(ActionSet &&other) noexcept
One owning callback and its metadata; construct before checked set admission. Captures and metadata c...
Definition ui_action.hpp:75
std::function< Result(const ActionInvocation &)> Callback
Owning compact callback adapter.
Definition ui_action.hpp:79
ActionMetadata metadata
Owning metadata admitted with the callback.
Definition ui_action.hpp:112
std::variant< std::expected< void, TreeError >, std::expected< void, ManagedError >, EditorCommandResult > Result
Exact callback outcome.
Definition ui_action.hpp:77
Action(ActionMetadata metadata, CallbackType callback)
Definition ui_action.hpp:86
Callback callback
Single owning callback; admitted invocations pin this object.
Definition ui_action.hpp:113
A compact action optionally receives owning event parameters.
Definition managed.hpp:239
EventReply
Explicit bubbling decision; ignored continues toward the original root.
Definition action.hpp:49
std::variant< ActionEvent, KeyEvent, TextInputEvent, PasteInputEvent, MouseInputEvent > RoutedEvent
Synchronous input value; callback borrows it only until return.
Definition action.hpp:47
ClickSource
Origin of an admitted activation, without inferred physical input.
Definition action.hpp:24
@ PROGRAMMATIC
Explicit semantic action, the default for caller-created ActionEvent.
Definition action.hpp:25
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
@ CALLBACK_FAILED
A user extension or callback threw.
Definition widget_types.hpp:37
@ NO_MEMORY
Allocation failed inside the operation boundary.
Definition widget_types.hpp:33
Owning activation parameters copied from the existing router delivery. target is the activated button...
Definition controls.hpp:204
Observed modifiers, also usable by a native input adapter.
Definition input.hpp:34
Owning key value without an inferred physical keyboard scan code.
Definition input.hpp:213
Owning synchronous invocation context; no widget or scene ownership.
Definition ui_action.hpp:30
ClickSource source
Original explicit, keyboard or pointer source.
Definition ui_action.hpp:31
InputModifiers modifiers
Original input intent where supplied.
Definition ui_action.hpp:32
Owning shared command presentation; shortcut text does not install a global binding.
Definition ui_action.hpp:9
bool enabled
Immediate callback admission gate.
Definition ui_action.hpp:15
bool visible
Shared presenter visibility and invocation admission.
Definition ui_action.hpp:16
~ActionMetadata()=default
Keep fallible metadata copies outside implicit noexcept transfers.
std::string shortcut
Display text; bind input explicitly with onKey.
Definition ui_action.hpp:13
std::string caption
Shared button caption.
Definition ui_action.hpp:12
std::string exclusiveGroup
Empty means independent selection.
Definition ui_action.hpp:18
bool checked
Shared selected presentation state.
Definition ui_action.hpp:17
std::string help
Owning help text.
Definition ui_action.hpp:14
Positive finite admission budgets for one owning set.
Definition ui_action.hpp:22
std::size_t maxActions
Live action count.
Definition ui_action.hpp:23
std::size_t maxPresenters
Live button projections and menu entries, including duplicates.
Definition ui_action.hpp:24
std::size_t maxMetadataBytes
Live keys/current metadata; external snapshots are independent.
Definition ui_action.hpp:25
std::size_t maxKeyBytes
Per-action key byte limit.
Definition ui_action.hpp:26