imgui-rs/src/menus.rs
Joonas Javanainen 9c56e48140 Use in-place mutation in builders
Since we consume self anyway, this style is much more readable
and might have better performance in non-optimized builds.
2016-08-07 20:01:20 +03:00

78 lines
1.9 KiB
Rust

use imgui_sys;
use std::marker::PhantomData;
use std::ptr;
use super::{ImStr, Ui};
#[must_use]
pub struct Menu<'ui, 'p> {
label: ImStr<'p>,
enabled: bool,
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> Menu<'ui, 'p> {
pub fn new(label: ImStr<'p>) -> Self {
Menu {
label: label,
enabled: true,
_phantom: PhantomData,
}
}
#[inline]
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn build<F: FnOnce()>(self, f: F) {
let render = unsafe { imgui_sys::igBeginMenu(self.label.as_ptr(), self.enabled) };
if render {
f();
unsafe { imgui_sys::igEndMenu() };
}
}
}
#[must_use]
pub struct MenuItem<'ui, 'p> {
label: ImStr<'p>,
shortcut: Option<ImStr<'p>>,
selected: Option<&'p mut bool>,
enabled: bool,
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> MenuItem<'ui, 'p> {
pub fn new(label: ImStr<'p>) -> Self {
MenuItem {
label: label,
shortcut: None,
selected: None,
enabled: true,
_phantom: PhantomData,
}
}
#[inline]
pub fn shortcut(mut self, shortcut: ImStr<'p>) -> Self {
self.shortcut = Some(shortcut);
self
}
#[inline]
pub fn selected(mut self, selected: &'p mut bool) -> Self {
self.selected = Some(selected);
self
}
#[inline]
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn build(self) -> bool {
let label = self.label.as_ptr();
let shortcut = self.shortcut.map(|x| x.as_ptr()).unwrap_or(ptr::null());
let selected = self.selected.map(|x| x as *mut bool).unwrap_or(ptr::null_mut());
let enabled = self.enabled;
unsafe { imgui_sys::igMenuItemPtr(label, shortcut, selected, enabled) }
}
}