pax_std/media/
youtube_video.rs1use pax_engine::api::LocalProperty;
2use pax_message::AnyCreatePatch;
3use pax_runtime::api::{Layer, Property};
4use pax_runtime::{
5 BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstantiationArgs, RuntimeContext,
6};
7use_RefCell!();
8use pax_engine::pax;
9use pax_runtime::api::*;
10
11use std::rc::Rc;
12
13use crate::common::{native_surface_opacity, patch_if_needed};
14
15#[pax]
17#[engine_import_path("pax_engine")]
18#[primitive("pax_std::media::youtube_video::YoutubeVideoInstance")]
19pub struct YoutubeVideo {
20 pub url: Property<String>,
22}
23
24pub struct YoutubeVideoInstance {
26 base: BaseInstance,
27}
28
29impl InstanceNode for YoutubeVideoInstance {
30 fn instantiate(args: InstantiationArgs) -> Rc<Self>
31 where
32 Self: Sized,
33 {
34 Rc::new(Self {
35 base: BaseInstance::new(
36 args,
37 InstanceFlags {
38 invisible_to_slot: false,
39 invisible_to_raycasting: false,
40 layer: Layer::Native,
41 is_component: false,
42 is_slot: false,
43 },
44 ),
45 })
46 }
47
48 fn handle_mount(
49 self: Rc<Self>,
50 expanded_node: &Rc<ExpandedNode>,
51 context: &Rc<RuntimeContext>,
52 ) {
53 let id = expanded_node.id.to_u32();
54 context.enqueue_native_message(pax_message::NativeMessage::YoutubeVideoCreate(
55 AnyCreatePatch {
56 id,
57 parent_frame: expanded_node.parent_frame.get().map(|v| v.to_u32()),
58 render_layer_id: 0,
59 },
60 ));
61 let weak_self_ref = Rc::downgrade(&expanded_node);
62 let context = Rc::clone(context);
63 let last_patch = Rc::new(RefCell::new(YoutubeVideoPatch {
64 id,
65 ..Default::default()
66 }));
67
68 let deps: Vec<_> = borrow!(expanded_node.properties_scope)
69 .values()
70 .cloned()
71 .map(|v| v.get_untyped_property().clone())
72 .chain([
73 expanded_node.transform_and_bounds.untyped(),
74 expanded_node.computed_opacity.untyped(),
75 expanded_node.occlusion.untyped(),
76 ])
77 .collect();
78 expanded_node
79 .changed_listener
80 .replace_with(LocalProperty::computed(
81 move || {
82 let Some(expanded_node) = weak_self_ref.upgrade() else {
83 return;
84 };
85 let mut old_state = borrow_mut!(last_patch);
86
87 let mut patch = YoutubeVideoPatch {
88 id,
89 ..Default::default()
90 };
91 expanded_node.with_properties_unwrapped(|properties: &mut YoutubeVideo| {
92 let computed_tab = expanded_node.transform_and_bounds.get();
93 let (width, height) = computed_tab.bounds;
94 let updates = [
95 patch_if_needed(
96 &mut old_state.url,
97 &mut patch.url,
98 properties.url.local().get(),
99 ),
100 patch_if_needed(&mut old_state.size_x, &mut patch.size_x, width),
101 patch_if_needed(&mut old_state.size_y, &mut patch.size_y, height),
102 patch_if_needed(
103 &mut old_state.parent_frame,
104 &mut patch.parent_frame,
105 expanded_node.parent_frame.get().map(|v| v.to_u32()),
106 ),
107 patch_if_needed(
108 &mut old_state.z_index,
109 &mut patch.z_index,
110 expanded_node.occlusion.get().z_index,
111 ),
112 patch_if_needed(
113 &mut old_state.transform,
114 &mut patch.transform,
115 computed_tab.transform.coeffs().to_vec(),
116 ),
117 patch_if_needed(
118 &mut old_state.opacity,
119 &mut patch.opacity,
120 native_surface_opacity(&expanded_node, &context),
121 ),
122 ];
123 if updates.into_iter().any(|v| v == true) {
124 context.enqueue_native_message(
125 pax_message::NativeMessage::YoutubeVideoUpdate(patch),
126 );
127 }
128 });
129 ()
130 },
131 &deps,
132 ));
133 }
134
135 fn handle_unmount(&self, expanded_node: &Rc<ExpandedNode>, context: &Rc<RuntimeContext>) {
136 let id = expanded_node.id.clone();
137 expanded_node
138 .changed_listener
139 .replace_with(LocalProperty::default());
140 context.enqueue_native_message(pax_message::NativeMessage::YoutubeVideoDelete(id.to_u32()));
141 }
142
143 fn base(&self) -> &BaseInstance {
144 &self.base
145 }
146
147 fn property_requires_occlusion_recompute(&self, _property_name: &str) -> bool {
148 false
149 }
150
151 fn resolve_debug(
152 &self,
153 f: &mut std::fmt::Formatter,
154 _expanded_node: Option<&ExpandedNode>,
155 ) -> std::fmt::Result {
156 f.debug_struct("YoutubeVideo").finish_non_exhaustive()
157 }
158}