Skip to content

Commit f1ae07e

Browse files
committed
Viewport, Platform: Using Platform_GetWindowFocus to provide a much stronger heuristic of platform z-order, in replacement for when the back-end cannot provide io.MouseHoveredViewport. The pressure for it to work well increased with the use of viewports by popups/menus. (#1542)
1 parent d9cd494 commit f1ae07e

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

TODO.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ It's mostly a bunch of personal notes, probably incomplete. Feel free to query i
262262
- viewport: platform: introduce getfocus/setfocus api, so e.g. focus flags can be honored, imgui-side ctrl-tab can focus os window, OS alt-tab can focus imgui window etc.
263263
- viewport: store per-viewport/monitor DPI in .ini file so an application reload or main window changing DPI on reload can be properly patched for.
264264
- viewport: vulkan renderer implementation.
265-
- viewport: fallback calculation of hovered window is very currently wrong without ImGuiBackendFlags_HasMouseHoveredViewport. typically affect half-overlapping viewported menus.
266265
- viewport: need to clarify how to use GetMousePos() from a user point of view.
267266

268267
- inputs: we need an explicit flag about whether the imgui window is focused, to be able to distinguish focused key releases vs alt-tabbing all release behaviors.

imgui.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3392,8 +3392,8 @@ static void ImGui::UpdateMovingWindow()
33923392
}
33933393

33943394
// If the back-end doesn't set MouseLastHoveredViewport or doesn't honor ImGuiViewportFlags_NoInputs, we do a search ourselves.
3395-
// This search won't take account of the possibility that non-imgui windows may be in-between our dragged window and our target window.
3396-
// FIXME-VIEWPORT: Need a proper notion of focus. At least use the equivalent of LastFrameAsRefViewport on a per-window basis.
3395+
// A) It won't take account of the possibility that non-imgui windows may be in-between our dragged window and our target window.
3396+
// B) It requires Platform_GetWindowFocus to be implemented by back-end.
33973397
static ImGuiViewportP* FindViewportHoveredFromPlatformWindowStack(const ImVec2 mouse_platform_pos)
33983398
{
33993399
ImGuiContext& g = *GImGui;
@@ -3402,7 +3402,7 @@ static ImGuiViewportP* FindViewportHoveredFromPlatformWindowStack(const ImVec2 m
34023402
{
34033403
ImGuiViewportP* viewport = g.Viewports[n];
34043404
if (!(viewport->Flags & ImGuiViewportFlags_NoInputs) && viewport->GetRect().Contains(mouse_platform_pos))
3405-
if (best_candidate == NULL || best_candidate->LastFrameAsRefViewport < viewport->LastFrameAsRefViewport)
3405+
if (best_candidate == NULL || best_candidate->LastFrontMostStampCount < viewport->LastFrontMostStampCount)
34063406
best_candidate = viewport;
34073407
}
34083408
return best_candidate;
@@ -3532,10 +3532,8 @@ static void ImGui::UpdateViewports()
35323532
if (!(g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
35333533
{
35343534
g.MouseRefViewport = g.MouseRefPrevViewport = main_viewport;
3535-
g.MouseRefViewport->LastFrameAsRefViewport = g.FrameCount;
35363535
return;
35373536
}
3538-
g.MouseRefViewport->LastFrameAsRefViewport = g.FrameCount;
35393537

35403538
// Mouse handling: decide on the actual mouse viewport for this frame between the active/focused viewport and the hovered viewport.
35413539
ImGuiViewportP* viewport_hovered = NULL;
@@ -3584,6 +3582,21 @@ void ImGui::UpdatePlatformWindows()
35843582
if (!(g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
35853583
return;
35863584

3585+
// Update our implicit z-order knowledge of platform windows, which is used when the back-end cannot provide io.MouseHoveredViewport.
3586+
if (g.PlatformIO.Platform_GetWindowFocus)
3587+
{
3588+
ImGuiViewportP* focused_viewport = NULL;
3589+
for (int i = 0; i < g.Viewports.Size && focused_viewport == NULL; i++)
3590+
if (g.PlatformIO.Platform_GetWindowFocus(g.Viewports[i]))
3591+
focused_viewport = g.Viewports[i];
3592+
if (focused_viewport && g.PlatformLastFocusedViewport != focused_viewport->ID)
3593+
{
3594+
if (focused_viewport->LastFrontMostStampCount != g.WindowsFrontMostStampCount)
3595+
focused_viewport->LastFrontMostStampCount = ++g.WindowsFrontMostStampCount;
3596+
g.PlatformLastFocusedViewport = focused_viewport->ID;
3597+
}
3598+
}
3599+
35873600
// Create/resize/destroy platform windows to match each active viewport.
35883601
// Skip the main viewport (index 0), which is always fully handled by the application!
35893602
for (int i = 1; i < g.Viewports.Size; i++)
@@ -3656,14 +3669,18 @@ void ImGui::UpdatePlatformWindows()
36563669
g.PlatformIO.Platform_SetWindowAlpha(viewport, viewport->Alpha);
36573670
viewport->LastAlpha = viewport->Alpha;
36583671

3659-
// Show window. On startup ensure platform window don't get focus.
3672+
// Show window. On startup ensure platform window don't get focus
36603673
if (is_new_window)
36613674
{
36623675
if (g.FrameCount < 2)
36633676
viewport->Flags |= ImGuiViewportFlags_NoFocusOnAppearing;
36643677
g.PlatformIO.Platform_ShowWindow(viewport);
36653678
}
36663679

3680+
// Even without focus, we assume the window becomes front-most. This is used by our platform z-order heuristic when io.MouseHoveredViewport is not available.
3681+
if (is_new_window && viewport->LastFrontMostStampCount != g.WindowsFrontMostStampCount)
3682+
viewport->LastFrontMostStampCount = ++g.WindowsFrontMostStampCount;
3683+
36673684
// Clear request flags
36683685
viewport->PlatformRequestClose = viewport->PlatformRequestMove = viewport->PlatformRequestResize = false;
36693686
}

imgui.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ struct ImGuiIO
10731073
float MouseWheel; // Mouse wheel Vertical: 1 unit scrolls about 5 lines text.
10741074
float MouseWheelH; // Mouse wheel Horizontal. Most users don't have a mouse with an horizontal wheel, may not be filled by all back-ends.
10751075
ImGuiID MousePosViewport; // (Optional) When using multiple viewports: viewport from which io.MousePos is based from (when dragging this is generally the captured/focused viewport, even though we can drag outside of it and then it's not hovered anymore). (0 == default viewport)
1076-
ImGuiID MouseHoveredViewport; // (Optional) When using multiple viewports: viewport the OS mouse cursor is hovering. (0 == default viewport) _IGNORING_ viewports with the ImGuiBackendFlags_HasMouseHoveredViewport flag, and _REGARDLESS_ of whether another viewport is focused. Set io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport if you can provide this information.
1076+
ImGuiID MouseHoveredViewport; // (Optional) When using multiple viewports: viewport the OS mouse cursor is hovering _IGNORING_ viewports with the ImGuiViewportFlags_NoInputs flag, and _REGARDLESS_ of whether another viewport is focused. Set io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport if you can provide this info. If you don't imgui will use a decent heuristic instead.
10771077
bool MouseDrawCursor; // Request ImGui to draw a mouse cursor for you (if you are on a platform without a mouse cursor).
10781078
bool KeyCtrl; // Keyboard modifier pressed: Control
10791079
bool KeyShift; // Keyboard modifier pressed: Shift

imgui_internal.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ struct ImGuiViewportP : public ImGuiViewport
511511
{
512512
int Idx;
513513
int LastFrameActive; // Last frame number this viewport was activated by a window
514-
int LastFrameAsRefViewport; // Last frame number this viewport was io.MouseViewportRef
515514
int LastFrameOverlayDrawList;
515+
int LastFrontMostStampCount; // Last stamp number from when a window hosted by this viewport was made front-most (by comparing this value between two viewport we have an implicit viewport z-order
516516
ImGuiID LastNameHash;
517517
ImVec2 LastPos;
518518
float Alpha; // Window opacity (when dragging dockable windows/viewports we make them transparent)
@@ -524,7 +524,7 @@ struct ImGuiViewportP : public ImGuiViewport
524524
ImDrawDataBuilder DrawDataBuilder;
525525
ImVec2 RendererLastSize;
526526

527-
ImGuiViewportP() { Idx = 1; LastFrameActive = LastFrameAsRefViewport = LastFrameOverlayDrawList = -1; LastNameHash = 0; Alpha = LastAlpha = 1.0f; PlatformMonitor = INT_MIN; Window = NULL; OverlayDrawList = NULL; RendererLastSize = ImVec2(-1.0f,-1.0f); }
527+
ImGuiViewportP() { Idx = 1; LastFrameActive = LastFrameOverlayDrawList = LastFrontMostStampCount = -1; LastNameHash = 0; Alpha = LastAlpha = 1.0f; PlatformMonitor = INT_MIN; Window = NULL; OverlayDrawList = NULL; RendererLastSize = ImVec2(-1.0f,-1.0f); }
528528
~ImGuiViewportP() { if (OverlayDrawList) IM_DELETE(OverlayDrawList); }
529529
ImRect GetRect() const { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); }
530530
ImVec2 GetCenter() const{ return ImVec2(Pos.x + Size.x * 0.5f, Pos.y + Size.y * 0.5f); }
@@ -610,6 +610,7 @@ struct ImGuiContext
610610
ImVector<ImGuiWindow*> CurrentWindowStack;
611611
ImGuiStorage WindowsById;
612612
int WindowsActiveCount;
613+
int WindowsFrontMostStampCount; // Every time the front-most window changes, we stamp its viewport with an incrementing counter
613614
ImGuiWindow* CurrentWindow; // Being drawn into
614615
ImGuiWindow* HoveredWindow; // Will catch mouse inputs
615616
ImGuiWindow* HoveredRootWindow; // Will catch mouse inputs (for focus/move only)
@@ -644,6 +645,7 @@ struct ImGuiContext
644645
ImGuiViewportP* MouseRefPrevViewport;
645646
ImGuiViewportP* MouseHoveredLastViewport; // Last viewport that was hovered by mouse (even if we are not hovering any viewport any more)
646647
ImGuiID MouseClickedPosViewportId[5]; // For rarely used fields we only compare to, store viewport ID only so we don't have to clean dangling pointers
648+
ImGuiID PlatformLastFocusedViewport; // Record of last focused platform window/viewport, when this changes we stamp the viewport as front-most
647649

648650
// Navigation data (for gamepad/keyboard)
649651
ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusWindow'
@@ -751,6 +753,7 @@ struct ImGuiContext
751753
FrameCount = 0;
752754
FrameCountEnded = FrameCountPlatformEnded = FrameCountRendered = -1;
753755
WindowsActiveCount = 0;
756+
WindowsFrontMostStampCount = 0;
754757
CurrentWindow = NULL;
755758
HoveredWindow = NULL;
756759
HoveredRootWindow = NULL;
@@ -776,6 +779,7 @@ struct ImGuiContext
776779
MouseRefViewport = NULL;
777780
MouseRefPrevViewport = MouseHoveredLastViewport = NULL;
778781
memset(MouseClickedPosViewportId, 0, sizeof(MouseClickedPosViewportId));
782+
PlatformLastFocusedViewport = 0;
779783

780784
NavWindow = NULL;
781785
NavId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavInputId = 0;

0 commit comments

Comments
 (0)