mirror of
https://github.com/Drezil/imgui.git
synced 2025-07-04 12:08:47 +02:00
Popups: Closing a popup restores the focused/nav window in place at the time of the popup opening, 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.
This commit is contained in:
44
imgui.cpp
44
imgui.cpp
@ -3603,7 +3603,7 @@ void ImGui::NewFrame()
|
||||
|
||||
// Closing the focused window restore focus to the first active root window in descending z-order
|
||||
if (g.NavWindow && !g.NavWindow->WasActive)
|
||||
FocusTopMostWindowIgnoringOne(NULL);
|
||||
FocusTopMostWindowUnderOne(NULL, NULL);
|
||||
|
||||
// No window should be open at the beginning of the frame.
|
||||
// But in order to allow the user to call NewFrame() multiple times without calling Render(), we are doing an explicit clear.
|
||||
@ -5726,10 +5726,18 @@ void ImGui::FocusWindow(ImGuiWindow* window)
|
||||
BringWindowToDisplayFront(window);
|
||||
}
|
||||
|
||||
void ImGui::FocusTopMostWindowIgnoringOne(ImGuiWindow* ignore_window)
|
||||
void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window)
|
||||
{
|
||||
ImGuiContext& g = *GImGui;
|
||||
for (int i = g.WindowsFocusOrder.Size - 1; i >= 0; i--)
|
||||
|
||||
int start_idx = g.WindowsFocusOrder.Size - 1;
|
||||
if (under_this_window != NULL)
|
||||
{
|
||||
int under_this_window_idx = FindWindowFocusIndex(under_this_window);
|
||||
if (under_this_window_idx != -1)
|
||||
start_idx = under_this_window_idx - 1;
|
||||
}
|
||||
for (int i = start_idx; i >= 0; i--)
|
||||
{
|
||||
// We may later decide to test for different NoXXXInputs based on the active navigation input (mouse vs nav) but that may feel more confusing to the user.
|
||||
ImGuiWindow* window = g.WindowsFocusOrder[i];
|
||||
@ -5741,6 +5749,7 @@ void ImGui::FocusTopMostWindowIgnoringOne(ImGuiWindow* ignore_window)
|
||||
return;
|
||||
}
|
||||
}
|
||||
FocusWindow(NULL);
|
||||
}
|
||||
|
||||
void ImGui::SetNextItemWidth(float item_width)
|
||||
@ -6949,7 +6958,7 @@ void ImGui::OpenPopupEx(ImGuiID id)
|
||||
ImGuiPopupData popup_ref; // Tagged as new ref as Window will be set back to NULL if we write this into OpenPopupStack.
|
||||
popup_ref.PopupId = id;
|
||||
popup_ref.Window = NULL;
|
||||
popup_ref.ParentWindow = parent_window;
|
||||
popup_ref.SourceWindow = g.NavWindow;
|
||||
popup_ref.OpenFrameCount = g.FrameCount;
|
||||
popup_ref.OpenParentId = parent_window->IDStack.back();
|
||||
popup_ref.OpenPopupPos = NavCalcPreferredRefPos();
|
||||
@ -7035,24 +7044,25 @@ void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window)
|
||||
|
||||
void ImGui::ClosePopupToLevel(int remaining, bool apply_focus_to_window_under)
|
||||
{
|
||||
IM_ASSERT(remaining >= 0);
|
||||
ImGuiContext& g = *GImGui;
|
||||
ImGuiWindow* focus_window = (remaining > 0) ? g.OpenPopupStack[remaining-1].Window : g.OpenPopupStack[0].ParentWindow;
|
||||
IM_ASSERT(remaining >= 0 && remaining < g.OpenPopupStack.Size);
|
||||
ImGuiWindow* focus_window = g.OpenPopupStack[remaining].SourceWindow;
|
||||
ImGuiWindow* popup_window = g.OpenPopupStack[remaining].Window;
|
||||
g.OpenPopupStack.resize(remaining);
|
||||
|
||||
// FIXME: This code is faulty and we may want to eventually to replace or remove the 'apply_focus_to_window_under=true' path completely.
|
||||
// Instead of using g.OpenPopupStack[remaining-1].Window etc. we should find the highest root window that is behind the popups we are closing.
|
||||
// The current code will set focus to the parent of the popup window which is incorrect.
|
||||
// It rarely manifested until now because UpdateMouseMovingWindowNewFrame() would call FocusWindow() again on the clicked window,
|
||||
// leading to a chain of focusing A (clicked window) then B (parent window of the popup) then A again.
|
||||
// However if the clicked window has the _NoMove flag set we would be left with B focused.
|
||||
// For now, we have disabled this path when called from ClosePopupsOverWindow() because the users of ClosePopupsOverWindow() don't need to alter focus anyway,
|
||||
// but we should inspect and fix this properly.
|
||||
if (apply_focus_to_window_under)
|
||||
{
|
||||
if (g.NavLayer == 0)
|
||||
focus_window = NavRestoreLastChildNavWindow(focus_window);
|
||||
FocusWindow(focus_window);
|
||||
if (focus_window && !focus_window->WasActive && popup_window)
|
||||
{
|
||||
// Fallback
|
||||
FocusTopMostWindowUnderOne(popup_window, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (g.NavLayer == 0 && focus_window)
|
||||
focus_window = NavRestoreLastChildNavWindow(focus_window);
|
||||
FocusWindow(focus_window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user