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

Recommended way to expand / collapse tree nodes with keyboard shortcuts? #7553

Open
sodamouse opened this issue May 4, 2024 · 3 comments
Open
Labels
tree tree nodes

Comments

@sodamouse
Copy link

Version/Branch of Dear ImGui:

Version 1.89.8, Branch: master

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp

Compiler, OS:

Windows 11 + MSVC 2022

Full config/build information:

No response

Details:

My Issue/Question:

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();
    }
// somewhere else in code
    if (!showTagsPopup && ImGui::IsKeyDown(ImGuiKey_LeftCtrl) && ImGui::IsKeyPressed(ImGuiKey_G)) filterNodeOpen = !filterNodeOpen;
@GamingMinds-DanielC
Copy link
Contributor

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().

@ocornut ocornut added the tree tree nodes label May 6, 2024
@ocornut
Copy link
Owner

ocornut commented 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.

if (ImGui::Shortcut(ImGuiMod_Ctrl | ImGuiKey_G))
{
    ImGuiStorage* storage = ImGui::GetStateStorage();
    (*storage->GetBoolRef(id_of_tree_node)) ^= 1; // toggle
}

@ocornut
Copy link
Owner

ocornut commented 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.

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

No branches or pull requests

3 participants