Skip to content

Commit

Permalink
Viewports: Fix issue inferring viewport z-order when new popups gets …
Browse files Browse the repository at this point in the history
…created. (#3734) + Metrics updates.

Revert 6bc5266
Showing inferred order and missing flags in metrics.
  • Loading branch information
ocornut committed Feb 10, 2021
1 parent 2a5eaf2 commit 3607c42
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
47 changes: 39 additions & 8 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11839,7 +11839,7 @@ void ImGui::UpdatePlatformWindows()
// This is useful for our platform z-order heuristic when io.MouseHoveredViewport is not available.
if (viewport->LastFrontMostStampCount != g.ViewportFrontMostStampCount)
viewport->LastFrontMostStampCount = ++g.ViewportFrontMostStampCount;
}
}

// Clear request flags
viewport->ClearRequestFlags();
Expand All @@ -11860,8 +11860,14 @@ void ImGui::UpdatePlatformWindows()
}

// Store a tag so we can infer z-order easily from all our windows
if (focused_viewport && focused_viewport->LastFrontMostStampCount != g.ViewportFrontMostStampCount)
focused_viewport->LastFrontMostStampCount = ++g.ViewportFrontMostStampCount;
// We compare PlatformLastFocusedViewportId so newly created viewports with _NoFocusOnAppearing flag
// will keep the front most stamp instead of losing it back to their parent viewport.
if (focused_viewport && g.PlatformLastFocusedViewportId != focused_viewport->ID)
{
if (focused_viewport->LastFrontMostStampCount != g.ViewportFrontMostStampCount)
focused_viewport->LastFrontMostStampCount = ++g.ViewportFrontMostStampCount;
g.PlatformLastFocusedViewportId = focused_viewport->ID;
}
}
}

Expand Down Expand Up @@ -15852,6 +15858,13 @@ static void RenderViewportsThumbnails()
ImGui::Dummy(bb_full.GetSize() * SCALE);
}

static int IMGUI_CDECL ViewportComparerByFrontMostStampCount(const void* lhs, const void* rhs)
{
const ImGuiViewportP* a = *(const ImGuiViewportP* const *)lhs;
const ImGuiViewportP* b = *(const ImGuiViewportP* const *)rhs;
return b->LastFrontMostStampCount - a->LastFrontMostStampCount;
}

// Avoid naming collision with imgui_demo.cpp's HelpMarker() for unity builds.
static void MetricsHelpMarker(const char* desc)
{
Expand Down Expand Up @@ -16059,6 +16072,18 @@ void ImGui::ShowMetricsWindow(bool* p_open)
TreePop();
}

if (TreeNode("Inferred order (front-to-back)"))
{
static ImVector<ImGuiViewportP*> viewports;
viewports.resize(g.Viewports.Size);
memcpy(viewports.Data, g.Viewports.Data, g.Viewports.size_in_bytes());
if (viewports.Size > 1)
ImQsort(viewports.Data, viewports.Size, sizeof(ImGuiViewport*), ViewportComparerByFrontMostStampCount);
for (int i = 0; i < viewports.Size; i++)
BulletText("Viewport #%d, ID: 0x%08X, FrontMostStampCount = %08d, Window: \"%s\"", viewports[i]->Idx, viewports[i]->ID, viewports[i]->LastFrontMostStampCount, viewports[i]->Window ? viewports[i]->Window->Name : "N/A");
TreePop();
}

for (int i = 0; i < g.Viewports.Size; i++)
DebugNodeViewport(g.Viewports[i]);
TreePop();
Expand Down Expand Up @@ -16557,11 +16582,17 @@ void ImGui::DebugNodeViewport(ImGuiViewportP* viewport)
viewport->WorkOffsetMin.x, viewport->WorkOffsetMin.y, viewport->WorkOffsetMax.x, viewport->WorkOffsetMax.y,
viewport->PlatformMonitor, viewport->DpiScale * 100.0f);
if (viewport->Idx > 0) { SameLine(); if (SmallButton("Reset Pos")) { viewport->Pos = ImVec2(200, 200); viewport->UpdateWorkRect(); if (viewport->Window) viewport->Window->Pos = viewport->Pos; } }
BulletText("Flags: 0x%04X =%s%s%s%s%s%s%s", viewport->Flags,
(flags & ImGuiViewportFlags_CanHostOtherWindows) ? " CanHostOtherWindows" : "", (flags & ImGuiViewportFlags_NoDecoration) ? " NoDecoration" : "",
(flags & ImGuiViewportFlags_NoFocusOnAppearing) ? " NoFocusOnAppearing" : "", (flags & ImGuiViewportFlags_NoInputs) ? " NoInputs" : "",
(flags & ImGuiViewportFlags_NoRendererClear) ? " NoRendererClear" : "", (flags & ImGuiViewportFlags_Minimized) ? " Minimized" : "",
(flags & ImGuiViewportFlags_NoAutoMerge) ? " NoAutoMerge" : "");
BulletText("Flags: 0x%04X =%s%s%s%s%s%s%s%s%s%s", viewport->Flags,
(flags & ImGuiViewportFlags_NoDecoration) ? " NoDecoration" : "",
(flags & ImGuiViewportFlags_NoTaskBarIcon) ? " NoTaskBarIcon" : "",
(flags & ImGuiViewportFlags_NoFocusOnAppearing) ? " NoFocusOnAppearing" : "",
(flags & ImGuiViewportFlags_NoFocusOnClick) ? " NoFocusOnClick" : "",
(flags & ImGuiViewportFlags_NoInputs) ? " NoInputs" : "",
(flags & ImGuiViewportFlags_NoRendererClear) ? " NoRendererClear" : "",
(flags & ImGuiViewportFlags_TopMost) ? " TopMost" : "",
(flags & ImGuiViewportFlags_Minimized) ? " Minimized" : "",
(flags & ImGuiViewportFlags_NoAutoMerge) ? " NoAutoMerge" : "",
(flags & ImGuiViewportFlags_CanHostOtherWindows) ? " CanHostOtherWindows" : "");
for (int layer_i = 0; layer_i < IM_ARRAYSIZE(viewport->DrawDataBuilder.Layers); layer_i++)
for (int draw_list_i = 0; draw_list_i < viewport->DrawDataBuilder.Layers[layer_i].Size; draw_list_i++)
DebugNodeDrawList(NULL, viewport, viewport->DrawDataBuilder.Layers[layer_i][draw_list_i], "DrawList");
Expand Down
2 changes: 2 additions & 0 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,7 @@ struct ImGuiContext
ImGuiViewportP* CurrentViewport; // We track changes of viewport (happening in Begin) so we can call Platform_OnChangedViewport()
ImGuiViewportP* MouseViewport;
ImGuiViewportP* MouseLastHoveredViewport; // Last known viewport that was hovered by mouse (even if we are not hovering any viewport any more) + honoring the _NoInputs flag.
ImGuiID PlatformLastFocusedViewportId;
int ViewportFrontMostStampCount; // Every time the front-most window changes, we stamp its viewport with an incrementing counter

// Gamepad/keyboard Navigation
Expand Down Expand Up @@ -1752,6 +1753,7 @@ struct ImGuiContext
CurrentDpiScale = 0.0f;
CurrentViewport = NULL;
MouseViewport = MouseLastHoveredViewport = NULL;
PlatformLastFocusedViewportId = 0;
ViewportFrontMostStampCount = 0;

NavWindow = NULL;
Expand Down

0 comments on commit 3607c42

Please sign in to comment.