diff --git a/pax-compiler/templates/cartridge_generation/macros.tera b/pax-compiler/templates/cartridge_generation/macros.tera index e3aabbe93..67d3780c4 100644 --- a/pax-compiler/templates/cartridge_generation/macros.tera +++ b/pax-compiler/templates/cartridge_generation/macros.tera @@ -68,7 +68,9 @@ impl ComponentFactory for {{component.pascal_identifier}}Factory { fn build_component_handlers(&self, handlers: Vec<(String, Vec)>) -> Rc> { let mut handler_registry = HandlerRegistry::default(); for (event, functions) in &handlers { - handler_registry.handlers.insert(event.clone(), functions.iter().map(|fn_name| self.build_handler(&fn_name)).collect()); + handler_registry.handlers.insert(event.clone(), functions.iter().map(|fn_name| { + Handler::new_component_handler(self.build_handler(&fn_name)) + }).collect()); } Rc::new(RefCell::new(handler_registry)) } @@ -78,7 +80,7 @@ impl ComponentFactory for {{component.pascal_identifier}}Factory { let mut handler_registry_mut = handler_registry.borrow_mut(); for (event, fn_name) in &handlers { let handler_vec = handler_registry_mut.handlers.entry(event.clone()).or_insert(Vec::new()); - handler_vec.push(self.build_handler(&fn_name)); + handler_vec.push(Handler::new_inline_handler(self.build_handler(&fn_name))); } } handler_registry diff --git a/pax-runtime/src/engine/expanded_node.rs b/pax-runtime/src/engine/expanded_node.rs index 0808fc31b..c2a98fd75 100644 --- a/pax-runtime/src/engine/expanded_node.rs +++ b/pax-runtime/src/engine/expanded_node.rs @@ -23,8 +23,8 @@ use crate::api::{ }; use crate::{ - compute_tab, ComponentInstance, InstanceNode, InstanceNodePtr, PropertiesComputable, - RuntimeContext, RuntimePropertiesStackFrame, TransformAndBounds, + compute_tab, ComponentInstance, HandlerLocation, InstanceNode, InstanceNodePtr, + PropertiesComputable, RuntimeContext, RuntimePropertiesStackFrame, TransformAndBounds, }; pub struct ExpandedNode { @@ -123,8 +123,13 @@ macro_rules! dispatch_event_handler { let borrowed_registry = &(*registry).borrow(); if let Some(handlers) = borrowed_registry.handlers.get($handler_key) { handlers.iter().for_each(|handler| { - handler( - Rc::clone(&component_properties), + let properties = if let HandlerLocation::Component = &handler.location { + Rc::clone(&self.properties) + } else { + Rc::clone(&component_properties) + }; + (handler.function)( + Rc::clone(&properties), &context, Some(Box::new(args.clone()) as Box), ); @@ -275,7 +280,7 @@ impl ExpandedNode { .get("tick") .unwrap_or(&Vec::new()) { - handler( + (handler.function)( Rc::clone(&self.properties), &self.get_node_context(context), None, @@ -293,7 +298,7 @@ impl ExpandedNode { .get("pre_render") .unwrap_or(&Vec::new()) { - handler( + (handler.function)( Rc::clone(&self.properties), &self.get_node_context(context), None, @@ -319,7 +324,7 @@ impl ExpandedNode { .get("mount") .unwrap_or(&Vec::new()) { - handler( + (handler.function)( Rc::clone(&self.properties), &self.get_node_context(context), None, diff --git a/pax-runtime/src/engine/mod.rs b/pax-runtime/src/engine/mod.rs index e8a2674d9..c67f0ef2e 100644 --- a/pax-runtime/src/engine/mod.rs +++ b/pax-runtime/src/engine/mod.rs @@ -80,9 +80,38 @@ impl PropertiesComputable for CommonProperties { } } +pub enum HandlerLocation { + Inline, + Component, +} + +pub struct Handler { + pub function: fn(Rc>, &NodeContext, Option>), + pub location: HandlerLocation, +} + +impl Handler { + pub fn new_inline_handler( + function: fn(Rc>, &NodeContext, Option>), + ) -> Self { + Handler { + function, + location: HandlerLocation::Inline, + } + } + + pub fn new_component_handler( + function: fn(Rc>, &NodeContext, Option>), + ) -> Self { + Handler { + function, + location: HandlerLocation::Component, + } + } +} + pub struct HandlerRegistry { - pub handlers: - HashMap>, &NodeContext, Option>)>>, + pub handlers: HashMap>, } impl Default for HandlerRegistry {