1use std::rc::Rc;
4
5use crate::ui::Ui;
6use crate::widget::{NodeId, Prop, WidgetKind};
7
8#[diagnostic::on_unimplemented(
13 message = "`{Self}` is not a view",
14 note = "a `view!` tag whose type has `()` or `Without…` in it is missing a required attribute: `props=…` for custom widgets, `when=…` for `Show`, or a component prop"
15)]
16pub trait View: 'static {
17 fn build(self, ui: &Ui) -> NodeId;
18}
19
20pub struct AnyView(Box<dyn FnOnce(&Ui) -> NodeId>);
22
23impl AnyView {
24 pub fn new(view: impl View) -> AnyView {
25 AnyView(Box::new(move |ui| view.build(ui)))
26 }
27}
28
29impl View for AnyView {
30 fn build(self, ui: &Ui) -> NodeId {
31 (self.0)(ui)
32 }
33}
34
35impl View for &'static str {
36 fn build(self, ui: &Ui) -> NodeId {
37 ui.create(WidgetKind::Text, vec![Prop::Text(self.to_owned())])
38 }
39}
40
41impl View for String {
42 fn build(self, ui: &Ui) -> NodeId {
43 ui.create(WidgetKind::Text, vec![Prop::Text(self)])
44 }
45}
46
47#[diagnostic::on_unimplemented(
50 message = "`{Self}` can't be children",
51 note = "children are views, or tuples, `Vec`s and `Option`s of views",
52 note = "a `view!` tag whose type has `()` or `Without…` in it is missing a required attribute: `props=…` for custom widgets, `when=…` for `Show`, or a component prop"
53)]
54pub trait Children {
55 fn into_views(self, out: &mut Vec<AnyView>);
56}
57
58impl<V: View> Children for V {
59 fn into_views(self, out: &mut Vec<AnyView>) {
60 out.push(AnyView::new(self));
61 }
62}
63
64impl<V: View> Children for Vec<V> {
65 fn into_views(self, out: &mut Vec<AnyView>) {
66 out.extend(self.into_iter().map(AnyView::new));
67 }
68}
69
70impl<V: View> Children for Option<V> {
71 fn into_views(self, out: &mut Vec<AnyView>) {
72 out.extend(self.map(AnyView::new));
73 }
74}
75
76impl Children for () {
77 fn into_views(self, _: &mut Vec<AnyView>) {}
78}
79
80macro_rules! tuple_children {
81 ($($name:ident),+) => {
82 impl<$($name: Children),+> Children for ($($name,)+) {
83 #[allow(non_snake_case)]
84 fn into_views(self, out: &mut Vec<AnyView>) {
85 let ($($name,)+) = self;
86 $($name.into_views(out);)+
87 }
88 }
89 };
90}
91
92tuple_children!(A);
93tuple_children!(A, B);
94tuple_children!(A, B, C);
95tuple_children!(A, B, C, D);
96tuple_children!(A, B, C, D, E);
97tuple_children!(A, B, C, D, E, F);
98tuple_children!(A, B, C, D, E, F, G);
99tuple_children!(A, B, C, D, E, F, G, H);
100tuple_children!(A, B, C, D, E, F, G, H, I);
101tuple_children!(A, B, C, D, E, F, G, H, I, J);
102tuple_children!(A, B, C, D, E, F, G, H, I, J, K);
103tuple_children!(A, B, C, D, E, F, G, H, I, J, K, L);
104
105#[derive(Default)]
108pub struct Slot(Vec<AnyView>);
109
110impl Slot {
111 pub fn new(children: impl Children) -> Slot {
112 let mut views = Vec::new();
113 children.into_views(&mut views);
114 Slot(views)
115 }
116
117 pub fn is_empty(&self) -> bool {
118 self.0.is_empty()
119 }
120}
121
122impl Children for Slot {
123 fn into_views(self, out: &mut Vec<AnyView>) {
124 out.extend(self.0);
125 }
126}
127
128pub struct Callback<T: 'static>(Rc<dyn Fn(T)>);
132
133impl<T: 'static> Callback<T> {
134 pub fn new(handler: impl Fn(T) + 'static) -> Callback<T> {
135 Callback(Rc::new(handler))
136 }
137
138 pub fn call(&self, value: T) {
139 (self.0)(value)
140 }
141}
142
143impl<T: 'static> Clone for Callback<T> {
144 fn clone(&self) -> Self {
145 Callback(self.0.clone())
146 }
147}
148
149impl<T: 'static> Default for Callback<T> {
150 fn default() -> Self {
151 Callback::new(|_| {})
152 }
153}
154
155impl<T: 'static, F: Fn(T) + 'static> From<F> for Callback<T> {
156 fn from(handler: F) -> Callback<T> {
157 Callback::new(handler)
158 }
159}