Add menu with an image button (#2488)
This commit is contained in:
parent
0eabd894bd
commit
53b1d0e5e9
2 changed files with 70 additions and 0 deletions
|
@ -100,6 +100,20 @@ pub fn menu_button<R>(
|
||||||
stationary_menu_impl(ui, title, Box::new(add_contents))
|
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.
|
||||||
|
///
|
||||||
|
/// Responds to primary clicks.
|
||||||
|
///
|
||||||
|
/// Returns `None` if the menu is not open.
|
||||||
|
pub fn menu_image_button<R>(
|
||||||
|
ui: &mut Ui,
|
||||||
|
texture_id: TextureId,
|
||||||
|
image_size: impl Into<Vec2>,
|
||||||
|
add_contents: impl FnOnce(&mut Ui) -> R,
|
||||||
|
) -> InnerResponse<Option<R>> {
|
||||||
|
stationary_menu_image_impl(ui, texture_id, image_size, Box::new(add_contents))
|
||||||
|
}
|
||||||
|
|
||||||
/// Construct a nested sub menu in another menu.
|
/// Construct a nested sub menu in another menu.
|
||||||
///
|
///
|
||||||
/// Opens on hover.
|
/// Opens on hover.
|
||||||
|
@ -177,6 +191,25 @@ fn stationary_menu_impl<'c, R>(
|
||||||
InnerResponse::new(inner.map(|r| r.inner), button_response)
|
InnerResponse::new(inner.map(|r| r.inner), button_response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Build a top level menu with an image button.
|
||||||
|
///
|
||||||
|
/// Responds to primary clicks.
|
||||||
|
fn stationary_menu_image_impl<'c, R>(
|
||||||
|
ui: &mut Ui,
|
||||||
|
texture_id: TextureId,
|
||||||
|
image_size: impl Into<Vec2>,
|
||||||
|
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(ImageButton::new(texture_id, image_size));
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
/// Response to secondary clicks (right-clicks) by showing the given menu.
|
/// Response to secondary clicks (right-clicks) by showing the given menu.
|
||||||
pub(crate) fn context_menu(
|
pub(crate) fn context_menu(
|
||||||
response: &Response,
|
response: &Response,
|
||||||
|
|
|
@ -2157,6 +2157,43 @@ impl Ui {
|
||||||
menu::menu_button(self, title, add_contents)
|
menu::menu_button(self, title, add_contents)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
/// Create a menu button with an image that when clicked will show the given menu.
|
||||||
|
///
|
||||||
|
/// If called from within a menu this will instead create a button for a sub-menu.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use egui_extras;
|
||||||
|
///
|
||||||
|
/// let img = egui_extrasRetainedImage::from_svg_bytes_with_size(
|
||||||
|
/// "rss",
|
||||||
|
/// include_bytes!("rss.svg"),
|
||||||
|
/// egui_extras::image::FitTo::Size(24, 24),
|
||||||
|
/// );
|
||||||
|
///
|
||||||
|
/// ui.menu_image_button(img.texture_id(ctx), img.size_vec2(), |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`].
|
||||||
|
pub fn menu_image_button<R>(
|
||||||
|
&mut self,
|
||||||
|
texture_id: TextureId,
|
||||||
|
image_size: impl Into<Vec2>,
|
||||||
|
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_image_button(self, texture_id, image_size, add_contents)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in a new issue