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

Context menu on docked window tab #7914

Closed
azonenberg opened this issue Aug 23, 2024 · 12 comments
Closed

Context menu on docked window tab #7914

azonenberg opened this issue Aug 23, 2024 · 12 comments
Labels

Comments

@azonenberg
Copy link
Contributor

Version/Branch of Dear ImGui:

1.90.7 WIP 19063

Back-ends:

vulkan

Compiler, OS:

gcc/Debian

Full config/build information:

No response

Details:

I want to be able to spawn a context menu by right clicking the tab widget of a docked window.

As of now, I'm able to create a context menu on the title bar when a window is not docked, but haven't found any way to detect a right click on the tab widget of the docked window. Is this currently possible?

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

@ocornut
Copy link
Owner

ocornut commented Aug 23, 2024

As of now, I'm able to create a context menu on the title bar when a window is not docked, but haven't found any way to detect a right click on the tab widget of the docked window.

How are you doing that?

Because IsItemHovered() - which is the right thing to use - should work after Begin(), docked or not, and therefore helpers such as BeginPopupContextItem() should work as well.

@azonenberg
Copy link
Contributor Author

I'm using ImGui::IsWindowHovered() right after Begin() as you suggested in #316 (comment). That was from 2015 so it's possible there's a better way now :)

@azonenberg
Copy link
Contributor Author

azonenberg commented Aug 23, 2024

If I use ImGui::IsItemHovered(), the popup appears as expected even when the window is docked.

But as soon as I let go of the right mouse button, the popup vanishes. It seems I need to right click then hold the right button down and move the mouse into the popup before releasing for the popup to stay open.

@ocornut
Copy link
Owner

ocornut commented Aug 23, 2024

Please provide an explicit repro because this works here:

ImGui::Begin("Hello, world!");
if (ImGui::BeginPopupContextItem())
{
    ImGui::MenuItem("Hello");
    ImGui::EndPopup();
}
ImGui::End();

Docked or not docked.

@azonenberg
Copy link
Contributor Author

azonenberg commented Aug 23, 2024

	if(!ImGui::Begin(GetID().c_str(), &open, ImGuiWindowFlags_NoScrollWithMouse))
	{
		//tabbed out, don't draw anything until we're back in the foreground
		ImGui::End();
		return true;
	}

	//Check for right click on the title bar
	if(ImGui::IsItemHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Right))
	{
		auto rect = ImGui::GetCurrentWindow()->TitleBarRect();
		if(ImGui::IsMouseHoveringRect(rect.Min, rect.Max, false))
			ImGui::OpenPopup("Rename Group");
	}

	if(ImGui::BeginPopup("Rename Group"))
	{
		ImGui::InputText("Name", &m_title);
		ImGui::EndPopup();
	}

	ImGui::End();

@azonenberg
Copy link
Contributor Author

The IsMouseHoveringRect seems to be redundant if using IsItemHovered() and I removed it with no change to behavior.

@ocornut
Copy link
Owner

ocornut commented Aug 23, 2024

It's not standard to open popups on mouse down, because typically the mouse down is used to close a previous popup.

This is unnecessary:

auto rect = ImGui::GetCurrentWindow()->TitleBarRect();
if(ImGui::IsMouseHoveringRect(rect.Min, rect.Max, false))

You can simply use if (ImGui::BeginPopupContextItem()) you don't need anything else.

But I was also puzzled as to why the popup would close. If I look at the log:
image

And I think there is an issue in DockNodeUpdateTabBar() which leads your code flow to refocus the window and close the popup. I will investigate it. However if you open popup on mouse up or simply use BeginPopupContextItem() you won't experience that issue.

@ocornut ocornut added bug tabs tab bars, tabs labels Aug 23, 2024
@azonenberg
Copy link
Contributor Author

Confirmed, using IsMouseReleased() to spawn the popup fixes the immediate problem. Keeping this ticket open until you find and fix the focus bug, you can close once that's finished.

Thanks :)

@ocornut
Copy link
Owner

ocornut commented Aug 23, 2024

Again you don't need to call IsMouseReleased() yourself, you only need to do:

ImGui::Begin("Hello, world!");
if (ImGui::BeginPopupContextItem())
{
    ImGui::MenuItem("Hello");
    ImGui::EndPopup();
}

@azonenberg
Copy link
Contributor Author

Even better, works like a charm.

ocornut added a commit that referenced this issue Aug 23, 2024
…d have the side-effect of e.g. closing popup on a mouse release. (#7914)
@ocornut
Copy link
Owner

ocornut commented Aug 23, 2024

Pushed a fix/workaround for the mouse release issue 8c4dceb but I will need to investigate this a little further.

ocornut added a commit that referenced this issue Sep 3, 2024
…d have the side-effect of e.g. closing popup on a mouse release. (#7914)

+ Debug Log: add details about closed popups.
ocornut added a commit to ocornut/imgui_test_engine that referenced this issue Sep 3, 2024
@ocornut
Copy link
Owner

ocornut commented Sep 3, 2024

Closing this after confirming.
Added a test ocornut/imgui_test_engine@030cc99

@ocornut ocornut closed this as completed Sep 3, 2024
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