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
//! `NcMenu*` methods and associated functions.

use super::{NcMenuItem, NcMenuSection};
use crate::{cstring_mut, NcInput};
use core::ptr::null_mut;

#[allow(unused_imports)]
use crate::widgets::NcMenu;

mod menu;
mod options;

pub use menu::*;
pub use options::*;

/// # `NcMenuItem` Constructors
impl NcMenuItem {
    /// New NcMenuItem for [`NcMenu`].
    pub fn new(desc: &str, shortcut: NcInput) -> Self {
        Self {
            // utf-8 menu item, NULL for horizontal separator
            desc: cstring_mut![desc],

            // ´NcInput´ shortcut, all should be distinct
            shortcut,
        }
    }

    /// New empty NcMenuItem for [`NcMenu`].
    pub fn new_empty() -> Self {
        Self {
            desc: null_mut(),
            shortcut: NcInput::new_empty(),
        }
    }
}

/// # `NcMenuSection` Constructors
///
// Must contain at least 1 NcMenuItem.
impl NcMenuSection {
    /// New NcMenuSection for [`NcMenu`].
    pub fn new(name: &str, items: &mut [NcMenuItem], shortcut: NcInput) -> Self {
        Self {
            // utf-8 name string
            name: cstring_mut![name],

            // array of itemcount `NcMenuItem`s
            items: items.as_mut_ptr(),

            //
            itemcount: items.len() as i32,

            // shortcut, will be underlined if present in name
            shortcut,
        }
    }

    /// New NcMenuSection separator for [`NcMenu`].
    ///
    pub fn new_separator() -> Self {
        Self {
            name: null_mut(),
            items: null_mut(),
            itemcount: 0,
            shortcut: NcInput::new_empty(),
        }
    }
}