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
+55 -9
View File
@@ -55,6 +55,39 @@ struct Toolkit {
renderer = false;
}
};
// Drops the toolkit's keyboard state and any keyboard/text input it has queued but not
// yet processed. Used when binding capture starts: releases of keys the toolkit already
// saw pressed will be consumed by capture, so they must not stay down there.
void reset_toolkit_keys() {
auto& queue = ImGui::GetCurrentContext()->InputEventsQueue;
for (int i = 0; i < queue.Size;) {
if (queue[i].Type == ImGuiInputEventType_Key || queue[i].Type == ImGuiInputEventType_Text)
queue.erase(queue.Data + i);
else
++i;
}
ImGui::GetIO().ClearInputKeys(); // Keys, modifiers and pending characters.
}
// Consumes the toolkit's queued input when nothing is drawn (no overlay, or a paused
// window), so its event queue stays bounded and its key state follows the events.
void idle_toolkit_frame() {
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
ImGui::EndFrame();
}
// The toolkit trickles queued input (one transition per key per frame), so a flood can
// outpace frames. Past this bound its queue is dropped and every toolkit key/button is
// released: lossy for the toolkit, never a stuck or replayed press.
constexpr int max_toolkit_events = 512;
void bound_toolkit_queue() {
auto& io = ImGui::GetIO();
if (ImGui::GetCurrentContext()->InputEventsQueue.Size <= max_toolkit_events)
return;
io.ClearEventsQueue();
io.ClearInputKeys();
io.ClearInputMouse();
}
constexpr float click_slop = 4; // Logical units between press and release.
// True when `at` lies on a toolkit window drawn in the last frame or any popup is open.
// Resolved per event from the toolkit's retained window rectangles, so a move onto a
@@ -93,6 +126,9 @@ struct Application::Impl {
bool backlogged{false};
bool key_capture{false}; // Binding capture armed.
std::optional<KeyCode> captured_key; // Taken once by the host.
// The chosen physical key stays owned by capture until its release (or focus loss):
// its repeats, text and release reach neither the toolkit nor the world.
std::optional<SDL_Scancode> owned_key;
// Capture sources: an external modal/launcher, the legacy Tab toggle and the
// toolkit's own keyboard focus. World actions run only when none applies.
bool external_capture{false}, tab_capture{false}, toolkit_capture{false};
@@ -157,6 +193,7 @@ const Bindings& Application::bindings() const noexcept {
return impl_->bindings;
}
void Application::begin_key_capture() noexcept {
reset_toolkit_keys();
impl_->captured_key.reset();
impl_->key_capture = true;
impl_->update_capture();
@@ -256,24 +293,27 @@ std::expected<void, Error> Application::poll_events() {
// A press on a panel in this batch may focus a text field before the toolkit's next
// frame can report it: world key-downs after it wait for that frame (releases pass).
bool ui_pressed = false;
bool key_chosen = false; // Binding capture completed in this batch.
SDL_Event event{};
int count = 0;
for (; count < 256 && SDL_PollEvent(&event); ++count) {
// Binding capture consumes keyboard events before the toolkit and world; text
// produced by the chosen key (later in this batch) is swallowed as well.
const bool keyboard = (event.type == SDL_EVENT_KEY_DOWN || event.type == SDL_EVENT_KEY_UP)
? event.key.windowID == id
// Binding capture consumes keyboard events before the toolkit and world. While
// the chosen key is still held, its repeats and release are consumed, and so is
// all text input (it cannot be attributed to a key).
const bool key_event = event.type == SDL_EVENT_KEY_DOWN || event.type == SDL_EVENT_KEY_UP;
const bool keyboard = key_event ? event.key.windowID == id
: event.type == SDL_EVENT_TEXT_INPUT ? event.text.windowID == id
: event.type == SDL_EVENT_TEXT_EDITING ? event.edit.windowID == id
: false;
if (keyboard && (p.key_capture || (key_chosen && (event.type == SDL_EVENT_TEXT_INPUT ||
event.type == SDL_EVENT_TEXT_EDITING)))) {
const bool owned =
keyboard && p.owned_key && (!key_event || event.key.scancode == *p.owned_key);
if (keyboard && (p.key_capture || owned)) {
if (p.key_capture && event.type == SDL_EVENT_KEY_DOWN && !event.key.repeat) {
p.captured_key = static_cast<KeyCode>(event.key.scancode);
p.owned_key = event.key.scancode;
p.key_capture = false;
key_chosen = true;
p.update_capture(); // Drains the rest of this batch before world input.
} else if (owned && event.type == SDL_EVENT_KEY_UP) {
p.owned_key.reset();
}
continue;
}
@@ -315,6 +355,7 @@ std::expected<void, Error> Application::poll_events() {
if (!p.state.accepts_input()) {
p.press.reset(); // A release may never arrive for this window.
p.ui_press = false;
p.owned_key.reset(); // The toolkit clears its own keys on focus loss.
if (p.key_capture) { // Focus loss cancels binding capture without a key.
p.key_capture = false;
p.update_capture();
@@ -375,6 +416,7 @@ std::expected<void, Error> Application::poll_events() {
reset = true;
}
}
bound_toolkit_queue();
if (p.failure) {
p.input.admit(false);
return std::unexpected(*p.failure);
@@ -414,8 +456,10 @@ std::expected<Step, Error> Application::present(const render::Frame& frame) {
if (p.state.closed)
return Step::closed;
if (p.state.hidden || p.state.minimized || p.state.logical_width == 0 ||
p.state.logical_height == 0 || p.state.pixel_width == 0 || p.state.pixel_height == 0)
p.state.logical_height == 0 || p.state.pixel_width == 0 || p.state.pixel_height == 0) {
idle_toolkit_frame();
return Step::paused;
}
if (auto result = detail::draw(p.renderer.get(), frame, p.state); !result) {
p.failure = result.error();
p.input.admit(false);
@@ -429,6 +473,8 @@ std::expected<Step, Error> Application::present(const render::Frame& frame) {
p.overlay();
ImGui::Render();
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), p.renderer.get());
} else {
idle_toolkit_frame();
}
if (!p.screenshot.empty()) {
// Engineering visual review only: the composed frame as an uncompressed BMP.