Skip to content
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

SetWindowFocus and Open/BeginPopup on right click breaks IsItemDeactivated #7509

Open
alektron opened this issue Apr 19, 2024 · 1 comment
Open

Comments

@alektron
Copy link

Version/Branch of Dear ImGui:

Version 1.90.6 (19052), Branch: docking (master/docking/etc.)

Back-ends:

imgui_impl_win32.cpp + imgui_impl_opengl3.cpp

Compiler, OS:

Visual Studio 2022 MSVC C++ 20

Full config/build information:

Dear ImGui 1.90.6 WIP (19052)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1938
define: _MSVC_LANG=202002
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_opengl3
io.ConfigFlags: 0x00000443
 NavEnableKeyboard
 NavEnableGamepad
 DockingEnable
 ViewportsEnable
io.ConfigViewportsNoDecoration
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00001C0E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
 RendererHasViewports
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 1264.00,761.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

Opening a popup when clicking in a window will not trigger IsItemDeactivated on a widget that was active in another window.
The same thing happens when calling SetWindowFocus instead of opening a popup.
Interesting to note is, that this only happens when checking for IsMouseClicked(ImGuiMouseButton_Right) but NOT for ImGuiMouseButton_Left.

{ //Note the comment at the bottom about window order
    ImGui::Begin("Main");

    static int cnt = 0;
    ImGui::Text("Deactivated count: %d", cnt);

    //Always make this textbox active before clicking into one of the test windows below.
    //When clicking the windows, the textbox becomes inactive and 'Deactivated count' should increment.
    //For the two 'BAD' windows, the textbox becomes inactive but the count does not increment
    static char buf[256] = { 0 };
    ImGui::InputText("MyText", buf, sizeof(buf));
    if (ImGui::IsItemDeactivated()) {
        cnt++;
    }
    ImGui::End();
}

bool openPopup = false;
ImGui::Begin("Left_Popup_OK"); //OK
if (ImGui::IsWindowHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Left))
    openPopup = true;
ImGui::End();

ImGui::Begin("Left_Focus_OK"); //OK
if (ImGui::IsWindowHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Left))
    ImGui::SetWindowFocus();
ImGui::End();

ImGui::Begin("Right_Popup_BAD"); //BAD
if (ImGui::IsWindowHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Right))
    openPopup = true;
ImGui::End();

ImGui::Begin("Right_Focus_BAD"); //BAD
if (ImGui::IsWindowHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Right))
    ImGui::SetWindowFocus();
ImGui::End();

//Open/Begin popup here or directly inside the windows, makes no difference
if (openPopup)
    ImGui::OpenPopup("MyPopup");
if (ImGui::BeginPopup("MyPopup")) {
    ImGui::Text("Popup");
    ImGui::EndPopup();
}

//Moving the code for window 'Main' down here resolves the issue.
//Not always an option or hard to keep track of and ensure in complex applications

What's curious to me is the fact that it seems like ImGui does indeed handle windows stealing focus correctly, just not on 'right' mouse clicks. But why would the mouse button matter?
Also note that this only happens when the test window's code comes AFTER the window the contains the widget in question.
Reordering the windows is not always an option and one could easily imagine a scenario where two windows both contain IsItemDeactivated widgets as well as context menus.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

@GamingMinds-DanielC
Copy link
Contributor

Known bug, see f.e. #6766 or #5904.

What basically happens is:

  • item is active
  • another item later in the frame steals the focus
  • when the new frame begins, the other item is memorized as the active item from the last frame
  • ImGui forgot that the first item was active last frame, therefore doesn't tell you about deactivation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants