22class Flow :
public std::enable_shared_from_this<Flow<TState>>
25 using Ptr = std::shared_ptr<Flow>;
27 using BuildFn = std::function<std::shared_ptr<Menu>(Flow& flow)>;
29 using AppliesFn = std::function<bool(
const TState&)>;
36 std::function<void(TState&,
int seconds)>
Set;
47 std::vector<Labeled<std::string>>
Options;
48 std::function<void(TState&,
const std::string& label,
const std::string& value)>
Set;
67 return Ptr(
new Flow(menus, slot, std::move(initial)));
73 _steps.push_back({.Build = std::move(build), .Applies = std::move(applies)});
74 return this->shared_from_this();
80 auto weak = this->weak_from_this();
82 auto applies = std::move(step.
Applies);
84 [weak, step = std::move(step)](Flow&) -> std::shared_ptr<Menu> {
85 auto self = weak.lock();
94 [self, set = step.
Set](int,
int seconds) {
97 set(self->_state, seconds);
110 auto weak = this->weak_from_this();
111 auto applies = std::move(step.
Applies);
113 [weak, step = std::move(step)](Flow&) -> std::shared_ptr<Menu> {
114 auto self = weak.lock();
115 return self ? self->BuildOptionsMenu(step) :
nullptr;
121 Ptr Validate(std::function<std::optional<std::string>(
const TState&)> check)
123 _validate = std::move(check);
124 return this->shared_from_this();
130 _confirm = std::move(spec);
131 return this->shared_from_this();
136 _finish = std::move(finish);
137 return this->shared_from_this();
149 Flow(
MenuSurface& menus,
int slot, TState initial) : _menus(&menus), _slot(slot), _state(std::move(initial)) {}
157 std::shared_ptr<Menu> BuildOptionsMenu(
const OptionsStep& step)
159 auto self = this->shared_from_this();
160 MenuBuilder builder(step.Title);
163 auto set = std::make_shared<
const decltype(step.Set)>(step.Set);
165 for (
const auto& [label, value] : step.Options)
167 builder.Button(label, [self, set, label, value](
int) {
170 (*set)(self->_state, label, value);
176 if (!step.CustomLabel.empty())
178 builder.Add(InputRow{.Label = step.CustomLabel,
179 .Prompt = step.CustomPrompt,
180 .Set = [self, set, customValue = step.CustomValue](int, std::string_view text) {
188 (*set)(self->_state, typed, customValue.empty() ? typed : customValue);
195 return builder.Build();
198 std::shared_ptr<Menu> BuildSummary()
200 auto self = this->shared_from_this();
203 if (_confirm.Summary)
205 _confirm.Summary(_state, rows);
209 std::string confirmLabel =
210 _confirm.ConfirmLabel.empty() ? _menus->Translate(_slot,
"menu.confirm",
"Confirm") : _confirm.ConfirmLabel;
211 std::string cancelLabel =
212 _confirm.CancelLabel.empty() ? _menus->Translate(_slot,
"menu.cancel",
"Cancel") : _confirm.CancelLabel;
215 .Lines = std::move(rows).Take(),
216 .ConfirmLabel = std::move(confirmLabel),
217 .CancelLabel = std::move(cancelLabel),
218 .Confirm = [self](int) { self->RunFinish(); }});
221 void OpenFrom(std::size_t from)
223 if (!RunValidation())
228 for (std::size_t i = from; i < _steps.size(); ++i)
230 if (_steps[i].Applies && !_steps[i].Applies(_state))
235 auto menu = _steps[i].Build(*
this);
240 _menus->CloseAll(_slot,
"menu.stepFailed");
243 _menus->Open(_slot, std::move(menu));
247 if (_confirm.Summary)
249 _menus->Open(_slot, BuildSummary());
260 if (!RunValidation())
268 _menus->CloseAll(_slot);
278 auto error = _validate(_state);
284 _menus->CloseAll(_slot, *error);
291 std::vector<Step> _steps;
292 std::size_t _stepIndex = 0;
293 std::function<std::optional<std::string>(
const TState&)> _validate;
295 std::function<void(TState&)> _finish;