pax_std/core/
liquid_glass.rs1use pax_engine::api::LocalProperty;
2use pax_engine::api::{Axis, Property};
3use pax_engine::pax;
4use pax_runtime::api::{borrow, Color, Layer, NativeLiquidGlassScope, Numeric, Size};
5use pax_runtime::{
6 BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstantiationArgs, RuntimeContext,
7};
8use std::iter;
9use std::rc::Rc;
10
11#[pax]
13#[engine_import_path("pax_engine")]
14#[custom(Default)]
15#[primitive("pax_std::core::liquid_glass::LiquidGlassInstance")]
16pub struct LiquidGlass {
17 pub enabled: Property<bool>,
19 pub spacing: Property<Size>,
21 pub interactive: Property<bool>,
23 pub tint: Property<Option<Color>>,
25 pub variant: Property<String>,
27}
28
29impl Default for LiquidGlass {
30 fn default() -> Self {
31 Self {
32 enabled: Property::new(true),
33 spacing: Property::new(Size::Pixels(Numeric::F64(0.0))),
34 interactive: Property::new(false),
35 tint: Property::new(None),
36 variant: Property::new("regular".to_string()),
37 }
38 }
39}
40
41pub struct LiquidGlassInstance {
42 base: BaseInstance,
43}
44
45impl LiquidGlassInstance {
46 fn scope_property(
47 expanded_node: &Rc<ExpandedNode>,
48 ) -> LocalProperty<Option<NativeLiquidGlassScope>> {
49 let (enabled, spacing, interactive, tint, variant) = expanded_node
50 .with_properties_unwrapped(|liquid_glass: &mut LiquidGlass| {
51 (
52 liquid_glass.enabled.clone(),
53 liquid_glass.spacing.clone(),
54 liquid_glass.interactive.clone(),
55 liquid_glass.tint.clone(),
56 liquid_glass.variant.clone(),
57 )
58 });
59 let transform_and_bounds = expanded_node.transform_and_bounds.clone();
60 let group_id = expanded_node.id.to_u32();
61 let deps = [
62 enabled.untyped(),
63 spacing.untyped(),
64 interactive.untyped(),
65 tint.untyped(),
66 variant.untyped(),
67 transform_and_bounds.untyped(),
68 ];
69
70 LocalProperty::computed(
71 move || {
72 if !enabled.get() {
73 return None;
74 }
75 let bounds = transform_and_bounds.get().bounds;
76 let spacing = spacing.get().evaluate(bounds, Axis::X).max(0.0);
77 Some(NativeLiquidGlassScope {
78 group_id,
79 spacing,
80 interactive: interactive.get(),
81 tint: tint.get(),
82 variant: variant.get(),
83 })
84 },
85 &deps,
86 )
87 }
88
89 fn mount_children_with_scope(
90 self: Rc<Self>,
91 expanded_node: &Rc<ExpandedNode>,
92 context: &Rc<RuntimeContext>,
93 recurse_control_flow: bool,
94 ) {
95 let env = Rc::clone(&expanded_node.stack);
96 let children = borrow!(self.base().get_instance_children());
97 let children_with_envs = children.iter().cloned().zip(iter::repeat(env));
98 let scope = Self::scope_property(expanded_node);
99 let children = expanded_node.generate_children_with_liquid_glass_scope(
100 children_with_envs,
101 context,
102 &expanded_node.parent_frame,
103 &scope,
104 true,
105 );
106 if recurse_control_flow {
107 for child in children.iter() {
108 child.recurse_control_flow_expansion(context);
109 }
110 }
111 expanded_node.children.set(children);
112 }
113}
114
115impl InstanceNode for LiquidGlassInstance {
116 fn instantiate(args: InstantiationArgs) -> Rc<Self>
117 where
118 Self: Sized,
119 {
120 Rc::new(Self {
121 base: BaseInstance::new(
122 args,
123 InstanceFlags {
124 invisible_to_slot: false,
125 invisible_to_raycasting: true,
126 layer: Layer::DontCare,
127 is_component: false,
128 is_slot: false,
129 },
130 ),
131 })
132 }
133
134 fn handle_mount(
135 self: Rc<Self>,
136 expanded_node: &Rc<ExpandedNode>,
137 context: &Rc<RuntimeContext>,
138 ) {
139 self.mount_children_with_scope(expanded_node, context, false);
140 }
141
142 fn handle_control_flow_node_expansion(
143 self: Rc<Self>,
144 expanded_node: &Rc<ExpandedNode>,
145 context: &Rc<RuntimeContext>,
146 ) {
147 self.mount_children_with_scope(expanded_node, context, true);
148 }
149
150 fn resolve_debug(
151 &self,
152 f: &mut std::fmt::Formatter,
153 expanded_node: Option<&ExpandedNode>,
154 ) -> std::fmt::Result {
155 match expanded_node {
156 Some(expanded_node) => {
157 expanded_node.with_properties_unwrapped(|_g: &mut LiquidGlass| {
158 f.debug_struct("LiquidGlass").finish()
159 })
160 }
161 None => f.debug_struct("LiquidGlass").finish_non_exhaustive(),
162 }
163 }
164
165 fn base(&self) -> &BaseInstance {
166 &self.base
167 }
168}