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

TreeNode doesn't activate when Ctrl is being held #4692

Open
ibachar opened this issue Nov 4, 2021 · 4 comments
Open

TreeNode doesn't activate when Ctrl is being held #4692

ibachar opened this issue Nov 4, 2021 · 4 comments
Labels

Comments

@ibachar
Copy link
Contributor

ibachar commented Nov 4, 2021

Version/Branch of Dear ImGui:

Version: 1.85 WIP
Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_SDL.cpp + imgui_impl_opengl3.cpp
Compiler: VS2019
Operating System: Windows 11

My Issue/Question:

I implemented multi selection in tree nodes by pressing control. Also, I wish to change the selection once the user
releases the mouse button over the tree item. To do so, I've used:

if (ImGui::IsItemDeactivated() && ImGui::IsItemHovered())
{
    ...
}

Looking at the code of ImGui::TreeNode, it is hard coded that the button behavior in the tree node uses the flag ImGuiButtonFlags_NoKeyModifiers when the mouse is not over the arrow button, preventing the above code from working (ImGui::IsItemDeactivated never returns true while control is held down)

Is there a workaround I can apply to support both my requirements (multi-select with control, change selection when releasing the button)?

Thanks

Standalone, minimal, complete and verifiable example: (see #2261)

// Here's some code anyone can copy and paste to reproduce your issue
ImGui::Begin("Example Bug");
ImGui::TreeNode("Test");
if (ImGui::IsItemDeactivated()) // Not entered when clicking node while control is down
{
    ...
}
ImGui::End();
@ocornut ocornut added the tree tree nodes label Nov 4, 2021
@ocornut ocornut changed the title ImGui::IsItemDeactivated() returns false for tree node while control is being held TreeNode doesn't activate when Ctrl is being held Nov 4, 2021
@ocornut
Copy link
Owner

ocornut commented Nov 4, 2021

May I suggest looking at the range_select branch it should be designed to solve your bigger problem more naturally?

This is being maintained and actively used (and should always merge into docking).
Also see #1861 I just posted a brief update (TL;DR; branch hasn't been merged yet because I've been rather unhappy with the API, but you can/should use it if you need).

(
On your specific issue as formulated;
It is a reasonable request to allow the TreeNode to "activate" (and therefore "deactivate" and return true in IsItemDeactivated()) when key modifiers are held. But if we allowed it by default, it would mean that TreeNode would visibly react ("active" color) when CTRL is held yet by default nothing would happen without extra code, which may be odd. So perhaps this can be opt-in? It may also be reasonable to use IsItemHovered() && IsMouseReleased(...)...
)

@ibachar
Copy link
Contributor Author

ibachar commented Nov 4, 2021

Thanks for the quick response, I went with using IsMouseReleased which solved my issue.

@ibachar ibachar closed this as completed Nov 4, 2021
@ibachar
Copy link
Contributor Author

ibachar commented Nov 4, 2021

OK, one issue with that, now I wish to use IsItemToggledOpen to prevent chaning selection while opening nodes. This doesn't work since IsItemToggledOpen return true upon clicking the arrow and I change selection when releasing the mouse button which will happen some frames afterwards. To summaize, the following will not work as expected:

if (!ImGui::IsItemToggledOpen() && ImGui::IsMouseReleased(0) && ImGui::IsItemHovered())
{
    ...
}

@ibachar ibachar reopened this Nov 4, 2021
@ocornut
Copy link
Owner

ocornut commented Nov 4, 2021 via email

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

No branches or pull requests

2 participants