MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
API/Prompt.hpp
Go to the documentation of this file.
1#pragma once
2
6
7namespace MayaFlux::detail {
8
9/**
10 * @brief Open a picker and wait for the choice.
11 *
12 * Blocks until the user chooses or closes the window, so it must not be called from
13 * the event thread. Portal::Forma must be initialized and the engine running.
14 *
15 * @return The chosen item, or nothing when the window was closed or @p items is empty.
16 */
17template <typename T>
18std::optional<T> pick_one(std::string title, std::vector<T> items,
19 std::function<std::string(const T&)> label)
20{
21 auto promise = std::make_shared<std::promise<std::optional<T>>>();
22 auto future = promise->get_future();
23
24 Portal::Forma::pick<T>(std::move(title), std::move(items), std::move(label),
25 [promise](std::optional<T> chosen) { promise->set_value(std::move(chosen)); });
26
27 return Parallel::await_future(std::move(future));
28}
29
30/**
31 * @brief Open a text prompt and wait for the text.
32 *
33 * Blocks like pick_one. Only text the validator accepts is returned.
34 *
35 * @return The text, or nothing when the prompt was cancelled.
36 */
37inline std::optional<std::string> ask_one(std::string title, std::string initial,
39{
40 auto promise = std::make_shared<std::promise<std::optional<std::string>>>();
41 auto future = promise->get_future();
42
43 Portal::Forma::ask(std::move(title), std::move(initial), std::move(validator),
44 [promise](std::optional<std::string> text) { promise->set_value(std::move(text)); });
45
46 return Parallel::await_future(std::move(future));
47}
48
49} // namespace MayaFlux::detail
T await_future(std::future< T > future)
Definition Dispatch.hpp:153
void pick(std::string title, std::vector< std::string > labels, PickCallback callback)
Open a window of clickable rows, one per label, and report the choice.
Definition Picker.cpp:49
void ask(std::string title, std::string initial, AskValidator validator, AskCallback callback)
Open a one line text prompt and report what the user typed.
Definition Prompt.cpp:94
std::function< bool(std::string_view)> AskValidator
Decides whether a text may be accepted.
std::optional< std::string > ask_one(std::string title, std::string initial, Portal::Forma::AskValidator validator)
Open a text prompt and wait for the text.
std::optional< T > pick_one(std::string title, std::vector< T > items, std::function< std::string(const T &)> label)
Open a picker and wait for the choice.