use std::hash::Hash;
use std::rc::Rc;
use crate::element::{Element, Keyed, KeyedItem, Node, key_hash};
pub struct Lit<V>(pub V);
pub struct Dyn<F>(pub F);
pub trait IntoProp<T> {
fn into_prop(self) -> crate::element::Prop<T>;
}
#[diagnostic::on_unimplemented(
message = "`{Self}` takes no child elements",
label = "this widget takes no children",
note = "containers (`View`, `Stack`, `Card`) and components with a `children: Element` argument take children"
)]
pub trait Children: Sized {
fn __child(self, child: Element) -> Self;
}
pub fn child<B: Children>(builder: B, child: Element) -> B {
builder.__child(child)
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a value for this property, which takes `{T}`",
label = "expected a `{T}` value"
)]
pub trait PropValue<T> {
fn into_value(self) -> T;
}
impl<T> PropValue<T> for T {
fn into_value(self) -> T {
self
}
}
impl PropValue<String> for &str {
fn into_value(self) -> String {
self.to_owned()
}
}
impl PropValue<String> for &String {
fn into_value(self) -> String {
self.clone()
}
}
#[doc(hidden)]
#[derive(Clone, Copy, Debug)]
pub struct Const<V>(pub V);
impl<T, V: PropValue<T>> IntoProp<T> for Const<V> {
fn into_prop(self) -> crate::element::Prop<T> {
crate::element::Prop::Const(self.0.into_value())
}
}
impl<T, V: PropValue<T>> IntoArg<T> for Const<V> {
fn into_arg(self) -> T {
self.0.into_value()
}
}
pub trait Sited: Sized {
fn __site(self, site: &'static crate::element::Site) -> Self;
}
pub fn site<B: Sited>(builder: B, site: &'static crate::element::Site) -> B {
builder.__site(site)
}
impl<T, V: PropValue<T>> IntoProp<T> for Lit<V> {
fn into_prop(self) -> crate::element::Prop<T> {
crate::element::Prop::Static(self.0.into_value())
}
}
impl<T: 'static, V: PropValue<T>, F: FnOnce() -> V + Clone + 'static> IntoProp<T> for Dyn<F> {
fn into_prop(self) -> crate::element::Prop<T> {
let f = self.0;
crate::element::Prop::Dynamic(Rc::new(move || (f.clone())().into_value()))
}
}
pub trait IntoArg<T> {
fn into_arg(self) -> T;
}
impl<T, V: PropValue<T>> IntoArg<T> for Lit<V> {
fn into_arg(self) -> T {
self.0.into_value()
}
}
impl<T, V: PropValue<T>, F: FnOnce() -> V> IntoArg<T> for Dyn<F> {
fn into_arg(self) -> T {
(self.0)().into_value()
}
}
pub trait Build {
const REGION: bool;
fn build(self) -> Element;
}
pub fn element<B: Build, F: FnOnce() -> B + Clone + 'static>(f: F) -> Element {
if B::REGION {
Element(Node::Region(Rc::new(move || (f.clone())().build())))
} else {
f().build()
}
}
pub fn region<F: FnOnce() -> Element + Clone + 'static>(f: F) -> Element {
Element(Node::Region(Rc::new(move || (f.clone())())))
}
pub fn keyed<T, I, K, FI, FK, FR>(items: FI, key: FK, render: FR) -> Element
where
T: Clone + PartialEq + 'static,
I: IntoIterator<Item = T>,
K: Hash,
FI: FnOnce() -> I + Clone + 'static,
FK: Fn(&T) -> K + 'static,
FR: FnOnce(T) -> Element + Clone + 'static,
{
Element(Node::Keyed(Rc::new(Keyed {
items: Box::new(move || {
let mut seen = std::collections::HashMap::<u64, u32>::new();
(items.clone())()
.into_iter()
.map(|item| {
let mut k = key_hash(&key(&item));
let n = seen.entry(k).or_default();
if *n > 0 {
k = key_hash(&(k, *n));
}
*n += 1;
let render = render.clone();
KeyedItem {
key: k,
value: Rc::new(item.clone()),
same: same::<T>,
build: Box::new(move || render(item)),
}
})
.collect()
}),
})))
}
fn same<T: PartialEq + 'static>(a: &dyn std::any::Any, b: &dyn std::any::Any) -> bool {
matches!((a.downcast_ref::<T>(), b.downcast_ref::<T>()), (Some(a), Some(b)) if a == b)
}
pub fn fragment(children: Vec<Element>) -> Element {
Element::fragment(children)
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Unset;
#[derive(Debug, Clone, Copy, Default)]
pub struct Set;
pub fn reuse0(f: impl FnOnce() + Clone + 'static) -> Rc<dyn Fn()> {
Rc::new(move || (f.clone())())
}
pub fn reuse1<A: 'static>(f: impl FnOnce(A) + Clone + 'static) -> Rc<dyn Fn(A)> {
Rc::new(move |a| (f.clone())(a))
}