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

Add custom button in window title bar #3882

Open
s-pravin opened this issue Mar 5, 2021 · 1 comment
Open

Add custom button in window title bar #3882

s-pravin opened this issue Mar 5, 2021 · 1 comment
Milestone

Comments

@s-pravin
Copy link

s-pravin commented Mar 5, 2021

Version/Branch of Dear ImGui:

Version: 1.81
Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp
Operating System: Windows 10

My Issue/Question:

I am trying to customize ImGui window title bar by adding a custom button over it. Though, button content gets rendered as per expectation, button click isn't captured. I have set cursor position to the button location before invoking ItemSize, but am not confident if that's the correct way to achieve this.

  • Does anyone know why button click isn't captured in the example below?
  • Any downsides to adding custom button in title bar? I am trying to achieve functionality offered by Pin button in Visual Studio e.g. Solution Explorer/Properties etc.

Standalone, minimal, complete and verifiable example:

        ImGui::SetNextWindowSize(ImVec2(200, 200));
        ImGui::Begin("Demo Window");
        ImGuiWindow* curWindow = ImGui::GetCurrentWindow();
        ImDrawList* drawList = ImGui::GetForegroundDrawList(curWindow);
        ImRect titlePos = curWindow->TitleBarRect();

        ImGuiContext& g = *GImGui;
        const ImGuiID id = curWindow->GetID("#snap");

        const ImRect bb(ImVec2(titlePos.Max.x - 30, titlePos.Max.y - 15), ImVec2(titlePos.Max.x - 10, titlePos.Max.y - 7));
        ImGui::SetCursorScreenPos(bb.Min);
        ImGui::SetCursorPos(bb.Min);
        ImGui::ItemSize(bb);
        ImGui::ItemAdd(bb, id);

        bool hovered, held;
        bool pressed = ImGui::ButtonBehavior(bb, id, &hovered, &held);

        // Render
        const ImU32 col = ImGui::GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);
        ImGui::RenderNavHighlight(bb, id);
        ImGui::RenderFrame(bb.Min, bb.Max, col, true);
        drawList->AddRectFilled(bb.Min, bb.Max, ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 1.0f, 1.0f)));
        drawList->AddTriangleFilled(ImVec2(titlePos.Max.x - 30, titlePos.Max.y - 7), ImVec2(titlePos.Max.x - 10, titlePos.Max.y - 7), ImVec2(titlePos.Max.x - 20, titlePos.Max.y - 15), ImGui::GetColorU32(ImVec4(1.0f, 0.0f, 0.0f, 1.0f)));

        ImGui::SetCursorScreenPos(curWindow->ContentRegionRect.Min);
        ImGui::Text("Test Content");
        if (pressed)
            ImGui::Text("Button pressed");
        ImGui::End();
@ocornut
Copy link
Owner

ocornut commented Mar 5, 2021

There are two things at play here:

  • After Begin() the window has a clip rectangle to cover only the scrollable client area, and interactions are affected by this clipping rectangle. You may need to manually call PushClipRect()/PopClipRect() to temporarily access to the whole area outside the window client area.

  • May or not affect you: by default Hovering tests are performed on a "first come first served" (~back to front) basis, so when two items are overlapping the first one will get the Hover and generally be able to interact. Using SetItemAllowOverlap() we have a (currently ill-defined and bit messy) mechanism to allow "last come first served" (~front to back) hovering tests. There are various treacherous aspect to the current implementation which I won't get into detail right now, but SetItemAllowOverlap() is not always sufficient currently.
    The reason I say this may not affect you is that the Title Bar actually doesn't submit a regular item, and therefore I think may not be affected by this requirement anyway.

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