egui_desktop/menu/
items.rs1use crate::menu::shortcuts::KeyboardShortcut;
2use std::fmt::{Debug, Formatter, Result};
3
4pub struct SubMenuItem {
9 pub label: String,
11 pub shortcut: Option<KeyboardShortcut>,
13 pub enabled: bool,
15 pub separator_after: bool,
17 pub callback: Option<Box<dyn Fn() + Send + Sync>>,
19 pub children: Vec<SubMenuItem>,
21}
22
23impl Debug for SubMenuItem {
24 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
25 f.debug_struct("SubMenuItem")
26 .field("label", &self.label)
27 .field("shortcut", &self.shortcut)
28 .field("enabled", &self.enabled)
29 .field("separator_after", &self.separator_after)
30 .field("callback", &"<function>")
31 .finish()
32 }
33}
34
35impl Clone for SubMenuItem {
36 fn clone(&self) -> Self {
37 Self {
38 label: self.label.clone(),
39 shortcut: self.shortcut.clone(),
40 enabled: self.enabled,
41 separator_after: self.separator_after,
42 callback: None, children: self.children.clone(),
44 }
45 }
46}
47
48impl SubMenuItem {
49 pub fn new(label: &str) -> Self {
51 Self {
52 label: label.to_string(),
53 shortcut: None,
54 enabled: true,
55 separator_after: false,
56 callback: None,
57 children: Vec::new(),
58 }
59 }
60
61 pub fn with_shortcut(mut self, shortcut: KeyboardShortcut) -> Self {
63 self.shortcut = Some(shortcut);
64 self
65 }
66
67 pub fn with_callback(mut self, callback: Box<dyn Fn() + Send + Sync>) -> Self {
69 self.callback = Some(callback);
70 self
71 }
72
73 pub fn disabled(mut self) -> Self {
75 self.enabled = false;
76 self
77 }
78
79 pub fn with_separator(mut self) -> Self {
81 self.separator_after = true;
82 self
83 }
84
85 pub fn add_child(mut self, child: SubMenuItem) -> Self {
87 self.children.push(child);
88 self
89 }
90
91 pub fn with_children(mut self, children: Vec<SubMenuItem>) -> Self {
93 self.children = children;
94 self
95 }
96}
97
98#[derive(Debug, Clone)]
100pub struct MenuItem {
101 pub label: String,
103 pub subitems: Vec<SubMenuItem>,
105 pub enabled: bool,
107}
108
109impl MenuItem {
110 pub fn new(label: &str) -> Self {
112 Self {
113 label: label.to_string(),
114 subitems: Vec::new(),
115 enabled: true,
116 }
117 }
118
119 pub fn add_subitem(mut self, subitem: SubMenuItem) -> Self {
121 self.subitems.push(subitem);
122 self
123 }
124
125 pub fn disabled(mut self) -> Self {
127 self.enabled = false;
128 self
129 }
130}