Skip to content

Difficulty in emulating regular buttons accurately using InVisibleButton + hover/active test #8877

Closed

Description

@achabense

Version/Branch of Dear ImGui:

Version 1.91.8

Back-ends:

Any (imgui_impl_sdl2.cpp + imgui_impl_sdlrenderer2.cpp)

Compiler, OS:

Any

Full config/build information:

No response

Details:

I find it hard to emulate regular buttons accurately using ImGui::InVisibleButton + ImGui::IsItemHovered.

The main problem is that, when the window is immovable and the user drag from background (of the same window) to button, regular buttons will not light up, but ImGui::IsItemHovered will return true in this case (will still be false it dragged from another immovable window). I find it very tricky to emulate regular button behavior in this case (see is_button_hovered).

  1. Is this difference intentional design? (Should ImGui::IsItemHovered return true when dragged from background of the same window?)
  2. If it's intentional, can you provide a way to emulate accurate behavior more easily? (It can be an overload of ImGui::InVisibleButton like below, or an additional ImGuiHoveredFlags for ImGui::IsItemHovered.)
// render_state <- ImGuiCol_ButtonActive/ImGuiCol_ButtonHovered/ImGuiCol_Button
bool InvisibleButton(const char* str_id, const ImVec2& size, ImGuiButtonFlags flags, ImGuiCol& render_state);

Screenshots/Video:

(Using ImGui::IsItemHovered alone for hover test.)
Image

Minimal, Complete and Verifiable Example code:

static bool is_button_hovered(bool accurate) {
    if (!accurate) {
        return ImGui::IsItemHovered();
    } else {
        // Trying to emulate the same condition for regular buttons to light up.
        return ImGui::IsItemHovered() &&
               (/*1:*/ (ImGui::IsItemActive() || !ImGui::IsAnyItemActive()) || /*2:*/ ImGui::GetIO().WantTextInput);
        // 1: when users drag from background (of the same window) to the button, regular buttons will not light up, but ImGui::IsItemHovered will return true.
        // 2: however, 1 will disable the hover when an input field is active (but not held), thus the exemption.
    }
}

static void issue() {
    static bool movable = true;
    static bool accurate = false;

    ImGui::SetNextWindowSize({0, 200});
    const ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoResize |
                                          (movable ? ImGuiWindowFlags_None : ImGuiWindowFlags_NoMove);
    if (ImGui::Begin("Example", 0, window_flags)) {
        ImGui::TextUnformatted("1. Turn off 'movable', and drag from background to both buttons.\n"
                               "2. Activate the input filed but not hold it, and hover on both buttons.");
        ImGui::Button("Regular button");

        // Trying to implement custom buttons using `ImGui::InVisibleButton()`.
        // Since `hovered` and `held` (gotten by `ImGui::ButtonBehavior()`) are discarded inside the function, use publicly available functions.
        ImGui::InvisibleButton("Invisible", ImGui::GetItemRectSize());
        {
            const ImGuiCol col = ImGui::IsItemActive() && ImGui::IsItemHovered() ? ImGuiCol_ButtonActive
                                 : is_button_hovered(accurate)                   ? ImGuiCol_ButtonHovered
                                                                                 : ImGuiCol_Button;
            ImGui::GetWindowDrawList()->AddRectFilled(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(),
                                                      ImGui::GetColorU32(col));
        }

        ImGui::SetNextItemWidth(ImGui::GetItemRectSize().x);
        char buf[100]{};
        ImGui::InputText("InputText", buf, 10);
        ImGui::Checkbox("Movable", &movable);
        ImGui::Checkbox("Accurate emulation", &accurate);
    }
    ImGui::End();
}

Activity

  1. achabense commented on Aug 11, 2025

    @achabense
    ContributorAuthor

    As to 1, I find the background id is intentionally checked in IsItemHovered, what does this exemption do? (Sorry but both github and my ide cannot git-blame on imgui.cpp)

    imgui/imgui.cpp

    Lines 4700 to 4702 in 5011559

    if (g.ActiveId != 0 && g.ActiveId != id && !g.ActiveIdAllowOverlap)
    if (g.ActiveId != window->MoveId)
    return false;

    So at least, it seems an accurate emulation can be simplified to

    static bool is_button_hovered(bool accurate) {
            return ImGui::IsItemHovered() && (ImGui::GetActiveID() != ImGui::GetCurrentWindowRead()->MoveId);
    }
  2. ocornut commented on Aug 11, 2025

    @ocornut
    Owner

    As to 1, I find the background id is intentionally checked in IsItemHovered
    if (g.ActiveId != window->MoveId)

    This seems like a bug to me.
    For reference, the initial logic was added in pre-jurassic era (7d26e85) but most interestingly the matching TabID logic in docking branch was added later (8eb8689).

    If I comment that check and run the imgui_test_suite very surprisingly I get no failing tests, which is quite encouraging. I'm going to investigate this further...

    Sorry but both github and my ide cannot git-blame on imgui.cpp)

    What's the problem?

  3. ocornut commented on Aug 11, 2025

    @ocornut
    Owner

    For reference, the initial logic was added in pre-jurassic era (7d26e85)

    I missed that there was a comment (essentially added in that older commit), which now says:

    // This is roughly matching the behavior of internal-facing ItemHoverable()
    // - we allow hovering to be true when ActiveId==window->MoveID, so that clicking on non-interactive items such as a Text() item still returns true with IsItemHovered()

    Honestly it doesn't make sense to me, but it also puzzle me a little that this went undetected for so long.
    (Presumably because it is only really noticeable nowadays when clicking on the background of a _NoMove window).

    I am aggressively pushing that change in 5a6fa33, with good changelog visibility + bumping version number.
    There is a possibility that some old code may be accidentally relying on this, but it seems unlikely and I am not too worried as it won't be too hard to diagnose and people can use ImGuiHoveredFlags_AllowWhenBlockedByActiveItem to bypass it and re-emulate the behavior.

    Thanks a lot for the good repro and explanation by the way.!!

  4. achabense commented on Aug 11, 2025

    @achabense
    ContributorAuthor

    Thanks for the quick fix!

    (As to the git-blame problem, GitHub says "Your blame took too long to complete.", and my IDE (visual studio) also becomes unresponsive for a long time due to large git history. This isn't a problem with the library.)

    Image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址