Skip to main content

dear_imgui_rs/widget/menu/
entry.rs

1use crate::sys;
2use crate::ui::Ui;
3
4use super::{MainMenuBarToken, MenuBarToken, MenuToken};
5
6/// # Menu Widgets
7impl Ui {
8    /// Creates and starts appending to a full-screen menu bar.
9    ///
10    /// Returns `Some(MainMenuBarToken)` if the menu bar is visible. After content has been
11    /// rendered, the token must be ended by calling `.end()`.
12    ///
13    /// Returns `None` if the menu bar is not visible and no content should be rendered.
14    #[must_use]
15    #[doc(alias = "BeginMainMenuBar")]
16    pub fn begin_main_menu_bar(&self) -> Option<MainMenuBarToken<'_>> {
17        if unsafe { sys::igBeginMainMenuBar() } {
18            Some(MainMenuBarToken::new(self))
19        } else {
20            None
21        }
22    }
23
24    /// Creates and starts appending to a menu bar for a window.
25    ///
26    /// Returns `Some(MenuBarToken)` if the menu bar is visible. After content has been
27    /// rendered, the token must be ended by calling `.end()`.
28    ///
29    /// Returns `None` if the menu bar is not visible and no content should be rendered.
30    #[must_use]
31    #[doc(alias = "BeginMenuBar")]
32    pub fn begin_menu_bar(&self) -> Option<MenuBarToken<'_>> {
33        if unsafe { sys::igBeginMenuBar() } {
34            Some(MenuBarToken::new(self))
35        } else {
36            None
37        }
38    }
39
40    /// Creates a menu and starts appending to it.
41    ///
42    /// Returns `Some(MenuToken)` if the menu is open. After content has been
43    /// rendered, the token must be ended by calling `.end()`.
44    ///
45    /// Returns `None` if the menu is not open and no content should be rendered.
46    #[must_use]
47    #[doc(alias = "BeginMenu")]
48    pub fn begin_menu(&self, label: impl AsRef<str>) -> Option<MenuToken<'_>> {
49        self.begin_menu_with_enabled(label, true)
50    }
51
52    /// Creates a menu with enabled state and starts appending to it.
53    ///
54    /// Returns `Some(MenuToken)` if the menu is open. After content has been
55    /// rendered, the token must be ended by calling `.end()`.
56    ///
57    /// Returns `None` if the menu is not open and no content should be rendered.
58    #[must_use]
59    #[doc(alias = "BeginMenu")]
60    pub fn begin_menu_with_enabled(
61        &self,
62        label: impl AsRef<str>,
63        enabled: bool,
64    ) -> Option<MenuToken<'_>> {
65        let label_ptr = self.scratch_txt(label);
66        if unsafe { sys::igBeginMenu(label_ptr, enabled) } {
67            Some(MenuToken::new(self))
68        } else {
69            None
70        }
71    }
72
73    /// Creates a menu and runs a closure to construct the contents.
74    ///
75    /// Note: the closure is not called if the menu is not visible.
76    ///
77    /// This is the equivalent of [menu_with_enabled](Self::menu_with_enabled)
78    /// with `enabled` set to `true`.
79    #[doc(alias = "BeginMenu")]
80    pub fn menu<F: FnOnce()>(&self, label: impl AsRef<str>, f: F) {
81        self.menu_with_enabled(label, true, f);
82    }
83
84    /// Creates a menu and runs a closure to construct the contents.
85    ///
86    /// Note: the closure is not called if the menu is not visible.
87    #[doc(alias = "BeginMenu")]
88    pub fn menu_with_enabled<F: FnOnce()>(&self, label: impl AsRef<str>, enabled: bool, f: F) {
89        if let Some(_menu) = self.begin_menu_with_enabled(label, enabled) {
90            f();
91        }
92    }
93}