Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
message_box.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/async.hpp>
6#include <portals/ui_ref.hpp>
7
8// Keep the exported facade name stable regardless of Windows header order, then restore the caller's macro.
9#pragma push_macro("MessageBox")
10#undef MessageBox
11
12namespace portals {
23
24using MessageResult = std::expected<MessageChoice, ManagedError>;
25
26namespace ui {
27template <class T> class Ref;
28template <class T> class Description;
29class MessageBox;
30
32enum class DialogButtons {
33 OK,
34 OK_CANCEL,
35 YES_NO,
36 YES_NO_CANCEL,
37 CONFIRM_CANCEL,
38 SAVE_DISCARD_CANCEL
39};
40
41namespace detail {
43struct DialogButtonPreset {
44 std::array<MessageChoice, 3> choices;
45 std::size_t count;
46 MessageChoice safeDefault;
47 std::string_view identity;
48};
49
50constexpr std::optional<DialogButtonPreset> dialogButtonPreset(DialogButtons buttons) noexcept {
51 using enum MessageChoice;
52
53 switch (buttons) {
54 case DialogButtons::OK:
55 return DialogButtonPreset{{ACKNOWLEDGE, CANCEL, CANCEL}, 1, ACKNOWLEDGE, "information"};
56 case DialogButtons::OK_CANCEL:
57 return DialogButtonPreset{{ACKNOWLEDGE, CANCEL, CANCEL}, 2, CANCEL, "ok-cancel"};
58 case DialogButtons::YES_NO:
59 return DialogButtonPreset{{YES, NO, CANCEL}, 2, NO, "yes-no"};
60 case DialogButtons::YES_NO_CANCEL:
61 return DialogButtonPreset{{YES, NO, CANCEL}, 3, CANCEL, "yes-no-cancel"};
62 case DialogButtons::CONFIRM_CANCEL:
63 return DialogButtonPreset{{CONFIRM, CANCEL, CANCEL}, 2, CANCEL, "confirm"};
64 case DialogButtons::SAVE_DISCARD_CANCEL:
65 return DialogButtonPreset{{SAVE, DISCARD, CANCEL}, 3, CANCEL, "unsaved"};
66 }
67
68 return {};
69}
70
71constexpr DialogButtons legacyDialogButtons(int kind) noexcept {
72 return kind == 0 ? DialogButtons::OK : kind == 1 ? DialogButtons::CONFIRM_CANCEL : DialogButtons::SAVE_DISCARD_CANCEL;
73}
74
76} // namespace detail
77
81 TextValue confirm{"Continue"};
82 TextValue save{"Save"};
83 TextValue discard{"Discard"};
84 TextValue cancel{"Cancel"};
85 TextValue yes{"Yes"};
86 TextValue no{"No"};
87};
88
91 ~MessageOptions() = default;
92
95 TextResolver resolver{};
96 std::optional<MessageChoice>
99 TextLimits textLimits{16384, 4096, 16384, 32768};
100 std::size_t maxBodyLines{4096};
101 std::vector<MessageChoice> order =
102 std::vector<MessageChoice>(0);
103};
104
105namespace detail {
107struct MessageTarget : RefTarget {
108 bool pending{};
109};
110
111struct MessageAccess {
112 template <class T> static std::shared_ptr<RefTarget> target(const Ref<T> &ref) {
113 return ref.target();
114 }
115
116 static DialogCommandResult command(std::shared_ptr<RefTarget> target, std::expected<void, ManagedError> result,
117 std::source_location origin);
118 static std::expected<void, ManagedError> start(ManagedScene &scene, Node<Widget> dialog, Node<Widget> owner,
119 std::function<EditorCommandResult(const MessageResult &)> callback,
120 std::source_location origin);
121 static std::expected<void, ManagedError> cancel(ManagedScene &scene, Node<Widget> dialog);
122 static std::expected<std::shared_ptr<const MessageResult>, ManagedError> last(ManagedScene &scene,
123 Node<Widget> dialog);
124 static std::expected<AsyncReceive<MessageResult>, ManagedError>
125 receive(ManagedScene &scene, Node<Widget> dialog, Node<Widget> owner, const UiAsyncContext &context);
126 static Description<Dialog> describe(MessageBox box, TextValue title, TextValue body, MessageOptions options,
127 int kind);
128 static Description<Dialog> describe(MessageBox box, TextValue title, std::optional<TextValue> text,
129 std::optional<Description<Widget>> body, MessageOptions options,
130 DialogButtons buttons);
131 static std::expected<void, ManagedError> validate(ManagedScene &scene, Node<Widget> dialog, Node<Widget> owner);
132 static std::expected<std::shared_ptr<portals::detail::MessageRequest>, ManagedError>
133 reserve(ManagedScene &scene, Node<Widget> dialog, Node<Widget> owner);
134
135 template <class Callback> static auto adapt(ManagedScene &scene, Callback callback) {
136 return scene.actionAdapter<MessageResult>(std::move(callback));
137 }
138};
139
141} // namespace detail
142
148public:
151
157 template <class Callback>
159 DialogCommandResult open(Callback callback, std::source_location origin = std::source_location::current()) const {
160 return openTarget({}, std::move(callback), origin);
161 }
162
168 template <class T, class Callback>
169 requires std::derived_from<T, Widget> && portals::detail::ManagedAction<Callback, MessageResult>
170 DialogCommandResult open(const Ref<T> &owner, Callback callback,
171 std::source_location origin = std::source_location::current()) const {
172 return openTarget(detail::MessageAccess::target(owner), std::move(callback), origin);
173 }
174
178 DialogCommandResult cancel(std::source_location origin = std::source_location::current()) const;
182 std::expected<std::shared_ptr<const MessageResult>, ManagedError> lastResult() const;
187 std::expected<AsyncReceive<MessageResult>, ManagedError> receive(const UiAsyncContext &context) const;
188
192 template <class T>
193 requires std::derived_from<T, Widget>
194 std::expected<AsyncReceive<MessageResult>, ManagedError> receive(const UiAsyncContext &context,
195 const Ref<T> &owner) const {
196 return receiveTarget(context, detail::MessageAccess::target(owner));
197 }
198
199private:
200 friend struct detail::MessageAccess;
201
202 template <class Callback>
203 DialogCommandResult openTarget(std::shared_ptr<detail::RefTarget> owner, Callback callback,
204 std::source_location origin) const {
205 auto scene = detail::eventScene(target_);
206 if (!scene) {
207 return detail::MessageAccess::command(target_, std::unexpected(scene.error()), origin);
208 }
209 Node<Widget> node = (*scene)->root();
210 if (owner) {
211 auto ownerScene = detail::eventScene(owner);
212 if (!ownerScene) {
213 return detail::MessageAccess::command(target_, std::unexpected(ownerScene.error()), origin);
214 }
215 if (*ownerScene != *scene) {
216 return detail::MessageAccess::command(
217 target_,
218 std::unexpected(ManagedError{ManagedPhase::INPUT, owner->node.id(), TreeError::WRONG_SESSION, {}, {}}),
219 origin);
220 }
221 node = owner->node;
222 }
223 try {
224 return detail::MessageAccess::command(
225 target_,
226 detail::MessageAccess::start(**scene, target_->node, node,
227 detail::MessageAccess::adapt(**scene, std::move(callback)), origin),
228 origin);
229 } catch (const std::bad_alloc &) {
230 return detail::MessageAccess::command(
231 target_, std::unexpected(ManagedError{ManagedPhase::INPUT, target_->node.id(), TreeError::NO_MEMORY, {}, {}}),
232 origin);
233 }
234 }
235
236 std::expected<AsyncReceive<MessageResult>, ManagedError>
237 receiveTarget(const UiAsyncContext &context, std::shared_ptr<detail::RefTarget> owner) const;
238 std::shared_ptr<detail::MessageTarget> target_;
239};
240
241#ifdef _WIN32
243using MessageBoxA = MessageBox;
245using MessageBoxW = MessageBox;
246#endif
247
253template <> class Ref<MessageBox> : public MessageBox {
254public:
256 Ref() = default;
257
261 explicit Ref(MessageBox reference) noexcept : MessageBox(std::move(reference)) {
262 }
263};
264
269Description<Dialog> messageBox(MessageBox handle, TextValue title, TextValue explanation, MessageOptions options = {});
273Description<Dialog> confirmDialog(MessageBox handle, TextValue title, TextValue explanation,
274 MessageOptions options = {});
278Description<Dialog> unsavedChangesDialog(MessageBox handle, TextValue title, TextValue explanation,
279 MessageOptions options = {});
283Description<Dialog> messageBox(MessageBox handle, std::string_view title, std::string_view explanation,
284 MessageOptions options = {});
288Description<Dialog> confirmDialog(MessageBox handle, std::string_view title, std::string_view explanation,
289 MessageOptions options = {});
293Description<Dialog> unsavedChangesDialog(MessageBox handle, std::string_view title, std::string_view explanation,
294 MessageOptions options = {});
295} // namespace ui
296
301} // namespace portals
302
303#pragma pop_macro("MessageBox")
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
Owning literal bytes or a resource key, measured only at resolution.
Definition text_value.hpp:42
Weak context copied into owning coroutine parameters; never extends the UI owner's life.
Definition async.hpp:462
Single-use owning construction recipe; no mounted widget ownership.
Definition ui_description.hpp:48
Owning checked command that also permits the admitted short callback form. This is a narrow facade re...
Definition dialog_command_result.hpp:20
Copyable UI-affine observation of a reusable choice dialog. Copies retain no scene/widget/callback....
Definition message_box.hpp:147
MessageBox()
Create an unpublished observing slot; allocation can throw.
DialogCommandResult cancel(std::source_location origin=std::source_location::current()) const
std::expected< AsyncReceive< MessageResult >, ManagedError > receive(const UiAsyncContext &context) const
std::expected< std::shared_ptr< const MessageResult >, ManagedError > lastResult() const
DialogCommandResult open(const Ref< T > &owner, Callback callback, std::source_location origin=std::source_location::current()) const
Definition message_box.hpp:170
std::expected< AsyncReceive< MessageResult >, ManagedError > receive(const UiAsyncContext &context, const Ref< T > &owner) const
Definition message_box.hpp:194
DialogCommandResult open(Callback callback, std::source_location origin=std::source_location::current()) const
Definition message_box.hpp:159
Ref(MessageBox reference) noexcept
Definition message_box.hpp:261
Ref()=default
Create the existing cold observing slot; construction may throw std::bad_alloc.
Typed Widget observation; explicit composite specializations reuse their existing weak facades.
Definition ui_ref.hpp:761
A compact action optionally receives owning event parameters.
Definition managed.hpp:239
auto describe(const StaticScene< Root > &value, Adapters... adapters)
Definition ui_static.hpp:605
Public Portals API.
Definition action.hpp:8
@ CANCEL
Cancel the current interaction.
Definition action.hpp:12
ui::MessageBox MessageDialog
Canonical composite type for ui::Ref<MessageDialog>; exactly the existing ui::MessageBox facade....
Definition message_box.hpp:300
@ INPUT
Dispatch input or deliver a managed control/property callback.
Definition managed_diagnostics.hpp:54
MessageChoice
A requested application action; SAVE does not confirm that data was written.
Definition message_box.hpp:14
@ ACKNOWLEDGE
Acknowledge an informational message.
Definition message_box.hpp:15
@ YES
Affirm the question.
Definition message_box.hpp:20
@ CONFIRM
Proceed with the described operation.
Definition message_box.hpp:16
@ NO
Decline the question.
Definition message_box.hpp:21
@ DISCARD
Proceed without saving.
Definition message_box.hpp:18
@ SAVE
Request saving before proceeding.
Definition message_box.hpp:17
std::expected< MessageChoice, ManagedError > MessageResult
Immutable domain outcome, preserving the complete original managed failure.
Definition message_box.hpp:24
@ WRONG_SESSION
An identity or scope belongs to another tree/scheduler.
Definition widget_types.hpp:29
@ NO_MEMORY
Allocation failed inside the operation boundary.
Definition widget_types.hpp:33
Owning panel geometry and managed input settings, in terminal cells.
Definition dialog.hpp:52
Original error plus independent runner/restoration and cleanup failures.
Definition managed_diagnostics.hpp:62
Owning limits checked without silently truncating input or clusters.
Definition text.hpp:57
Owning localizable button captions, resolved once during complete mount.
Definition message_box.hpp:79
TextValue yes
Affirmative answer.
Definition message_box.hpp:85
TextValue cancel
Safe cancellation action.
Definition message_box.hpp:84
TextValue no
Negative answer.
Definition message_box.hpp:86
TextValue acknowledge
Information action.
Definition message_box.hpp:80
TextValue confirm
Confirmation action.
Definition message_box.hpp:81
TextValue save
Save request.
Definition message_box.hpp:82
TextValue discard
Discard request.
Definition message_box.hpp:83
Immutable settings for a ready-made modal choice dialog.
Definition message_box.hpp:90
MessageLabels labels
Owning captions; empty or multiline captions are rejected.
Definition message_box.hpp:94
bool allowDestructiveDefault
Explicit permission for CONFIRM/DISCARD initial focus.
Definition message_box.hpp:98
DialogOptions dialog
Existing panel settings; MODAL and closeOnEscape are required.
Definition message_box.hpp:93
std::optional< MessageChoice > defaultChoice
Absent selects preset safety: ACKNOWLEDGE for OK, NO for YES_NO, otherwise CANCEL.
Definition message_box.hpp:97
std::vector< MessageChoice > order
Empty uses preset order; otherwise all active roles exactly once, at most three.
Definition message_box.hpp:101
TextResolver resolver
Explicit synchronous localization; never retained by mounted widgets.
Definition message_box.hpp:95
std::size_t maxBodyLines
Positive wrapped explanation line cap; excess is NODE_LIMIT.
Definition message_box.hpp:100
TextLimits textLimits
Positive per-value UTF-8/cluster/grapheme/cell caps.
Definition message_box.hpp:99