pax_runtime/
component.rs

1use std::cell::Ref;
2use std::collections::HashMap;
3use std::iter;
4use std::rc::Rc;
5
6use pax_runtime_api::{
7    borrow, borrow_mut, use_RefCell, Interpolatable, PaxValue, Property, ToPaxValue, Variable,
8};
9
10use_RefCell!();
11use crate::api::{Layer, Timeline};
12use crate::{
13    BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstanceNodePtrList,
14    InstantiationArgs, RuntimeContext,
15};
16
17/// A render node with its own runtime context.  Will push a frame
18/// to the runtime stack including the specified `slot_children` and
19/// a `PaxType` properties object.  `Component` is used at the root of
20/// applications, at the root of reusable components like `Stacker`, and
21/// in special applications like `Repeat` where it houses the `RepeatItem`
22/// properties attached to each of Repeat's virtual nodes.
23pub struct ComponentInstance {
24    pub template: InstanceNodePtrList,
25    pub timeline: Option<Rc<RefCell<Timeline>>>,
26    base: BaseInstance,
27}
28
29// #[derive(Default)]
30// pub struct ComponentProperties {
31//     pub slot_children: BTreeSet<Rc<ExpandedNode>>,
32// }
33
34impl InstanceNode for ComponentInstance {
35    fn instantiate(mut args: InstantiationArgs) -> Rc<Self> {
36        let component_template = args.component_template.take();
37        let template = component_template.unwrap_or_default();
38        let base = BaseInstance::new(
39            args,
40            InstanceFlags {
41                invisible_to_slot: false,
42                invisible_to_raycasting: true,
43                layer: Layer::DontCare,
44                is_component: true,
45                is_slot: false,
46            },
47        );
48        Rc::new(ComponentInstance {
49            base,
50            template,
51            timeline: None,
52        })
53    }
54
55    fn handle_setup_slot_children(
56        self: Rc<Self>,
57        expanded_node: &Rc<ExpandedNode>,
58        context: &Rc<RuntimeContext>,
59    ) {
60        if let Some(containing_component) = expanded_node.containing_component.upgrade() {
61            let env = if let Some(stack_frame) =
62                ScrollPosition::create_builtin_if_exists(borrow!(expanded_node.properties_scope))
63            {
64                expanded_node.stack.push(stack_frame)
65            } else {
66                Rc::clone(&expanded_node.stack)
67            };
68            let children = borrow!(self.base().get_instance_children());
69            let children_with_env = children.iter().cloned().zip(iter::repeat(env));
70            let new_slot_children = containing_component.create_children_detached(
71                children_with_env,
72                context,
73                &Rc::downgrade(expanded_node),
74            );
75            *borrow_mut!(expanded_node.expanded_slot_children) = Some(new_slot_children);
76        }
77    }
78
79    fn handle_mount(
80        self: Rc<Self>,
81        expanded_node: &Rc<ExpandedNode>,
82        context: &Rc<RuntimeContext>,
83    ) {
84        let mut properties_scope = borrow_mut!(expanded_node.properties_scope);
85        properties_scope.insert(
86            "$suspended".to_string(),
87            Variable::new_from_typed_property(expanded_node.suspended.clone()),
88        );
89        let new_env = expanded_node.stack.push(properties_scope.clone());
90        let children = borrow!(self.template);
91        let children_with_envs = children.iter().cloned().zip(iter::repeat(new_env));
92        expanded_node.children.replace_with(Property::new_with_name(
93            expanded_node.generate_children(
94                children_with_envs,
95                context,
96                &expanded_node.parent_frame,
97                true,
98            ),
99            &format!("component (node id: {})", expanded_node.id.0),
100        ));
101    }
102
103    fn handle_unmount(&self, expanded_node: &Rc<ExpandedNode>, context: &Rc<RuntimeContext>) {
104        if let Some(slot_children) = borrow_mut!(expanded_node.expanded_slot_children).take() {
105            for slot_child in slot_children {
106                slot_child.recurse_unmount(context);
107            }
108        }
109    }
110
111    fn resolve_debug(
112        &self,
113        f: &mut std::fmt::Formatter,
114        _expanded_node: Option<&ExpandedNode>,
115    ) -> std::fmt::Result {
116        f.debug_struct("Component").finish()
117    }
118
119    fn base(&self) -> &BaseInstance {
120        &self.base
121    }
122
123    fn get_template(&self) -> Option<&InstanceNodePtrList> {
124        Some(&self.template)
125    }
126}
127
128#[derive(Debug, Clone, Default)]
129pub struct ScrollPosition {
130    pub x: f64,
131    pub y: f64,
132}
133
134impl Interpolatable for ScrollPosition {
135    fn interpolate(&self, other: &Self, t: f64) -> Self {
136        ScrollPosition {
137            x: self.x + (other.x - self.x) * t,
138            y: self.y + (other.y - self.y) * t,
139        }
140    }
141}
142
143impl ToPaxValue for ScrollPosition {
144    fn to_pax_value(self) -> PaxValue {
145        PaxValue::Object(
146            vec![
147                ("x".to_string(), self.x.to_pax_value()),
148                ("y".to_string(), self.y.to_pax_value()),
149            ]
150            .into_iter()
151            .collect(),
152        )
153    }
154}
155
156impl ScrollPosition {
157    pub fn create_builtin_if_exists(
158        property_scope: Ref<HashMap<String, Variable>>,
159    ) -> Option<HashMap<String, Variable>> {
160        let scroll_pos_x: Property<f64> = Property::new_from_untyped(
161            property_scope
162                .get("scroll_pos_x")?
163                .get_untyped_property()
164                .clone(),
165        );
166        let scroll_pos_y: Property<f64> = Property::new_from_untyped(
167            property_scope
168                .get("scroll_pos_y")?
169                .get_untyped_property()
170                .clone(),
171        );
172        let deps = [scroll_pos_x.untyped(), scroll_pos_y.untyped()];
173        let scroll_position = Property::computed(
174            move || ScrollPosition {
175                x: scroll_pos_x.get(),
176                y: scroll_pos_y.get(),
177            },
178            &deps,
179        );
180
181        let scroll_position_var = Variable::new_from_typed_property(scroll_position);
182        let stack_frame = vec![("$scroll_position".to_string(), scroll_position_var)]
183            .into_iter()
184            .collect();
185        Some(stack_frame)
186    }
187}