MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ open_file() [2/2]

template<typename T >
T MayaFlux::Portal::System::Dialog::open_file ( std::function< T(std::filesystem::path)>  on_success,
std::function< void(Core::SystemDialogError)>  on_error,
std::vector< ChooserFilter filters = {},
std::filesystem::path  start_dir = {} 
)

Present a native open-file dialog, returning T produced by on_success.

Blocks until the user completes or dismisses the dialog. on_success is called with the chosen path and its return value is returned directly to the caller. on_error is called on cancellation or backend failure; in that case the return value is a default-constructed T.

auto tex = open_file<std::shared_ptr<TextureBuffer>>(
[](std::filesystem::path p) {
r.open(p.string());
},
);
std::shared_ptr< Buffers::TextureBuffer > create_texture_buffer()
Create a VKBuffer containing the loaded image pixel data.
bool open(const std::string &filepath, FileReadOptions options=FileReadOptions::ALL) override
Open a file for reading.
File reader for image formats (PNG, JPG, BMP, TGA, etc.)
SystemDialogError
Failure modes for OS dialog operations.
Template Parameters
TReturn type of on_success.
Parameters
on_successCalled with the chosen path on success. Must return T.
on_errorCalled with the error on cancellation or failure.
filtersExtension filter groups shown in the dialog.
start_dirDirectory the dialog opens in. Platform default if empty.
Returns
The value returned by on_success, or a default T on error.
Todo:
Post-0.4: when C++23 is the minimum, add a non-blocking overload using std::expected::transform / and_then on a future-returning variant.

Definition at line 79 of file Chooser.hpp.

82 {},
83 std::filesystem::path start_dir = {})
84{
85 std::promise<T> promise;
86 auto future = promise.get_future();
87
88 open_file(
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}