New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple Viewports Mode Allows Multiple Windows to Have Focus #6299
Comments
What's happening is that one has Platform Focus and the other has Dear ImGui-side Focus, which are different things. I guess part of your issue is that you'd want Platform Focus of your main host window to focus the underlying Dear ImGui window, which is something we are not doing yet. As that host window can contain multiple Dear ImGui windows it would involve finding the most recently focused one and focusing that one.
The whole premise seems incorrect, I believe you want to route shortcuts based on which Dear ImGui is focused.
I suggest you look at You can find some demo code in this branch https://github.com/ocornut/imgui/commits/features/demo_input_owner_and_routing and particularly in fbc9395 but mostly read the comment around the |
Thanks for the suggestion and the speedy reply! I sincerely appreciate it :)
This is incorrect, although I should have been more clear in explaining my actual use case as opposed to the minimal example. In my actual use case, the host window is also allowed to have shortcuts. And since it's a game, it will also process some inputs that are not shortcuts (i.e. WASD for movement).
As such, I don't think this solution will work for me, as the host window is not an imgui window.
That's not actually my issue with the behavior. My issue is that clicking the center of the host window will relinquish the imgui viewport's focus (desired behavior), but clicking the title bar or main menu bar of the host window will not relinquish the imgui viewport's focus (undesired behavior). My issue is the inconsistency in the behavior based on where I click on the window. Clicking on the host window should either clear the viewport's focus or not; it seems quite bizarre that clicking the title bar would not do this, but clicking elsewhere on the window does. |
What's in your platform host window exactly? A menu-bar + a decoration-less window showing an
That seems to be the issue I'm referring to in my second paragraph. Clicking the platform window decoration has no effect on imgui logic presently. It should be fixable. It may not be trivial because we need to distinguish intentional platform window focus from the one happening when e.g. creating a new one. Another manifestation of that same issue is, e.g.
|
The game is displayed in the underlying screen-buffer before dear imgui renders. The menu-bar is then drawn on top of that when dear imgui renders. I think for now I can just work around the issue by doing a check for if any viewport has focus. If there is a viewport with focus, I will just assume the input is for the viewport instead of the host window even if both of them have focus. It's not ideal, but it should be fine until you're able to fix the issue. Thanks again for the help! |
…d "viewport_platform_focus" test. ocornut/imgui#6299
I have pushed support for this dcb6335 And a test as well as some support in test-engine: (
For avoidance of doubt, it is better and more consistent if secondary viewports are not child of main viewport, as that relationship enforce z-ordering at platform level, at least on Windows. I think that adding a I'd appreciate if you can update and confirm that this fixes things for you. |
I see no change in behavior with regards to clicking on the window decorations in the minimum example I posted between the original version of Dear ImGui I was using and commit dcb6335. For me, clicking on the menu bar in both cases refocuses the last ImGui viewport, clicking the center of the window clears ImGui focus, and clicking the title bar focuses the main window and keeps any previous ImGui viewport's focus.
What is the expected behavior in the above scenarios I listed? And where should differences in behavior be expected between the old and new version? I think maybe I am not fully understanding what you think the proper behavior should be. I do now understand why the behavior exists in the way that it does, though. If the user is not using multiple viewports mode, it makes sense that merely clicking the title bar should not clear their ImGui focus. My original argument was that if a user is using multiple viewports mode, where each viewport is a platform window, it should exhibit the same behavior as normal desktop windows, where clicking anywhere on Window A will clear the focus of Window B. I understand if this is not a change you want to make, as it would lead to a discrepancy in behavior between multiple viewports mode and normal mode. I also understand that it would be tricky (or maybe impossible) to handle a scenario where some of the viewports are within the main window (where they are treated as normal viewports to my understanding) and some are outside the main window (where they are treated as platform windows to my understanding). Therefore, without major changes to the way that things work, at least the way I understand things, the anticipated behavior should be that clicking the center of the window should clear ImGui focus, and clicking the menu bar or the title bar should refocus the last ImGui viewport. However, on the new commit, I do not get an ImGui refocus when clicking the title bar. |
As the subject matter can be extremely confusing/ambiguous, best to be extra clear and explicit with your wording.
This is ambiguous so I can't say if that's intended of not.
Assuming clicking the void/black in the center of the main viewport: yes it does on only on second click which is a new bug (see 2 below).
That seems to be the bug I mentioned in (1) below. (1) From what I understand, the issue has to do with If you change things to e.g. ImGui::Begin("test", NULL, ImGuiWindowFlags_MenuBar);
if (ImGui::BeginMenuBar()) {
if (ImGui::BeginMenu("Foo")) {
if (ImGui::MenuItem("Open win"))
show_win = true;
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
ImGui::End(); You'll observe different behavior. (2) There's also an issue with my new code where if "Conf" is focused (both Platform and ImGui focus) and you click in the void/black section of the main viewport, my newly added code will focus whatever was last-focused in that viewport. Specificially when clicking on "void" it should unfocus imgui window. It works on the second click. |
…n was actionned from a click within imgui boundaries and (2) restore a null focus as well. (#6299)
I have pushed fixes for (2) main commit 63370be (two other earlier commits with small refactor) + new tests ocornut/imgui_test_engine@0320aee AFAIK things work better except in the case of clicking inside the main menu bar which is still a specific thing as it auto-relinquish focus. That logic exists intentionally so that e.g. with keyboard input you can ctrl+tab to main menu bar, activate an item and return when you are. As a result clicking the menu-menu bar tends to never visibly steal focus from another place at the moment. Could you evaluate with this update and in light of understanding that main menu bar does that? Thank you! |
I observe this behavior
Yup, clicking in the "void" space properly unfocuses the imgui window on the first click. I am now also observing that clicking on the platform decoration correctly clears the imgui window focus when the imgui window is being treated as a platform window. It does not clear the imgui window focus when the imgui window is not a platform window, which again is the anticipated behavior. Everything is working as you say it should now. I do observe the bug behavior for (1), but as you say, that is what I should be experiencing as of now. Let me know when you solve (1) so I can test that out as well. Thanks for working so diligently on this! |
Version/Branch of Dear ImGui:
Version: 1.89.4
Branch: docking
Back-end/Renderer/Compiler/OS
Back-ends: imgui_impl_sdl2.cpp + imgui_impl_opengl3.cpp
Compiler: Clang 15.0.1
Operating System: Windows 11
My Issue/Question:
I have an application where I would like certain keyboard shortcuts to do different things depending on which viewport is active. However, there is a scenario that arises where if I have a viewport spawned from the main OS window that has focus, and I either click the title bar or the main menu bar of the OS window, both windows have focus at once. If I click anywhere in the OS window besides these regions, the ImGui viewport correctly loses focus. Obviously this is an issue if I have a scenario where two windows share the same keyboard shortcut, as the code for both of them will execute.
Screenshots/Video
imgui_bug.mp4
Standalone, minimal, complete and verifiable example:
The text was updated successfully, but these errors were encountered: