pax_std/core/
event_blocker.rs1use std::iter;
2use std::rc::Rc;
3
4use crate::common::native_surface_opacity;
5use crate::patch_if_needed;
6use pax_engine::api::{borrow, borrow_mut, use_RefCell, Color};
7use pax_engine::pax;
8use pax_message::{AnyCreatePatch, EventBlockerPatch};
9use pax_runtime::api::{Layer, Property};
10use pax_runtime::{
11 BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstantiationArgs, RuntimeContext,
12};
13use_RefCell!();
14
15#[pax]
16#[engine_import_path("pax_engine")]
17#[primitive("pax_std::core::event_blocker::EventBlockerInstance")]
18#[custom(Default)]
19pub struct EventBlocker {
24 pub background: Property<Color>,
26}
27
28impl Default for EventBlocker {
29 fn default() -> Self {
30 Self {
31 background: Property::new(Color::TRANSPARENT),
32 }
33 }
34}
35
36pub struct EventBlockerInstance {
38 base: BaseInstance,
39}
40
41impl InstanceNode for EventBlockerInstance {
42 fn instantiate(args: InstantiationArgs) -> Rc<Self>
43 where
44 Self: Sized,
45 {
46 Rc::new(Self {
47 base: BaseInstance::new(
48 args,
49 InstanceFlags {
50 invisible_to_slot: false,
51 invisible_to_raycasting: false,
52 layer: Layer::Native,
53 is_component: false,
54 is_slot: false,
55 },
56 ),
57 })
58 }
59
60 fn handle_mount(
61 self: Rc<Self>,
62 expanded_node: &Rc<ExpandedNode>,
63 context: &Rc<RuntimeContext>,
64 ) {
65 let id = expanded_node.id.clone();
66 context.enqueue_native_message(pax_message::NativeMessage::EventBlockerCreate(
67 AnyCreatePatch {
68 id: id.to_u32(),
69 parent_frame: expanded_node.parent_frame.get().map(|v| v.to_u32()),
70 render_layer_id: 0,
71 },
72 ));
73
74 let env = Rc::clone(&expanded_node.stack);
76 let children = borrow!(self.base().get_instance_children());
77 let children_with_envs = children.iter().cloned().zip(iter::repeat(env));
78
79 let new_children = expanded_node.generate_children(
80 children_with_envs,
81 context,
82 &expanded_node.parent_frame,
83 true,
84 );
85 expanded_node.children.set(new_children);
86
87 let weak_self_ref = Rc::downgrade(&expanded_node);
89 let context = Rc::clone(context);
90 let last_patch = Rc::new(RefCell::new(EventBlockerPatch {
91 id: id.to_u32(),
92 ..Default::default()
93 }));
94
95 let deps: Vec<_> = borrow!(expanded_node.properties_scope)
96 .values()
97 .cloned()
98 .map(|v| v.get_untyped_property().clone())
99 .chain([
100 expanded_node.transform_and_bounds.untyped(),
101 expanded_node.computed_opacity.untyped(),
102 expanded_node.occlusion.untyped(),
103 ])
104 .collect();
105 expanded_node
106 .changed_listener
107 .replace_with(Property::computed(
108 move || {
109 let Some(expanded_node) = weak_self_ref.upgrade() else {
110 return;
111 };
112 let id = expanded_node.id.to_u32();
113 let mut old_state = borrow_mut!(last_patch);
114
115 let mut patch = EventBlockerPatch {
116 id,
117 ..Default::default()
118 };
119 expanded_node.with_properties_unwrapped(|properties: &mut EventBlocker| {
120 let computed_tab = expanded_node.transform_and_bounds.get();
121 let (width, height) = computed_tab.bounds;
122
123 let updates = [
124 patch_if_needed(&mut old_state.size_x, &mut patch.size_x, width),
125 patch_if_needed(&mut old_state.size_y, &mut patch.size_y, height),
126 patch_if_needed(
127 &mut old_state.parent_frame,
128 &mut patch.parent_frame,
129 expanded_node.parent_frame.get().map(|v| v.to_u32()),
130 ),
131 patch_if_needed(
132 &mut old_state.z_index,
133 &mut patch.z_index,
134 expanded_node.occlusion.get().z_index,
135 ),
136 patch_if_needed(
137 &mut old_state.transform,
138 &mut patch.transform,
139 computed_tab.transform.coeffs().to_vec(),
140 ),
141 patch_if_needed(
142 &mut old_state.opacity,
143 &mut patch.opacity,
144 native_surface_opacity(&expanded_node, &context),
145 ),
146 patch_if_needed(
147 &mut old_state.background,
148 &mut patch.background,
149 (&properties.background.get()).into(),
150 ),
151 ];
152
153 if updates.into_iter().any(|v| v == true) {
154 context.enqueue_native_message(
155 pax_message::NativeMessage::EventBlockerUpdate(patch),
156 );
157 }
158 });
159 ()
160 },
161 &deps,
162 ));
163 }
164
165 fn handle_unmount(&self, expanded_node: &Rc<ExpandedNode>, context: &Rc<RuntimeContext>) {
166 let id = expanded_node.id.clone();
167 expanded_node
168 .changed_listener
169 .replace_with(Property::default());
170 context.enqueue_native_message(pax_message::NativeMessage::EventBlockerDelete(id.to_u32()));
171 }
172
173 fn resolve_debug(
174 &self,
175 f: &mut std::fmt::Formatter,
176 expanded_node: Option<&ExpandedNode>,
177 ) -> std::fmt::Result {
178 match expanded_node {
179 Some(expanded_node) => {
180 expanded_node.with_properties_unwrapped(|_f: &mut EventBlocker| {
181 f.debug_struct("EventBlocker").finish()
182 })
183 }
184 None => f.debug_struct("EventBlocker").finish_non_exhaustive(),
185 }
186 }
187
188 fn base(&self) -> &BaseInstance {
189 &self.base
190 }
191}