1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! Menus and menu bars
//!
//! Helpers to build main-menu bars, menu bars within windows, and nested menus
//! with optional enable/disable states and separators.
//!
use crate::sys;
use crate::ui::Ui;
/// # Menu Widgets
impl Ui {
/// Creates and starts appending to a full-screen menu bar.
///
/// Returns `Some(MainMenuBarToken)` if the menu bar is visible. After content has been
/// rendered, the token must be ended by calling `.end()`.
///
/// Returns `None` if the menu bar is not visible and no content should be rendered.
#[must_use]
#[doc(alias = "BeginMainMenuBar")]
pub fn begin_main_menu_bar(&self) -> Option<MainMenuBarToken<'_>> {
if unsafe { sys::igBeginMainMenuBar() } {
Some(MainMenuBarToken::new(self))
} else {
None
}
}
/// Creates and starts appending to a menu bar for a window.
///
/// Returns `Some(MenuBarToken)` if the menu bar is visible. After content has been
/// rendered, the token must be ended by calling `.end()`.
///
/// Returns `None` if the menu bar is not visible and no content should be rendered.
#[must_use]
#[doc(alias = "BeginMenuBar")]
pub fn begin_menu_bar(&self) -> Option<MenuBarToken<'_>> {
if unsafe { sys::igBeginMenuBar() } {
Some(MenuBarToken::new(self))
} else {
None
}
}
/// Creates a menu and starts appending to it.
///
/// Returns `Some(MenuToken)` if the menu is open. After content has been
/// rendered, the token must be ended by calling `.end()`.
///
/// Returns `None` if the menu is not open and no content should be rendered.
#[must_use]
#[doc(alias = "BeginMenu")]
pub fn begin_menu(&self, label: impl AsRef<str>) -> Option<MenuToken<'_>> {
self.begin_menu_with_enabled(label, true)
}
/// Creates a menu with enabled state and starts appending to it.
///
/// Returns `Some(MenuToken)` if the menu is open. After content has been
/// rendered, the token must be ended by calling `.end()`.
///
/// Returns `None` if the menu is not open and no content should be rendered.
#[must_use]
#[doc(alias = "BeginMenu")]
pub fn begin_menu_with_enabled(
&self,
label: impl AsRef<str>,
enabled: bool,
) -> Option<MenuToken<'_>> {
let label_ptr = self.scratch_txt(label);
if unsafe { sys::igBeginMenu(label_ptr, enabled) } {
Some(MenuToken::new(self))
} else {
None
}
}
/// Creates a menu and runs a closure to construct the contents.
///
/// Note: the closure is not called if the menu is not visible.
///
/// 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: impl AsRef<str>, f: F) {
self.menu_with_enabled(label, true, f);
}
/// Creates a menu and runs a closure to construct the contents.
///
/// Note: the closure is not called if the menu is not visible.
#[doc(alias = "BeginMenu")]
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();
}
}
/// Creates a menu item.
///
/// Returns true if the menu item is activated.
#[doc(alias = "MenuItem")]
pub fn menu_item(&self, label: impl AsRef<str>) -> bool {
let label_ptr = self.scratch_txt(label);
unsafe { sys::igMenuItemEx(label_ptr, std::ptr::null(), std::ptr::null(), false, true) }
}
/// Creates a menu item with a shortcut.
///
/// Returns true if the menu item is activated.
#[doc(alias = "MenuItem")]
pub fn menu_item_with_shortcut(
&self,
label: impl AsRef<str>,
shortcut: impl AsRef<str>,
) -> bool {
let (label_ptr, shortcut_ptr) = self.scratch_txt_two(label, shortcut);
unsafe { sys::igMenuItemEx(label_ptr, std::ptr::null(), shortcut_ptr, false, true) }
}
/// Creates a menu item with explicit enabled/selected state.
/// Returns true if the menu item is activated.
#[doc(alias = "MenuItem")]
pub fn menu_item_enabled_selected(
&self,
label: impl AsRef<str>,
shortcut: Option<impl AsRef<str>>,
selected: bool,
enabled: bool,
) -> bool {
let label = label.as_ref();
match shortcut {
Some(shortcut) => {
let (label_ptr, shortcut_ptr) = self.scratch_txt_two(label, shortcut.as_ref());
unsafe { sys::igMenuItem_Bool(label_ptr, shortcut_ptr, selected, enabled) }
}
None => {
let label_ptr = self.scratch_txt(label);
unsafe { sys::igMenuItem_Bool(label_ptr, std::ptr::null(), selected, enabled) }
}
}
}
/// Creates a menu item with explicit enabled/selected state (no shortcut).
///
/// Returns true if the menu item is activated.
#[doc(alias = "MenuItem")]
pub fn menu_item_enabled_selected_no_shortcut(
&self,
label: impl AsRef<str>,
selected: bool,
enabled: bool,
) -> bool {
self.menu_item_enabled_selected(label, None::<&str>, selected, enabled)
}
/// Creates a menu item with explicit enabled/selected state and a shortcut.
///
/// Returns true if the menu item is activated.
#[doc(alias = "MenuItem")]
pub fn menu_item_enabled_selected_with_shortcut(
&self,
label: impl AsRef<str>,
shortcut: impl AsRef<str>,
selected: bool,
enabled: bool,
) -> bool {
self.menu_item_enabled_selected(label, Some(shortcut), selected, enabled)
}
/// Creates a toggleable menu item bound to `selected` (updated in place).
/// Returns true if the menu item is activated.
#[doc(alias = "MenuItem")]
pub fn menu_item_toggle(
&self,
label: impl AsRef<str>,
shortcut: Option<impl AsRef<str>>,
selected: &mut bool,
enabled: bool,
) -> bool {
let label = label.as_ref();
match shortcut {
Some(shortcut) => {
let (label_ptr, shortcut_ptr) = self.scratch_txt_two(label, shortcut.as_ref());
unsafe { sys::igMenuItem_BoolPtr(label_ptr, shortcut_ptr, selected, enabled) }
}
None => {
let label_ptr = self.scratch_txt(label);
unsafe { sys::igMenuItem_BoolPtr(label_ptr, std::ptr::null(), selected, enabled) }
}
}
}
/// Creates a toggleable menu item bound to `selected` (no shortcut).
///
/// Returns true if the menu item is activated.
#[doc(alias = "MenuItem")]
pub fn menu_item_toggle_no_shortcut(
&self,
label: impl AsRef<str>,
selected: &mut bool,
enabled: bool,
) -> bool {
self.menu_item_toggle(label, None::<&str>, selected, enabled)
}
/// Creates a toggleable menu item bound to `selected` with a shortcut.
///
/// Returns true if the menu item is activated.
#[doc(alias = "MenuItem")]
pub fn menu_item_toggle_with_shortcut(
&self,
label: impl AsRef<str>,
shortcut: impl AsRef<str>,
selected: &mut bool,
enabled: bool,
) -> bool {
self.menu_item_toggle(label, Some(shortcut), selected, enabled)
}
}
/// Tracks a main menu bar that can be ended by calling `.end()` or by dropping
#[must_use]
pub struct MainMenuBarToken<'ui> {
_ui: &'ui Ui,
}
impl<'ui> MainMenuBarToken<'ui> {
/// Creates a new main menu bar token
fn new(ui: &'ui Ui) -> Self {
MainMenuBarToken { _ui: ui }
}
/// Ends the main menu bar
pub fn end(self) {
// The drop implementation will handle the actual ending
}
}
impl<'ui> Drop for MainMenuBarToken<'ui> {
fn drop(&mut self) {
unsafe {
sys::igEndMainMenuBar();
}
}
}
/// Tracks a menu bar that can be ended by calling `.end()` or by dropping
#[must_use]
pub struct MenuBarToken<'ui> {
_ui: &'ui Ui,
}
impl<'ui> MenuBarToken<'ui> {
/// Creates a new menu bar token
fn new(ui: &'ui Ui) -> Self {
MenuBarToken { _ui: ui }
}
/// Ends the menu bar
pub fn end(self) {
// The drop implementation will handle the actual ending
}
}
impl<'ui> Drop for MenuBarToken<'ui> {
fn drop(&mut self) {
unsafe {
sys::igEndMenuBar();
}
}
}
/// Tracks a menu that can be ended by calling `.end()` or by dropping
#[must_use]
pub struct MenuToken<'ui> {
_ui: &'ui Ui,
}
impl<'ui> MenuToken<'ui> {
/// Creates a new menu token
fn new(ui: &'ui Ui) -> Self {
MenuToken { _ui: ui }
}
/// Ends the menu
pub fn end(self) {
// The drop implementation will handle the actual ending
}
}
impl<'ui> Drop for MenuToken<'ui> {
fn drop(&mut self) {
unsafe {
sys::igEndMenu();
}
}
}