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
- Add an optional
bool* p_open out-param to OpenPopupOnItemClick.
- Introduce
OpenPopupOnWindowClick.
- 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.
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
BeginPopupContextItemandBeginPopupContextWindowtogether 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 insideBeginPopup.Before 1.79 I could write:
This was removed in 1.79 with a good rationale about API consistency.
Proposal
bool* p_openout-param toOpenPopupOnItemClick.OpenPopupOnWindowClick.OpenPopupOnVoidClick.p_openwould be set totrueonly on the frame the popup transitions from closed to open, andfalseotherwise — a one-shot event, consistent with thep_openpattern used elsewhere in the API.Usage:
This keeps full backward compatibility and allows mixing both triggers on the same popup ID without resorting to manual
IsItemHovered+OpenPopupcalls.Happy to submit a PR for this if the proposal looks good to you.