1use std::rc::Rc;
4
5use mitsuami_core::services::{ItemMenus, Menu, MenuEntries};
6use mitsuami_core::{
7 CurrentWindow, Element, EventValue, NodeId, Prop, SidebarItemData, SidebarSectionData, Tweak, Ui, UiEvent, View,
8 WidgetKind,
9};
10use mitsuami_reactive::{IntoValue, Signal, Value, effect, inject, on_cleanup, signal};
11
12pub struct Sidebar<T: 'static> {
54 selection: Signal<T>,
55 sections: Vec<SidebarSection<T>>,
56 dynamic: Option<Box<dyn Fn() -> Vec<SidebarSection<T>>>>,
58 shown: Option<Signal<bool>>,
59 activate: Option<Rc<dyn Fn(T)>>,
60 tweak: Option<Tweak<Sidebar<T>>>,
61}
62
63impl<T: PartialEq + Clone + 'static> Sidebar<T> {
64 pub fn new(selection: Signal<T>) -> Sidebar<T> {
65 Sidebar { selection, sections: Vec::new(), dynamic: None, shown: None, activate: None, tweak: None }
66 }
67
68 pub fn children_with<E: SidebarEntries<T>>(mut self, entries: impl Fn() -> E + 'static) -> Sidebar<T> {
73 self.dynamic = Some(Box::new(move || {
74 let mut sections = Vec::new();
75 entries().add_to(&mut sections);
76 sections
77 }));
78 self
79 }
80
81 pub fn on_activate(mut self, activate: impl Fn(T) + 'static) -> Sidebar<T> {
86 self.activate = Some(Rc::new(activate));
87 self
88 }
89
90 pub fn shown(mut self, shown: Signal<bool>) -> Sidebar<T> {
96 self.shown = Some(shown);
97 self
98 }
99
100 pub fn native(mut self, tweak: Tweak<Sidebar<T>>) -> Sidebar<T> {
103 self.tweak = Some(tweak);
104 self
105 }
106
107 pub fn children(mut self, children: impl SidebarEntries<T>) -> Sidebar<T> {
110 children.add_to(&mut self.sections);
111 self
112 }
113
114 pub fn item(self, item: SidebarItem<T>) -> Sidebar<T> {
115 self.children(item)
116 }
117
118 pub fn section(self, section: SidebarSection<T>) -> Sidebar<T> {
119 self.children(section)
120 }
121}
122
123impl<T: PartialEq + Clone + 'static> View for Sidebar<T> {
124 fn build(self, ui: &Ui) -> NodeId {
127 let placeholder = ui.create(WidgetKind::Fragment, Vec::new());
128 let Some(CurrentWindow(window)) = inject::<CurrentWindow>() else {
129 panic!("a Sidebar goes in a window's content");
130 };
131 let Sidebar { selection, sections, dynamic, shown: showing, activate, tweak } = self;
132 let values: Signal<Vec<T>> = signal(Vec::new());
135 let menus = ItemMenus::new();
136 let mut element = Element::new(WidgetKind::Sidebar);
137 if let Some(tweak) = tweak {
138 tweak.apply(&mut element);
139 }
140 let sidebar = element.build(ui);
141 ui.append_child(window, sidebar);
142 let (shown, item_menus) = (ui.clone(), menus.clone());
144 effect(move || {
145 let built = dynamic.as_ref().map(|entries| entries()).unwrap_or_default();
146 let all: Vec<&SidebarSection<T>> = sections.iter().chain(&built).collect();
147 let items = all.iter().flat_map(|s| &s.items);
148 let mut entries =
149 item_menus.collect(&items.clone().map(|i| i.menu.as_ref()).collect::<Vec<_>>()).into_iter();
150 let data = all
151 .iter()
152 .map(|section| SidebarSectionData {
153 title: section.title.as_ref().map(|t| t.get()),
154 items: section
155 .items
156 .iter()
157 .map(|item| SidebarItemData {
158 title: item.title.get(),
159 icon: item.icon.clone(),
160 subtitle: item.subtitle.as_ref().map(|s| s.get()),
161 menu: entries.next().unwrap_or_default(),
162 })
163 .collect(),
164 })
165 .collect();
166 let now: Vec<T> = items.map(|i| i.value.clone()).collect();
167 if values.with_untracked(|v| *v != now) {
168 values.set(now);
169 }
170 shown.set_prop(sidebar, Prop::Sections(data));
171 });
172 let chosen = ui.clone();
173 effect(move || {
174 let index = selection.with(|value| values.with(|v| v.iter().position(|v| v == value)));
175 chosen.set_prop(sidebar, Prop::SelectedIndex(index));
176 });
177 let value_at = move |index: usize| values.with_untracked(|v| v.get(index).cloned());
178 ui.on_event(sidebar, move |event| match event {
179 UiEvent::Changed(EventValue::Index(index)) => {
180 if let Some(value) = value_at(*index) {
181 selection.set(value);
182 }
183 }
184 UiEvent::SidebarItemActivated(index) => {
185 if let (Some(activate), Some(value)) = (&activate, value_at(*index)) {
186 activate(value);
187 }
188 }
189 UiEvent::ContextMenuItem(_) => menus.handle(event),
190 _ => {}
191 });
192 if let Some(showing) = showing {
193 let shown = ui.clone();
194 effect(move || shown.set_prop(sidebar, Prop::SidebarShown(showing.get())));
195 ui.on_event(sidebar, move |event| {
196 if let UiEvent::SidebarShownChanged(on) = event {
197 showing.set(*on);
198 }
199 });
200 }
201 let ui = ui.clone();
202 on_cleanup(move || ui.destroy(sidebar));
203 placeholder
204 }
205}
206
207pub struct SidebarSection<T: 'static> {
209 title: Option<Value<String>>,
210 items: Vec<SidebarItem<T>>,
211 loose: bool,
213}
214
215impl<T: 'static> SidebarSection<T> {
216 pub fn new(title: impl IntoValue<String>) -> SidebarSection<T> {
217 SidebarSection { title: Some(title.into_value()), items: Vec::new(), loose: false }
218 }
219
220 pub fn untitled() -> SidebarSection<T> {
222 SidebarSection { title: None, items: Vec::new(), loose: false }
223 }
224
225 pub fn title(mut self, title: impl IntoValue<String>) -> SidebarSection<T> {
226 self.title = Some(title.into_value());
227 self
228 }
229
230 pub fn item(mut self, item: SidebarItem<T>) -> SidebarSection<T> {
231 self.items.push(item);
232 self
233 }
234
235 pub fn children(mut self, items: impl SidebarItems<T>) -> SidebarSection<T> {
237 items.add_to(&mut self.items);
238 self
239 }
240}
241
242pub struct SidebarItem<T: 'static> {
249 title: Value<String>,
250 icon: Option<String>,
251 subtitle: Option<Value<String>>,
252 menu: Option<Menu>,
253 value: T,
254}
255
256impl<T: 'static> SidebarItem<T> {
257 pub fn new(title: impl IntoValue<String>, value: T) -> SidebarItem<T> {
258 SidebarItem { title: title.into_value(), icon: None, subtitle: None, menu: None, value }
259 }
260
261 pub fn value<U: 'static>(self, value: U) -> SidebarItem<U> {
263 SidebarItem { title: self.title, icon: self.icon, subtitle: self.subtitle, menu: self.menu, value }
264 }
265
266 pub fn subtitle(mut self, subtitle: impl IntoValue<String>) -> SidebarItem<T> {
270 self.subtitle = Some(subtitle.into_value());
271 self
272 }
273
274 pub fn context_menu(mut self, entries: impl MenuEntries) -> SidebarItem<T> {
279 self.menu = Some(Menu::new(String::new()).children(entries));
280 self
281 }
282
283 pub fn icon(mut self, name: impl Into<String>) -> SidebarItem<T> {
289 self.icon = Some(name.into()).filter(|n| !n.is_empty());
290 self
291 }
292}
293
294pub trait SidebarEntries<T: 'static> {
297 fn add_to(self, sections: &mut Vec<SidebarSection<T>>);
298}
299
300impl<T: 'static> SidebarEntries<T> for SidebarItem<T> {
301 fn add_to(self, sections: &mut Vec<SidebarSection<T>>) {
302 match sections.last_mut().filter(|s| s.loose) {
303 Some(section) => section.items.push(self),
304 None => sections.push(SidebarSection { title: None, items: vec![self], loose: true }),
305 }
306 }
307}
308
309impl<T: 'static> SidebarEntries<T> for SidebarSection<T> {
310 fn add_to(self, sections: &mut Vec<SidebarSection<T>>) {
311 sections.push(self);
312 }
313}
314
315impl<T: 'static, E: SidebarEntries<T>> SidebarEntries<T> for Vec<E> {
316 fn add_to(self, sections: &mut Vec<SidebarSection<T>>) {
317 for entry in self {
318 entry.add_to(sections);
319 }
320 }
321}
322
323impl<T: 'static, E: SidebarEntries<T>> SidebarEntries<T> for Option<E> {
324 fn add_to(self, sections: &mut Vec<SidebarSection<T>>) {
325 if let Some(entry) = self {
326 entry.add_to(sections);
327 }
328 }
329}
330
331impl<T: 'static> SidebarEntries<T> for () {
332 fn add_to(self, _: &mut Vec<SidebarSection<T>>) {}
333}
334
335pub trait SidebarItems<T: 'static> {
338 fn add_to(self, items: &mut Vec<SidebarItem<T>>);
339}
340
341impl<T: 'static> SidebarItems<T> for SidebarItem<T> {
342 fn add_to(self, items: &mut Vec<SidebarItem<T>>) {
343 items.push(self);
344 }
345}
346
347impl<T: 'static, I: SidebarItems<T>> SidebarItems<T> for Vec<I> {
348 fn add_to(self, items: &mut Vec<SidebarItem<T>>) {
349 for item in self {
350 item.add_to(items);
351 }
352 }
353}
354
355impl<T: 'static, I: SidebarItems<T>> SidebarItems<T> for Option<I> {
356 fn add_to(self, items: &mut Vec<SidebarItem<T>>) {
357 if let Some(item) = self {
358 item.add_to(items);
359 }
360 }
361}
362
363impl<T: 'static> SidebarItems<T> for () {
364 fn add_to(self, _: &mut Vec<SidebarItem<T>>) {}
365}
366
367macro_rules! tuple_sidebar {
368 ($($name:ident),+) => {
369 impl<T: 'static, $($name: SidebarEntries<T>),+> SidebarEntries<T> for ($($name,)+) {
370 #[allow(non_snake_case)]
371 fn add_to(self, sections: &mut Vec<SidebarSection<T>>) {
372 let ($($name,)+) = self;
373 $($name.add_to(sections);)+
374 }
375 }
376
377 impl<T: 'static, $($name: SidebarItems<T>),+> SidebarItems<T> for ($($name,)+) {
378 #[allow(non_snake_case)]
379 fn add_to(self, items: &mut Vec<SidebarItem<T>>) {
380 let ($($name,)+) = self;
381 $($name.add_to(items);)+
382 }
383 }
384 };
385}
386
387tuple_sidebar!(A);
388tuple_sidebar!(A, B);
389tuple_sidebar!(A, B, C);
390tuple_sidebar!(A, B, C, D);
391tuple_sidebar!(A, B, C, D, E);
392tuple_sidebar!(A, B, C, D, E, F);
393tuple_sidebar!(A, B, C, D, E, F, G);
394tuple_sidebar!(A, B, C, D, E, F, G, H);
395tuple_sidebar!(A, B, C, D, E, F, G, H, I);
396tuple_sidebar!(A, B, C, D, E, F, G, H, I, J);
397tuple_sidebar!(A, B, C, D, E, F, G, H, I, J, K);
398tuple_sidebar!(A, B, C, D, E, F, G, H, I, J, K, L);
399
400impl Sidebar<()> {
401 #[doc(hidden)]
402 pub fn __tag() -> SidebarWithoutSelection {
403 SidebarWithoutSelection
404 }
405}
406
407#[doc(hidden)]
409pub struct SidebarWithoutSelection;
410
411impl SidebarWithoutSelection {
412 pub fn selection<T: PartialEq + Clone + 'static>(self, selection: Signal<T>) -> Sidebar<T> {
413 Sidebar::new(selection)
414 }
415}
416
417impl<T: PartialEq + Clone + 'static> Sidebar<T> {
418 #[doc(hidden)]
419 pub fn __children<E: SidebarEntries<T>>(self, children: impl FnOnce() -> E) -> Sidebar<T> {
420 self.children(children())
421 }
422}
423
424impl<T: 'static> SidebarSection<T> {
425 #[doc(hidden)]
426 pub fn __tag() -> SidebarSection<T> {
427 SidebarSection::untitled()
428 }
429
430 #[doc(hidden)]
431 pub fn __children<I: SidebarItems<T>>(self, items: impl FnOnce() -> I) -> SidebarSection<T> {
432 self.children(items())
433 }
434}
435
436impl SidebarItem<()> {
437 #[doc(hidden)]
439 pub fn __tag() -> SidebarItem<()> {
440 SidebarItem::new(String::new(), ())
441 }
442}
443
444impl<T: 'static> SidebarItem<T> {
445 #[doc(hidden)]
447 pub fn __children<S: IntoValue<String>>(mut self, title: impl FnOnce() -> S) -> SidebarItem<T> {
448 self.title = title().into_value();
449 self
450 }
451}