-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
TreeNodes with context menus #861
Comments
This is a good question. So regardless of the tree node being open or not you could just do that perhaps?
Note that you can also enclose the treenode between the PushID/PopID block, it wouldn't matter and you may already be doing that if you have items alongside your tree node. As a convenience, I think we need to be able to pass actual ID directly to the Popup functions or have more convenient way of passing ID. In case ideally you would want to just want to avoid the PushID/PopID alltogether and include in the same BeginPopup call. If
That would be a high-breaking change, not acceptable. Actually Begin/End is pretty much the inconsistent exception there, not TreeNode. I regret this but it's not really something we can easily change without breaking code (tho not impossible, since typically codebase have less |
Your suggestion looks like the right way to go for ease of use. The id stack is one element bigger than it needs to be but that isn't a problem. FWIW, I would prefer API breaking changes periodically when that leads to a better and more consistent design. |
Cool. I'm still keeping note of the idea noted here I think they are valid and I would like to implement them at some point. I am doing API breaking changes (they are usually well documented) but some are more acceptable than others. Making TreePop() required would break a lot of code in many places so that's unlikely. |
Hey @ocornut , sorry for this but I really dont understand how popup work with treenodes, Im trying to add a simple right click for each node on a hierarchy. I couldnt find any explanation, did I miss something ? anyway here is the most basic snippet I tried every order , if somebody can point me in the right direction in a simple way i'd be very gratefull. thx :) at best I get a popup bool node1 = ImGui::TreeNode("node1");
if (node1) {
bool node2 = ImGui::TreeNode("node2");
if (node2) { }
if (BeginPopupContextItem()) {
Text("n2");
EndPopup();
}
if (node2) { TreePop(); }
}
if (BeginPopupContextItem()) {
Text("n1");
EndPopup();
}
if (node1) { TreePop(); } |
The BeginPopupContextItem() call needs to come right after the TreeNode() call. The way your code is structured means that Node 2 effectively has 2 contexts menu items. |
ok thank you for your fast answer.. I guess I was right to feel sorry I though I had tryed this... I spent lot of time close but not good enough before finding this post and apparently didnt try or understand correctly so here is a copy paste full exemple in case anybody like me ^^ bool node1 = ImGui::TreeNode("node1");
if (BeginPopupContextItem()) {
Text("n1");
EndPopup();
}
if (node1) {
bool node2 = ImGui::TreeNode("node2");
if (BeginPopupContextItem()) {
Text("n2");
EndPopup();
}
if (node2) { }
if (node2) {TreePop();}
}
if (node1) { TreePop(); } |
It’s a frequent mistake. Because dear imgui use so many stacks its easy to visualize this in that manner. But we as we don’t really store any item data aside from the last item, queries on items usually come right after the item. |
I'm curious if anyone has a better pattern for attaching context menus to tree nodes. The context menu needs a unique id that works correctly when the TreeNode is opened, but it needs an extra push/pop when closed:
If TreeNode always pushed an ID and TreePop was always required, I think the code could be simplified like this, which is also more similar to how Begin/End work with windows.
The text was updated successfully, but these errors were encountered: