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