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

Keep item hovered, when popup is opened #6032

Closed
GenTexX opened this issue Dec 30, 2022 · 2 comments
Closed

Keep item hovered, when popup is opened #6032

GenTexX opened this issue Dec 30, 2022 · 2 comments
Labels
popups tree tree nodes

Comments

@GenTexX
Copy link

GenTexX commented Dec 30, 2022

Version/Branch of Dear ImGui:

Version: v1.89.2 WIP
Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_opengl3.cpp + imgui_impl_win32.cpp
Compiler: MSVC
Operating System: Windows 11

My Issue/Question:
What I am doing.

  1. I have a Tree.
  2. Every TreeNode changes color when hovered.
  3. I open a Popup, if a TreeNode is rightcklicked.
  4. When the Popup opens, the right-clicked TreeNode loses it's "hovered-state".

This is expected behavior, since the TreeNode is not hovered anymore.
But I want to keep that TreeNode hovered, so that it is colored like it is, as long as the Popup is opened.

There are definetly some solutions, like manually color the rightclicked TreeNode differently, but i wonder if there is a better one, like a flag to keep the hovered- and activated- states for the popup lifetime. (Because I think this is pretty common in other applications)

I think it is pretty important to show the user, which item he right-clicked.

Screenshots/Video

When Hovered: (Note that my cursor got hidden in this screenshots)
image

On Popup:
image

Standalone, minimal, complete and verifiable example:

ImGui::Begin("Example");
	for (size_t i = 0; i < 5; i++) {
			bool expanded = ImGui::TreeNodeEx((void*)i, 0, "Item %d", i);
			if (ImGui::BeginPopupContextItem())	{
				if (ImGui::BeginMenu("Option 1")) ImGui::EndMenu();
				if (ImGui::BeginMenu("Option 2")) ImGui::EndMenu();
				ImGui::EndPopup();
			}
			if (expanded) ImGui::TreePop();
		}
ImGui::End();

Thanks! Have a beautiful day!

@ocornut
Copy link
Owner

ocornut commented Dec 30, 2022

You can maintain a selection state (eg which node is selected) when the popup is open, and pass the ImGuiTreeNodeFlags_Selected to this node.

@ocornut ocornut added popups tree tree nodes labels Dec 30, 2022
ocornut added a commit that referenced this issue Jan 2, 2023
…nd update selection state when popup is open. (#6032)
@ocornut
Copy link
Owner

ocornut commented Jan 2, 2023

I have pushed a minor amend to the Demo 156e0a8 to showcase doing this with Selectable().

We are passing the bool selected parameter to Selectable() as selected == n.
In the case of a TreeNodeEx() it would be via the ImGuiTreeNodeFlags_Selected flag:

TreeNodeEx(...., (selected == n) ? ImGuiTreeNodeFlags_Selected : ImGuiTreeNodeFlags_None);

(Tangent: As it is expected that 1.90 will finish merging the work on range/multi-select (#1861) note that in the WIP BeginMultiSelect() feature, right-clicking automatically amend selection as well.)

@ocornut ocornut closed this as completed Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
popups tree tree nodes
Projects
None yet
Development

No branches or pull requests

2 participants