1use std::sync::Arc;
2
3use crate::component::Component;
4use crate::types::{ComponentId, Key};
5
6pub trait WidgetPayload: Send + Sync + 'static {
11 fn as_any(&self) -> &dyn std::any::Any;
13}
14
15#[derive(Clone)]
17pub struct ComponentElement {
18 pub id: ComponentId,
19 pub key: Option<Key>,
20 pub component: Arc<dyn Component>,
22 pub children: Vec<Element>,
23}
24
25#[derive(Clone)]
27pub struct NativeElement {
28 pub tag: &'static str,
30 pub payload: Option<Arc<dyn WidgetPayload>>,
33 pub children: Vec<Element>,
34 pub key: Option<Key>,
36}
37
38#[derive(Clone)]
40pub struct TextElement {
41 pub content: String,
42}
43
44#[derive(Clone)]
49pub enum Element {
50 Component(ComponentElement),
51 Native(NativeElement),
52 Text(TextElement),
53 Empty,
54}
55
56impl Element {
57 pub fn empty() -> Self { Element::Empty }
58
59 pub fn text(content: impl Into<String>) -> Self {
60 Element::Text(TextElement { content: content.into() })
61 }
62
63 pub fn with_key(self, key: impl Into<Key>) -> Self {
65 match self {
66 Element::Native(mut n) => { n.key = Some(key.into()); Element::Native(n) }
67 Element::Component(mut c) => { c.key = Some(key.into()); Element::Component(c) }
68 other => other,
69 }
70 }
71}
72
73impl std::fmt::Debug for Element {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 match self {
76 Element::Component(c) => write!(f, "Component(id={})", c.id.0),
77 Element::Native(n) => write!(f, "Native({})", n.tag),
78 Element::Text(t) => write!(f, "Text({:?})", t.content),
79 Element::Empty => write!(f, "Empty"),
80 }
81 }
82}