Skip to content

Commit 1f0b46b

Browse files
committed
Viewports: moved Platform_GetWindowFocus queries in UpdateViewportsNewFrame(). Added ImGuiViewportFlags_IsFocused status flag. (#1542)
Not sure why queries were in UpdatePlatformWindows(). - initially added there on 2018/04/26 f1ae07e (squashed) - slightly moved in cd51f37 for the purpose of putting less constraint on backend but that check is now done on our side anyhow. Seems more consistent to do it nxt to other polling in UpdateViewportsNewFrame(). Not using ImGuiViewportFlags_IsFocused yet.
1 parent ed72fcd commit 1f0b46b

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

imgui.cpp

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13874,21 +13874,54 @@ static void ImGui::UpdateViewportsNewFrame()
1387413874
IM_ASSERT(g.PlatformIO.Viewports.Size <= g.Viewports.Size);
1387513875

1387613876
// Update Minimized status (we need it first in order to decide if we'll apply Pos/Size of the main viewport)
13877+
// Update Focused status
1387713878
const bool viewports_enabled = (g.ConfigFlagsCurrFrame & ImGuiConfigFlags_ViewportsEnable) != 0;
1387813879
if (viewports_enabled)
1387913880
{
13881+
ImGuiViewportP* focused_viewport = NULL;
1388013882
for (int n = 0; n < g.Viewports.Size; n++)
1388113883
{
1388213884
ImGuiViewportP* viewport = g.Viewports[n];
1388313885
const bool platform_funcs_available = viewport->PlatformWindowCreated;
1388413886
if (g.PlatformIO.Platform_GetWindowMinimized && platform_funcs_available)
1388513887
{
13886-
bool minimized = g.PlatformIO.Platform_GetWindowMinimized(viewport);
13887-
if (minimized)
13888+
bool is_minimized = g.PlatformIO.Platform_GetWindowMinimized(viewport);
13889+
if (is_minimized)
1388813890
viewport->Flags |= ImGuiViewportFlags_IsMinimized;
1388913891
else
1389013892
viewport->Flags &= ~ImGuiViewportFlags_IsMinimized;
1389113893
}
13894+
13895+
// Update our implicit z-order knowledge of platform windows, which is used when the backend cannot provide io.MouseHoveredViewport.
13896+
// When setting Platform_GetWindowFocus, it is expected that the platform backend can handle calls without crashing if it doesn't have data stored.
13897+
if (g.PlatformIO.Platform_GetWindowFocus && platform_funcs_available)
13898+
{
13899+
bool is_focused = g.PlatformIO.Platform_GetWindowFocus(viewport);
13900+
if (is_focused)
13901+
viewport->Flags |= ImGuiViewportFlags_IsFocused;
13902+
else
13903+
viewport->Flags &= ~ImGuiViewportFlags_IsFocused;
13904+
if (is_focused)
13905+
focused_viewport = viewport;
13906+
}
13907+
}
13908+
13909+
// Focused viewport has changed?
13910+
if (focused_viewport && g.PlatformLastFocusedViewportId != focused_viewport->ID)
13911+
{
13912+
// Store a tag so we can infer z-order easily from all our windows
13913+
// We compare PlatformLastFocusedViewportId so newly created viewports with _NoFocusOnAppearing flag
13914+
// will keep the front most stamp instead of losing it back to their parent viewport.
13915+
if (focused_viewport->LastFocusedStampCount != g.ViewportFocusedStampCount)
13916+
focused_viewport->LastFocusedStampCount = ++g.ViewportFocusedStampCount;
13917+
g.PlatformLastFocusedViewportId = focused_viewport->ID;
13918+
13919+
// Focus associated dear imgui window (#6299)
13920+
// FIXME: perhaps 'FocusTopMostWindowUnderOne()' can handle both cases?
13921+
if (focused_viewport->Window != NULL)
13922+
FocusWindow(NavRestoreLastChildNavWindow(focused_viewport->Window));
13923+
else
13924+
FocusTopMostWindowUnderOne(NULL, NULL, focused_viewport);
1389213925
}
1389313926
}
1389413927

@@ -14088,7 +14121,7 @@ ImGuiViewportP* ImGui::AddUpdateViewport(ImGuiWindow* window, ImGuiID id, const
1408814121
viewport->Pos = pos;
1408914122
if (!viewport->PlatformRequestResize || viewport->ID == IMGUI_VIEWPORT_DEFAULT_ID)
1409014123
viewport->Size = size;
14091-
viewport->Flags = flags | (viewport->Flags & ImGuiViewportFlags_IsMinimized); // Preserve existing flags
14124+
viewport->Flags = flags | (viewport->Flags & (ImGuiViewportFlags_IsMinimized | ImGuiViewportFlags_IsFocused)); // Preserve existing flags
1409214125
}
1409314126
else
1409414127
{
@@ -14472,38 +14505,6 @@ void ImGui::UpdatePlatformWindows()
1447214505
// Clear request flags
1447314506
viewport->ClearRequestFlags();
1447414507
}
14475-
14476-
// Update our implicit z-order knowledge of platform windows, which is used when the backend cannot provide io.MouseHoveredViewport.
14477-
// When setting Platform_GetWindowFocus, it is expected that the platform backend can handle calls without crashing if it doesn't have data stored.
14478-
// FIXME-VIEWPORT: We should use this information to also set dear imgui-side focus, allowing us to handle os-level alt+tab.
14479-
if (g.PlatformIO.Platform_GetWindowFocus != NULL)
14480-
{
14481-
ImGuiViewportP* focused_viewport = NULL;
14482-
for (int n = 0; n < g.Viewports.Size && focused_viewport == NULL; n++)
14483-
{
14484-
ImGuiViewportP* viewport = g.Viewports[n];
14485-
if (viewport->PlatformWindowCreated)
14486-
if (g.PlatformIO.Platform_GetWindowFocus(viewport))
14487-
focused_viewport = viewport;
14488-
}
14489-
14490-
// Focused viewport has changed?
14491-
if (focused_viewport && g.PlatformLastFocusedViewportId != focused_viewport->ID)
14492-
{
14493-
// Store a tag so we can infer z-order easily from all our windows
14494-
// We compare PlatformLastFocusedViewportId so newly created viewports with _NoFocusOnAppearing flag
14495-
// will keep the front most stamp instead of losing it back to their parent viewport.
14496-
if (focused_viewport->LastFocusedStampCount != g.ViewportFocusedStampCount)
14497-
focused_viewport->LastFocusedStampCount = ++g.ViewportFocusedStampCount;
14498-
g.PlatformLastFocusedViewportId = focused_viewport->ID;
14499-
14500-
// Focus associated dear imgui window (#6299)
14501-
if (focused_viewport->Window != NULL)
14502-
FocusWindow(NavRestoreLastChildNavWindow(focused_viewport->Window));
14503-
else
14504-
FocusTopMostWindowUnderOne(NULL, NULL, focused_viewport);
14505-
}
14506-
}
1450714508
}
1450814509

