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