1use std::hash::Hash;
11use std::rc::Rc;
12
13use crate::element::{Element, Keyed, KeyedItem, Node, key_hash};
14
15pub struct Lit<V>(pub V);
17
18pub struct Dyn<F>(pub F);
21
22pub trait IntoProp<T> {
24 fn into_prop(self) -> crate::element::Prop<T>;
26}
27
28#[diagnostic::on_unimplemented(
31 message = "`{Self}` takes no child elements",
32 label = "this widget takes no children",
33 note = "containers (`View`, `Stack`, `Card`) and components with a `children: Element` argument take children"
34)]
35pub trait Children: Sized {
36 fn __child(self, child: Element) -> Self;
38}
39
40pub fn child<B: Children>(builder: B, child: Element) -> B {
42 builder.__child(child)
43}
44
45#[diagnostic::on_unimplemented(
50 message = "`{Self}` is not a value for this property, which takes `{T}`",
51 label = "expected a `{T}` value"
52)]
53pub trait PropValue<T> {
54 fn into_value(self) -> T;
56}
57
58impl<T> PropValue<T> for T {
59 fn into_value(self) -> T {
60 self
61 }
62}
63
64impl PropValue<String> for &str {
65 fn into_value(self) -> String {
66 self.to_owned()
67 }
68}
69
70impl PropValue<String> for &String {
71 fn into_value(self) -> String {
72 self.clone()
73 }
74}
75
76#[doc(hidden)]
79#[derive(Clone, Copy, Debug)]
80pub struct Const<V>(pub V);
81
82impl<T, V: PropValue<T>> IntoProp<T> for Const<V> {
83 fn into_prop(self) -> crate::element::Prop<T> {
84 crate::element::Prop::Const(self.0.into_value())
85 }
86}
87
88impl<T, V: PropValue<T>> IntoArg<T> for Const<V> {
89 fn into_arg(self) -> T {
90 self.0.into_value()
91 }
92}
93
94pub trait Sited: Sized {
97 fn __site(self, site: &'static crate::element::Site) -> Self;
99}
100
101pub fn site<B: Sited>(builder: B, site: &'static crate::element::Site) -> B {
103 builder.__site(site)
104}
105
106impl<T, V: PropValue<T>> IntoProp<T> for Lit<V> {
107 fn into_prop(self) -> crate::element::Prop<T> {
108 crate::element::Prop::Static(self.0.into_value())
109 }
110}
111
112impl<T: 'static, V: PropValue<T>, F: FnOnce() -> V + Clone + 'static> IntoProp<T> for Dyn<F> {
113 fn into_prop(self) -> crate::element::Prop<T> {
114 let f = self.0;
115 crate::element::Prop::Dynamic(Rc::new(move || (f.clone())().into_value()))
116 }
117}
118
119pub trait IntoArg<T> {
122 fn into_arg(self) -> T;
124}
125
126impl<T, V: PropValue<T>> IntoArg<T> for Lit<V> {
127 fn into_arg(self) -> T {
128 self.0.into_value()
129 }
130}
131
132impl<T, V: PropValue<T>, F: FnOnce() -> V> IntoArg<T> for Dyn<F> {
133 fn into_arg(self) -> T {
134 (self.0)().into_value()
135 }
136}
137
138pub trait Build {
140 const REGION: bool;
144
145 fn build(self) -> Element;
147}
148
149pub fn element<B: Build, F: FnOnce() -> B + Clone + 'static>(f: F) -> Element {
151 if B::REGION {
152 Element(Node::Region(Rc::new(move || (f.clone())().build())))
153 } else {
154 f().build()
155 }
156}
157
158pub fn region<F: FnOnce() -> Element + Clone + 'static>(f: F) -> Element {
160 Element(Node::Region(Rc::new(move || (f.clone())())))
161}
162
163pub fn keyed<T, I, K, FI, FK, FR>(items: FI, key: FK, render: FR) -> Element
167where
168 T: Clone + PartialEq + 'static,
169 I: IntoIterator<Item = T>,
170 K: Hash,
171 FI: FnOnce() -> I + Clone + 'static,
172 FK: Fn(&T) -> K + 'static,
173 FR: FnOnce(T) -> Element + Clone + 'static,
174{
175 Element(Node::Keyed(Rc::new(Keyed {
176 items: Box::new(move || {
177 let mut seen = std::collections::HashMap::<u64, u32>::new();
178 (items.clone())()
179 .into_iter()
180 .map(|item| {
181 let mut k = key_hash(&key(&item));
182 let n = seen.entry(k).or_default();
184 if *n > 0 {
185 k = key_hash(&(k, *n));
186 }
187 *n += 1;
188 let render = render.clone();
189 KeyedItem {
190 key: k,
191 value: Rc::new(item.clone()),
192 same: same::<T>,
193 build: Box::new(move || render(item)),
194 }
195 })
196 .collect()
197 }),
198 })))
199}
200
201fn same<T: PartialEq + 'static>(a: &dyn std::any::Any, b: &dyn std::any::Any) -> bool {
202 matches!((a.downcast_ref::<T>(), b.downcast_ref::<T>()), (Some(a), Some(b)) if a == b)
203}
204
205pub fn fragment(children: Vec<Element>) -> Element {
207 Element::fragment(children)
208}
209
210#[derive(Debug, Clone, Copy, Default)]
212pub struct Unset;
213
214#[derive(Debug, Clone, Copy, Default)]
216pub struct Set;
217
218pub fn reuse0(f: impl FnOnce() + Clone + 'static) -> Rc<dyn Fn()> {
220 Rc::new(move || (f.clone())())
221}
222
223pub fn reuse1<A: 'static>(f: impl FnOnce(A) + Clone + 'static) -> Rc<dyn Fn(A)> {
225 Rc::new(move |a| (f.clone())(a))
226}