1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 22:00:03 -04:00

Clean up some of the new context menus

Follow-up to https://github.com/emilk/egui/pull/543

* Add entry to CHANGELOG.md
* Add entry to contributors in README.md
* Improve documentation
* Simplify demo
This commit is contained in:
Emil Ernerfeldt
2021-10-26 20:16:46 +02:00
parent 46fb9ff09b
commit 41f77ba7d7
7 changed files with 106 additions and 88 deletions

View File

@@ -305,6 +305,7 @@ impl CtxRef {
Self::layer_painter(self, LayerId::debug())
}
/// Respond to secondary clicks (right-clicks) by showing the given menu.
pub(crate) fn show_context_menu(
&self,
response: &Response,

View File

@@ -349,6 +349,7 @@ impl MenuRoot {
MenuResponse::Stay => {}
}
}
/// Respond to secondary (right) clicks.
pub fn context_click_interaction(response: &Response, root: &mut MenuRootManager, id: Id) {
let menu_response = Self::context_interaction(response, root, id);
Self::handle_menu_response(root, menu_response);

View File

@@ -471,6 +471,17 @@ impl Response {
}
}
/// Response to secondary clicks (right-clicks) by showing the given menu.
///
/// ``` rust
/// # let mut ui = &mut egui::Ui::__test();
/// let response = ui.label("Right-click me!");
/// response.context_menu(|ui|{
/// if ui.button("Close the menu").clicked() {
/// ui.close_menu();
/// }
/// });
/// ```
pub fn context_menu(&self, add_contents: impl FnOnce(&mut Ui)) -> &Self {
self.ctx.show_context_menu(self, add_contents);
self

View File

@@ -1737,6 +1737,7 @@ impl Ui {
self.advance_cursor_after_rect(Rect::from_min_size(top_left, size));
result
}
/// Close menu (with submenus), if any.
pub fn close_menu(&mut self) {
if let Some(menu_state) = &mut self.menu_state {
@@ -1744,12 +1745,15 @@ impl Ui {
}
self.menu_state = None;
}
pub(crate) fn get_menu_state(&self) -> Option<Arc<RwLock<MenuState>>> {
self.menu_state.clone()
}
pub(crate) fn set_menu_state(&mut self, menu_state: Option<Arc<RwLock<MenuState>>>) {
self.menu_state = menu_state;
}
#[inline(always)]
/// Create a menu button. Creates a button for a sub-menu when the `Ui` is inside a menu.
///
@@ -1757,7 +1761,9 @@ impl Ui {
/// # let mut ui = egui::Ui::__test();
/// ui.menu_button("My menu", |ui| {
/// ui.menu_button("My sub-menu", |ui| {
/// ui.label("Item");
/// if ui.button("Close the menu").clicked() {
/// ui.close_menu();
/// }
/// });
/// });
/// ```