Popups: fix to allow child popups to be opened from a normal window without an intermediate popup window.

This commit is contained in:
ocornut 2015-05-15 23:57:43 +01:00
parent 4250357ed2
commit ecda785cbc

View File

@ -1150,9 +1150,10 @@ struct ImGuiMouseCursorData
struct ImGuiPopupRef struct ImGuiPopupRef
{ {
ImGuiID PopupID; // Set on OpenPopup() ImGuiID PopupID; // Set on OpenPopup()
ImGuiWindow* ParentWindow; // Set on OpenPopup()
ImGuiWindow* Window; // Resolved on BeginPopup() - may stay unresolved if user never calls OpenPopup() ImGuiWindow* Window; // Resolved on BeginPopup() - may stay unresolved if user never calls OpenPopup()
ImGuiPopupRef(ImGuiID id) { PopupID = id; Window = NULL; } ImGuiPopupRef(ImGuiID id, ImGuiWindow* parent_window) { PopupID = id; ParentWindow = parent_window; Window = NULL; }
}; };
// Main state for ImGui // Main state for ImGui
@ -2136,24 +2137,36 @@ static void CloseInactivePopups()
return; return;
// User has clicked outside of a popup // User has clicked outside of a popup
if (!g.FocusedWindow || !(g.FocusedWindow->RootWindow->Flags & ImGuiWindowFlags_Popup)) if (!g.FocusedWindow)
{ {
g.OpenedPopupStack.resize(0); g.OpenedPopupStack.resize(0);
return; return;
} }
// When popups are stacked, clicking on a lower level popups puts focus back to it and close popups above it // When popups are stacked, clicking on a lower level popups puts focus back to it and close popups above it
IM_ASSERT(g.FocusedWindow->RootWindow->PopupID != 0); // Don't close our own child popup windows
for (int n = 0; n < (int)g.OpenedPopupStack.size()-1; n++) int n;
if (g.OpenedPopupStack[n].PopupID == g.FocusedWindow->RootWindow->PopupID) for (n = 0; n < (int)g.OpenedPopupStack.size(); n++)
{ {
// Don't close our own Child Popup windows ImGuiPopupRef& popup = g.OpenedPopupStack[n];
while (++n < (int)g.OpenedPopupStack.size()) if (!popup.Window)
if (!g.OpenedPopupStack[n].Window || !(g.OpenedPopupStack[n].Window->Flags & ImGuiWindowFlags_ChildWindow)) continue;
IM_ASSERT((popup.Window->Flags & ImGuiWindowFlags_Popup) != 0);
if (popup.Window->Flags & ImGuiWindowFlags_ChildWindow)
{
if (g.FocusedWindow->RootWindow != popup.Window->RootWindow)
break; break;
g.OpenedPopupStack.resize(n);
return;
} }
else
{
bool has_focus = false;
for (int m = n; m < (int)g.OpenedPopupStack.size() && !has_focus; m++)
has_focus = (g.OpenedPopupStack[m].Window && g.OpenedPopupStack[m].Window->RootWindow == g.FocusedWindow->RootWindow);
if (!has_focus)
break;
}
}
g.OpenedPopupStack.resize(n);
} }
// NB: behavior of ImGui after Shutdown() is not tested/guaranteed at the moment. This function is merely here to free heap allocations. // NB: behavior of ImGui after Shutdown() is not tested/guaranteed at the moment. This function is merely here to free heap allocations.
@ -2985,15 +2998,9 @@ void ImGui::OpenPopup(const char* str_id)
const ImGuiID id = window->GetID(str_id); const ImGuiID id = window->GetID(str_id);
// One open popup per level of the popup hierarchy (NB: when assigning we reset the Window member of ImGuiPopupRef to NULL) // One open popup per level of the popup hierarchy (NB: when assigning we reset the Window member of ImGuiPopupRef to NULL)
if (g.OpenedPopupStack.size() == g.CurrentPopupStack.size()) g.OpenedPopupStack.resize(g.CurrentPopupStack.size() + 1);
g.OpenedPopupStack.push_back(ImGuiPopupRef(id));
else if (g.OpenedPopupStack.size() == g.CurrentPopupStack.size() + 1)
{
if (g.OpenedPopupStack.back().PopupID != id) if (g.OpenedPopupStack.back().PopupID != id)
g.OpenedPopupStack.back() = ImGuiPopupRef(id); g.OpenedPopupStack.back() = ImGuiPopupRef(id, window);
}
else
IM_ASSERT(0); // Invalid state
} }
void ImGui::CloseCurrentPopup() void ImGui::CloseCurrentPopup()
@ -3032,7 +3039,7 @@ static bool BeginPopupEx(const char* str_id, ImGuiWindowFlags extra_flags)
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGuiWindowFlags flags = ImGuiWindowFlags_Popup|ImGuiWindowFlags_ShowBorders|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_AlwaysAutoResize; ImGuiWindowFlags flags = ImGuiWindowFlags_Popup|ImGuiWindowFlags_ShowBorders|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_AlwaysAutoResize;
flags |= extra_flags; flags |= extra_flags;
if ((flags & ImGuiWindowFlags_Menu) && (window->Flags & ImGuiWindowFlags_Popup)) if ((flags & ImGuiWindowFlags_Menu))
flags |= ImGuiWindowFlags_ChildWindow; flags |= ImGuiWindowFlags_ChildWindow;
char name[32]; char name[32];