1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use crate::{view::Empty, View};
use std::rc::Rc;

/// Builder for a [`View`].
///
/// [`View`] is implemented for anything that implements this trait.
pub trait ViewBuilder: 'static {
    fn build(&self) -> impl View;
}

impl ViewBuilder for () {
    fn build(&self) -> impl View {
        Empty
    }
}

impl<V: ViewBuilder> ViewBuilder for Rc<V> {
    fn build(&self) -> impl View {
        (&**self).build()
    }
}

macro_rules! impl_string_view {
    ($t:ty) => {
        impl ViewBuilder for $t {
            fn build(&self) -> impl View {
                let cx = crate::hook::use_context::<crate::TextViewContext>().unwrap();
                let mut view = cx.view.borrow_mut();
                view(self.clone().into())
            }
        }
    };
}

impl_string_view!(&'static str);
impl_string_view!(String);