Skip to content

Commit

Permalink
Popups: popups can be closed with a right-click anywhere, without alt…
Browse files Browse the repository at this point in the history
…ering focus under the popup.(~#439)
  • Loading branch information
ocornut committed Oct 20, 2017
1 parent 853018d commit 87ae408
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ static void MarkIniSettingsDirty(ImGuiWindow* window);

static ImRect GetVisibleRect();

static void CloseInactivePopups();
static void CloseInactivePopups(ImGuiWindow* ref_window);
static void ClosePopupToLevel(int remaining);
static ImGuiWindow* GetFrontMostModalRootWindow();
static ImVec2 FindBestPopupWindowPos(const ImVec2& base_pos, const ImVec2& size, int* last_dir, const ImRect& rect_to_avoid);
Expand Down Expand Up @@ -2441,7 +2441,7 @@ void ImGui::NewFrame()
// 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.CurrentPopupStack.resize(0);
CloseInactivePopups();
CloseInactivePopups(g.NavWindow);

// Create implicit window - 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.
Expand Down Expand Up @@ -2773,7 +2773,7 @@ void ImGui::EndFrame()

if (g.ActiveId == 0 && g.HoveredId == 0)
{
if (!g.NavWindow || g.NavWindow->WasActive || !g.NavWindow->Active) // Unless we just made a popup appear
if (!g.NavWindow || !g.NavWindow->Appearing) // Unless we just made a window/popup appear
{
// Click to focus window and start moving (after we're done with all our widgets)
if (g.IO.MouseClicked[0])
Expand All @@ -2794,6 +2794,13 @@ void ImGui::EndFrame()
FocusWindow(NULL);
}
}

// With right mouse button we close popups without changing focus
// (The left mouse button path calls FocusWindow which will lead NewFrame->CloseInactivePopups to trigger)
if (g.IO.MouseClicked[1])
{
CloseInactivePopups(g.HoveredWindow);
}
}
}

Expand Down Expand Up @@ -3567,7 +3574,7 @@ void ImGui::OpenPopup(const char* str_id)
OpenPopupEx(g.CurrentWindow->GetID(str_id), false);
}

static void CloseInactivePopups()
static void CloseInactivePopups(ImGuiWindow* ref_window)
{
ImGuiContext& g = *GImGui;
if (g.OpenPopupStack.empty())
Expand All @@ -3576,7 +3583,7 @@ static void CloseInactivePopups()
// When popups are stacked, clicking on a lower level popups puts focus back to it and close popups above it.
// Don't close our own child popup windows
int n = 0;
if (g.NavWindow)
if (ref_window)
{
for (n = 0; n < g.OpenPopupStack.Size; n++)
{
Expand All @@ -3589,7 +3596,7 @@ static void CloseInactivePopups()

bool has_focus = false;
for (int m = n; m < g.OpenPopupStack.Size && !has_focus; m++)
has_focus = (g.OpenPopupStack[m].Window && g.OpenPopupStack[m].Window->RootWindow == g.NavWindow->RootWindow);
has_focus = (g.OpenPopupStack[m].Window && g.OpenPopupStack[m].Window->RootWindow == ref_window->RootWindow);
if (!has_focus)
break;
}
Expand Down

0 comments on commit 87ae408

Please sign in to comment.