Skip to main content

dear_imgui_rs/widget/menu/
items.rs

1use crate::sys;
2use crate::ui::Ui;
3
4impl Ui {
5    /// Creates a menu item.
6    ///
7    /// Returns true if the menu item is activated.
8    #[doc(alias = "MenuItem")]
9    pub fn menu_item(&self, label: impl AsRef<str>) -> bool {
10        let label_ptr = self.scratch_txt(label);
11        unsafe { sys::igMenuItemEx(label_ptr, std::ptr::null(), std::ptr::null(), false, true) }
12    }
13
14    /// Creates a menu item with a shortcut.
15    ///
16    /// Returns true if the menu item is activated.
17    #[doc(alias = "MenuItem")]
18    pub fn menu_item_with_shortcut(
19        &self,
20        label: impl AsRef<str>,
21        shortcut: impl AsRef<str>,
22    ) -> bool {
23        let (label_ptr, shortcut_ptr) = self.scratch_txt_two(label, shortcut);
24        unsafe { sys::igMenuItemEx(label_ptr, std::ptr::null(), shortcut_ptr, false, true) }
25    }
26
27    /// Creates a menu item with explicit enabled/selected state.
28    /// Returns true if the menu item is activated.
29    #[doc(alias = "MenuItem")]
30    pub fn menu_item_enabled_selected(
31        &self,
32        label: impl AsRef<str>,
33        shortcut: Option<impl AsRef<str>>,
34        selected: bool,
35        enabled: bool,
36    ) -> bool {
37        let label = label.as_ref();
38        match shortcut {
39            Some(shortcut) => {
40                let (label_ptr, shortcut_ptr) = self.scratch_txt_two(label, shortcut.as_ref());
41                unsafe { sys::igMenuItem_Bool(label_ptr, shortcut_ptr, selected, enabled) }
42            }
43            None => {
44                let label_ptr = self.scratch_txt(label);
45                unsafe { sys::igMenuItem_Bool(label_ptr, std::ptr::null(), selected, enabled) }
46            }
47        }
48    }
49
50    /// Creates a menu item with explicit enabled/selected state (no shortcut).
51    ///
52    /// Returns true if the menu item is activated.
53    #[doc(alias = "MenuItem")]
54    pub fn menu_item_enabled_selected_no_shortcut(
55        &self,
56        label: impl AsRef<str>,
57        selected: bool,
58        enabled: bool,
59    ) -> bool {
60        self.menu_item_enabled_selected(label, None::<&str>, selected, enabled)
61    }
62
63    /// Creates a menu item with explicit enabled/selected state and a shortcut.
64    ///
65    /// Returns true if the menu item is activated.
66    #[doc(alias = "MenuItem")]
67    pub fn menu_item_enabled_selected_with_shortcut(
68        &self,
69        label: impl AsRef<str>,
70        shortcut: impl AsRef<str>,
71        selected: bool,
72        enabled: bool,
73    ) -> bool {
74        self.menu_item_enabled_selected(label, Some(shortcut), selected, enabled)
75    }
76
77    /// Creates a toggleable menu item bound to `selected` (updated in place).
78    /// Returns true if the menu item is activated.
79    #[doc(alias = "MenuItem")]
80    pub fn menu_item_toggle(
81        &self,
82        label: impl AsRef<str>,
83        shortcut: Option<impl AsRef<str>>,
84        selected: &mut bool,
85        enabled: bool,
86    ) -> bool {
87        let label = label.as_ref();
88        match shortcut {
89            Some(shortcut) => {
90                let (label_ptr, shortcut_ptr) = self.scratch_txt_two(label, shortcut.as_ref());
91                unsafe { sys::igMenuItem_BoolPtr(label_ptr, shortcut_ptr, selected, enabled) }
92            }
93            None => {
94                let label_ptr = self.scratch_txt(label);
95                unsafe { sys::igMenuItem_BoolPtr(label_ptr, std::ptr::null(), selected, enabled) }
96            }
97        }
98    }
99
100    /// Creates a toggleable menu item bound to `selected` (no shortcut).
101    ///
102    /// Returns true if the menu item is activated.
103    #[doc(alias = "MenuItem")]
104    pub fn menu_item_toggle_no_shortcut(
105        &self,
106        label: impl AsRef<str>,
107        selected: &mut bool,
108        enabled: bool,
109    ) -> bool {
110        self.menu_item_toggle(label, None::<&str>, selected, enabled)
111    }
112
113    /// Creates a toggleable menu item bound to `selected` with a shortcut.
114    ///
115    /// Returns true if the menu item is activated.
116    #[doc(alias = "MenuItem")]
117    pub fn menu_item_toggle_with_shortcut(
118        &self,
119        label: impl AsRef<str>,
120        shortcut: impl AsRef<str>,
121        selected: &mut bool,
122        enabled: bool,
123    ) -> bool {
124        self.menu_item_toggle(label, Some(shortcut), selected, enabled)
125    }
126}