finished menu

This commit is contained in:
Jack Spira 2021-09-07 22:53:39 -07:00 committed by Jack Mac
parent 540aa5f292
commit d490093dae
2 changed files with 28 additions and 42 deletions

View File

@ -837,23 +837,15 @@ fn show_example_app_main_menu_bar<'a>(ui: &Ui<'a>, state: &mut State) {
menu.end();
}
if let Some(menu) = ui.begin_menu(im_str!("Edit")) {
MenuItem::new(im_str!("Undo"))
.shortcut(im_str!("CTRL+Z"))
.build(ui);
MenuItem::new(im_str!("Undo")).shortcut("CTRL+Z").build(ui);
MenuItem::new(im_str!("Redo"))
.shortcut(im_str!("CTRL+Y"))
.shortcut("CTRL+Y")
.enabled(false)
.build(ui);
ui.separator();
MenuItem::new(im_str!("Cut"))
.shortcut(im_str!("CTRL+X"))
.build(ui);
MenuItem::new(im_str!("Copy"))
.shortcut(im_str!("CTRL+C"))
.build(ui);
MenuItem::new(im_str!("Paste"))
.shortcut(im_str!("CTRL+V"))
.build(ui);
MenuItem::new(im_str!("Cut")).shortcut("CTRL+X").build(ui);
MenuItem::new(im_str!("Copy")).shortcut("CTRL+C").build(ui);
MenuItem::new(im_str!("Paste")).shortcut("CTRL+V").build(ui);
menu.end();
}
menu_bar.end();
@ -865,9 +857,7 @@ fn show_example_menu_file<'a>(ui: &Ui<'a>, state: &mut FileMenuState) {
.enabled(false)
.build(ui);
MenuItem::new(im_str!("New")).build(ui);
MenuItem::new(im_str!("Open"))
.shortcut(im_str!("Ctrl+O"))
.build(ui);
MenuItem::new(im_str!("Open")).shortcut("Ctrl+O").build(ui);
if let Some(menu) = ui.begin_menu(im_str!("Open Recent")) {
MenuItem::new(im_str!("fish_hat.c")).build(ui);
MenuItem::new(im_str!("fish_hat.inl")).build(ui);
@ -884,7 +874,7 @@ fn show_example_menu_file<'a>(ui: &Ui<'a>, state: &mut FileMenuState) {
menu.end();
}
MenuItem::new(im_str!("Save"))
.shortcut(im_str!("Ctrl+S"))
.shortcut("Ctrl+S")
.build(ui);
MenuItem::new(im_str!("Save As..")).build(ui);
ui.separator();
@ -919,7 +909,7 @@ fn show_example_menu_file<'a>(ui: &Ui<'a>, state: &mut FileMenuState) {
.is_none());
MenuItem::new(im_str!("Checked")).selected(true).build(ui);
MenuItem::new(im_str!("Quit"))
.shortcut(im_str!("Alt+F4"))
.shortcut("Alt+F4")
.build(ui);
}

View File

@ -1,6 +1,4 @@
use std::ptr;
use crate::string::ImStr;
// use crate::string::ImStr;
use crate::sys;
use crate::Ui;
@ -66,7 +64,7 @@ impl<'ui> Ui<'ui> {
/// with `enabled` set to `true`.
#[must_use]
#[doc(alias = "BeginMenu")]
pub fn begin_menu(&self, label: &ImStr) -> Option<MenuToken<'_>> {
pub fn begin_menu(&self, label: impl AsRef<str>) -> Option<MenuToken<'_>> {
self.begin_menu_with_enabled(label, true)
}
@ -78,8 +76,12 @@ impl<'ui> Ui<'ui> {
/// Returns `None` if the menu is not visible and no content should be rendered.
#[must_use]
#[doc(alias = "BeginMenu")]
pub fn begin_menu_with_enabled(&self, label: &ImStr, enabled: bool) -> Option<MenuToken<'_>> {
if unsafe { sys::igBeginMenu(label.as_ptr(), enabled) } {
pub fn begin_menu_with_enabled(
&self,
label: impl AsRef<str>,
enabled: bool,
) -> Option<MenuToken<'_>> {
if unsafe { sys::igBeginMenu(self.scratch_txt(label), enabled) } {
Some(MenuToken::new(self))
} else {
None
@ -92,7 +94,7 @@ impl<'ui> Ui<'ui> {
/// This is the equivalent of [menu_with_enabled](Self::menu_with_enabled)
/// with `enabled` set to `true`.
#[doc(alias = "BeginMenu")]
pub fn menu<F: FnOnce()>(&self, label: &ImStr, f: F) {
pub fn menu<F: FnOnce()>(&self, label: impl AsRef<str>, f: F) {
self.menu_with_enabled(label, true, f);
}
@ -100,7 +102,7 @@ impl<'ui> Ui<'ui> {
///
/// Note: the closure is not called if the menu is not visible.
#[doc(alias = "BeginMenu")]
pub fn menu_with_enabled<F: FnOnce()>(&self, label: &ImStr, enabled: bool, f: F) {
pub fn menu_with_enabled<F: FnOnce()>(&self, label: impl AsRef<str>, enabled: bool, f: F) {
if let Some(_menu) = self.begin_menu_with_enabled(label, enabled) {
f();
}
@ -110,16 +112,16 @@ impl<'ui> Ui<'ui> {
/// Builder for a menu item.
#[derive(Copy, Clone, Debug)]
#[must_use]
pub struct MenuItem<'a> {
label: &'a ImStr,
shortcut: Option<&'a ImStr>,
pub struct MenuItem<'a, T> {
label: T,
shortcut: Option<&'a str>,
selected: bool,
enabled: bool,
}
impl<'a> MenuItem<'a> {
impl<'a, T: 'a + AsRef<str>> MenuItem<'a, T> {
/// Construct a new menu item builder.
pub fn new(label: &ImStr) -> MenuItem {
pub fn new(label: T) -> Self {
MenuItem {
label,
shortcut: None,
@ -131,7 +133,7 @@ impl<'a> MenuItem<'a> {
///
/// Shortcuts are displayed for convenience only and are not automatically handled.
#[inline]
pub fn shortcut(mut self, shortcut: &'a ImStr) -> Self {
pub fn shortcut(mut self, shortcut: &'a str) -> Self {
self.shortcut = Some(shortcut);
self
}
@ -155,20 +157,14 @@ impl<'a> MenuItem<'a> {
///
/// Returns true if the menu item is activated.
#[doc(alias = "MenuItemBool")]
pub fn build(self, _: &Ui) -> bool {
pub fn build(self, ui: &Ui) -> bool {
unsafe {
sys::igMenuItemBool(
self.label.as_ptr(),
self.shortcut.map(ImStr::as_ptr).unwrap_or(ptr::null()),
self.selected,
self.enabled,
)
let (label, shortcut) = ui.scratch_txt_with_opt(self.label, self.shortcut);
sys::igMenuItemBool(label, shortcut, self.selected, self.enabled)
}
}
}
/// # Convenience functions
impl<'a> MenuItem<'a> {
#[doc(alias = "MenuItemBool")]
/// Builds the menu item using a mutable reference to selected state.
pub fn build_with_ref(self, ui: &Ui, selected: &mut bool) -> bool {
if self.selected(*selected).build(ui) {