ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
59.73k stars 10.17k forks source link

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

Open ibachar opened 2 years ago

ibachar commented 2 years ago

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 https://github.com/ocornut/imgui/issues/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 commented 2 years ago

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 commented 2 years ago

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

ibachar commented 2 years ago

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())
{
    ...
}
ocornut commented 2 years ago

Please consider looking at the range_select branch.