use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
use std::path::PathBuf;
use std::rc::Rc;
use std::time::Duration;
use mitsuami_reactive::{IntoValue, Signal, Value, signal};
use crate::element::{Element, ElementBuilder};
use crate::list::{FileFn, KeyFn, ListHandle, Mount, MoveFn, RowParts, RowRender, Shape, TreeParts, build_rows};
use crate::services::Shortcut;
use crate::tweak::Tweak;
use crate::ui::Ui;
use crate::units::Length;
use crate::view::{AnyView, View};
use crate::widget::{ListStyle, NodeId, Prop, RowDrop, RowMoves, SelectionMode, WidgetKind};
type ChildrenFn<T> = Rc<dyn Fn(&T) -> Option<Vec<T>>>;
type LoadingFn<T> = Rc<dyn Fn(&T) -> bool>;
pub struct Tree<T: 'static = (), K: 'static = (), R = RowRender<T>> {
element: Element,
roots: Value<Vec<T>>,
key: KeyFn<T, K>,
children: ChildrenFn<T>,
render: R,
expanded: Option<Signal<Vec<K>>>,
mode: Option<Value<SelectionMode>>,
style: Option<ListStyle>,
selected: Option<Signal<Vec<K>>>,
on_activate: Option<Rc<dyn Fn(K)>>,
estimate: Option<f32>,
handle: Option<ListHandle<K>>,
files: Option<FileFn<T>>,
moves: Option<(RowMoves, MoveFn<K>)>,
loading: Option<LoadingFn<T>>,
}
impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static> Tree<T, K> {
pub fn new<V: View>(
roots: impl IntoValue<Vec<T>>,
key: impl Fn(&T) -> K + 'static,
children: impl Fn(&T) -> Option<Vec<T>> + 'static,
render: impl Fn(T) -> V + 'static,
) -> Tree<T, K> {
Tree::with_element(
Element::new(WidgetKind::Tree),
roots.into_value(),
Rc::new(key),
Rc::new(children),
RowRender::new(move |item| AnyView::new(render(item))),
)
}
}
impl<T: 'static, K: 'static, R> Tree<T, K, R> {
fn with_element(
element: Element,
roots: Value<Vec<T>>,
key: KeyFn<T, K>,
children: ChildrenFn<T>,
render: R,
) -> Tree<T, K, R> {
Tree {
element,
roots,
key,
children,
render,
expanded: None,
mode: None,
style: None,
selected: None,
on_activate: None,
estimate: None,
handle: None,
files: None,
moves: None,
loading: None,
}
}
pub fn on_move(mut self, moves: RowMoves, handler: impl Fn(Vec<K>, RowDrop<K>) + 'static) -> Self {
self.moves = Some((moves, Rc::new(handler)));
self
}
pub fn loading(mut self, loading: impl Fn(&T) -> bool + 'static) -> Self {
self.loading = Some(Rc::new(loading));
self
}
pub fn expanded(mut self, expanded: Signal<Vec<K>>) -> Self {
self.expanded = Some(expanded);
self
}
pub fn drag_files(mut self, file: impl Fn(&T) -> Option<PathBuf> + 'static) -> Self {
self.files = Some(Rc::new(file));
self
}
pub fn selected(mut self, selected: Signal<Vec<K>>) -> Self {
self.selected = Some(selected);
self
}
pub fn selection_mode(mut self, mode: impl IntoValue<SelectionMode>) -> Self {
self.mode = Some(mode.into_value());
self
}
pub fn list_style(mut self, style: ListStyle) -> Self {
self.style = Some(style);
self
}
pub fn on_activate(mut self, handler: impl Fn(K) + 'static) -> Self {
self.on_activate = Some(Rc::new(handler));
self
}
pub fn on_key(mut self, key: impl Into<Shortcut>, handler: impl Fn() + 'static) -> Self {
self.element.on_key(key.into(), handler);
self
}
pub fn estimated_row_height(mut self, height: impl Into<Length>) -> Self {
self.estimate = match height.into() {
Length::Px(px) => Some(px),
_ => None,
};
self
}
pub fn handle(mut self, handle: ListHandle<K>) -> Self {
self.handle = Some(handle);
self
}
pub fn native(mut self, tweak: Tweak<Tree>) -> Self {
tweak.apply(&mut self.element);
self
}
}
impl<T: 'static, K: 'static, R> ElementBuilder for Tree<T, K, R> {
fn element(&mut self) -> &mut Element {
&mut self.element
}
}
const SPINNER_DELAY: Duration = Duration::from_millis(500);
struct Delayed<T: 'static, K: 'static> {
ui: Ui,
key: KeyFn<T, K>,
loading: LoadingFn<T>,
since: RefCell<HashMap<K, (Duration, u64)>>,
walk: Cell<u64>,
due: Signal<u64>,
}
impl<T, K: Eq + Hash + Clone> Delayed<T, K> {
fn shows(&self, item: &T) -> bool {
if !(self.loading)(item) {
return false;
}
self.due.get();
let now = self.ui.executor().now();
let mut since = self.since.borrow_mut();
let walk = self.walk.get();
let entry = since.entry((self.key)(item)).or_insert_with(|| {
let (ui, due) = (self.ui.clone(), self.due);
self.ui.spawn_local(async move {
ui.sleep(SPINNER_DELAY).await;
if due.is_alive() {
due.update(|n| *n += 1);
}
});
(now, walk)
});
entry.1 = walk;
now.saturating_sub(entry.0) >= SPINNER_DELAY
}
fn walk<R>(&self, f: impl FnOnce() -> R) -> R {
let walk = self.walk.get() + 1;
self.walk.set(walk);
let out = f();
self.since.borrow_mut().retain(|_, (_, seen)| *seen == walk);
out
}
}
#[derive(Clone)]
struct Flat<T> {
item: T,
shape: Shape,
}
fn flatten<T: Clone, K: Eq + Hash>(
roots: Vec<T>,
key: &dyn Fn(&T) -> K,
children: &dyn Fn(&T) -> Option<Vec<T>>,
loading: Option<&dyn Fn(&T) -> bool>,
open: &HashSet<K>,
) -> Vec<Flat<T>> {
struct Walk<'a, T, K> {
key: &'a dyn Fn(&T) -> K,
children: &'a dyn Fn(&T) -> Option<Vec<T>>,
loading: Option<&'a dyn Fn(&T) -> bool>,
open: &'a HashSet<K>,
folders: Vec<K>,
out: Vec<Flat<T>>,
}
impl<T: Clone, K: Eq + Hash> Walk<'_, T, K> {
fn visit(&mut self, items: Vec<T>, parent: Option<usize>, depth: u32) {
for item in items {
let k = (self.key)(&item);
let kids = (self.children)(&item);
let expandable = kids.is_some();
let expanded = expandable && self.open.contains(&k) && !self.folders.contains(&k);
let loading = self.loading.is_some_and(|loading| loading(&item));
let index = self.out.len();
self.out.push(Flat { item, shape: Shape { parent, depth, expandable, expanded, loading } });
if let (true, Some(kids)) = (expanded, kids) {
self.folders.push(k);
self.visit(kids, Some(index), depth + 1);
self.folders.pop();
}
}
}
}
let mut walk = Walk { key, children, loading, open, folders: Vec::new(), out: Vec::new() };
walk.visit(roots, None, 0);
walk.out
}
impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static> View for Tree<T, K> {
fn build(self, ui: &Ui) -> NodeId {
let Tree {
element,
roots,
key,
children,
render,
expanded,
mode,
style,
selected,
on_activate,
estimate,
handle,
files,
moves,
loading,
} = self;
let expanded = expanded.unwrap_or_else(|| signal(Vec::new()));
let loads = loading.is_some();
let delayed = loading.map(|loading| Delayed {
ui: ui.clone(),
key: key.clone(),
loading,
since: RefCell::default(),
walk: Cell::new(0),
due: signal(0),
});
let each = {
let (key, children) = (key.clone(), children.clone());
Value::Dynamic(Rc::new(move || {
let open: HashSet<K> = expanded.get().into_iter().collect();
match &delayed {
Some(delayed) => {
let shows = |item: &T| delayed.shows(item);
delayed.walk(|| flatten(roots.get(), &*key, &*children, Some(&shows), &open))
}
None => flatten(roots.get(), &*key, &*children, None, &open),
}
}))
};
let mount: Mount<Flat<T>> = Rc::new(move |ui: &Ui, row, flat: Flat<T>| {
let host = ui.create(WidgetKind::Container, vec![Prop::Row(row)]);
let content = render.render(flat.item).build(ui);
ui.append_child(host, content);
vec![host]
});
let tree = TreeParts {
shape: Rc::new(|flat: &Flat<T>| flat.shape),
on_expand: Rc::new(move |k: K, open: bool| {
let has = expanded.with_untracked(|keys| keys.contains(&k));
match (open, has) {
(true, false) => expanded.update(|keys| keys.push(k)),
(false, true) => expanded.update(|keys| keys.retain(|o| *o != k)),
_ => {}
}
}),
loading: loads,
};
let item_key = key.clone();
let parts = RowParts {
each,
key: Rc::new(move |flat: &Flat<T>| item_key(&flat.item)),
mount,
mode,
style,
selected,
on_activate,
estimate,
handle,
files: files.map(|file| Rc::new(move |flat: &Flat<T>| file(&flat.item)) as FileFn<Flat<T>>),
moves,
tree: Some(tree),
};
build_rows(ui, element, parts)
}
}
impl Tree<(), (), ()> {
#[doc(hidden)]
pub fn __tag() -> TreeWithoutRoots {
TreeWithoutRoots(Element::new(WidgetKind::Tree))
}
}
#[doc(hidden)]
pub struct TreeWithoutRoots(Element);
impl TreeWithoutRoots {
pub fn roots<T: Clone + 'static>(self, roots: impl IntoValue<Vec<T>>) -> TreeWithoutKey<T> {
TreeWithoutKey { element: self.0, roots: roots.into_value() }
}
}
impl ElementBuilder for TreeWithoutRoots {
fn element(&mut self) -> &mut Element {
&mut self.0
}
}
#[doc(hidden)]
pub struct TreeWithoutKey<T: 'static> {
element: Element,
roots: Value<Vec<T>>,
}
impl<T: Clone + 'static> TreeWithoutKey<T> {
pub fn key<K: Eq + Hash + Clone + 'static>(self, key: impl Fn(&T) -> K + 'static) -> TreeWithoutChildren<T, K> {
TreeWithoutChildren { element: self.element, roots: self.roots, key: Rc::new(key) }
}
}
impl<T: 'static> ElementBuilder for TreeWithoutKey<T> {
fn element(&mut self) -> &mut Element {
&mut self.element
}
}
#[doc(hidden)]
pub struct TreeWithoutChildren<T: 'static, K: 'static> {
element: Element,
roots: Value<Vec<T>>,
key: KeyFn<T, K>,
}
impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static> TreeWithoutChildren<T, K> {
pub fn children(self, children: impl Fn(&T) -> Option<Vec<T>> + 'static) -> Tree<T, K, ()> {
Tree::with_element(self.element, self.roots, self.key, Rc::new(children), ())
}
}
impl<T: 'static, K: 'static> ElementBuilder for TreeWithoutChildren<T, K> {
fn element(&mut self) -> &mut Element {
&mut self.element
}
}
impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static> Tree<T, K, ()> {
#[doc(hidden)]
pub fn __children<V: View>(self, render: impl Fn(T) -> V + 'static) -> Tree<T, K> {
let Tree {
element,
roots,
key,
children,
expanded,
mode,
style,
selected,
on_activate,
estimate,
handle,
files,
moves,
loading,
..
} = self;
Tree {
element,
roots,
key,
children,
render: RowRender::new(move |item| AnyView::new(render(item))),
expanded,
mode,
style,
selected,
on_activate,
estimate,
handle,
files,
moves,
loading,
}
}
}