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
use std::ptr;
use std::thread;

use crate::context::Context;
use crate::string::ImStr;
use crate::sys;
use crate::Ui;

/// # Widgets: Menus
impl<'ui> Ui<'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]
    pub fn begin_main_menu_bar(&self) -> Option<MainMenuBarToken> {
        if unsafe { sys::igBeginMainMenuBar() } {
            Some(MainMenuBarToken { ctx: self.ctx })
        } else {
            None
        }
    }
    /// Creates a full-screen main menu bar and runs a closure to construct the contents.
    ///
    /// Note: the closure is not called if the menu bar is not visible.
    pub fn main_menu_bar<F: FnOnce()>(&self, f: F) {
        if let Some(menu_bar) = self.begin_main_menu_bar() {
            f();
            menu_bar.end(self);
        }
    }
    /// Creates and starts appending to the menu bar of the current 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]
    pub fn begin_menu_bar(&self) -> Option<MenuBarToken> {
        if unsafe { sys::igBeginMenuBar() } {
            Some(MenuBarToken { ctx: self.ctx })
        } else {
            None
        }
    }
    /// Creates a menu bar in the current window and runs a closure to construct the contents.
    ///
    /// Note: the closure is not called if the menu bar is not visible.
    pub fn menu_bar<F: FnOnce()>(&self, f: F) {
        if let Some(menu_bar) = self.begin_menu_bar() {
            f();
            menu_bar.end(self);
        }
    }
    /// Creates and starts appending to a sub-menu entry.
    ///
    /// Returns `Some(MenuToken)` if the menu is visible. After content has been
    /// rendered, the token must be ended by calling `.end()`.
    ///
    /// Returns `None` if the menu is not visible and no content should be rendered.
    #[must_use]
    pub fn begin_menu(&self, label: &ImStr, enabled: bool) -> Option<MenuToken> {
        if unsafe { sys::igBeginMenu(label.as_ptr(), enabled) } {
            Some(MenuToken { ctx: self.ctx })
        } 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.
    pub fn menu<F: FnOnce()>(&self, label: &ImStr, enabled: bool, f: F) {
        if let Some(menu) = self.begin_menu(label, enabled) {
            f();
            menu.end(self);
        }
    }
}

/// Builder for a menu item.
#[derive(Copy, Clone, Debug)]
#[must_use]
pub struct MenuItem<'a> {
    label: &'a ImStr,
    shortcut: Option<&'a ImStr>,
    selected: bool,
    enabled: bool,
}

impl<'a> MenuItem<'a> {
    /// Construct a new menu item builder.
    pub fn new(label: &ImStr) -> MenuItem {
        MenuItem {
            label,
            shortcut: None,
            selected: false,
            enabled: true,
        }
    }
    /// Sets the menu item shortcut.
    ///
    /// Shortcuts are displayed for convenience only and are not automatically handled.
    #[inline]
    pub fn shortcut(mut self, shortcut: &'a ImStr) -> Self {
        self.shortcut = Some(shortcut);
        self
    }
    /// Sets the selected state of the menu item.
    ///
    /// Default: false
    #[inline]
    pub fn selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }
    /// Enables/disables the menu item.
    ///
    /// Default: enabled
    #[inline]
    pub fn enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }
    /// Builds the menu item.
    ///
    /// Returns true if the menu item is activated.
    pub fn build(self, _: &Ui) -> bool {
        unsafe {
            sys::igMenuItemBool(
                self.label.as_ptr(),
                self.shortcut.map(ImStr::as_ptr).unwrap_or(ptr::null()),
                self.selected,
                self.enabled,
            )
        }
    }
}

/// # Convenience functions
impl<'a> MenuItem<'a> {
    /// 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) {
            *selected = true;
            true
        } else {
            false
        }
    }
}

/// Tracks a main menu bar that must be ended by calling `.end()`
#[must_use]
pub struct MainMenuBarToken {
    ctx: *const Context,
}

impl MainMenuBarToken {
    /// Ends a main menu bar
    pub fn end(mut self, _: &Ui) {
        self.ctx = ptr::null();
        unsafe { sys::igEndMainMenuBar() };
    }
}

impl Drop for MainMenuBarToken {
    fn drop(&mut self) {
        if !self.ctx.is_null() && !thread::panicking() {
            panic!("A MainMenuBarToken was leaked. Did you call .end()?");
        }
    }
}

/// Tracks a menu bar that must be ended by calling `.end()`
#[must_use]
pub struct MenuBarToken {
    ctx: *const Context,
}

impl MenuBarToken {
    /// Ends a menu bar
    pub fn end(mut self, _: &Ui) {
        self.ctx = ptr::null();
        unsafe { sys::igEndMenuBar() };
    }
}

impl Drop for MenuBarToken {
    fn drop(&mut self) {
        if !self.ctx.is_null() && !thread::panicking() {
            panic!("A MenuBarToken was leaked. Did you call .end()?");
        }
    }
}

/// Tracks a menu that must be ended by calling `.end()`
#[must_use]
pub struct MenuToken {
    ctx: *const Context,
}

impl MenuToken {
    /// Ends a menu
    pub fn end(mut self, _: &Ui) {
        self.ctx = ptr::null();
        unsafe { sys::igEndMenu() };
    }
}

impl Drop for MenuToken {
    fn drop(&mut self) {
        if !self.ctx.is_null() && !thread::panicking() {
            panic!("A MenuToken was leaked. Did you call .end()?");
        }
    }
}