MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Chooser.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <future>
6
8
9/**
10 * @brief Result type for the primitive callback overload.
11 */
13
14/**
15 * @brief Callback type for the primitive callback overload.
16 */
18
19/**
20 * @brief A named group of accepted file extensions.
21 *
22 * @code
23 * ChooserFilter{ "Audio", { "wav", "flac", "aiff" } }
24 * ChooserFilter{ "All Files", { "*" } }
25 * @endcode
26 */
28
29// =============================================================================
30// open_file
31// =============================================================================
32
33/**
34 * @brief Present a native open-file dialog, delivering the result via @p callback.
35 *
36 * Primitive overload. The callback receives a ChooserResult and is responsible
37 * for checking and unwrapping it. Invoked exactly once on the platform backend
38 * thread; callers that need thread affinity must marshal the result themselves.
39 *
40 * @param callback Invoked with the chosen path or a SystemDialogError.
41 * @param filters Extension filter groups shown in the dialog.
42 * @param start_dir Directory the dialog opens in. Platform default if empty.
43 */
44MAYAFLUX_API void open_file(
45 ChooserCallback callback,
46 std::vector<ChooserFilter> filters = {},
47 std::filesystem::path start_dir = {});
48
49/**
50 * @brief Present a native open-file dialog, returning @p T produced by @p on_success.
51 *
52 * Blocks until the user completes or dismisses the dialog. @p on_success is
53 * called with the chosen path and its return value is returned directly to the
54 * caller. @p on_error is called on cancellation or backend failure; in that
55 * case the return value is a default-constructed @p T.
56 *
57 * @code
58 * auto tex = open_file<std::shared_ptr<TextureBuffer>>(
59 * [](std::filesystem::path p) {
60 * IO::ImageReader r;
61 * r.open(p.string());
62 * return r.create_texture_buffer();
63 * },
64 * [](Core::SystemDialogError) {}
65 * );
66 * @endcode
67 *
68 * @tparam T Return type of @p on_success.
69 * @param on_success Called with the chosen path on success. Must return T.
70 * @param on_error Called with the error on cancellation or failure.
71 * @param filters Extension filter groups shown in the dialog.
72 * @param start_dir Directory the dialog opens in. Platform default if empty.
73 * @return The value returned by @p on_success, or a default T on error.
74 *
75 * @todo Post-0.4: when C++23 is the minimum, add a non-blocking overload using
76 * std::expected::transform / and_then on a future-returning variant.
77 */
78template <typename T>
80 std::function<T(std::filesystem::path)> on_success,
81 std::function<void(Core::SystemDialogError)> on_error,
82 std::vector<ChooserFilter> filters = {},
83 std::filesystem::path start_dir = {})
84{
85 std::promise<T> promise;
86 auto future = promise.get_future();
87
89 [&promise, on_success = std::move(on_success), on_error = std::move(on_error)](ChooserResult result) {
90 if (result) {
91 promise.set_value(on_success(std::move(*result)));
92 } else {
93 on_error(result.error());
94 promise.set_value(T {});
95 }
96 },
97 std::move(filters),
98 std::move(start_dir));
99
100 return future.get();
101}
102
103// =============================================================================
104// save_file
105// =============================================================================
106
107/**
108 * @brief Present a native save-file dialog, delivering the result via @p callback.
109 *
110 * Primitive overload.
111 *
112 * @param callback Invoked with the chosen path or a SystemDialogError.
113 * @param suggested_name Filename pre-filled in the dialog name field.
114 * @param filters Extension filter groups shown in the dialog.
115 * @param start_dir Directory the dialog opens in. Platform default if empty.
116 */
117MAYAFLUX_API void save_file(
118 ChooserCallback callback,
119 std::string suggested_name = {},
120 std::vector<ChooserFilter> filters = {},
121 std::filesystem::path start_dir = {});
122
123/**
124 * @brief Present a native save-file dialog, returning @p T produced by @p on_success.
125 *
126 * Blocks until the user completes or dismisses the dialog.
127 *
128 * @tparam T Return type of @p on_success.
129 * @param on_success Called with the chosen path on success. Must return T.
130 * @param on_error Called with the error on cancellation or failure.
131 * @param suggested_name Filename pre-filled in the dialog name field.
132 * @param filters Extension filter groups shown in the dialog.
133 * @param start_dir Directory the dialog opens in. Platform default if empty.
134 * @return The value returned by @p on_success, or a default T on error.
135 *
136 * @todo Post-0.4: when C++23 is the minimum, add a non-blocking overload using
137 * std::expected::transform / and_then on a future-returning variant.
138 */
139template <typename T>
141 std::function<T(std::filesystem::path)> on_success,
142 std::function<void(Core::SystemDialogError)> on_error,
143 std::string suggested_name = {},
144 std::vector<ChooserFilter> filters = {},
145 std::filesystem::path start_dir = {})
146{
147 std::promise<T> promise;
148 auto future = promise.get_future();
149
150 save_file(
151 [&promise, on_success = std::move(on_success), on_error = std::move(on_error)](ChooserResult result) {
152 if (result) {
153 promise.set_value(on_success(std::move(*result)));
154 } else {
155 on_error(result.error());
156 promise.set_value(T {});
157 }
158 },
159 std::move(suggested_name),
160 std::move(filters),
161 std::move(start_dir));
162
163 return future.get();
164}
165
166} // namespace MayaFlux::Portal::System::Dialog
SystemDialogError
Failure modes for OS dialog operations.
std::function< void(FileDialogResult)> FileDialogCallback
Callback type for all file dialog operations.
std::expected< std::filesystem::path, SystemDialogError > FileDialogResult
Result type delivered to all file dialog callbacks.
Core::FileDialogCallback ChooserCallback
Callback type for the primitive callback overload.
Definition Chooser.hpp:17
Core::FileDialogResult ChooserResult
Result type for the primitive callback overload.
Definition Chooser.hpp:12
void open_file(ChooserCallback callback, std::vector< ChooserFilter > filters, std::filesystem::path start_dir)
Present a native open-file dialog, delivering the result via callback.
Definition Chooser.cpp:8
void save_file(ChooserCallback callback, std::string suggested_name, std::vector< ChooserFilter > filters, std::filesystem::path start_dir)
Present a native save-file dialog, delivering the result via callback.
Definition Chooser.cpp:23
A named group of accepted file extensions for dialog filters.