Skip to content

OpenPopupOnItemClick: add optional bool* p_open #9429

@Glordim

Description

@Glordim

Version/Branch of Dear ImGui:

Version 1.9X, Branch: master

Details:

Context

I have a context menu popup that must open both when clicking on a list item and when clicking on the empty area of the container. Because of this dual trigger, I cannot use BeginPopupContextItem and BeginPopupContextWindow together on the same popup ID.

Problem

For the item-click case I would naturally use OpenPopupOnItemClick, but I also need to capture which item was clicked so I can prepare the context (e.g. _contextItem = item) at the moment the popup opens — not every frame inside BeginPopup.

Before 1.79 I could write:

if (ImGui::OpenPopupOnItemClick("##ctx"))
    _contextItem = item;

This was removed in 1.79 with a good rationale about API consistency.

Proposal

  1. Add an optional bool* p_open out-param to OpenPopupOnItemClick.
  2. Introduce OpenPopupOnWindowClick.
  3. Introduce OpenPopupOnVoidClick.
IMGUI_API void OpenPopupOnItemClick(const char* str_id = NULL,
                                    ImGuiPopupFlags popup_flags = 1,
                                    bool* p_open= NULL);

IMGUI_API void OpenPopupOnWindowClick(const char* str_id = NULL,
                                      ImGuiPopupFlags popup_flags = 1,
                                      bool* p_open= NULL);

IMGUI_API void OpenPopupOnVoidClick(const char* str_id = NULL,
                                    ImGuiPopupFlags popup_flags = 1,
                                    bool* p_open= NULL);

p_open would be set to true only on the frame the popup transitions from closed to open, and false otherwise — a one-shot event, consistent with the p_open pattern used elsewhere in the API.

Usage:

for (const auto& item : items)
{
    ImGui::Selectable(item.name);

    bool open = false;
    ImGui::OpenPopupOnItemClick("##ctx", ImGuiPopupFlags_MouseButtonRight, &open);
    if (open)
        _contextItem = &item;
}

bool open = false;
ImGui::OpenPopupOnWindowClick("##ctx", ImGuiPopupFlags_MouseButtonRight, &open);
if (open)
    _contextItem = nullptr;

if (ImGui::BeginPopup("##ctx"))
{
    // ...
    ImGui::EndPopup();
}

This keeps full backward compatibility and allows mixing both triggers on the same popup ID without resorting to manual IsItemHovered + OpenPopup calls.

Happy to submit a PR for this if the proposal looks good to you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions