From b0010389011de8c5229775b71233a79e05ec1e7f Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 14 Oct 2024 16:57:34 +0200 Subject: [PATCH] Nav: added io.ConfigNavEscapeClearFocusWindow to clear focused window on Escape. (#3200) + pressing escape to hide nav highlight doesn't clear location from when ctrl+tabbing back into same window later. --- docs/CHANGELOG.txt | 3 +++ imgui.cpp | 7 +++++-- imgui.h | 1 + imgui_demo.cpp | 2 ++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index d67abea37..aa64698c8 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -67,6 +67,9 @@ Other changes: state to draw callbacks. (#6969, #5834, #7468, #3590) - IO: WantCaptureKeyboard is never set when ImGuiConfigFlags_NoKeyboard is enabled. (#4921) - Error Handling: turned a few more functions into recoverable errors. (#1651) +- Nav: added io.ConfigNavEscapeClearFocusWindow to clear focused window on Escape. (#3200) +- Nav: pressing escape to hide nav highlight doesn't clear location from when ctrl+tabbing back + into same window later. - DrawList: AddCallback() added an optional size parameter allowing to copy and store any amount of user data for usage by callbacks: (#6969, #4770, #7665) - If userdata_size == 0: we copy/store the 'userdata' argument as-is (existing behavior). diff --git a/imgui.cpp b/imgui.cpp index 47ea5f471..486832ec3 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1408,6 +1408,7 @@ ImGuiIO::ImGuiIO() ConfigNavSwapGamepadButtons = false; ConfigNavMoveSetMousePos = false; ConfigNavCaptureKeyboard = true; + ConfigNavEscapeClearFocusWindow = false; ConfigInputTrickleEventQueue = true; ConfigInputTextCursorBlink = true; ConfigInputTextEnterKeepActive = false; @@ -13316,7 +13317,7 @@ void ImGui::NavMoveRequestApplyResult() NavRestoreHighlightAfterMove(); } -// Process NavCancel input (to close a popup, get back to parent, clear focus) +// Process Escape/NavCancel input (to close a popup, get back to parent, clear focus) // FIXME: In order to support e.g. Escape to clear a selection we'll need: // - either to store the equivalent of ActiveIdUsingKeyInputMask for a FocusScope and test for it. // - either to move most/all of those tests to the epilogue/end functions of the scope they are dealing with (e.g. exit child window in EndChild()) or in EndFrame(), to allow an earlier intercept @@ -13358,11 +13359,13 @@ static void ImGui::NavUpdateCancelRequest() { // Clear NavLastId for popups but keep it for regular child window so we can leave one and come back where we were // FIXME-NAV: This should happen on window appearing. - if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup) || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow))) + if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup)))// || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow))) g.NavWindow->NavLastIds[0] = 0; // Clear nav focus g.NavId = 0; + if (g.IO.ConfigNavEscapeClearFocusWindow) + FocusWindow(NULL); } } diff --git a/imgui.h b/imgui.h index 6a2523010..7d17aede6 100644 --- a/imgui.h +++ b/imgui.h @@ -2250,6 +2250,7 @@ struct ImGuiIO bool ConfigNavSwapGamepadButtons; // = false // Swap Activate<>Cancel (A<>B) buttons, matching typical "Nintendo/Japanese style" gamepad layout. bool ConfigNavMoveSetMousePos; // = false // Directional/tabbing navigation teleports the mouse cursor. May be useful on TV/console systems where moving a virtual mouse is difficult. Will update io.MousePos and set io.WantSetMousePos=true. bool ConfigNavCaptureKeyboard; // = true // Sets io.WantCaptureKeyboard when io.NavActive is set. + bool ConfigNavEscapeClearFocusWindow;// = false // Pressing Escape (when no item is active, no popup open etc.) clears focused window + navigation id/highlight. bool ConfigInputTrickleEventQueue; // = true // Enable input queue trickling: some types of events submitted during the same frame (e.g. button down + up) will be spread over multiple frames, improving interactions with low framerates. bool ConfigInputTextCursorBlink; // = true // Enable blinking cursor (optional as some users consider it to be distracting). bool ConfigInputTextEnterKeepActive; // = false // [BETA] Pressing Enter will keep item active and select contents (single-line only). diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 9c9868aa5..dca674054 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -532,6 +532,8 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::Checkbox("io.ConfigNavMoveSetMousePos", &io.ConfigNavMoveSetMousePos); ImGui::SameLine(); HelpMarker("Directional/tabbing navigation teleports the mouse cursor. May be useful on TV/console systems where moving a virtual mouse is difficult"); ImGui::Checkbox("io.ConfigNavCaptureKeyboard", &io.ConfigNavCaptureKeyboard); + ImGui::Checkbox("io.ConfigNavEscapeClearFocusWindow", &io.ConfigNavEscapeClearFocusWindow); + ImGui::SameLine(); HelpMarker("Pressing Escape clears focused window."); ImGui::SeparatorText("Widgets"); ImGui::Checkbox("io.ConfigInputTextCursorBlink", &io.ConfigInputTextCursorBlink);