Popups: Closes popup at the time of FocusWindow(). Fixes right-click from closing all popups instead of aiming at the hovered popup level (regression in 1.67's ae76a1fd).
diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt
index e6d387b..a753438 100644
--- a/docs/CHANGELOG.txt
+++ b/docs/CHANGELOG.txt
@@ -63,6 +63,8 @@
instead of restoring the window that was in the window stack at the time of the OpenPopup call. (#2517)
Among other things, this allows opening a popup while no window are focused, and pressing Escape to
clear the focus again.
+- Popups: Fixed right-click from closing all popups instead of aiming at the hovered popup level
+ (regression in 1.67).
- GetMouseDragDelta(): also returns the delta on the mouse button released frame. (#2419)
- GetMouseDragDelta(): verify that mouse positions are valid otherwise returns zero.
- Inputs: Also add support for horizontal scroll with Shift+Mouse Wheel. (#2424, #1463) [@LucaRood]
diff --git a/imgui.cpp b/imgui.cpp
index e465fdf..d58ba77 100644
--- a/imgui.cpp
+++ b/imgui.cpp
@@ -2880,7 +2880,8 @@
if (g.ActiveId != 0 && g.ActiveId != window->DC.LastItemId && !g.ActiveIdAllowOverlap && g.ActiveId != window->MoveId)
return false;
- // Test if interactions on this window are blocked by an active popup or modal
+ // Test if interactions on this window are blocked by an active popup or modal.
+ // The ImGuiHoveredFlags_AllowWhenBlockedByPopup flag will be tested here.
if (!IsWindowContentHoverable(window, flags))
return false;
@@ -3213,8 +3214,9 @@
}
}
- // With right mouse button we close popups without changing focus
- // (The left mouse button path calls FocusWindow which will lead NewFrame->ClosePopupsOverWindow to trigger)
+ // With right mouse button we close popups without changing focus based on where the mouse is aimed
+ // Instead, focus will be restored to the window under the bottom-most closed popup.
+ // (The left mouse button path calls FocusWindow on the hovered window, which will lead NewFrame->ClosePopupsOverWindow to trigger)
if (g.IO.MouseClicked[1])
{
// Find the top-most window between HoveredWindow and the front most Modal Window.
@@ -3231,7 +3233,7 @@
if (window == g.HoveredWindow)
hovered_window_above_modal = true;
}
- ClosePopupsOverWindow(hovered_window_above_modal ? g.HoveredWindow : modal);
+ ClosePopupsOverWindow(hovered_window_above_modal ? g.HoveredWindow : modal, true);
}
}
@@ -3609,7 +3611,7 @@
// But in order to allow the user to call NewFrame() multiple times without calling Render(), we are doing an explicit clear.
g.CurrentWindowStack.resize(0);
g.BeginPopupStack.resize(0);
- ClosePopupsOverWindow(g.NavWindow);
+ ClosePopupsOverWindow(g.NavWindow, false);
// Create implicit/fallback window - which we will only render it if the user has added something to it.
// We don't use "Debug" to avoid colliding with user trying to create a "Debug" window with custom flags.
@@ -5707,6 +5709,9 @@
//IMGUI_DEBUG_LOG("FocusWindow(\"%s\")\n", window ? window->Name : NULL);
}
+ // Close popups if any
+ ClosePopupsOverWindow(window, false);
+
// Passing NULL allow to disable keyboard focus
if (!window)
return;
@@ -7005,7 +7010,7 @@
return false;
}
-void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window)
+void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup)
{
ImGuiContext& g = *GImGui;
if (g.OpenPopupStack.empty())
@@ -7026,23 +7031,23 @@
if (popup.Window->Flags & ImGuiWindowFlags_ChildWindow)
continue;
- // Trim the stack if popups are not direct descendant of the reference window (which is often the NavWindow)
- bool popup_or_descendent_has_focus = false;
- for (int m = popup_count_to_keep; m < g.OpenPopupStack.Size && !popup_or_descendent_has_focus; m++)
+ // Trim the stack when popups are not direct descendant of the reference window (the reference window is often the NavWindow)
+ bool popup_or_descendent_is_ref_window = false;
+ for (int m = popup_count_to_keep; m < g.OpenPopupStack.Size && !popup_or_descendent_is_ref_window; m++)
if (g.OpenPopupStack[m].Window && g.OpenPopupStack[m].Window->RootWindow == ref_window->RootWindow)
- popup_or_descendent_has_focus = true;
- if (!popup_or_descendent_has_focus)
+ popup_or_descendent_is_ref_window = true;
+ if (!popup_or_descendent_is_ref_window)
break;
}
}
if (popup_count_to_keep < g.OpenPopupStack.Size) // This test is not required but it allows to set a convenient breakpoint on the statement below
{
//IMGUI_DEBUG_LOG("ClosePopupsOverWindow(%s) -> ClosePopupToLevel(%d)\n", ref_window->Name, popup_count_to_keep);
- ClosePopupToLevel(popup_count_to_keep, false);
+ ClosePopupToLevel(popup_count_to_keep, restore_focus_to_window_under_popup);
}
}
-void ImGui::ClosePopupToLevel(int remaining, bool apply_focus_to_window_under)
+void ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup)
{
ImGuiContext& g = *GImGui;
IM_ASSERT(remaining >= 0 && remaining < g.OpenPopupStack.Size);
@@ -7050,7 +7055,7 @@
ImGuiWindow* popup_window = g.OpenPopupStack[remaining].Window;
g.OpenPopupStack.resize(remaining);
- if (apply_focus_to_window_under)
+ if (restore_focus_to_window_under_popup)
{
if (focus_window && !focus_window->WasActive && popup_window)
{
@@ -8278,11 +8283,11 @@
// Apply final focus
if (apply_focus_window && (g.NavWindow == NULL || apply_focus_window != g.NavWindow->RootWindow))
{
+ ClearActiveID();
g.NavDisableHighlight = false;
g.NavDisableMouseHover = true;
apply_focus_window = NavRestoreLastChildNavWindow(apply_focus_window);
- ClosePopupsOverWindow(apply_focus_window);
- ClearActiveID();
+ ClosePopupsOverWindow(apply_focus_window, false);
FocusWindow(apply_focus_window);
if (apply_focus_window->NavLastIds[0] == 0)
NavInitWindow(apply_focus_window, false);
diff --git a/imgui_demo.cpp b/imgui_demo.cpp
index 695fd86..a57cfac 100644
--- a/imgui_demo.cpp
+++ b/imgui_demo.cpp
@@ -2209,6 +2209,13 @@
if (ImGui::BeginMenu("Sub-menu"))
{
ImGui::MenuItem("Click me");
+ if (ImGui::Button("Stacked Popup"))
+ ImGui::OpenPopup("another popup");
+ if (ImGui::BeginPopup("another popup"))
+ {
+ ImGui::Text("I am the last one here.");
+ ImGui::EndPopup();
+ }
ImGui::EndMenu();
}
ImGui::EndPopup();
diff --git a/imgui_internal.h b/imgui_internal.h
index 415dfae..23b9525 100644
--- a/imgui_internal.h
+++ b/imgui_internal.h
@@ -1466,8 +1466,8 @@
// Popups, Modals, Tooltips
IMGUI_API void OpenPopupEx(ImGuiID id);
- IMGUI_API void ClosePopupToLevel(int remaining, bool apply_focus_to_window_under);
- IMGUI_API void ClosePopupsOverWindow(ImGuiWindow* ref_window);
+ IMGUI_API void ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup);
+ IMGUI_API void ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup);
IMGUI_API bool IsPopupOpen(ImGuiID id); // Test for id within current popup stack level (currently begin-ed into); this doesn't scan the whole popup stack!
IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags);
IMGUI_API void BeginTooltipEx(ImGuiWindowFlags extra_flags, bool override_previous_tooltip = true);