1450914510
// This is a default/basic function for performing the rendering/swap of multiple Platform Windows.
@@ -19915,19 +19916,20 @@ void ImGui::DebugNodeViewport(ImGuiViewportP* viewport)
1991519916
viewport->WorkOffsetMin.x, viewport->WorkOffsetMin.y, viewport->WorkOffsetMax.x, viewport->WorkOffsetMax.y,
1991619917
viewport->PlatformMonitor, viewport->DpiScale * 100.0f);
1991719918
if (viewport->Idx > 0) { SameLine(); if (SmallButton("Reset Pos")) { viewport->Pos = ImVec2(200, 200); viewport->UpdateWorkRect(); if (viewport->Window) viewport->Window->Pos = viewport->Pos; } }
19918-
BulletText("Flags: 0x%04X =%s%s%s%s%s%s%s%s%s%s%s%s", viewport->Flags,
19919+
BulletText("Flags: 0x%04X =%s%s%s%s%s%s%s%s%s%s%s%s%s", viewport->Flags,
1991919920
//(flags & ImGuiViewportFlags_IsPlatformWindow) ? " IsPlatformWindow" : "", // Omitting because it is the standard
1992019921
(flags & ImGuiViewportFlags_IsPlatformMonitor) ? " IsPlatformMonitor" : "",
19922+
(flags & ImGuiViewportFlags_IsMinimized) ? " IsMinimized" : "",
19923+
(flags & ImGuiViewportFlags_IsFocused) ? " IsFocused" : "",
1992119924
(flags & ImGuiViewportFlags_OwnedByApp) ? " OwnedByApp" : "",
1992219925
(flags & ImGuiViewportFlags_NoDecoration) ? " NoDecoration" : "",
1992319926
(flags & ImGuiViewportFlags_NoTaskBarIcon) ? " NoTaskBarIcon" : "",
1992419927
(flags & ImGuiViewportFlags_NoFocusOnAppearing) ? " NoFocusOnAppearing" : "",
1992519928
(flags & ImGuiViewportFlags_NoFocusOnClick) ? " NoFocusOnClick" : "",
1992619929
(flags & ImGuiViewportFlags_NoInputs) ? " NoInputs" : "",
1992719930
(flags & ImGuiViewportFlags_NoRendererClear) ? " NoRendererClear" : "",
19928-
(flags & ImGuiViewportFlags_TopMost) ? " TopMost" : "",
19929-
(flags & ImGuiViewportFlags_IsMinimized) ? " IsMinimized" : "",
1993019931
(flags & ImGuiViewportFlags_NoAutoMerge) ? " NoAutoMerge" : "",
19932+
(flags & ImGuiViewportFlags_TopMost) ? " TopMost" : "",
1993119933
(flags & ImGuiViewportFlags_CanHostOtherWindows) ? " CanHostOtherWindows" : "");
1993219934
for (int layer_i = 0; layer_i < IM_ARRAYSIZE(viewport->DrawDataBuilder.Layers); layer_i++)
1993319935
for (int draw_list_i = 0; draw_list_i < viewport->DrawDataBuilder.Layers[layer_i].Size; draw_list_i++)

imgui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,6 +3088,7 @@ enum ImGuiViewportFlags_
30883088

30893089
// Output status flags (from Platform)
30903090
ImGuiViewportFlags_IsMinimized = 1 << 12, // Platform Window: Window is minimized, can skip render. When minimized we tend to avoid using the viewport pos/size for clipping window or testing if they are contained in the viewport.
3091+
ImGuiViewportFlags_IsFocused = 1 << 13, // Platform Window: Window is focused (last call to Platform_GetWindowFocus() returned true)
30913092
};
30923093

30933094
// - Currently represents the Platform Window created by the application which is hosting our Dear ImGui windows.

0 commit comments

Comments
 (0)