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

Add a menu button with text and image #4748

Merged
merged 7 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions crates/egui/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ pub fn menu_image_button<R>(
stationary_menu_image_impl(ui, image_button, Box::new(add_contents))
}

/// Construct a top level menu with an image and a text in a menu bar. This would be e.g. "File", "Edit" etc.
///
/// Responds to primary clicks.
///
/// Returns `None` if the menu is not open.
pub fn menu_text_image_button<R>(
ui: &mut Ui,
image_button: Button<'_>,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<Option<R>> {
stationary_menu_text_image_impl(ui, image_button, Box::new(add_contents))
}

/// Construct a nested sub menu in another menu.
///
/// Opens on hover.
Expand Down Expand Up @@ -230,6 +243,24 @@ fn stationary_menu_image_impl<'c, R>(
InnerResponse::new(inner.map(|r| r.inner), button_response)
}

/// Build a top level menu with an text and image button.
///
/// Responds to primary clicks.
fn stationary_menu_text_image_impl<'c, R>(
ui: &mut Ui,
image_button: Button<'_>,
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
) -> InnerResponse<Option<R>> {
let bar_id = ui.id();

let mut bar_state = BarState::load(ui.ctx(), bar_id);
let button_response = ui.add(image_button);
let inner = bar_state.bar_menu(&button_response, add_contents);

bar_state.store(ui.ctx(), bar_id);
InnerResponse::new(inner.map(|r| r.inner), button_response)
}

pub(crate) const CONTEXT_MENU_ID_STR: &str = "__egui::context_menu";

/// Response to secondary clicks (right-clicks) by showing the given menu.
Expand Down
32 changes: 32 additions & 0 deletions crates/egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2567,6 +2567,38 @@ impl Ui {
menu::menu_image_button(self, ImageButton::new(image), add_contents)
}
}

/// Create a menu button with an image and a text that when clicked will show the given menu.
///
/// If called from within a menu this will instead create a button for a sub-menu.
///
/// ```ignore
/// let img = egui::include_image!("../assets/ferris.png");
/// let title = "My Menu";
///
/// ui.menu_text_image_button(title, img, |ui| {
/// ui.menu_button("My sub-menu", |ui| {
/// if ui.button("Close the menu").clicked() {
/// ui.close_menu();
/// }
/// });
/// });
/// ```
///
/// See also: [`Self::close_menu`] and [`Response::context_menu`].
#[inline]
pub fn menu_text_image_button<'a, R>(
&mut self,
title: impl Into<WidgetText>,
image: impl Into<Image<'a>>,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<Option<R>> {
if let Some(menu_state) = self.menu_state.clone() {
menu::submenu_button(self, menu_state, String::new(), add_contents)
} else {
menu::menu_text_image_button(self, Button::image_and_text(image, title), add_contents)
}
}
}

// ----------------------------------------------------------------------------
Expand Down