From 3f8ff53e65b27bb1d9f35c23d0fa30c4227f30a1 Mon Sep 17 00:00:00 2001 From: Kris Austin Date: Thu, 22 Oct 2026 10:22:23 -0500 Subject: [PATCH] Keep keys away from the 3D view while ImGui has the keyboard (#16673) The 3D view decided whether to run its shortcuts from ImGui's "wants the keyboard" flag, which is updated a frame late. The first key typed after clicking into a field could still reach the canvas. The canvas now leaves every key to ImGui while an ImGui text field is active, while a popup (dropdown, menu or modal) is open, or while a slider or button is held with the mouse. A focused sidebar text field works the same way. Global shortcuts still run unless they edit text. Every key still goes to ImGui. - ImGuiWrapper::claim_key_event() decides on the key-down. The key's repeats, char event and release follow that decision, so a key never both runs a shortcut and types. - Esc closes an open dropdown or menu before the gizmo. - Esc and Tab during a slider drag no longer close the gizmo or switch views. - text_input_active() reads ImGui's text input request, so a slider that once had a value typed into it no longer counts as a text field when dragged. - A modifier whose key-up went to a dialog no longer stays stuck. The canvas releases held modifiers when it loses the focus, and ImGui takes its modifiers from each mouse event. - The macOS Edit menu's key equivalents follow the same rule. --- docs/HLSD/keyboard-shortcuts.md | 31 +++- src/slic3r/GUI/CAD/SketchInlineEditor.hpp | 4 +- src/slic3r/GUI/GLCanvas3D.cpp | 32 +++- src/slic3r/GUI/GLCanvas3D.hpp | 2 + src/slic3r/GUI/ImGuiWrapper.cpp | 88 +++++++--- src/slic3r/GUI/ImGuiWrapper.hpp | 17 ++ src/slic3r/GUI/MainFrame.cpp | 13 +- tests/slic3rutils/test_imgui_wrapper.cpp | 193 +++++++++++++++++++++- 8 files changed, 343 insertions(+), 37 deletions(-) diff --git a/docs/HLSD/keyboard-shortcuts.md b/docs/HLSD/keyboard-shortcuts.md index cf48a13c8d..18b8cf8578 100644 --- a/docs/HLSD/keyboard-shortcuts.md +++ b/docs/HLSD/keyboard-shortcuts.md @@ -56,9 +56,34 @@ combo boxes), so it opens the dial from the canvases and the tab strip only. `MainFrame::handle_global_shortcut` skips a chord that `KeyChord::edits_text()` reports while a text field has the focus (a wx text entry, spin control, web view or ImGui InputText), and skips every chord while an ImGui modal is open, as a wx modal dialog -would. `ImGuiWrapper::owns_key()` reads the active InputText and the popup stack -directly, since `WantCaptureKeyboard` lags a frame, and is called only when the focus is on -a visible canvas from `OpenGLManager::create_wxglcanvas()`. +would. `ImGuiWrapper::owns_key()` reads the text input request and the popup stack of the +last drawn frame, since `WantCaptureKeyboard` and `WantTextInput` only catch up when the +next frame starts, and is called only when the focus is on a visible canvas from +`OpenGLManager::create_wxglcanvas()`. + +The `Plater`, `Preview` and `Painting` shortcuts act on the 3D view, so they run only while +the 3D view has the keyboard. `GLCanvas3D` feeds every key event to ImGui and asks +`ImGuiWrapper::claim_key_event()` whether to act on it as well. While an ImGui popup (a +dropdown, a menu or a modal) is open or an ImGui InputText is active, ImGui claims every key, +as a focused sidebar text field or an open native dropdown keeps every key from the canvas. +Esc then closes the topmost dropdown or menu, since this ImGui closes popups on Esc only +through keyboard navigation, which is off; a modal handles Esc itself. A second Esc reaches +the canvas once nothing claims it. A slider or button held with the mouse also claims every +key until it is released, since a shortcut that switches the view or closes the tool would +leave the drag behind. One side handles a key from its key-down to its release. Its +repeats, its char event and its release follow the key-down's decision even when a field or +modal opens or closes in between, so a key never both runs a shortcut and types, and a +keyboard move of the selection always sees the release of the key that started it. + +Key-ups go to whatever window has the focus, so a shortcut that opens a dialog, such as +Ctrl+P, takes the release of its modifier with it. When the canvas loses the focus it sends +itself a key-up for each Shift, Ctrl or Alt still held, which ends a gizmo's Shift mode and +clears ImGui's own copy of the modifier, and then drops its record of held keys. +`ImGuiWrapper::update_mouse_data()` also takes ImGui's modifiers from each mouse event, which +carries the real state, since a stale Ctrl would turn every slider click into a Ctrl+click +and open the slider's text box. The macOS +Edit menu, whose key equivalents run before the canvas sees the key, follows the same rule +as the canvas. ## Which event a chord matches diff --git a/src/slic3r/GUI/CAD/SketchInlineEditor.hpp b/src/slic3r/GUI/CAD/SketchInlineEditor.hpp index 677e74b3a0..1721066cd8 100644 --- a/src/slic3r/GUI/CAD/SketchInlineEditor.hpp +++ b/src/slic3r/GUI/CAD/SketchInlineEditor.hpp @@ -68,8 +68,8 @@ public: // (nothing further) <- the canvas has nothing to redraw, so it stops // // This canvas repaints ON DEMAND. ImGui decides whether it wants the keyboard at the END of a - // frame, from the active item, and GLCanvas3D::on_char only calls render() when - // update_key_data() says ImGui wants it. No frames -> WantTextInput never turns on -> no + // frame, from the active item, and GLCanvas3D::on_char only calls render() when ImGui + // wants or claims the key. No frames -> WantTextInput never turns on -> no // render on a keystroke -> still no frames. The characters sit in ImGui's queue and the field // looks exactly as deaf as the window it replaced. One repaint per frame while it is open // breaks the circle. diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 12e05605fc..2f72a8e976 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -3899,6 +3899,8 @@ void GLCanvas3D::bind_event_handlers() // The key-up that would commit a keyboard edit goes to whatever took the focus. if (m_selection_edit.kind != SelectionEdit::None) finish_selection_edit(); + release_held_modifiers(); + wxGetApp().imgui()->forget_key_claims(); ImGui::SetWindowFocus(nullptr); render(); evt.Skip(); @@ -4032,11 +4034,12 @@ void GLCanvas3D::on_char(wxKeyEvent& evt) // see include/wx/defs.h enum wxKeyCode int keyCode = evt.GetKeyCode(); - auto imgui = wxGetApp().imgui(); - if (imgui->update_key_data(evt)) { + auto imgui = wxGetApp().imgui(); + const bool imgui_claimed = imgui->claim_key_event(evt); + if (imgui->update_key_data(evt) || imgui_claimed) render(); + if (imgui_claimed) return; - } // Design tab: Delete, Esc, Ctrl+Z/Y and F are NOT handled here. DesignPanel's CHAR_HOOK owns // them (one route per key, whatever holds focus) and only lets a key through to this canvas @@ -4327,20 +4330,35 @@ static bool key_repeats(int key) static void key_released(int key) { s_keys_down.erase(key); } +void GLCanvas3D::release_held_modifiers() +{ + for (const int key : { WXK_SHIFT, WXK_CONTROL, WXK_ALT }) { + if (s_keys_down.count(key) == 0) + continue; + wxKeyEvent release(wxEVT_KEY_UP); + release.m_keyCode = key; + on_key(release); + } +} + void GLCanvas3D::on_key(wxKeyEvent& evt) { - const int keyCode = evt.GetKeyCode(); + auto imgui = wxGetApp().imgui(); + const int keyCode = evt.GetKeyCode(); + const bool imgui_claimed = imgui->claim_key_event(evt); if (evt.GetEventType() == wxEVT_KEY_DOWN) m_key_down = { keyCode, key_repeats(keyCode) }; else if (evt.GetEventType() == wxEVT_KEY_UP) key_released(keyCode); const bool alt_wheel_released = evt.GetEventType() == wxEVT_KEY_UP && keyCode == WXK_ALT && std::exchange(m_alt_wheel_used, false); - auto imgui = wxGetApp().imgui(); - if (imgui->update_key_data(evt)) + // ImGui closes a popup on Esc only through keyboard navigation, which is off. + if (imgui_claimed && evt.GetEventType() == wxEVT_KEY_DOWN && keyCode == WXK_ESCAPE) + imgui->close_top_popup(); + if (imgui->update_key_data(evt) || imgui_claimed) render(); - else + if (!imgui_claimed) { // Before the gizmos and the selection, which Esc also closes. if (evt.GetEventType() == wxEVT_KEY_DOWN && keyCode == WXK_ESCAPE && std::exchange(m_section_view->panel_open, false)) { diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index 5acbba39a1..ce537643bd 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -1275,6 +1275,8 @@ public: void on_idle(wxIdleEvent& evt); void on_char(wxKeyEvent& evt); void on_key(wxKeyEvent& evt); + // Sends a key-up for each modifier still held, whose own key-up is sent to the window that takes the focus. + void release_held_modifiers(); // Runs the Plater/Preview shortcut bound to chord, swallowing auto-repeats of one-shot // shortcuts; false when nothing is bound. bool handle_shortcut(const KeyChord& chord); diff --git a/src/slic3r/GUI/ImGuiWrapper.cpp b/src/slic3r/GUI/ImGuiWrapper.cpp index 080e85ad4e..b37b99f783 100644 --- a/src/slic3r/GUI/ImGuiWrapper.cpp +++ b/src/slic3r/GUI/ImGuiWrapper.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include "libslic3r/Technologies.hpp" @@ -506,6 +505,11 @@ bool ImGuiWrapper::update_mouse_data(wxMouseEvent& evt) io.MouseDown[0] = evt.LeftIsDown(); io.MouseDown[1] = evt.RightIsDown(); io.MouseDown[2] = evt.MiddleIsDown(); + // Replaces a modifier left held when its key-up was sent to another window. + io.KeyCtrl = evt.ControlDown(); + io.KeyShift = evt.ShiftDown(); + io.KeyAlt = evt.AltDown(); + io.KeySuper = evt.MetaDown(); // Only a vertical wheel event with a direction is a wheel step. A macOS trackpad reports sideways motion // as separate horizontal events, and its wheel events often carry a rotation of 0: wx truncates the // sub-pixel deltas to whole ones and passes on events with no delta at all. Read as vertical steps, @@ -588,16 +592,6 @@ void ImGuiWrapper::new_frame() // synchronize key states // when the application loses the focus it may happen that the key up event is not processed - // synchronize modifier keys - constexpr std::array, 3> imgui_mod_keys{ - std::make_pair(ImGuiKeyModFlags_Ctrl, WXK_CONTROL), - std::make_pair(ImGuiKeyModFlags_Shift, WXK_SHIFT), - std::make_pair(ImGuiKeyModFlags_Alt, WXK_ALT) }; - for (const std::pair& key : imgui_mod_keys) { - if ((io.KeyMods & key.first) != 0 && !wxGetKeyState(key.second)) - io.KeyMods &= ~key.first; - } - // Not sure if it is neccessary // values from 33 to 126 are reserved for the standard ASCII characters for (size_t i = 33; i <= 126; ++i) { @@ -2137,18 +2131,41 @@ bool ImGuiWrapper::want_any_input() const bool ImGuiWrapper::text_input_active() const { - const ImGuiID id = ImGui::GetActiveID(); - return id != 0 && ImGui::GetInputTextState(id) != nullptr; + // An active InputText requests text input in each frame that draws it, and NewFrame() moves the + // request into io.WantTextInput. + const ImGuiContext& g = *ImGui::GetCurrentContext(); + return g.WithinFrameScope ? g.IO.WantTextInput : g.WantTextInputNextFrame == 1; +} + +// A popup stays on the popup stack while its canvas is hidden, so it counts only if the last finished frame +// drew it, which NewFrame() moves from Active to WasActive. +static bool drawn_last_frame(const ImGuiWindow* popup) +{ + return popup != nullptr && (ImGui::GetCurrentContext()->WithinFrameScope ? popup->WasActive : popup->Active); } bool ImGuiWrapper::modal_popup_open() const { - // A modal stays on the popup stack while its canvas is hidden, so it counts only if the last finished - // frame drew it, which NewFrame() moves from Active to WasActive. - const ImGuiWindow* modal = ImGui::GetTopMostPopupModal(); - if (modal == nullptr) - return false; - return ImGui::GetCurrentContext()->WithinFrameScope ? modal->WasActive : modal->Active; + return drawn_last_frame(ImGui::GetTopMostPopupModal()); +} + +bool ImGuiWrapper::popup_open() const +{ + const ImVector& stack = ImGui::GetCurrentContext()->OpenPopupStack; + return std::any_of(stack.begin(), stack.end(), [](const ImGuiPopupData& popup) { return drawn_last_frame(popup.Window); }); +} + +void ImGuiWrapper::close_top_popup() +{ + const ImVector& stack = ImGui::GetCurrentContext()->OpenPopupStack; + for (int level = stack.Size - 1; level >= 0; --level) { + const ImGuiWindow* popup = stack[level].Window; + if (!drawn_last_frame(popup)) + continue; + if ((popup->Flags & ImGuiWindowFlags_Modal) == 0) + ImGui::ClosePopupToLevel(level, true); + return; + } } bool ImGuiWrapper::owns_key(const KeyChord& chord) const @@ -2156,6 +2173,39 @@ bool ImGuiWrapper::owns_key(const KeyChord& chord) const return modal_popup_open() || (chord.edits_text() && text_input_active()); } +bool ImGuiWrapper::item_held() const +{ + return ImGui::GetIO().MouseDown[0] && ImGui::GetActiveID() != 0; +} + +bool ImGuiWrapper::owns_keyboard() const +{ + return popup_open() || item_held() || text_input_active(); +} + +bool ImGuiWrapper::claim_key_event(const wxKeyEvent& evt) +{ + const bool claimed = owns_keyboard(); + const int key = evt.GetKeyCode(); + if (evt.GetEventType() == wxEVT_KEY_DOWN) { + m_char_claim = m_key_claims.try_emplace(key, claimed).first->second; + return *m_char_claim; + } + if (evt.GetEventType() == wxEVT_KEY_UP) { + m_char_claim.reset(); + if (const auto press = m_key_claims.extract(key)) + return press.mapped(); + return claimed; + } + return std::exchange(m_char_claim, std::nullopt).value_or(claimed); +} + +void ImGuiWrapper::forget_key_claims() +{ + m_key_claims.clear(); + m_char_claim.reset(); +} + ImFontAtlasCustomRect* ImGuiWrapper::GetTextureCustomRect(const wchar_t& tex_id) { auto item = m_custom_glyph_rects_ids.find(tex_id); diff --git a/src/slic3r/GUI/ImGuiWrapper.hpp b/src/slic3r/GUI/ImGuiWrapper.hpp index 6b5487bb5f..e4b8dabd77 100644 --- a/src/slic3r/GUI/ImGuiWrapper.hpp +++ b/src/slic3r/GUI/ImGuiWrapper.hpp @@ -73,6 +73,10 @@ class ImGuiWrapper #endif // ENABLE_ENHANCED_IMGUI_SLIDER_FLOAT std::map m_custom_glyph_rects_ids; std::string m_clipboard_text; + // Keys held down, each with whether ImGui claimed its press. + std::map m_key_claims; + // Whether ImGui claimed the last key-down, for the char event that follows it. + std::optional m_char_claim; public: struct LastSliderStatus { @@ -224,8 +228,21 @@ public: bool text_input_active() const; // True while an ImGui modal popup is on screen. bool modal_popup_open() const; + // True while any ImGui popup is on screen, modal or not. + bool popup_open() const; + // Closes the topmost popup on screen unless it is a modal, which handles Esc itself. + void close_top_popup(); // True for every key while a modal is open, and for text-editing keys while an InputText is active. bool owns_key(const KeyChord& chord) const; + // True while the left mouse button holds an ImGui item, such as a slider being dragged. + bool item_held() const; + // True while a popup is open, an item is held or an InputText is active. + bool owns_keyboard() const; + // Returns owns_keyboard() as of the key's key-down, so its repeats, char event and release get the + // same answer. Call it once per event. + bool claim_key_event(const wxKeyEvent& evt); + // Drops the record of held keys, whose key-ups are sent to the window that takes the focus. + void forget_key_claims(); // Optional inputs are used for set up value inside of an optional, with default value // diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index e729e33ad7..f9baeb2a12 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -3136,12 +3136,15 @@ void MainFrame::init_menubar_as_editor() { editMenu = new wxMenu(); + // Feeds the key to ImGui instead when ImGui has the focused canvas's keyboard, since the menu + // handles its key equivalents before the canvas receives the key. auto handle_key_event = [](wxKeyEvent& evt) { - if (wxGetApp().imgui()->update_key_data(evt)) { - wxGetApp().plater()->get_current_canvas3D()->render(); - return true; - } - return false; + const ImGuiWrapper* imgui = focused_canvas_imgui(wxWindow::FindFocus()); + if (imgui == nullptr || !imgui->owns_keyboard()) + return false; + wxGetApp().imgui()->update_key_data(evt); + wxGetApp().plater()->get_current_canvas3D()->render(); + return true; }; #ifndef __APPLE__ // BBS undo diff --git a/tests/slic3rutils/test_imgui_wrapper.cpp b/tests/slic3rutils/test_imgui_wrapper.cpp index 544a3832f5..bdb6f24966 100644 --- a/tests/slic3rutils/test_imgui_wrapper.cpp +++ b/tests/slic3rutils/test_imgui_wrapper.cpp @@ -75,7 +75,27 @@ struct ImGuiFrames ImGui::End(); } - char text[16] = ""; + void draw_slider() + { + ImGui::Begin("window"); + ImGui::SliderFloat("##slider", &value, 0.f, 1.f); + const ImVec2 min = ImGui::GetItemRectMin(), max = ImGui::GetItemRectMax(); + slider_center = ImVec2((min.x + max.x) / 2, (min.y + max.y) / 2); + ImGui::End(); + } + + // Presses the left button at pos, or releases it, and draws the slider in two frames. + void mouse(const ImVec2& pos, bool down) + { + ImGui::GetIO().MousePos = pos; + ImGui::GetIO().MouseDown[0] = down; + frame([this] { draw_slider(); }); + frame([this] { draw_slider(); }); + } + + char text[16] = ""; + float value = 0.f; + ImVec2 slider_center; }; void draw_modal() @@ -84,6 +104,28 @@ void draw_modal() ImGui::EndPopup(); } +void draw_dropdown(bool open) +{ + ImGui::Begin("window"); + if (open) + ImGui::OpenPopup("dropdown"); + if (ImGui::BeginPopup("dropdown")) { + ImGui::Text("item"); + ImGui::EndPopup(); + } + ImGui::End(); +} + +wxKeyEvent key_event(wxEventType type, int key_code, int modifiers = wxMOD_NONE) +{ + wxKeyEvent evt(type); + evt.m_keyCode = key_code; + evt.SetControlDown(modifiers & wxMOD_CONTROL); + evt.SetShiftDown(modifiers & wxMOD_SHIFT); + evt.SetAltDown(modifiers & wxMOD_ALT); + return evt; +} + } // namespace TEST_CASE("a vertical wheel event steps ImGui's wheel in its own direction", "[ImGuiWrapper]") @@ -103,6 +145,21 @@ TEST_CASE("a wheel event with no rotation or on the horizontal axis does not ste CHECK_THAT(wheel_after(rotation, axis), WithinAbs(0.0, 0.0)); } +TEST_CASE("a mouse event replaces a modifier whose key-up went to another window", "[ImGuiWrapper][Regression]") +{ + ImGuiWrapper imgui; + imgui.set_display_size(800.f, 600.f); + ImGui::GetIO().KeyCtrl = true; + + wxMouseEvent move(wxEVT_MOTION); + imgui.update_mouse_data(move); + CHECK_FALSE(ImGui::GetIO().KeyCtrl); + + move.SetControlDown(true); + imgui.update_mouse_data(move); + CHECK(ImGui::GetIO().KeyCtrl); +} + TEST_CASE("an InputText counts as active only while it holds the keyboard", "[ImGuiWrapper]") { ImGuiFrames f; @@ -161,3 +218,137 @@ TEST_CASE("ImGui owns every key under a modal and text-editing keys in an active }); CHECK(f.imgui.owns_key(save)); } + +TEST_CASE("ImGui owns the whole keyboard under a modal or in an active InputText", "[ImGuiWrapper]") +{ + ImGuiFrames f; + CHECK_FALSE(f.imgui.owns_keyboard()); + + f.frame([&] { f.draw_input(true); }); + f.frame([&] { f.draw_input(false); }); + CHECK(f.imgui.owns_keyboard()); + + f.frame([] {}); + CHECK_FALSE(f.imgui.owns_keyboard()); + f.frame([] { + ImGui::OpenPopup("modal"); + draw_modal(); + }); + CHECK(f.imgui.owns_keyboard()); +} + +TEST_CASE("an open dropdown owns the whole keyboard until it is closed", "[ImGuiWrapper]") +{ + ImGuiFrames f; + f.frame([] { draw_dropdown(true); }); + CHECK(f.imgui.popup_open()); + CHECK(f.imgui.owns_keyboard()); + + f.imgui.close_top_popup(); + f.frame([] { draw_dropdown(false); }); + CHECK_FALSE(f.imgui.popup_open()); + CHECK_FALSE(f.imgui.owns_keyboard()); +} + +TEST_CASE("closing the top popup leaves a modal open", "[ImGuiWrapper]") +{ + ImGuiFrames f; + f.frame([] { + ImGui::OpenPopup("modal"); + draw_modal(); + }); + f.imgui.close_top_popup(); + f.frame([] { draw_modal(); }); + CHECK(f.imgui.modal_popup_open()); +} + +TEST_CASE("a held slider owns the keyboard but not the keys Global shortcuts use", "[ImGuiWrapper]") +{ + ImGuiFrames f; + f.mouse(ImVec2(0, 0), false); + CHECK_FALSE(f.imgui.item_held()); + f.mouse(f.slider_center, true); + CHECK(f.imgui.item_held()); + CHECK(f.imgui.owns_keyboard()); + CHECK_FALSE(f.imgui.owns_key(KeyChord{ 'M' })); + CHECK_FALSE(f.imgui.owns_key(KeyChord{ WXK_LEFT })); + + f.mouse(f.slider_center, false); + CHECK_FALSE(f.imgui.owns_keyboard()); +} + +TEST_CASE("a slider typed into once is not a text field when dragged", "[ImGuiWrapper][Regression]") +{ + ImGuiFrames f; + f.mouse(ImVec2(0, 0), false); + ImGui::GetIO().KeyCtrl = true; // Ctrl+click types a value + f.mouse(f.slider_center, true); + ImGui::GetIO().KeyCtrl = false; + f.mouse(f.slider_center, false); + REQUIRE(f.imgui.text_input_active()); + + f.mouse(ImVec2(0, 0), true); // a click elsewhere closes the field + f.mouse(ImVec2(0, 0), false); + f.mouse(f.slider_center, true); + REQUIRE(ImGui::GetInputTextState(ImGui::GetActiveID()) != nullptr); + CHECK_FALSE(f.imgui.text_input_active()); + CHECK_FALSE(f.imgui.owns_key(KeyChord{ 'M' })); +} + +TEST_CASE("a key's char event gets its key-down's answer", "[ImGuiWrapper]") +{ + ImGuiFrames f; + f.frame([&] { f.draw_input(true); }); + f.frame([&] { f.draw_input(false); }); + CHECK(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, 'A'))); + f.frame([] {}); // the field closes before the char arrives + REQUIRE_FALSE(f.imgui.text_input_active()); + CHECK(f.imgui.claim_key_event(key_event(wxEVT_CHAR, 'a'))); + + CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, 'B'))); + f.frame([&] { f.draw_input(true); }); // the field opens before the char arrives + f.frame([&] { f.draw_input(false); }); + CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_CHAR, 'b'))); + // A char with no key-down before it is decided on its own. + CHECK(f.imgui.claim_key_event(key_event(wxEVT_CHAR, 'c'))); +} + +TEST_CASE("a held key's repeats and release get its press's answer", "[ImGuiWrapper]") +{ + ImGuiFrames f; + CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, WXK_RIGHT))); + f.frame([&] { f.draw_input(true); }); + f.frame([&] { f.draw_input(false); }); + CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, WXK_RIGHT))); + CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_UP, WXK_RIGHT))); + + CHECK(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, WXK_RIGHT))); + CHECK(f.imgui.claim_key_event(key_event(wxEVT_KEY_UP, WXK_RIGHT))); + // The release also clears the press's answer for a later char with no key-down. + f.frame([] {}); + CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_CHAR, 'a'))); +} + +TEST_CASE("a key pressed again after the canvas lost the focus is decided afresh", "[ImGuiWrapper]") +{ + ImGuiFrames f; + CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, 'M'))); + f.imgui.forget_key_claims(); // the key-up went to the window that took the focus + + f.frame([&] { f.draw_input(true); }); + f.frame([&] { f.draw_input(false); }); + CHECK(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, 'M'))); +} + +TEST_CASE("an active ImGui field claims every key, whether it edits text or not", "[ImGuiWrapper]") +{ + const int key = GENERATE(int('A'), int('D'), int(WXK_SHIFT)); + ImGuiFrames f; + CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, key, wxMOD_CONTROL))); + CHECK_FALSE(f.imgui.claim_key_event(key_event(wxEVT_KEY_UP, key, wxMOD_CONTROL))); + + f.frame([&] { f.draw_input(true); }); + f.frame([&] { f.draw_input(false); }); + CHECK(f.imgui.claim_key_event(key_event(wxEVT_KEY_DOWN, key, wxMOD_CONTROL))); + CHECK(f.imgui.claim_key_event(key_event(wxEVT_KEY_UP, key, wxMOD_CONTROL))); +}