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
use std::marker::PhantomData;
use std::ptr;
use sys;

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(_: &Ui<'ui>, label: &'p ImStr) -> Self {
        Menu {
            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 { sys::igBeginMenu(self.label.as_ptr(), self.enabled) };
        if render {
            f();
            unsafe { 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(_: &Ui<'ui>, label: &'p ImStr) -> Self {
        MenuItem {
            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 { sys::igMenuItemBoolPtr(label, shortcut, selected, enabled) }
    }
}