use std::cell::Cell;
use std::rc::Rc;
use mitsuami_core::{
DocumentTabsStyle, Element, ElementBuilder, EventValue, NodeId, Prop, Tweak, Ui, UiEvent, View, WidgetKind,
};
use mitsuami_reactive::{IntoValue, Signal, Value, effect, signal};
pub struct DocumentTabs<T: 'static> {
element: Element,
selection: Signal<T>,
tabs: Vec<DocumentTab<T>>,
dynamic: Option<Box<dyn Fn() -> Vec<DocumentTab<T>>>>,
close: Option<Rc<dyn Fn(T)>>,
reorder: Option<Rc<dyn Fn(T, usize)>>,
new_tab: Option<Rc<dyn Fn()>>,
}
impl<T: 'static> ElementBuilder for DocumentTabs<T> {
fn element(&mut self) -> &mut Element {
&mut self.element
}
}
impl<T: PartialEq + Clone + 'static> DocumentTabs<T> {
pub fn new(selection: Signal<T>) -> DocumentTabs<T> {
DocumentTabs {
element: Element::new(WidgetKind::DocumentTabs),
selection,
tabs: Vec::new(),
dynamic: None,
close: None,
reorder: None,
new_tab: None,
}
}
pub fn children(mut self, tabs: impl DocumentTabsChildren<T>) -> DocumentTabs<T> {
tabs.add_to(&mut self.tabs);
self
}
pub fn tab(self, tab: DocumentTab<T>) -> DocumentTabs<T> {
self.children(tab)
}
pub fn children_with<E: DocumentTabsChildren<T>>(mut self, tabs: impl Fn() -> E + 'static) -> DocumentTabs<T> {
self.dynamic = Some(Box::new(move || {
let mut built = Vec::new();
tabs().add_to(&mut built);
built
}));
self
}
pub fn on_close(mut self, close: impl Fn(T) + 'static) -> DocumentTabs<T> {
self.close = Some(Rc::new(close));
self
}
pub fn on_reorder(mut self, reorder: impl Fn(T, usize) + 'static) -> DocumentTabs<T> {
self.reorder = Some(Rc::new(reorder));
self
}
pub fn on_new_tab(mut self, new_tab: impl Fn() + 'static) -> DocumentTabs<T> {
self.new_tab = Some(Rc::new(new_tab));
self
}
pub fn tabs_style(mut self, style: impl IntoValue<DocumentTabsStyle>) -> DocumentTabs<T> {
self.element.prop(style.into_value(), Prop::DocumentTabsStyle);
self
}
pub fn native(mut self, tweak: Tweak<DocumentTabs<T>>) -> DocumentTabs<T> {
tweak.apply(&mut self.element);
self
}
}
impl<T: PartialEq + Clone + 'static> View for DocumentTabs<T> {
fn build(self, ui: &Ui) -> NodeId {
let DocumentTabs { mut element, selection, tabs, dynamic, close, reorder, new_tab } = self;
if close.is_some() {
element.prop(Value::Static(true), Prop::TabsClosable);
}
if reorder.is_some() {
element.prop(Value::Static(true), Prop::TabsReorderable);
}
if new_tab.is_some() {
element.prop(Value::Static(true), Prop::NewTabButton);
}
let values: Signal<Vec<T>> = signal(Vec::new());
let value_at = move |index: usize| values.with_untracked(|v| v.get(index).cloned());
element.on(move |event| match event {
UiEvent::Changed(EventValue::Index(index)) => {
if let Some(value) = value_at(*index) {
selection.set(value);
}
}
UiEvent::TabCloseRequested(index) => {
if let (Some(close), Some(value)) = (&close, value_at(*index)) {
close(value);
}
}
UiEvent::TabMoved { from, to } => {
let Some(value) = value_at(*from) else { return };
let mut now = values.get_untracked();
if *to >= now.len() {
return;
}
let moved = now.remove(*from);
now.insert(*to, moved);
values.set(now);
if let Some(reorder) = &reorder {
reorder(value, *to);
}
}
UiEvent::NewTabRequested => {
if let Some(new_tab) = &new_tab {
new_tab();
}
}
_ => {}
});
let strip = element.build(ui);
let titled = ui.clone();
effect(move || {
let built = dynamic.as_ref().map(|tabs| tabs()).unwrap_or_default();
let all: Vec<&DocumentTab<T>> = tabs.iter().chain(&built).collect();
let now: Vec<T> = all.iter().map(|t| t.value.clone()).collect();
if values.with_untracked(|v| *v != now) {
values.set(now);
}
titled.set_prop(strip, Prop::TabTitles(all.iter().map(|t| t.title.get()).collect()));
});
let last = Rc::new(Cell::new(0));
let chosen = ui.clone();
effect(move || {
let index = values.with(|v| {
if v.is_empty() {
return None;
}
Some(selection.with(|value| v.iter().position(|v| v == value)).unwrap_or_else(|| {
let index = last.get().min(v.len() - 1);
selection.set(v[index].clone());
index
}))
});
if let Some(index) = index {
last.set(index);
}
chosen.set_prop(strip, Prop::SelectedIndex(index));
});
strip
}
}
pub struct DocumentTab<T: 'static> {
title: Value<String>,
value: T,
}
impl<T: 'static> DocumentTab<T> {
pub fn new(title: impl IntoValue<String>, value: T) -> DocumentTab<T> {
DocumentTab { title: title.into_value(), value }
}
pub fn title(mut self, title: impl IntoValue<String>) -> DocumentTab<T> {
self.title = title.into_value();
self
}
pub fn value<U: 'static>(self, value: U) -> DocumentTab<U> {
DocumentTab { title: self.title, value }
}
}
pub trait DocumentTabsChildren<T: 'static> {
fn add_to(self, tabs: &mut Vec<DocumentTab<T>>);
}
impl<T: 'static> DocumentTabsChildren<T> for DocumentTab<T> {
fn add_to(self, tabs: &mut Vec<DocumentTab<T>>) {
tabs.push(self);
}
}
impl<T: 'static, C: DocumentTabsChildren<T>> DocumentTabsChildren<T> for Vec<C> {
fn add_to(self, tabs: &mut Vec<DocumentTab<T>>) {
for tab in self {
tab.add_to(tabs);
}
}
}
impl<T: 'static, C: DocumentTabsChildren<T>> DocumentTabsChildren<T> for Option<C> {
fn add_to(self, tabs: &mut Vec<DocumentTab<T>>) {
if let Some(tab) = self {
tab.add_to(tabs);
}
}
}
impl<T: 'static> DocumentTabsChildren<T> for () {
fn add_to(self, _: &mut Vec<DocumentTab<T>>) {}
}
macro_rules! tuple_document_tabs {
($($name:ident),+) => {
impl<T: 'static, $($name: DocumentTabsChildren<T>),+> DocumentTabsChildren<T> for ($($name,)+) {
#[allow(non_snake_case)]
fn add_to(self, tabs: &mut Vec<DocumentTab<T>>) {
let ($($name,)+) = self;
$($name.add_to(tabs);)+
}
}
};
}
tuple_document_tabs!(A);
tuple_document_tabs!(A, B);
tuple_document_tabs!(A, B, C);
tuple_document_tabs!(A, B, C, D);
tuple_document_tabs!(A, B, C, D, E);
tuple_document_tabs!(A, B, C, D, E, F);
tuple_document_tabs!(A, B, C, D, E, F, G);
tuple_document_tabs!(A, B, C, D, E, F, G, H);
impl DocumentTabs<()> {
#[doc(hidden)]
pub fn __tag() -> DocumentTabsWithoutSelection {
DocumentTabsWithoutSelection
}
}
#[doc(hidden)]
pub struct DocumentTabsWithoutSelection;
impl DocumentTabsWithoutSelection {
pub fn selection<T: PartialEq + Clone + 'static>(self, selection: Signal<T>) -> DocumentTabs<T> {
DocumentTabs::new(selection)
}
}
impl<T: PartialEq + Clone + 'static> DocumentTabs<T> {
#[doc(hidden)]
pub fn __children<C: DocumentTabsChildren<T>>(self, tabs: impl FnOnce() -> C) -> DocumentTabs<T> {
self.children(tabs())
}
}
impl DocumentTab<()> {
#[doc(hidden)]
pub fn __tag() -> DocumentTab<()> {
DocumentTab::new(String::new(), ())
}
}
impl<T: 'static> DocumentTab<T> {
#[doc(hidden)]
pub fn __children<S: IntoValue<String>>(mut self, title: impl FnOnce() -> S) -> DocumentTab<T> {
self.title = title().into_value();
self
}
}