Fix M9.8d review findings in binding capture

Arming capture drops the toolkit's held keys/modifiers and queued keyboard
input; the chosen key stays owned until release or focus loss so its repeats,
text and release reach neither toolkit nor world. Idle toolkit frames run when
nothing is drawn and the toolkit queue is bounded. Controls host logic moves to
ControlsFlow with an Apply -> save -> restart -> load rehearsal; key test events
now carry realistic keycodes and modifiers with positive controls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-02 16:07:09 -04:00
co-authored by Claude Opus 5.5
parent 5cf8709912
commit 14e676426f
8 changed files with 512 additions and 89 deletions
+136
View File
@@ -2,11 +2,13 @@
// software renderer, pinned toolkit). Notices fit their fixed-width window with a
// visible close button; clicking it (move + press + release in one batch, no settling
// frame) dismisses exactly that notice and never reaches the world.
#include "controls_flow.hpp"
#include "evo/client/application.hpp"
#include "panels.hpp"
#include <SDL3/SDL.h>
#include <array>
#include <cstdio>
#include <filesystem>
#include <imgui.h>
#include <imgui_internal.h>
#include <source_location>
@@ -206,6 +208,8 @@ void controls_panel() {
key.type = SDL_EVENT_KEY_DOWN;
key.key.windowID = id;
key.key.scancode = SDL_SCANCODE_J;
key.key.key = SDL_GetKeyFromScancode(SDL_SCANCODE_J, SDL_KMOD_NONE, false);
key.key.down = true;
CHECK(SDL_PushEvent(&key) && app->poll_events());
CHECK(app->take_captured_key() == KeyCode{SDL_SCANCODE_J});
CHECK(editor.assign(Action::move_left, false, SDL_SCANCODE_J));
@@ -214,11 +218,143 @@ void controls_panel() {
CHECK(editor.apply() && editor.apply()->action(SDL_SCANCODE_J) == Action::move_left);
CHECK(!app->take_navigation().click);
}
// M9.8d review: host rehearsal through the real adapter, production panels and the
// host's ControlsFlow: click a slot, a conflicting key is refused, choose J, Apply
// writes the file; a restarted application loads it and J moves left.
SDL_Event key_event(SDL_WindowID id, SDL_Scancode scancode, bool down) {
SDL_Event event{};
event.type = down ? SDL_EVENT_KEY_DOWN : SDL_EVENT_KEY_UP;
event.key.windowID = id;
event.key.scancode = scancode;
event.key.key = SDL_GetKeyFromScancode(scancode, SDL_KMOD_NONE, false);
event.key.down = down;
return event;
}
void apply_save_restart() {
const auto dir = std::filesystem::temp_directory_path() / "evo-controls-rehearsal";
std::filesystem::remove_all(dir);
std::filesystem::create_directories(dir);
const auto file = dir / "controls-v1.txt";
const std::array<render::Rectangle, 1> boxes{{{0, 0, 32, 32, {40, 40, 40, 255}}}};
auto frame = render::Frame::create(boxes);
CHECK(frame);
{
auto opened = Application::open({960, 640, false, "software"});
CHECK(opened);
auto app = std::move(*opened);
const auto id = SDL_GetWindowID(window());
SDL_Event focus{};
focus.type = SDL_EVENT_WINDOW_FOCUS_GAINED;
focus.window.windowID = id;
CHECK(SDL_PushEvent(&focus) && app->poll_events());
UiState ui;
EnvironmentLayer environment;
OrganismLayer organisms;
Selection selection;
Launcher launcher;
FormBuffers form;
render::EnvironmentDisplay display;
ControlsFlow flow{file};
flow.load(*app, ui); // No file yet: defaults.
CHECK(app->bindings() == Bindings::defaults());
flow.show(*app);
PanelActions pending;
app->set_overlay([&] {
PanelView view;
view.mode = Mode::fixture;
view.launcher = &launcher;
view.environment = &environment;
view.organisms = &organisms;
view.selection = &selection;
view.ui = &ui;
view.bindings = &app->bindings();
flow.fill(view);
pending = draw_panels(view, form, display);
});
// The host's frame order: poll, panel actions of the last frame, capture, draw.
const auto host_frame = [&] {
CHECK(app->poll_events());
flow.step(pending, *app, ui);
pending = {};
app->set_ui_capture(flow.open());
CHECK(!app->take_navigation().click && app->present(*frame) == Step::rendered);
};
for (int i = 0; i < 2; ++i)
host_frame();
const auto* w = ImGui::FindWindowByName("Controls");
CHECK(w != nullptr && w->Active);
const auto& style = ImGui::GetStyle();
// Probe one-batch clicks until `done` reports the intended widget answered.
const auto probe = [&](float x, float y0, float y1, float dy, const auto& done) {
for (float y = y0; (dy > 0 ? y < y1 : y > y1); y += dy) {
mouse(id, SDL_EVENT_MOUSE_MOTION, x, y);
mouse(id, SDL_EVENT_MOUSE_BUTTON_DOWN, x, y);
mouse(id, SDL_EVENT_MOUSE_BUTTON_UP, x, y);
for (int i = 0; i < 3; ++i) {
host_frame();
if (done())
return true;
}
}
return false;
};
const float primary_x =
w->Pos.x + w->Size.x - style.WindowPadding.x - 150 - (2 * style.CellPadding.x) - 50;
CHECK(probe(primary_x, w->Pos.y + 20, w->Pos.y + w->Size.y, 4,
[&] { return app->key_capture_active(); }));
// Up is Pan up's key: refused, the draft keeps A. Then arm again and choose J.
auto up = key_event(id, SDL_SCANCODE_UP, true);
CHECK(SDL_PushEvent(&up));
host_frame();
CHECK(!app->key_capture_active());
PanelView shown;
flow.fill(shown);
CHECK(shown.controls != nullptr && shown.controls->draft()[0].primary == SDL_SCANCODE_A);
CHECK(shown.controls_problem && shown.controls_problem->error == BindingError::conflict &&
shown.controls_problem->other == Action::pan_up);
auto up_release = key_event(id, SDL_SCANCODE_UP, false);
CHECK(SDL_PushEvent(&up_release));
host_frame();
CHECK(probe(primary_x, w->Pos.y + 20, w->Pos.y + w->Size.y, 4,
[&] { return app->key_capture_active(); }));
auto j = key_event(id, SDL_SCANCODE_J, true);
auto j_release = key_event(id, SDL_SCANCODE_J, false);
CHECK(SDL_PushEvent(&j) && SDL_PushEvent(&j_release));
host_frame();
CHECK(!app->key_capture_active() && flow.open() && !std::filesystem::exists(file));
// Apply sits at the bottom left of the panel.
CHECK(probe(w->Pos.x + style.WindowPadding.x + 40, w->Pos.y + w->Size.y - 6, w->Pos.y, -4,
[&] { return !flow.open(); }));
CHECK(app->bindings().action(SDL_SCANCODE_J) == Action::move_left);
CHECK(!app->bindings().action(SDL_SCANCODE_A));
CHECK(std::filesystem::exists(file) && !ui.notices().empty() &&
ui.notices().back().text == "Controls saved.");
}
// Restart: a new application loads the saved file; J moves left after a press.
auto opened = Application::open({320, 240, false, "software"});
CHECK(opened);
auto app = std::move(*opened);
const auto id = SDL_GetWindowID(window());
UiState ui;
ControlsFlow{file}.load(*app, ui);
CHECK(ui.notices().empty() && app->bindings().action(SDL_SCANCODE_J) == Action::move_left);
SDL_Event focus{};
focus.type = SDL_EVENT_WINDOW_FOCUS_GAINED;
focus.window.windowID = id;
CHECK(SDL_PushEvent(&focus) && app->poll_events() && app->poll_events());
auto j = key_event(id, SDL_SCANCODE_J, true);
CHECK(SDL_PushEvent(&j) && app->poll_events());
const auto intention = app->sample_intention(1);
CHECK(intention && intention->move_x == -1000);
std::filesystem::remove_all(dir);
}
} // namespace
int main() try {
notices();
controls_panel();
apply_save_restart();
std::puts("client panel tests passed");
return 0;
} catch (const std::exception& e) {