Skip to content

Switching between control type using the same label hits assertion #6304

@afaikafk

Description

@afaikafk

Version/Branch of Dear ImGui:

Version: 1.89.4
Branch: master

Back-end/Renderer/Compiler/OS

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

My Issue/Question:

When rendering either an int input or a combo control based on the value of a variable (and the controls use the same label, since only one of them will be rendered at any time based on the value), when switching back from the int input to combo, I hit an assert as seen in following screenshot.
Picking the value 2 in the combobox will switch to the int input control, and entering 0 into the int control will trigger the assert.

Screenshot:

image

Standalone, minimal, complete and verifiable example:

        static int itemIndex = 0;
        static int limit = 1;
        static const int itemCount = 3;
        static const char* items[] = {"0", "1", "2"};
        if (itemIndex < limit) {
            if (ImGui::BeginCombo("SharedControlName", items[itemIndex])) {
                for (int i = {}; i < itemCount; ++i) {
                    const bool isSelected = (itemIndex == i);
                    if (ImGui::Selectable(items[i], isSelected)) {
                        itemIndex = i;
                    }
                    if (isSelected) {
                        ImGui::SetItemDefaultFocus();
                    }
                }
                ImGui::EndCombo();
            }
        } else {
            ImGui::InputInt("SharedControlName", &itemIndex, 1, 100, ImGuiInputTextFlags_CharsNoBlank);
        }

Workaround #1 for problem:

Same snippet, only labels are unique:

        static int itemIndex = 0;
        static int limit = 1;
        static const int itemCount = 3;
        static const char* items[] = {"0", "1", "2"};
        if (itemIndex < limit) {
            if (ImGui::BeginCombo("NotSharedControlName1", items[itemIndex])) {
                for (int i = {}; i < itemCount; ++i) {
                    const bool isSelected = (itemIndex == i);
                    if (ImGui::Selectable(items[i], isSelected)) {
                        itemIndex = i;
                    }
                    if (isSelected) {
                        ImGui::SetItemDefaultFocus();
                    }
                }
                ImGui::EndCombo();
            }
        } else {
            ImGui::InputInt("NotSharedControlName2", &itemIndex, 1, 100, ImGuiInputTextFlags_CharsNoBlank);
        }

Hackaround #2 for problem:

Ugly tampering with imgui internals to simulate a mouse button down (in else branch of top-level if).

#include <imgui_internal.h>

...

        static int itemIndex = 0;
        static int limit = 1;
        static const int itemCount = 3;
        static const char* items[] = {"0", "1", "2"};
        if (itemIndex < limit) {
            if (ImGui::BeginCombo("SharedControlName", items[itemIndex])) {
                for (int i = {}; i < itemCount; ++i) {
                    const bool isSelected = (itemIndex == i);
                    if (ImGui::Selectable(items[i], isSelected)) {
                        itemIndex = i;
                    }
                    if (isSelected) {
                        ImGui::SetItemDefaultFocus();
                    }
                }
                ImGui::EndCombo();
            }
        } else {
            if (ImGui::InputInt("SharedControlName", &itemIndex, 1, 100, ImGuiInputTextFlags_CharsNoBlank)) {
                ImGuiContext* context = ImGui::GetCurrentContext();
                context->ActiveIdMouseButton = ImGuiButtonFlags_MouseButtonLeft;
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions