mirror of
https://github.com/eliasstepanik/imgui-rs.git
synced 2026-01-14 15:08:36 +00:00
It might be better to use the utf8-cstr crate, but it doesn't have the owned<->borrowed duality, so it would be used as an implementation detail only.
80 lines
1.9 KiB
Rust
80 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: &'p ImStr,
|
|
enabled: bool,
|
|
_phantom: PhantomData<&'ui Ui<'ui>>,
|
|
}
|
|
|
|
impl<'ui, 'p> Menu<'ui, 'p> {
|
|
pub fn new(label: &'p ImStr) -> 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: &'p ImStr,
|
|
shortcut: Option<&'p ImStr>,
|
|
selected: Option<&'p mut bool>,
|
|
enabled: bool,
|
|
_phantom: PhantomData<&'ui Ui<'ui>>,
|
|
}
|
|
|
|
impl<'ui, 'p> MenuItem<'ui, 'p> {
|
|
pub fn new(label: &'p ImStr) -> Self {
|
|
MenuItem {
|
|
label: label,
|
|
shortcut: None,
|
|
selected: None,
|
|
enabled: true,
|
|
_phantom: PhantomData,
|
|
}
|
|
}
|
|
#[inline]
|
|
pub fn shortcut(mut self, shortcut: &'p ImStr) -> 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) }
|
|
}
|
|
}
|