use std::rc::Rc;
use crate::ui::Ui;
use crate::widget::{NodeId, Prop, WidgetKind};
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a view",
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"
)]
pub trait View: 'static {
fn build(self, ui: &Ui) -> NodeId;
}
pub struct AnyView(Box<dyn FnOnce(&Ui) -> NodeId>);
impl AnyView {
pub fn new(view: impl View) -> AnyView {
AnyView(Box::new(move |ui| view.build(ui)))
}
}
impl View for AnyView {
fn build(self, ui: &Ui) -> NodeId {
(self.0)(ui)
}
}
impl View for &'static str {
fn build(self, ui: &Ui) -> NodeId {
ui.create(WidgetKind::Text, vec![Prop::Text(self.to_owned())])
}
}
impl View for String {
fn build(self, ui: &Ui) -> NodeId {
ui.create(WidgetKind::Text, vec![Prop::Text(self)])
}
}
#[diagnostic::on_unimplemented(
message = "`{Self}` can't be children",
note = "children are views, or tuples, `Vec`s and `Option`s of views",
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"
)]
pub trait Children {
fn into_views(self, out: &mut Vec<AnyView>);
}
impl<V: View> Children for V {
fn into_views(self, out: &mut Vec<AnyView>) {
out.push(AnyView::new(self));
}
}
impl<V: View> Children for Vec<V> {
fn into_views(self, out: &mut Vec<AnyView>) {
out.extend(self.into_iter().map(AnyView::new));
}
}
impl<V: View> Children for Option<V> {
fn into_views(self, out: &mut Vec<AnyView>) {
out.extend(self.map(AnyView::new));
}
}
impl Children for () {
fn into_views(self, _: &mut Vec<AnyView>) {}
}
macro_rules! tuple_children {
($($name:ident),+) => {
impl<$($name: Children),+> Children for ($($name,)+) {
#[allow(non_snake_case)]
fn into_views(self, out: &mut Vec<AnyView>) {
let ($($name,)+) = self;
$($name.into_views(out);)+
}
}
};
}
tuple_children!(A);
tuple_children!(A, B);
tuple_children!(A, B, C);
tuple_children!(A, B, C, D);
tuple_children!(A, B, C, D, E);
tuple_children!(A, B, C, D, E, F);
tuple_children!(A, B, C, D, E, F, G);
tuple_children!(A, B, C, D, E, F, G, H);
tuple_children!(A, B, C, D, E, F, G, H, I);
tuple_children!(A, B, C, D, E, F, G, H, I, J);
tuple_children!(A, B, C, D, E, F, G, H, I, J, K);
tuple_children!(A, B, C, D, E, F, G, H, I, J, K, L);
#[derive(Default)]
pub struct Slot(Vec<AnyView>);
impl Slot {
pub fn new(children: impl Children) -> Slot {
let mut views = Vec::new();
children.into_views(&mut views);
Slot(views)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl Children for Slot {
fn into_views(self, out: &mut Vec<AnyView>) {
out.extend(self.0);
}
}
pub struct Callback<T: 'static>(Rc<dyn Fn(T)>);
impl<T: 'static> Callback<T> {
pub fn new(handler: impl Fn(T) + 'static) -> Callback<T> {
Callback(Rc::new(handler))
}
pub fn call(&self, value: T) {
(self.0)(value)
}
}
impl<T: 'static> Clone for Callback<T> {
fn clone(&self) -> Self {
Callback(self.0.clone())
}
}
impl<T: 'static> Default for Callback<T> {
fn default() -> Self {
Callback::new(|_| {})
}
}
impl<T: 'static, F: Fn(T) + 'static> From<F> for Callback<T> {
fn from(handler: F) -> Callback<T> {
Callback::new(handler)
}
}