21template <
class TState>
22class Flow :
public std::enable_shared_from_this<Flow<TState>>
25 using Ptr = std::shared_ptr<Flow>;
47 std::vector<Labeled<std::string>>
Options;
73 _steps.push_back({.Build = std::move(build), .Applies = std::move(
applies)});
92 .Presets =
step.Presets,
101 .CustomLabel =
step.CustomLabel,
102 .CustomPrompt =
step.CustomPrompt});
123 _validate = std::move(
check);
130 _confirm = std::move(
spec);
136 _finish = std::move(
finish);
157 std::shared_ptr<Menu> BuildOptionsMenu(
const OptionsStep&
step)
163 auto set = std::make_shared<
const decltype(
step.Set)>(
step.Set);
165 for (
const auto& [
label, value] :
step.Options)
176 if (!
step.CustomLabel.empty())
179 .Prompt =
step.CustomPrompt,
198 std::shared_ptr<Menu> BuildSummary()
215 .Lines = std::move(
rows).Take(),
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));
249 _menus->
Open(_slot, BuildSummary());
260 if (!RunValidation())
278 auto error = _validate(_state);
291 std::vector<Step> _steps;
292 std::size_t _stepIndex = 0;
293 std::function<std::optional<std::string>(
const TState&)> _validate;
294 ConfirmSpec _confirm;
std::function< bool(const TState &)> AppliesFn
static Ptr Create(MenuSurface &menus, int slot, TState initial)
Ptr AddStep(BuildFn build, AppliesFn applies={})
std::shared_ptr< Flow > Ptr
std::function< std::shared_ptr< Menu >(Flow &flow)> BuildFn
Ptr Finish(std::function< void(TState &)> finish)
Ptr AddOptionsStep(OptionsStep step)
Ptr Confirm(ConfirmSpec spec)
Ptr Validate(std::function< std::optional< std::string >(const TState &)> check)
Ptr AddDurationStep(DurationStep step)
static std::string Trim(std::string_view str)
static std::string ReadFile(const std::filesystem::path &path)
std::shared_ptr< Menu > BuildDurationMenu(DurationMenu spec)
std::shared_ptr< Menu > BuildConfirmMenu(ConfirmMenu spec)
std::function< void(const TState &state, SummaryRows &rows)> Summary
std::vector< Labeled< int > > Presets
std::function< void(TState &, int seconds)> Set
std::function< void(TState &, const std::string &label, const std::string &value)> Set
std::vector< Labeled< std::string > > Options