Open bedhededdy opened 1 year ago
both windows have focus at once.
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.
I have an application where I would like certain keyboard shortcuts to do different things depending on which viewport is active.
The whole premise seems incorrect, I believe you want to route shortcuts based on which Dear ImGui is focused. Here your code is mashing two concepts together, but I do understand the lack of auto-focus-imgui-window-on-platform-focus may lead you to that.
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.
I suggest you look at Shortcut()
in imgui_internal.h which is a thing I committed a few months ago which aims to solve this.
You can find some demo code in this branch https://github.com/ocornut/imgui/commits/features/demo_input_owner_and_routing and particularly in https://github.com/ocornut/imgui/commit/fbc939518a2382cd702b4fd8c2afceb970b72829 but mostly read the comment around the Shortcut()
declaration.
Thanks for the suggestion and the speedy reply! I sincerely appreciate it :)
I believe you want to route shortcuts based on which Dear ImGui is focused.
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).
I suggest you look at Shortcut() in imgui_internal.h which is a thing I committed a few months ago which aims to solve this."
As such, I don't think this solution will work for me, as the host window is not an imgui window.
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.
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 ImGui::Image()
with a render-to-texture? Or is the game diplayed in the underlying screen-buffer before dear imgui renders?
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).
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.
What's in your platform host window exactly? A menu-bar + a decoration-less window showing an ImGui::Image() with a render-to-texture? Or is the game diplayed in the underlying screen-buffer before dear imgui renders?
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!
I have pushed support for this dcb6335b It seemed fairly straightforward but out of experience we will probably hear about this again.
And a test as well as some support in test-engine: https://github.com/ocornut/imgui_test_engine/commit/2b3fb617c557d4559cc403e5f8abe7b27ba34c61
( I also noticed that while clicking on Platform Title Bar always sets focus (and thus makes Dear ImGui sets its focus too), ALT+Tab doesn't always and it depends on whether both windows have a Parent<>Child relation at Platform Level.
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 ImGuiBackendFlags_HasViewportParenting
flag which win32 backend would set (since it is the only one that supports both configuration) we could set a better dynamic default.
)
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 dcb6335bfed5934d6b1e377820e19d3e08be689b.
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 I observe seems to contradict what you observe here.
I also noticed that while clicking on Platform Title Bar always sets focus (and thus makes Dear ImGui sets its focus too)
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.
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 I observe seems to contradict what you observe here.
As the subject matter can be extremely confusing/ambiguous, best to be extra clear and explicit with your wording. Maybe the Glossary can help.
For me, clicking on the menu bar in both cases refocuses the last ImGui viewport,
This is ambiguous so I can't say if that's intended of not.
clicking the center of the window clears ImGui focus,
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).
and clicking the title bar focuses the main window and keeps any previous ImGui viewport's focus.
That seems to be the bug I mentioned in (1) below.
(1) From what I understand, the issue has to do with BeginMainMenuBar()/EndMainMenuBar()
which is itself programmed to relinquish focus to the previously focused window when not active. In this configuration it seems to be undoing what we're doing to do.
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.
I have pushed fixes for (2) main commit 63370be (two other earlier commits with small refactor) + new tests https://github.com/ocornut/imgui_test_engine/commit/0320aee2116c8b275966fa9af987bbca3c0c2d71
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!
From what I understand, the issue has to do with BeginMainMenuBar()/EndMainMenuBar() which is itself programmed to relinquish focus to the previously focused window when not active
I observe this behavior
I have pushed fixes for (2)
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
https://user-images.githubusercontent.com/28909307/229762297-a99e1b03-cca8-4685-b08c-cfc41ae5443d.mp4
Standalone, minimal, complete and verifiable example: