Skip to main content

mitsuami_core/
view.rs

1//! Views: descriptions of UI that build nodes when mounted.
2
3use std::rc::Rc;
4
5use crate::ui::Ui;
6use crate::widget::{NodeId, Prop, WidgetKind};
7
8/// Something that can build itself into the node tree.
9///
10/// Component functions run once and return a `View`. Building creates the
11/// nodes and the effects that keep their props up to date.
12#[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
20/// A type-erased [`View`].
21pub 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/// Anything that can be a list of children: a view, a tuple of views, a
48/// `Vec`, an `Option`, or `()`.
49#[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/// The children a component receives: what goes between its tags. Pass it
106/// on as children (`Column::new().children(children)`); it builds once.
107#[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
128/// A component's event, Vue's `emit`: `on_change: Callback<f32>` in a
129/// `#[component]` is set with `@change=…` and fired with `on_change.call(v)`.
130/// The default does nothing.
131pub 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}