Files
aevolution/apps/evo-client/controls_flow.hpp
T

117 lines
4.4 KiB
C++

#pragma once
// M9.8d host side of the Controls panel (ADR-0085): startup loading, draft editing,
// binding capture and Apply/save. Kept out of main() so the real adapter and panels
// can rehearse Apply -> save -> restart -> load in tests.
#include "evo/client/application.hpp"
#include "evo/client/bindings.hpp"
#include "evo/client/ui_state.hpp"
#include "panels.hpp"
#include <filesystem>
#include <optional>
#include <string>
#include <system_error>
#include <utility>
namespace evo::client_app {
class ControlsFlow {
public:
// `file` empty: nothing is loaded or saved (bindings still apply for the session).
explicit ControlsFlow(std::filesystem::path file) : file_{std::move(file)} {}
// Installs saved bindings. An invalid file is reported and left untouched; defaults
// stay in force until the user applies new bindings.
void load(client::Application& app, client::UiState& ui) const {
std::error_code missing;
if (file_.empty() || !std::filesystem::exists(file_, missing))
return;
if (const auto loaded = client::load_bindings(file_))
app.set_bindings(*loaded);
else
notify(ui,
"Saved controls rejected: " + client::describe(loaded.error()) +
" Default controls are in use; the file was not changed.",
client::NoticeLevel::warning, 15000);
}
[[nodiscard]] bool open() const noexcept {
return editor_.has_value();
}
void show(const client::Application& app) {
if (!editor_) {
editor_.emplace(app.bindings());
problem_.reset();
}
}
void fill(PanelView& view) const {
view.controls = editor_ ? &*editor_ : nullptr;
view.capturing = capturing_;
view.controls_problem = problem_;
}
// One frame: panel actions, then a finished capture (key, Escape or focus loss),
// which only changes the draft.
void step(const PanelActions& a, client::Application& app, client::UiState& ui) {
if (a.open_controls)
show(app);
if (editor_) {
if (a.capture_key && !capturing_) {
capturing_ = a.capture_key;
problem_.reset();
app.begin_key_capture();
}
if (a.cancel_capture && capturing_) {
app.cancel_key_capture();
capturing_.reset();
}
if (a.clear_secondary)
editor_->clear_secondary(*a.clear_secondary);
if (a.reset_controls) {
editor_->reset_defaults();
problem_.reset();
}
if (a.apply_controls)
apply(app, ui);
if (a.close_controls) {
editor_.reset();
problem_.reset();
}
if (!editor_ && capturing_) {
app.cancel_key_capture();
capturing_.reset();
}
}
if (capturing_ && editor_ && !app.key_capture_active()) {
if (const auto key = app.take_captured_key(); key && *key != client::escape_key) {
if (auto assigned =
editor_->assign(capturing_->action, capturing_->secondary, *key);
!assigned)
problem_ = assigned.error();
}
capturing_.reset();
}
}
private:
static void notify(client::UiState& ui, const std::string& text, client::NoticeLevel level,
std::uint64_t lifetime_ms = 5000) {
static_cast<void>(ui.notify(text, level, lifetime_ms).has_value()); // Bounded.
}
void apply(client::Application& app, client::UiState& ui) {
if (!editor_)
return;
const auto applied = editor_->apply();
if (!applied) {
problem_ = applied.error();
return;
}
app.set_bindings(*applied); // Releases held actions.
const bool saved = !file_.empty() && client::save_bindings(*applied, file_);
notify(ui,
saved ? "Controls saved." : "Controls apply to this session but could not be saved.",
saved ? client::NoticeLevel::info : client::NoticeLevel::warning);
editor_.reset();
}
std::filesystem::path file_;
std::optional<client::BindingEditor> editor_;
std::optional<ControlSlot> capturing_;
std::optional<client::BindingProblem> problem_;
};
} // namespace evo::client_app