use crate::view::any_element::AnyElement;
use crate::view::context::ElementContext;
use crate::view::text_element::Text;
use taffy::NodeId;
pub trait Element: 'static {
fn request_layout(&mut self, cx: &mut ElementContext) -> NodeId;
fn prepaint(&mut self, cx: &mut ElementContext);
fn paint(&mut self, cx: &mut ElementContext);
}
pub trait IntoElement {
fn into_element(self) -> AnyElement;
}
impl<E: Element> IntoElement for E {
fn into_element(self) -> AnyElement {
AnyElement::new(self)
}
}
impl IntoElement for AnyElement {
fn into_element(self) -> AnyElement {
self
}
}
impl IntoElement for &'static str {
fn into_element(self) -> AnyElement {
AnyElement::new(Text::new(self))
}
}
impl IntoElement for String {
fn into_element(self) -> AnyElement {
AnyElement::new(Text::new(self))
}
}
pub trait ParentElement: Sized {
fn extend_children(&mut self, children: impl IntoIterator<Item = AnyElement>);
fn child(mut self, child: impl IntoElement) -> Self {
self.extend_children([child.into_element()]);
self
}
fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self {
self.extend_children(children.into_iter().map(IntoElement::into_element));
self
}
}