use std::rc::Rc;
use crate::{Column, Container};
use mitsuami_core::{
Children, Element, ElementBuilder, EventValue, Length, NodeId, Prop, TabsStyle, Ui, UiEvent, View, WidgetKind,
};
use mitsuami_reactive::{IntoValue, Signal, Value};
pub struct Tabs<T: 'static> {
element: Element,
selection: Signal<T>,
tabs: Vec<Tab<T>>,
}
impl<T: 'static> ElementBuilder for Tabs<T> {
fn element(&mut self) -> &mut Element {
&mut self.element
}
}
impl<T: PartialEq + Clone + 'static> Tabs<T> {
pub fn new(selection: Signal<T>) -> Tabs<T> {
Tabs { element: Element::new(WidgetKind::Tabs), selection, tabs: Vec::new() }
}
pub fn children(mut self, tabs: impl TabsChildren<T>) -> Tabs<T> {
tabs.add_to(&mut self.tabs);
self
}
pub fn tab(self, tab: Tab<T>) -> Tabs<T> {
self.children(tab)
}
pub fn tabs_style(mut self, style: impl IntoValue<TabsStyle>) -> Tabs<T> {
self.element.prop(style.into_value(), Prop::TabsStyle);
self
}
}
impl<T: PartialEq + Clone + 'static> View for Tabs<T> {
fn build(self, ui: &Ui) -> NodeId {
let Tabs { mut element, selection, tabs } = self;
let titles: Vec<Value<String>> = tabs.iter().map(|t| t.title.clone()).collect();
let values: Rc<Vec<T>> = Rc::new(tabs.iter().map(|t| t.value.clone()).collect());
element.prop(Value::Dynamic(Rc::new(move || titles.iter().map(|t| t.get()).collect())), Prop::TabTitles);
if tabs.iter().any(|t| t.icon.is_some()) {
let icons: Vec<Option<Value<String>>> = tabs.iter().map(|t| t.icon.clone()).collect();
element.prop(
Value::Dynamic(Rc::new(move || {
icons.iter().map(|i| i.as_ref().map(|i| i.get()).unwrap_or_default()).collect()
})),
Prop::TabIcons,
);
}
let shown = values.clone();
element.prop(
Value::Dynamic(Rc::new(move || {
let index = selection.with(|value| shown.iter().position(|v| v == value));
(!shown.is_empty()).then(|| index.unwrap_or(0))
})),
Prop::SelectedIndex,
);
element.on(move |event| {
if let UiEvent::Changed(EventValue::Index(index)) = event
&& let Some(value) = values.get(*index)
{
selection.set(value.clone());
}
});
for tab in tabs {
element.add_children(tab.page);
}
element.build(ui)
}
}
pub struct Tab<T: 'static> {
title: Value<String>,
icon: Option<Value<String>>,
value: T,
page: Container,
}
impl<T: 'static> ElementBuilder for Tab<T> {
fn element(&mut self) -> &mut Element {
self.page.element()
}
}
impl<T: 'static> Tab<T> {
pub fn new(title: impl IntoValue<String>, value: T) -> Tab<T> {
Tab { title: title.into_value(), icon: None, value, page: Column::new() }
}
pub fn title(mut self, title: impl IntoValue<String>) -> Tab<T> {
self.title = title.into_value();
self
}
pub fn icon(mut self, name: impl IntoValue<String>) -> Tab<T> {
self.icon = Some(name.into_value());
self
}
pub fn value<U: 'static>(self, value: U) -> Tab<U> {
Tab { title: self.title, icon: self.icon, value, page: self.page }
}
pub fn children(mut self, children: impl Children) -> Tab<T> {
self.page = self.page.children(children);
self
}
pub fn child(self, child: impl View) -> Tab<T> {
self.children(child)
}
pub fn gap(mut self, gap: impl IntoValue<Length>) -> Tab<T> {
self.page = self.page.gap(gap);
self
}
}
pub trait TabsChildren<T: 'static> {
fn add_to(self, tabs: &mut Vec<Tab<T>>);
}
impl<T: 'static> TabsChildren<T> for Tab<T> {
fn add_to(self, tabs: &mut Vec<Tab<T>>) {
tabs.push(self);
}
}
impl<T: 'static, C: TabsChildren<T>> TabsChildren<T> for Vec<C> {
fn add_to(self, tabs: &mut Vec<Tab<T>>) {
for tab in self {
tab.add_to(tabs);
}
}
}
impl<T: 'static, C: TabsChildren<T>> TabsChildren<T> for Option<C> {
fn add_to(self, tabs: &mut Vec<Tab<T>>) {
if let Some(tab) = self {
tab.add_to(tabs);
}
}
}
impl<T: 'static> TabsChildren<T> for () {
fn add_to(self, _: &mut Vec<Tab<T>>) {}
}
macro_rules! tuple_tabs {
($($name:ident),+) => {
impl<T: 'static, $($name: TabsChildren<T>),+> TabsChildren<T> for ($($name,)+) {
#[allow(non_snake_case)]
fn add_to(self, tabs: &mut Vec<Tab<T>>) {
let ($($name,)+) = self;
$($name.add_to(tabs);)+
}
}
};
}
tuple_tabs!(A);
tuple_tabs!(A, B);
tuple_tabs!(A, B, C);
tuple_tabs!(A, B, C, D);
tuple_tabs!(A, B, C, D, E);
tuple_tabs!(A, B, C, D, E, F);
tuple_tabs!(A, B, C, D, E, F, G);
tuple_tabs!(A, B, C, D, E, F, G, H);
tuple_tabs!(A, B, C, D, E, F, G, H, I);
tuple_tabs!(A, B, C, D, E, F, G, H, I, J);
tuple_tabs!(A, B, C, D, E, F, G, H, I, J, K);
tuple_tabs!(A, B, C, D, E, F, G, H, I, J, K, L);
impl Tabs<()> {
#[doc(hidden)]
pub fn __tag() -> TabsWithoutSelection {
TabsWithoutSelection
}
}
#[doc(hidden)]
pub struct TabsWithoutSelection;
impl TabsWithoutSelection {
pub fn selection<T: PartialEq + Clone + 'static>(self, selection: Signal<T>) -> Tabs<T> {
Tabs::new(selection)
}
}
impl<T: PartialEq + Clone + 'static> Tabs<T> {
#[doc(hidden)]
pub fn __children<C: TabsChildren<T>>(self, children: impl FnOnce() -> C) -> Tabs<T> {
self.children(children())
}
}
impl Tab<()> {
#[doc(hidden)]
pub fn __tag() -> Tab<()> {
Tab::new(String::new(), ())
}
}
impl<T: 'static> Tab<T> {
#[doc(hidden)]
pub fn __children<C: Children>(self, children: impl FnOnce() -> C) -> Tab<T> {
self.children(children())
}
}