pax_runtime/engine/
node_interface.rs

1use std::rc::Rc;
2
3use pax_manifest::UniqueTemplateNodeIdentifier;
4use pax_runtime_api::Property;
5use pax_runtime_api::{borrow, pax_value::ToFromPaxAny, Interpolatable};
6
7use crate::{
8    api::{math::Space, Window},
9    ExpandedNode, LayoutProperties, TransformAndBounds,
10};
11use crate::{ExpandedNodeIdentifier, InstanceFlags};
12
13impl Interpolatable for NodeInterface {}
14
15impl PartialEq for NodeInterface {
16    fn eq(&self, other: &Self) -> bool {
17        self.inner.id.eq(&other.inner.id)
18    }
19}
20
21impl Eq for NodeInterface {}
22
23impl PartialOrd for NodeInterface {
24    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
25        self.inner.id.partial_cmp(&other.inner.id)
26    }
27}
28
29impl Ord for NodeInterface {
30    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
31        self.inner.id.cmp(&other.inner.id)
32    }
33}
34
35#[derive(Clone)]
36pub struct NodeInterface {
37    inner: Rc<ExpandedNode>,
38}
39
40impl From<Rc<ExpandedNode>> for NodeInterface {
41    fn from(expanded_node: Rc<ExpandedNode>) -> Self {
42        Self {
43            inner: expanded_node,
44        }
45    }
46}
47
48impl std::fmt::Debug for NodeInterface {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        write!(f, "NodeInterface({:?})", self.inner)
51    }
52}
53
54pub struct NodeLocal;
55
56impl Space for NodeLocal {}
57
58impl NodeInterface {
59    pub fn global_id(&self) -> Option<UniqueTemplateNodeIdentifier> {
60        let instance_node = borrow!(self.inner.instance_node);
61        let base = instance_node.base();
62        base.template_node_identifier.clone()
63    }
64
65    pub fn engine_id(&self) -> ExpandedNodeIdentifier {
66        self.inner.id.clone()
67    }
68
69    pub fn layout_properties(&self) -> LayoutProperties {
70        self.inner.layout_properties().get()
71    }
72
73    pub fn auto_size(&self) -> Option<(f64, f64)> {
74        self.inner.rendered_size.get()
75    }
76
77    pub fn with_properties<V, T: ToFromPaxAny>(&self, f: impl FnOnce(&mut T) -> V) -> Option<V> {
78        self.inner.try_with_properties_unwrapped(|tp: &mut T| f(tp))
79    }
80
81    pub fn is_of_type<T: ToFromPaxAny>(&self) -> bool {
82        self.inner
83            .try_with_properties_unwrapped::<T, _>(|_| ())
84            .is_some()
85    }
86
87    pub fn instance_flags(&self) -> InstanceFlags {
88        let instance_node = borrow!(self.inner.instance_node);
89        let base = instance_node.base();
90        base.flags().clone()
91    }
92
93    pub fn transform_and_bounds(&self) -> Property<TransformAndBounds<NodeLocal, Window>> {
94        self.inner.transform_and_bounds.clone()
95    }
96
97    pub fn render_parent(&self) -> Option<NodeInterface> {
98        let parent = borrow!(self.inner.render_parent);
99        Some(parent.upgrade()?.into())
100    }
101
102    pub fn containing_component(&self) -> Option<NodeInterface> {
103        Some(self.inner.containing_component.upgrade()?.into())
104    }
105
106    pub fn template_parent(&self) -> Option<NodeInterface> {
107        Some(self.inner.template_parent.upgrade()?.into())
108    }
109
110    pub fn is_descendant_of(&self, node: &NodeInterface) -> bool {
111        self.inner.is_descendant_of(&node.inner.id)
112    }
113
114    pub fn children(&self) -> Vec<NodeInterface> {
115        let children = borrow!(self.inner.mounted_children);
116        (&*children)
117            .into_iter()
118            .map(Rc::clone)
119            .map(Into::into)
120            .collect()
121    }
122
123    pub fn flattened_slot_children_count(&self) -> Property<usize> {
124        self.inner.flattened_slot_children_count.clone()
125    }
126}