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.common.mask_region = self.common.mask_region;
193 element
194 }
195}
196
197struct Built(Element);
200
201impl Widget for Built {
202 fn into_element(self: Box<Self>) -> Element {
203 self.0
204 }
205}
206
207fn menu(look: Rc<Look>, entries: Shared<Vec<MenuEntry>>) -> Element {
209 let style = Rc::clone(&look.menu);
210 let column = Column::new().style(move || style()).children_keyed(
211 move || entries(),
212 |entry: &MenuEntry| (entry.id, entry.separator),
215 move |_cx, entry| entry_view(&look, entry),
216 );
217 let mut element = Box::new(column).into_element();
218 element.name = "Menu";
219 element
220}
221
222fn entry_view(look: &Rc<Look>, entry: Memo<MenuEntry>) -> Built {
225 if entry.with_untracked(|entry| entry.separator) {
226 let separator = Rc::clone(&look.separator);
227 let line = Stack::new().style(move || separator());
228 return Built(Box::new(Column::new().padding(3, 0).child(line)).into_element());
229 }
230 let mark = move || match entry.get().toggle {
231 Some(MenuToggle::Radio) => "●".to_owned(),
232 _ => "✓".to_owned(),
233 };
234 let shown = move || {
235 let entry = entry.get();
236 if entry.toggle.is_some() && entry.checked {
237 Style::new()
238 } else {
239 HIDDEN
240 }
241 };
242 let arrow = move || if entry.get().submenu { "▸" } else { "" }.to_owned();
243 let marks = Stack::new()
245 .child(Text::new("✓").style(HIDDEN))
246 .child(Text::new("●").style(HIDDEN))
247 .child(Text::new(mark).style(shown));
248 let row = Row::new()
249 .gap(8)
250 .align(Align::Center)
251 .child(marks)
252 .child(Text::new(move || entry.get().label))
253 .child(Spacer::new())
254 .child(Text::new(arrow));
255 let mut row = Box::new(row).into_element();
258 if let ElementKind::Flex(flex) = &mut row.kind {
259 flex.grow = true;
260 }
261 let (item, hover) = (Rc::clone(&look.item), Rc::clone(&look.hover));
262 let clicked = Rc::clone(look);
263 let button = Button::new(Built(row))
264 .style(move || {
265 let style = item();
266 if entry.get().enabled {
267 style
268 } else {
269 style.merge(DISABLED)
270 }
271 })
272 .hover_style(move || {
273 if entry.get().enabled {
274 hover()
275 } else {
276 Style::new()
277 }
278 })
279 .on_click(move |cx| click(&clicked, cx, entry));
280 Built(Box::new(button).into_element())
281}
282
283fn click(look: &Rc<Look>, cx: &Cx, entry: Memo<MenuEntry>) {
286 let (id, enabled, submenu) =
287 entry.with_untracked(|entry| (entry.id, entry.enabled, entry.submenu));
288 if !enabled {
289 return;
290 }
291 if submenu {
292 if let Some(open) = &look.on_open_submenu {
293 open(cx, id);
294 }
295 let look = Rc::clone(look);
296 let children: Shared<Vec<MenuEntry>> = Rc::new(move || {
297 let children = entry.try_with(|entry| entry.children.clone());
298 children.unwrap_or_default()
299 });
300 cx.open_popup(move |_| PopupWindow::new().child(Built(menu(look, children))));
301 return;
302 }
303 if let Some(activate) = &look.on_activate {
304 activate(cx, id);
305 }
306 cx.close_popups_if_any();
308}