1use std::rc::Rc;
6
7use crabshell_reactive::Memo;
8
9use crate::cx::Cx;
10use crate::popup::PopupWindow;
11use crate::prop::{IntoProp, Prop};
12use crate::style::{Color, Style};
13use crate::widgets::{
14 Align, Button, Column, Common, Element, ElementKind, Row, Spacer, Stack, Text, Widget,
15 visibility_methods,
16};
17
18#[derive(Clone, Debug, PartialEq)]
20pub struct MenuEntry {
21 pub id: i32,
23 pub label: String,
25 pub separator: bool,
27 pub enabled: bool,
29 pub toggle: Option<MenuToggle>,
31 pub checked: bool,
33 pub submenu: bool,
36 pub children: Vec<MenuEntry>,
38}
39
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42pub enum MenuToggle {
43 Checkmark,
45 Radio,
47}
48
49const ITEM: Style = Style::new().padding(4, 12);
51const HOVER: Style = Style::new().background(Color::rgba(128, 128, 128, 64));
53const SEPARATOR: Style = Style::new()
55 .background(Color::rgba(128, 128, 128, 128))
56 .padding(1, 0);
57const DISABLED: Style = Style::new().opacity(0.5);
59const HIDDEN: Style = Style::new().opacity(0.0);
61
62pub struct Menu {
88 entries: Prop<Vec<MenuEntry>>,
89 style: Option<Prop<Style>>,
90 item_style: Option<Prop<Style>>,
91 hover_style: Option<Prop<Style>>,
92 separator_style: Option<Prop<Style>>,
93 on_activate: Option<EntryHandler>,
94 on_open_submenu: Option<EntryHandler>,
95 common: Common,
96}
97
98impl Menu {
99 pub fn new(entries: impl IntoProp<Vec<MenuEntry>>) -> Menu {
101 Menu {
102 entries: entries.into_prop(),
103 style: None,
104 item_style: None,
105 hover_style: None,
106 separator_style: None,
107 on_activate: None,
108 on_open_submenu: None,
109 common: Common::default(),
110 }
111 }
112
113 pub fn style(mut self, style: impl IntoProp<Style>) -> Menu {
115 self.style = Some(style.into_prop());
116 self
117 }
118
119 pub fn item_style(mut self, style: impl IntoProp<Style>) -> Menu {
122 self.item_style = Some(style.into_prop());
123 self
124 }
125
126 pub fn hover_style(mut self, style: impl IntoProp<Style>) -> Menu {
129 self.hover_style = Some(style.into_prop());
130 self
131 }
132
133 pub fn separator_style(mut self, style: impl IntoProp<Style>) -> Menu {
136 self.separator_style = Some(style.into_prop());
137 self
138 }
139
140 pub fn on_activate(mut self, handler: impl Fn(&Cx, i32) + 'static) -> Menu {
142 self.on_activate = Some(Rc::new(handler));
143 self
144 }
145
146 pub fn on_open_submenu(mut self, handler: impl Fn(&Cx, i32) + 'static) -> Menu {
149 self.on_open_submenu = Some(Rc::new(handler));
150 self
151 }
152
153 visibility_methods!();
154}
155
156struct Look {
159 menu: Shared<Style>,
160 item: Shared<Style>,
161 hover: Shared<Style>,
162 separator: Shared<Style>,
163 on_activate: Option<EntryHandler>,
164 on_open_submenu: Option<EntryHandler>,
165}
166
167type Shared<T> = Rc<dyn Fn() -> T>;
168
169type EntryHandler = Rc<dyn Fn(&Cx, i32)>;
171
172fn shared<T: Clone + 'static>(prop: Option<Prop<T>>, default: T) -> Shared<T> {
174 prop.map_or_else(|| Rc::new(move || default.clone()), Prop::shared)
175}
176
177impl Widget for Menu {
178 fn into_element(self: Box<Self>) -> Element {
179 let look = Rc::new(Look {
180 menu: shared(self.style, Style::new()),
181 item: shared(self.item_style, ITEM),
182 hover: shared(self.hover_style, HOVER),
183 separator: shared(self.separator_style, SEPARATOR),
184 on_activate: self.on_activate,
185 on_open_submenu: self.on_open_submenu,
186 });
187 let mut element = menu(look, shared(Some(self.entries), Vec::new()));
188 element.common.visible = self.common.visible;
189 element.common.geometry = self.common.geometry;
190 element.common.mapped_geometry = self.common.mapped_geometry;
191 element.common.accessible_name = self.common.accessible_name;
192 element
193 }
194}
195
196struct Built(Element);
199
200impl Widget for Built {
201 fn into_element(self: Box<Self>) -> Element {
202 self.0
203 }
204}
205
206fn menu(look: Rc<Look>, entries: Shared<Vec<MenuEntry>>) -> Element {
208 let style = Rc::clone(&look.menu);
209 let column = Column::new().style(move || style()).children_keyed(
210 move || entries(),
211 |entry: &MenuEntry| (entry.id, entry.separator),
214 move |_cx, entry| entry_view(&look, entry),
215 );
216 let mut element = Box::new(column).into_element();
217 element.name = "Menu";
218 element
219}
220
221fn entry_view(look: &Rc<Look>, entry: Memo<MenuEntry>) -> Built {
224 if entry.with_untracked(|entry| entry.separator) {
225 let separator = Rc::clone(&look.separator);
226 let line = Stack::new().style(move || separator());
227 return Built(Box::new(Column::new().padding(3, 0).child(line)).into_element());
228 }
229 let mark = move || match entry.get().toggle {
230 Some(MenuToggle::Radio) => "●".to_owned(),
231 _ => "✓".to_owned(),
232 };
233 let shown = move || {
234 let entry = entry.get();
235 if entry.toggle.is_some() && entry.checked {
236 Style::new()
237 } else {
238 HIDDEN
239 }
240 };
241 let arrow = move || if entry.get().submenu { "▸" } else { "" }.to_owned();
242 let marks = Stack::new()
244 .child(Text::new("✓").style(HIDDEN))
245 .child(Text::new("●").style(HIDDEN))
246 .child(Text::new(mark).style(shown));
247 let row = Row::new()
248 .gap(8)
249 .align(Align::Center)
250 .child(marks)
251 .child(Text::new(move || entry.get().label))
252 .child(Spacer::new())
253 .child(Text::new(arrow));
254 let mut row = Box::new(row).into_element();
257 if let ElementKind::Flex(flex) = &mut row.kind {
258 flex.grow = true;
259 }
260 let (item, hover) = (Rc::clone(&look.item), Rc::clone(&look.hover));
261 let clicked = Rc::clone(look);
262 let button = Button::new(Built(row))
263 .style(move || {
264 let style = item();
265 if entry.get().enabled {
266 style
267 } else {
268 style.merge(DISABLED)
269 }
270 })
271 .hover_style(move || {
272 if entry.get().enabled {
273 hover()
274 } else {
275 Style::new()
276 }
277 })
278 .on_click(move |cx| click(&clicked, cx, entry));
279 Built(Box::new(button).into_element())
280}
281
282fn click(look: &Rc<Look>, cx: &Cx, entry: Memo<MenuEntry>) {
285 let (id, enabled, submenu) =
286 entry.with_untracked(|entry| (entry.id, entry.enabled, entry.submenu));
287 if !enabled {
288 return;
289 }
290 if submenu {
291 if let Some(open) = &look.on_open_submenu {
292 open(cx, id);
293 }
294 let look = Rc::clone(look);
295 let children: Shared<Vec<MenuEntry>> = Rc::new(move || {
296 let children = entry.try_with(|entry| entry.children.clone());
297 children.unwrap_or_default()
298 });
299 cx.open_popup(move |_| PopupWindow::new().child(Built(menu(look, children))));
300 return;
301 }
302 if let Some(activate) = &look.on_activate {
303 activate(cx, id);
304 }
305 cx.close_popups_if_any();
307}