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

ImGui::SetWindowFocus does nothing the first frame after a window has been created #5289

Closed
peter1745 opened this issue May 5, 2022 · 4 comments

Comments

@peter1745
Copy link

Version/Branch of Dear ImGui:

Version: 1.87 (18701)
Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_glfw.cpp + imgui_impl_vulkan_with_textures.cpp
Operating System: Windows 11

My Issue/Question:
There seems to be a slight problem with the ImGui::SetWindowFocus function when trying to focus a newly created docked window, where if you call ImGui::SetWindowFocus immediately after the first call to ImGui::Begin (e.g immediately after the window has been created and docked for the first time) the call is ignored.

This essentially means that you as the user of ImGui have to delay the call until after ImGui::Begin has been called for the second time, which is a bit annoying.

I ran into this issue when I was trying to make it so that when a new tab is opened for the first time (in a dockspace) that tab would be focused, and it would be great if ImGui would handle this internally since it can be really confusing when it simply doesn't work.

I will note that this is not an issue when focusing a newly created window that isn't docked immediately after creation.

Again, it's possible to work around this issue pretty easily but it can be confusing when encountering the issue for the first time.

Example Code:

static bool s_RebuildDockspace = true;
static ImGuiID s_DockSpaceID;

{
    ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_MenuBar;
    ImGui::Begin("Dockspace", nullptr, window_flags);

    if (s_RebuildDockspace)
    {
        s_DockSpaceID = ImGui::GetID("TestDockspace");
        ImGui::DockBuilderRemoveNode(s_DockSpaceID);
        ImGuiID rootNode = ImGui::DockBuilderAddNode(s_DockSpaceID, ImGuiDockNodeFlags_DockSpace);
        ImGui::DockBuilderFinish(s_DockSpaceID);
    }

    ImGui::DockSpace(s_DockSpaceID, ImVec2(0.0f, 0.0f), ImGuiDockNodeFlags_NoCloseButton | ImGuiDockNodeFlags_NoWindowMenuButton);

    {
        ImGui::Begin("Panel 0");
        if (s_RebuildDockspace)
            ImGui::DockBuilderDockWindow("Panel 0", s_DockSpaceID);
        ImGui::Text("Hello");
        ImGui::End();
    }

    ImGui::Begin("Panel 1");
    if (s_RebuildDockspace)
        ImGui::DockBuilderDockWindow("Panel 1", s_DockSpaceID);
    ImGui::Text("Hello Again");
    ImGui::End();

    ImGui::End();
}

s_RebuildDockspace = false;

if (s_FirstFrame)
{
    // Will not set focus to Panel 1 unless we've called ImGui::Begin at least 2 times
    ImGui::SetWindowFocus("Panel 1");
    s_FirstFrame = false;
}
@ocornut
Copy link
Owner

ocornut commented May 6, 2022

I ran into this issue when I was trying to make it so that when a new tab is opened for the first time (in a dockspace) that tab would be focused, and it would be great if ImGui would handle this internally since it can be really confusing when it simply doesn't work.

This is planned and desirable (#2304). I had a 90% working solution last year but it had complications and never got around to finish it fully.

@peter1745
Copy link
Author

Thanks for the quick reply! Good to know it will eventually be the default behavior 🙂

@ocornut
Copy link
Owner

ocornut commented May 6, 2022

#5005 is essentially the same problem as well.

Note that reopening multiple docked windows at the same time (same frame) should restore focus, but it's currently a bit of a mess.

@peter1745
Copy link
Author

#5005 is essentially the same problem as well.

Note that reopening multiple docked windows at the same time (same frame) should restore focus, but it's currently a bit of a mess.

Yeah, in that case I'll probably continue delaying the call until after the second time that ImGui::Begin has been called, it seems to work reliably so

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