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 5 commits
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
14 changes: 7 additions & 7 deletions crates/egui/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ pub fn menu_button<R>(
stationary_menu_impl(ui, title, Box::new(add_contents))
}

/// Construct a top level menu with an image in a menu bar. This would be e.g. "File", "Edit" etc.
/// Construct a top level menu with a custom button in a menu bar.
///
/// Responds to primary clicks.
///
/// Returns `None` if the menu is not open.
pub fn menu_image_button<R>(
NicolasBircksZR marked this conversation as resolved.
Show resolved Hide resolved
pub fn menu_custom_button<R>(
ui: &mut Ui,
image_button: ImageButton<'_>,
button: Button<'_>,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<Option<R>> {
stationary_menu_image_impl(ui, image_button, Box::new(add_contents))
stationary_menu_button_impl(ui, button, Box::new(add_contents))
}

/// Construct a nested sub menu in another menu.
Expand Down Expand Up @@ -226,15 +226,15 @@ fn stationary_menu_impl<'c, R>(
/// Build a top level menu with an image button.
///
/// Responds to primary clicks.
fn stationary_menu_image_impl<'c, R>(
fn stationary_menu_button_impl<'c, R>(
ui: &mut Ui,
image_button: ImageButton<'_>,
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 button_response = ui.add(button);
let inner = bar_state.bar_menu(&button_response, add_contents);

bar_state.store(ui.ctx(), bar_id);
Expand Down
34 changes: 33 additions & 1 deletion crates/egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2564,7 +2564,39 @@ impl Ui {
if let Some(menu_state) = self.menu_state.clone() {
menu::submenu_button(self, menu_state, String::new(), add_contents)
} else {
menu::menu_image_button(self, ImageButton::new(image), add_contents)
menu::menu_custom_button(self, Button::image(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();
/// }
/// });
/// });
/// ```
NicolasBircksZR marked this conversation as resolved.
Show resolved Hide resolved
///
/// 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>>,
NicolasBircksZR marked this conversation as resolved.
Show resolved Hide resolved
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, title, add_contents)
} else {
menu::menu_custom_button(self, Button::image_and_text(image, title), add_contents)
}
}
}
Expand Down
Loading