use std::sync::Arc;
use crate::component::Component;
use crate::types::{ComponentId, Key};
pub trait WidgetPayload: Send + Sync + 'static {
fn as_any(&self) -> &dyn std::any::Any;
}
#[derive(Clone)]
pub struct ComponentElement {
pub id: ComponentId,
pub key: Option<Key>,
pub component: Arc<dyn Component>,
pub children: Vec<Element>,
}
#[derive(Clone)]
pub struct NativeElement {
pub tag: &'static str,
pub payload: Option<Arc<dyn WidgetPayload>>,
pub children: Vec<Element>,
pub key: Option<Key>,
}
#[derive(Clone)]
pub struct TextElement {
pub content: String,
}
#[derive(Clone)]
pub enum Element {
Component(ComponentElement),
Native(NativeElement),
Text(TextElement),
Empty,
}
impl Element {
pub fn empty() -> Self { Element::Empty }
pub fn text(content: impl Into<String>) -> Self {
Element::Text(TextElement { content: content.into() })
}
pub fn with_key(self, key: impl Into<Key>) -> Self {
match self {
Element::Native(mut n) => { n.key = Some(key.into()); Element::Native(n) }
Element::Component(mut c) => { c.key = Some(key.into()); Element::Component(c) }
other => other,
}
}
}
impl std::fmt::Debug for Element {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Element::Component(c) => write!(f, "Component(id={})", c.id.0),
Element::Native(n) => write!(f, "Native({})", n.tag),
Element::Text(t) => write!(f, "Text({:?})", t.content),
Element::Empty => write!(f, "Empty"),
}
}
}