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

How can I define different context menus for items inside a table and outside of a table? #6426

Open
GasimGasimzada opened this issue May 12, 2023 · 3 comments

Comments

@GasimGasimzada
Copy link

GasimGasimzada commented May 12, 2023

Version/Branch of Dear ImGui:

Version: latest
Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: Custom backend
Compiler: All
Operating System: All

My Issue/Question:

I am using ImGui table to create a grid for my asset browser. I am currently using context menu that opens whenever I click anywhere in the window. Now, I want to add a new context menu when I right click specific item in the table. However, the context menu never gets called. I even tried to use a OpenPopup./BeginPopup instead of BeginPopupContextItem but no luck. Here is a snippet that I am trying out:

if (ImGui::BeginTable("CurrentDir", itemsPerRow,
                          ImGuiTableFlags_NoPadInnerX)) {
      size_t i = 0;
      for (; i < mEntries.size(); ++i) {
        auto colIndex = i % itemsPerRow;
        if (colIndex == 0) {
          ImGui::TableNextRow(ImGuiTableRowFlags_None, ItemHeight * 1.0f);
        }
        ImGui::TableNextColumn();

        const auto &entry = mEntries.at(i);
        if (ImGui::Selectable(id.c_str(), mSelected == i,
                              ImGuiSelectableFlags_AllowDoubleClick,
                              ImVec2(ItemWidth, ItemHeight))) {
             // making my item clickable using Selectable
        }
        if (ImGui::IsItemHovered())  {
           // hover logic here
        }

        // Open popup when right button is clicked
        if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
          // Note that I reach this condition when right clicking
          // (checked it with debugger to make sure)
          ImGui::OpenPopup("AssetBrowserEntryPopup");
        }

        // Open my popup
        if (ImGui::BeginPopup("AssetBrowserEntryPopup")) {
          if (ImGui::MenuItem("View contents")) {}
          ImGui::EndPopup();
        }
      }

    ImGui::EndTable();
}

// Popup menu for non selected items
if (!ImGui::isPopupOpen("AssetBrowserEntryPopup") && ImGui::BeginPopupContextWindow("AssetBrowserPopup")) {
      if (ImGui::MenuItem("Import asset")) {}
  ImGui::EndPopup();
}

When hold right click, I see the item entry popup; however, the moment I release the click, context menu overwrites it. Here is a GIF that demonstrated what I mean (ignore the duplicate menu item entries; I'll fix it with IDs but need to get this working first):

Animation23

What am I doing wrong here? Why does BeginPopupContextWindow overwrite my currently active popup, when I am explicitly calling it only when isPopupOpen is false?

@ocornut
Copy link
Owner

ocornut commented May 13, 2023

Popup identifiers are relative to ID stack (see #331) so “ AssetBrowserEntryPopup” doesn’t refer from the same thing in both locations. Use in ImGuiID and equivalent functions from imgui_internal.h, or handle your popups from the same location.

@ocornut
Copy link
Owner

ocornut commented May 13, 2023

And you should probably also call PushID() inside your loop to make the popup id of each item unique. Using BeginPopupContextItem() i recall use last item ID so it’s not a problem with it.

@GasimGasimzada
Copy link
Author

After playing around with it a bit more, I fixed it by providing flags to BeginPopupContextWindow:

ImGui::BeginPopupContextWindow("AssetBrowserPopup",
  ImGuiPopupFlags_NoOpenOverItems |
  ImGuiPopupFlags_MouseButtonRight |
  ImGuiPopupFlags_NoOpenOverExistingPopup
);

Popup identifiers are relative to ID stack (see #331) so “ AssetBrowserEntryPopup” doesn’t refer from the same thing in both locations. Use in ImGuiID and equivalent functions from imgui_internal.h, or handle your popups from the same location.

I am using them from the same point right? Since the last item in my id stack is the ImGui::Selectable and I am calling the popup functions right after that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants