ocornut / imgui

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

Have a way to handle a event from window title bar? #7369

Closed RicardoDazzling closed 8 months ago

RicardoDazzling commented 8 months ago

Version/Branch of Dear ImGui:

Version 1.90.4, Branch: master

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp

Compiler, OS:

Windows 10

Full config/build information:

Dear ImGui 1.90.5 WIP (19043)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1939
define: _MSVC_LANG=201402
--------------------------------
io.BackendPlatformName: imgui_impl_glfw
io.BackendRendererName: imgui_impl_opengl3
io.ConfigFlags: 0x00000003
 NavEnableKeyboard
 NavEnableGamepad
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00000006
 HasMouseCursors
 HasSetMousePos
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 1280.00,720.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:

My Issue/Question:

I'm new in C++ and ImGui, so I'm not finded a exemple to it:

I need to handle a double click event in the window titlebar. My idea was create a way to the user change the title with double click, so the window title would to be a input. But I know that it can be hard to implement, so if I show a modal asking the user a new window name, solve my problem. But I don't know how to add this event for the window title...

Thank you everyone!

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

ocornut commented 8 months ago

After Begin() you can query title bar (or docking tab) item data, so this would work:

ImGui::Begin("Hello, world!", nullptr, ImGuiWindowFlags_NoCollapse); 
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0))
    printf("hi\n");

You need to disable collapsing otherwise this would conflict with the double-clicking.

I pushed 65dc67f which allows overriding this via the key-ownership mechanism, so if you pull you could use:

ImGui::Begin("Hello, world!");
if (ImGui::IsItemHovered())
{
    ImGuiID my_id = ImGui::GetID("left-click-interceptor");
    ImGui::SetKeyOwner(ImGuiKey_MouseLeft, my_id);
    if (ImGui::IsMouseDoubleClicked(0, my_id))
        printf("hi\n");
}

However, while that answers your question I think both solutions are incorrect as they would likely interact wrongly with docked window.

I think you would better use a context-menu for that:

ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.
if (ImGui::BeginPopupContextItem(nullptr, ImGuiPopupFlags_MouseButtonRight))
{
    if (ImGui::MenuItem("Rename..."))
        printf("hi\n");
    ImGui::EndPopup();
}

This will work with docked window.

Note that if you rename windows you need to use the ### operator to preserve their unique identifier, so their imgui-side names should be "User Visible Name###Internal Name". Otherwise after you rename dear imgui will think it is a different window. See "Demo->Examples->Manipulating Window Titles" for an example.

RicardoDazzling commented 8 months ago

Thank you! It solved my doubt