You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Recommended way to expand / collapse tree nodes with keyboard shortcuts? #7553
Hi, I am trying to figure out the best way to control expanding / collapsing a tree node using a keyboard shortcut.
Currently, I use a simple bool flag + ImGuiTreeNodeFlag_DefaultOpen the to do this (see example).
I know this is very hacky, but it works fine until I start to also use the mouse to expand / collapse the node, after which the keyboard shortcut stops working. I'm sure that there is a recommended way to do this already, but I did not manage to find it.
Any suggestions?
Screenshots/Video:
No response
Minimal, Complete and Verifiable Example code:
ImGuiTreeNodeFlags filterNodeFlags = 0;
if (filterNodeOpen) filterNodeFlags = ImGuiTreeNodeFlags_DefaultOpen;
if (ImGui::TreeNodeEx("Filter", filterNodeFlags))
{
// ...ImGui::TreePop();
}
Maintain a flag as you do now, toggle it with your keyboard shortcut. Then you can set the state reliably with ImGui::SetNextItemOpen() before calling ImGui::TreeNodeEx(). As a last step you need to update your flag with the result of TreeNodeEx().
There's a named wrapper called ImGui::TreeNodeSetOpen() but it's signature is not ideal for a toggle (read followed by write).
...but if you store/defer your request until the item is submitted and call SetNextItemOpen() before it (as suggested by Daniel) you may likely in a better position to e.g. request scrolling or some other associated desirable behavior. Both ways are possible.
Activity
GamingMinds-DanielC commentedon May 6, 2024
Maintain a flag as you do now, toggle it with your keyboard shortcut. Then you can set the state reliably with
ImGui::SetNextItemOpen()before callingImGui::TreeNodeEx(). As a last step you need to update your flag with the result ofTreeNodeEx().ocornut commentedon May 6, 2024
It may be easier to access the storage directly in this case, assuming you can compute the ID of your tree node.
ocornut commentedon May 6, 2024
There's a named wrapper called
ImGui::TreeNodeSetOpen()but it's signature is not ideal for a toggle (read followed by write)....but if you store/defer your request until the item is submitted and call
SetNextItemOpen()before it (as suggested by Daniel) you may likely in a better position to e.g. request scrolling or some other associated desirable behavior. Both ways are possible.TreeNode: rename/rework ImGuiNavTreeNodeData system to be usable by m…
sodamouse commentedon Jul 13, 2024
Thanks, all of these approaches work fine. I ended up going with GamingMinds-DanielC's suggestion as it required the least amount of rewrite.
This issue can be closed.
ocornut commentedon Jul 13, 2024
Good to hear.
I’ll keep the issue open because i’ve been trying to work on this topic too.
Internals: Added TreeNodeIsOpen() to facilitate discoverability. (#7553…
Internals: rename recently added TreeNodeIsOpen() -> TreeNodeGetOpen(…
Ocornut master (24/07) (#4)
Added SetNextItemStorageID() for tree nodes. (#7553, #6990, #3823, #1131
TreeNode: moved TreeNodeGetOpen() helper to public API. (#3823, #9251, …
TreeNode: moved TreeNodeGetOpen() helper to public API. (ocornut#3823, …
TreeNode: fixed duplicate TreeNodeGetOpen() declaration. (#3823, #9251,