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

Begin() to append doesn't set LastItemData for title-bar/tab (was BeginItemTooltip may react to wrong widgets?) #7506

Closed
achabense opened this issue Apr 18, 2024 · 2 comments

Comments

@achabense
Copy link

Version/Branch of Dear ImGui:

Version 1.90.5

Back-ends:

imgui_impl_sdl2.cpp + imgui_impl_sdlrenderer2.cpp

Compiler, OS:

Windows 10 + MSVC 2022

Full config/build information:

No response

Details:

When the window is collapsed, BeginItemTooltip may react to the a widget in other windows. (I'm not sure whether I've misused the functions.)

Screenshots/Video:

(The title color is irrelevant here.)
effect

Minimal, Complete and Verifiable Example code:

ImGui::Begin("Test");
{
    ImGui::Button("...", {100, 100});

    if (1) {
        ImGui::Begin("Strange");
        ImGui::End();

        // The same window, intentionally do not early out:
        // When this window is collapsed, `BeginItemTooltip` will react to the "..." button above.
        // (When not collapsed, will react to "123" button normally.)
        ImGui::Begin("Strange");
        ImGui::Button("123");
        if (ImGui::BeginItemTooltip()) {
            ImGui::Text("Hovered");
            ImGui::EndTooltip();
        }
        ImGui::End();
    } else {
        // A single block do not have the same problem.
        ImGui::Begin("Strange");
        ImGui::Button("123");
        if (ImGui::BeginItemTooltip()) {
            ImGui::Text("Hovered");
            ImGui::EndTooltip();
        }
        ImGui::End();
    }
}
ImGui::End();
@ocornut ocornut changed the title BeginItemTooltip may react to wrong widgets? Begin() to append doesn't set LastItemData for title-bar/tab (was BeginItemTooltip may react to wrong widgets?) Apr 18, 2024
ocornut added a commit that referenced this issue Apr 18, 2024
@ocornut
Copy link
Owner

ocornut commented Apr 18, 2024

The problem is caused by the fact that on a Begin() call to append, we don't call this block:

// We fill last item data based on Title Bar/Tab, in order for IsItemHovered() and IsItemActive() to be usable after Begin().
// This is useful to allow creating context menus on title bar only, etc.
SetLastItemData(window->MoveId, g.CurrentItemFlags, IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0, title_bar_rect);

Therefore we inherit last value of g.LastItemData.ID & co.

There is special handling in IsItemHovered() to handle situation where a window is collapsed, leading all widgets (e.g. Button("123") to early out and therefore never write to this identifier.

We could perfectly compute title_bar_rect and copy the whole block in the else/appending case:

// When reusing window again multiple times a frame, just append content (don't need to setup again)
if (first_begin_of_the_frame)
{
    ....
}
else
{
   // Append
   SetCurrentWindow(window);

   ImRect title_bar_rect = window->TitleBarRect();
   SetLastItemData(window->MoveId, g.CurrentItemFlags, IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0, title_bar_rect);
}

That technically also fixes using e.g. IsItemHovered() right after an append-Begin():

ImGui::Begin("Another #7506 issue");
ImGui::End();
ImGui::Begin("Another #7506 issue");
if (ImGui::IsItemHovered())
    ImGui::SetTooltip("Hovering title bar!");
ImGui::End();

This is now fixed as well.

Actual fix goes through a function as the logic is longer in docking.
Pushed fa0120e

@ocornut ocornut closed this as completed Apr 18, 2024
ocornut added a commit to ocornut/imgui_test_engine that referenced this issue Apr 19, 2024
@ocornut
Copy link
Owner

ocornut commented Apr 19, 2024

Added a regression test: ocornut/imgui_test_engine@fc5105f

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