ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
59.11k stars 10.09k forks source link

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

Open alektron opened 4 months ago

alektron commented 4 months ago

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 commented 4 months ago

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

What basically happens is: