imgui icon indicating copy to clipboard operation
imgui copied to clipboard

ImGui::SetWindowFocus() doesn't set OS-level focus on viewport

Open cribalik opened this issue 6 years ago • 11 comments

Version/Branch of Dear ImGui:

Version: 171WIP (17003) (commit 596d81a973237c17008a4b64c72fa08bd380b79e) Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: custom Compiler: Visual Studio 2017 Operating System: Windows

My Issue/Question:

If the user calls ImGui::SetWindowFocus(), the intended behavior is (i assume?):

  • If the viewport is not in focus (on OS-level), make it focused by calling Platform_SetWindowFocus().

Currently though, Platform_SetWindowFocus() is only called from inside NavUpdateWindowing().

I am not very familiar with the internals, but moving the call to inside ImGui::FocusWindow() instead seems to fix the issue.

Standalone, minimal, complete and verifiable example:

ImGui::Begin("Example Bug");
ImGui::SetWindowFocus();
// The viewport containing the window doesn't get OS level focus; 
// PlatformIO.Platform_SetWindowFocus() is never called

cribalik avatar Jun 03 '19 12:06 cribalik

Currently we're running with the temporary fix of moving the Platform_SetWindowFocus() call inside ImGui::FocusWindow().

I'll get a PR up today or tomorrow after we've battle-tested it a bit :)

cribalik avatar Jun 04 '19 03:06 cribalik

We've seen the same issue right now. Is there any plans to apply this PR on docking branch?

bsviglo avatar Oct 28 '19 07:10 bsviglo

If memory serves there were a few edge cases that this fix didn't solve, so wasn't confident in putting up a PR. If ocornut is happy with the fix as it is though, I'm okay with that.

cribalik avatar Oct 28 '19 09:10 cribalik

Has there been any further movement on this issue. We just hit the same problem, where we want to bring to the front an already existing window (We're using the Docking branch), and the lack of OS callback leaves that window potentially hidden behind another. I added

if (g.PlatformIO.Platform_SetWindowFocus)
    g.PlatformIO.Platform_SetWindowFocus(display_front_window->Viewport);

At the bottom of ImGui::FocusWindow() and it resolves the issue, but it would be better to have an official solution.

ldsPatrick avatar Mar 31 '21 23:03 ldsPatrick

My solution for this was to alter BringWindowToDisplayFront, as this respects the ImGuiWindowFlags_NoBringToFrontOnFocus flag:

https://github.com/dougbinks/imgui/commit/b97e4e3c82ac56e13944acce9a8c57b391620ff7

dougbinks avatar Apr 14 '21 11:04 dougbinks

I've pushed a fix to the above as the viewport needs to be checked for NULL (edit:) and that PlatformWindowCreated is true.

    if (g.PlatformIO.Platform_SetWindowFocus && window->Viewport && window->Viewport->PlatformWindowCreated )
        g.PlatformIO.Platform_SetWindowFocus(window->Viewport);

dougbinks avatar Apr 14 '21 11:04 dougbinks

I recently found that my fix above doesn't always work as the early out in BringWindowToDisplayFront was bypassing the Platform_SetWindowFocus if the window was the current ImGui front window. Fixed in: https://github.com/dougbinks/imgui/commit/e24c3978ef334b1708a821aef49fb657f2126e37

dougbinks avatar Jun 30 '21 15:06 dougbinks

I've recently found that my implementation is the cause of the the mouse focus being lost when a window is pulled outside the main window, so a solution is to only set platform focus when explicitly called by the user through SetWindowFocus or SetNextWindowFocus.

I've implemented that in https://github.com/dougbinks/imgui/commit/7e03bd4fd9ca6f63848b9f5f84accd1a9a390c83 and this seems pretty smooth now, so I'll consider submitting this as a PR once I've done some more testing.

dougbinks avatar Sep 16 '21 14:09 dougbinks

Bump, once again I ran into the same issue. Any news on the PR ?

matrefeytontias avatar Jun 29 '22 21:06 matrefeytontias

@matrefeytontias does my branch docking_display_front or the commit https://github.com/dougbinks/imgui/commit/7e03bd4fd9ca6f63848b9f5f84accd1a9a390c83 resolve your problems? If so I will look into submitting this as a PR.

dougbinks avatar Aug 20 '22 14:08 dougbinks

Bumping this one in the context of this project : https://github.com/cfillion/reaimgui , as use cases may happen more frequently when ImGui is embedded within another already windowed software.

BenTalagan avatar Mar 27 '25 10:03 BenTalagan