mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 21:30:03 -04:00
Added Context::is_context_menu_open() (#3267)
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). I found that every resource I needed to detect that the menu was open was either private, or pub(crate). 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 use a context menu. In this commit, I add `Context::is_context_menu_open()`. Named similarly to `Context::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 helper method, `BarState::has_root()`, was added as all the fields needed to perform the check were private to the `menu` 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.
This commit is contained in:
@@ -66,6 +66,11 @@ impl super::View for ContextMenus {
|
||||
ui.menu_button("Click for menu", Self::nested_menus);
|
||||
ui.button("Right-click for menu")
|
||||
.context_menu(Self::nested_menus);
|
||||
if ui.ctx().is_context_menu_open() {
|
||||
ui.label("Context menu is open");
|
||||
} else {
|
||||
ui.label("Context menu is closed");
|
||||
}
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
Reference in New Issue
Block a user