waft_winui/controls/menu_flyout/
item.rs1use std::{fmt, rc::Rc};
4use waft_foundation::{App, Listener};
5use waft_widgets::{IntoWidget, KeyRef, WidgetRef};
6
7pub type MenuCheckedChanged = Rc<dyn Fn(&mut App, bool)>;
9
10#[derive(Clone)]
12pub enum MenuFlyoutItemKind {
13 Item,
15
16 Toggle {
18 is_checked: bool,
20
21 changed: MenuCheckedChanged,
23 },
24
25 Radio {
27 is_checked: bool,
29
30 checked: Listener,
32 },
33
34 SubItem {
36 items: Vec<MenuFlyoutItem>,
38 },
39
40 Separator,
42}
43
44impl fmt::Debug for MenuFlyoutItemKind {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 match self {
47 Self::Item => f.write_str("Item"),
48 Self::Toggle { is_checked, .. } => f.debug_tuple("Toggle").field(is_checked).finish(),
49 Self::Radio { is_checked, .. } => f.debug_tuple("Radio").field(is_checked).finish(),
50 Self::SubItem { items } => f.debug_tuple("SubItem").field(items).finish(),
51 Self::Separator => f.write_str("Separator"),
52 }
53 }
54}
55
56#[derive(Clone)]
58pub struct MenuFlyoutItem {
59 pub text: String,
61
62 pub icon: Option<WidgetRef>,
64
65 pub is_enabled: bool,
67
68 pub keyboard_accelerator_text_override: String,
70
71 pub click: Option<Listener>,
73
74 pub prevent_dismiss_on_pointer: bool,
76
77 pub kind: MenuFlyoutItemKind,
79
80 pub key: Option<KeyRef>,
82}
83
84impl fmt::Debug for MenuFlyoutItem {
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 f.debug_struct("MenuFlyoutItem")
87 .field("text", &self.text)
88 .field("kind", &self.kind)
89 .field("is_enabled", &self.is_enabled)
90 .finish_non_exhaustive()
91 }
92}
93
94impl MenuFlyoutItem {
95 pub fn new(text: impl Into<String>, click: Listener) -> Self {
97 Self {
98 text: text.into(),
99 icon: None,
100 is_enabled: true,
101 keyboard_accelerator_text_override: String::new(),
102 click: Some(click),
103 prevent_dismiss_on_pointer: false,
104 kind: MenuFlyoutItemKind::Item,
105 key: None,
106 }
107 }
108
109 pub fn toggle(
111 text: impl Into<String>,
112 is_checked: bool,
113 changed: impl Fn(&mut App, bool) + 'static,
114 ) -> Self {
115 let mut item = Self::new(text, Listener::new(|_| {}));
116 item.click = None;
117 item.kind = MenuFlyoutItemKind::Toggle {
118 is_checked,
119 changed: Rc::new(changed),
120 };
121 item
122 }
123
124 pub fn radio(text: impl Into<String>, is_checked: bool, checked: Listener) -> Self {
126 let mut item = Self::new(text, Listener::new(|_| {}));
127 item.click = None;
128 item.kind = MenuFlyoutItemKind::Radio {
129 is_checked,
130 checked,
131 };
132 item
133 }
134
135 pub fn sub_item(text: impl Into<String>, items: Vec<MenuFlyoutItem>) -> Self {
137 let mut item = Self::new(text, Listener::new(|_| {}));
138 item.click = None;
139 item.kind = MenuFlyoutItemKind::SubItem { items };
140 item
141 }
142
143 pub fn separator() -> Self {
145 let mut item = Self::new("", Listener::new(|_| {}));
146 item.click = None;
147 item.kind = MenuFlyoutItemKind::Separator;
148 item
149 }
150
151 #[cfg_attr(debug_assertions, track_caller)]
153 pub fn icon<K>(mut self, icon: impl IntoWidget<K>) -> Self {
154 self.icon = Some(icon.into_widget());
155 self
156 }
157
158 pub fn is_enabled(mut self, enabled: bool) -> Self {
160 self.is_enabled = enabled;
161 self
162 }
163
164 pub fn keyboard_accelerator_text_override(mut self, text: impl Into<String>) -> Self {
166 self.keyboard_accelerator_text_override = text.into();
167 self
168 }
169
170 pub fn click(mut self, click: Listener) -> Self {
172 self.click = Some(click);
173 self
174 }
175
176 pub fn prevent_dismiss_on_pointer(mut self, value: bool) -> Self {
178 self.prevent_dismiss_on_pointer = value;
179 self
180 }
181
182 pub fn key(mut self, key: KeyRef) -> Self {
184 self.key = Some(key);
185 self
186 }
187}