Added Context::is_context_menu_open() #3267
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I encountered a case where I needed to know if an egui context menu was open, even if the mouse cursor was not in an egui area (full details in discord: https://discord.com/channels/900275882684477440/1142659953102966884).
I found that every resource I needed to detect that the context menu was open was either private, or pub(crate). Also, the
Response
returned byResponse::context_menu()
contains no way to detect that the context menu was opened or closed, so there's nothing to hook there.While it is possible to wrap the
Response::context_menu()
in code to do state-tracking, it becomes necessary to duplicate that code in every place you callcontext_menu()
.In this commit, I add
Context::is_context_menu_open()
. Named similarly toContext::is_pointer_over_area()
, this method will return true if any context menu is open. This is possible because the context menu uses a temp var with a fixed Id to store its state. This method just fetches the state, and interrogates it to see if there is a menu present.One
pub(crate)
helper method,BarState::has_root()
, was added as all the fields needed to perform the check were private to themenu
module.I've also updated the Context Menu demo to show the result of
is_context_menu_open()
to verify the code functions as expected.