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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::rc::Rc;

use pax_manifest::UniqueTemplateNodeIdentifier;
use pax_runtime_api::Property;
use pax_runtime_api::{borrow, pax_value::ToFromPaxAny, Interpolatable};

use crate::{
    api::{math::Space, Window},
    ExpandedNode, LayoutProperties, TransformAndBounds,
};

impl Interpolatable for NodeInterface {}

impl PartialEq for NodeInterface {
    fn eq(&self, other: &Self) -> bool {
        self.inner.id.eq(&other.inner.id)
    }
}

impl Eq for NodeInterface {}

impl PartialOrd for NodeInterface {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.inner.id.partial_cmp(&other.inner.id)
    }
}

impl Ord for NodeInterface {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.inner.id.cmp(&other.inner.id)
    }
}

#[derive(Clone)]
pub struct NodeInterface {
    inner: Rc<ExpandedNode>,
}

impl From<Rc<ExpandedNode>> for NodeInterface {
    fn from(expanded_node: Rc<ExpandedNode>) -> Self {
        Self {
            inner: expanded_node,
        }
    }
}

impl std::fmt::Debug for NodeInterface {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "NodeInterface({:?})", self.inner)
    }
}

pub struct NodeLocal;

impl Space for NodeLocal {}

impl NodeInterface {
    pub fn global_id(&self) -> Option<UniqueTemplateNodeIdentifier> {
        let instance_node = borrow!(self.inner.instance_node);
        let base = instance_node.base();
        base.template_node_identifier.clone()
    }

    pub fn layout_properties(&self) -> LayoutProperties {
        self.inner.layout_properties().get()
    }

    pub fn auto_size(&self) -> Option<(f64, f64)> {
        self.inner.rendered_size.get()
    }

    pub fn with_properties<V, T: ToFromPaxAny>(&self, f: impl FnOnce(&mut T) -> V) -> Option<V> {
        self.inner.try_with_properties_unwrapped(|tp: &mut T| f(tp))
    }

    pub fn is_of_type<T: ToFromPaxAny>(&self) -> bool {
        self.inner
            .try_with_properties_unwrapped::<T, _>(|_| ())
            .is_some()
    }

    pub fn transform_and_bounds(&self) -> Property<TransformAndBounds<NodeLocal, Window>> {
        self.inner.transform_and_bounds.clone()
    }

    pub fn render_parent(&self) -> Option<NodeInterface> {
        let parent = borrow!(self.inner.render_parent);
        Some(parent.upgrade()?.into())
    }

    pub fn containing_component(&self) -> Option<NodeInterface> {
        Some(self.inner.containing_component.upgrade()?.into())
    }

    pub fn template_parent(&self) -> Option<NodeInterface> {
        Some(self.inner.template_parent.upgrade()?.into())
    }

    pub fn is_descendant_of(&self, node: &NodeInterface) -> bool {
        self.inner.is_descendant_of(&node.inner.id)
    }

    pub fn children(&self) -> Vec<NodeInterface> {
        let children = borrow!(self.inner.mounted_children);
        (&*children)
            .into_iter()
            .map(Rc::clone)
            .map(Into::into)
            .collect()
    }

    pub fn flattened_slot_children_count(&self) -> Property<usize> {
        self.inner.flattened_slot_children_count.clone()
    }
}