ocornut / imgui

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

Context menu on docked window tab #7914

Closed azonenberg closed 1 week ago

azonenberg commented 2 weeks ago

Version/Branch of Dear ImGui:

1.90.7 WIP 19063

Back-ends:

vulkan

Compiler, OS:

gcc/Debian

Full config/build information:

No response

Details:

I want to be able to spawn a context menu by right clicking the tab widget of a docked window.

As of now, I'm able to create a context menu on the title bar when a window is not docked, but haven't found any way to detect a right click on the tab widget of the docked window. Is this currently possible?

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

ocornut commented 2 weeks ago

As of now, I'm able to create a context menu on the title bar when a window is not docked, but haven't found any way to detect a right click on the tab widget of the docked window.

How are you doing that?

Because IsItemHovered() - which is the right thing to use - should work after Begin(), docked or not, and therefore helpers such as BeginPopupContextItem() should work as well.

azonenberg commented 2 weeks ago

I'm using ImGui::IsWindowHovered() right after Begin() as you suggested in https://github.com/ocornut/imgui/issues/316#issuecomment-137420317. That was from 2015 so it's possible there's a better way now :)

azonenberg commented 2 weeks ago

If I use ImGui::IsItemHovered(), the popup appears as expected even when the window is docked.

But as soon as I let go of the right mouse button, the popup vanishes. It seems I need to right click then hold the right button down and move the mouse into the popup before releasing for the popup to stay open.

ocornut commented 2 weeks ago

Please provide an explicit repro because this works here:

ImGui::Begin("Hello, world!");
if (ImGui::BeginPopupContextItem())
{
    ImGui::MenuItem("Hello");
    ImGui::EndPopup();
}
ImGui::End();

Docked or not docked.

azonenberg commented 2 weeks ago
    if(!ImGui::Begin(GetID().c_str(), &open, ImGuiWindowFlags_NoScrollWithMouse))
    {
        //tabbed out, don't draw anything until we're back in the foreground
        ImGui::End();
        return true;
    }

    //Check for right click on the title bar
    if(ImGui::IsItemHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Right))
    {
        auto rect = ImGui::GetCurrentWindow()->TitleBarRect();
        if(ImGui::IsMouseHoveringRect(rect.Min, rect.Max, false))
            ImGui::OpenPopup("Rename Group");
    }

    if(ImGui::BeginPopup("Rename Group"))
    {
        ImGui::InputText("Name", &m_title);
        ImGui::EndPopup();
    }

    ImGui::End();
azonenberg commented 2 weeks ago

The IsMouseHoveringRect seems to be redundant if using IsItemHovered() and I removed it with no change to behavior.

ocornut commented 2 weeks ago

It's not standard to open popups on mouse down, because typically the mouse down is used to close a previous popup.

This is unnecessary:

auto rect = ImGui::GetCurrentWindow()->TitleBarRect();
if(ImGui::IsMouseHoveringRect(rect.Min, rect.Max, false))

You can simply use if (ImGui::BeginPopupContextItem()) you don't need anything else.

But I was also puzzled as to why the popup would close. If I look at the log: image

And I think there is an issue in DockNodeUpdateTabBar() which leads your code flow to refocus the window and close the popup. I will investigate it. However if you open popup on mouse up or simply use BeginPopupContextItem() you won't experience that issue.

azonenberg commented 2 weeks ago

Confirmed, using IsMouseReleased() to spawn the popup fixes the immediate problem. Keeping this ticket open until you find and fix the focus bug, you can close once that's finished.

Thanks :)

ocornut commented 2 weeks ago

Again you don't need to call IsMouseReleased() yourself, you only need to do:

ImGui::Begin("Hello, world!");
if (ImGui::BeginPopupContextItem())
{
    ImGui::MenuItem("Hello");
    ImGui::EndPopup();
}
azonenberg commented 2 weeks ago

Even better, works like a charm.

ocornut commented 2 weeks ago

Pushed a fix/workaround for the mouse release issue 8c4dceb but I will need to investigate this a little further.

ocornut commented 1 week ago

Closing this after confirming. Added a test https://github.com/ocornut/imgui_test_engine/commit/030cc99f2bfbd17a70ca012685846d5870495791