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

Multiple stacked dock windows seems to cause incorrect window focus on start up #2960

Closed
JackMcCallum opened this issue Jan 2, 2020 · 4 comments

Comments

@JackMcCallum
Copy link

Dear ImGui 1.74 (17400)
--------------------------------
sizeof(size_t): 4, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _MSC_VER=1923
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx11
io.ConfigFlags: 0x0000C441
 NavEnableKeyboard
 DockingEnable
 ViewportsEnable
 DpiEnableScaleViewports
 DpiEnableScaleFonts
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigWindowsMemoryCompactTimer = 60.0f
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

There is a bug when you have a certain docking setup, when running the application for the first time imgui is in a semi-stuck position where you cant open menus until you fiddle with the tabs a bit

Code to repro the bug

void ReproBuggyWindows()
{
    char buffer[256];

    for (int i = 0; i < 3; i++)
    {
        ImGuiWindowClass mainWinClass;
        mainWinClass.DockingAlwaysTabBar = true;
        mainWinClass.DockingAllowUnclassed = true;
        mainWinClass.ClassId = ImGui::GetID((void*)("DOCUMENT_DOCKING"));

        ImGuiWindowClass subWinClass;
        subWinClass.DockingAlwaysTabBar = true;
        subWinClass.DockingAllowUnclassed = false;
        subWinClass.ClassId = ImGui::GetID((void*)("CUSTOM_SUB_WINDOW_CLASS" + i));

        { // Setup dock space and menu bar
            ImGui::SetNextWindowClass(&mainWinClass);
            
            snprintf(buffer, sizeof(buffer), "TEST_WINDOW_%i", i);
            int windowFlags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoCollapse;
            ImGui::Begin(buffer, nullptr, windowFlags);

            int dockspaceFlags = 0;
            ImGui::DockSpace(ImGui::GetID("##DOCK_SPACE"), ImVec2(0, 0), dockspaceFlags, &subWinClass);

            ImGui::BeginMenuBar();
            ImGui::MenuItem("TEST");
            ImGui::EndMenuBar();

            // End the dockspace window
            ImGui::End();
        }

        snprintf(buffer, sizeof(buffer), "TEST_WINDOW_%i_A", i);
        ImGui::SetNextWindowClass(&subWinClass);
        if (ImGui::Begin(buffer, nullptr, 0))
        {

        }
        ImGui::End();

        snprintf(buffer, sizeof(buffer), "TEST_WINDOW_%i_B", i);
        ImGui::SetNextWindowClass(&subWinClass);
        if (ImGui::Begin(buffer, nullptr, 0))
        {

        }
        ImGui::End();
    }
}

Call code after new frame
image

Ini file with docking setup for windows
imgui.txt
You will need to save this as a .ini, i had to change it to .txt to upload it

Should look like this:
image

Notice that only 1 window will ever focus properly.
Notice that you cannot open any of the menu bar menus

When you fiddle with the tabs on the left most window, things begin working again but this bug will repro on next start up.

Seems like in this state ImGui::FocusWindow is being called every frame.

Hope this is enough information

@ocornut
Copy link
Owner

ocornut commented Jan 2, 2020

Thank you Jack, this is a perfect report and I can repro the issue. Will look into it when possible.

@ocornut
Copy link
Owner

ocornut commented Jan 2, 2020

Simplified example, since the WindowClass stuff doesn't relate to the bug.

Unrelated comments about your repro:
(

  1. "CUSTOM_SUB_WINDOW_CLASS" + i would lead to varying class id because the literal string will move accross rebuild, but I assume your own code used another string class?
  2. ImGui::GetID() uses the stack id, and by default we have the debug window in the stack. So your class ids will include "Debug##Default" in the hash, not an immediate problem but could be misleading. I just realized we have no way around this, I would use ImHashStr() from imgui_internal.h but that's not exposed in public API. I am adding a note in my todo list.

)

void ReproBuggyWindows()
{
    char buffer[256];

    for (int i = 1; i < 3; i++)
    {
        { 
            // Setup dock space and menu bar
            snprintf(buffer, sizeof(buffer), "TEST_WINDOW_%i", i);
            ImGui::Begin(buffer, nullptr, ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoCollapse);

            ImGui::DockSpace(ImGui::GetID("##DOCK_SPACE"));

            ImGui::BeginMenuBar();
            if (ImGui::BeginMenu("TEST"))
            {
                ImGui::MenuItem("Test");
                ImGui::EndMenu();
            }
            ImGui::EndMenuBar();

            // End the dockspace window
            ImGui::End();
        }

        snprintf(buffer, sizeof(buffer), "TEST_WINDOW_%i_A", i);
        if (ImGui::Begin(buffer, nullptr, 0))
        {
            ImGui::TextUnformatted(buffer);
        }
        ImGui::End();

        snprintf(buffer, sizeof(buffer), "TEST_WINDOW_%i_B", i);
        if (ImGui::Begin(buffer, nullptr, 0))
        {
            ImGui::TextUnformatted(buffer);
        }
        ImGui::End();
    }
}

ocornut added a commit that referenced this issue Jan 2, 2020
@ocornut
Copy link
Owner

ocornut commented Jan 2, 2020

Hello Jack,
I have pushed a fix for this issue now.

I don't think there will be side-effects to the change but Docking system being so complex, if you suspect anything let me know! Thanks!)

@ocornut ocornut closed this as completed Jan 2, 2020
@JackMcCallum
Copy link
Author

Thanks for pointing out the ID issues, i was using my own string class but it would work the same.
And thanks for the fix and the fast response time, ill look into it when i can.

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

No branches or pull requests

2 participants