1use pax_engine::api::LocalProperty;
2use pax_message::{AnyCreatePatch, NativeInterrupt, SliderPatch};
3use pax_runtime::api::{use_RefCell, Layer, Property};
4use pax_runtime::{
5 BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstantiationArgs, RuntimeContext,
6};
7
8use_RefCell!();
9use pax_runtime::api::*;
10
11use pax_engine::pax;
12use std::rc::Rc;
13
14use crate::common::{native_surface_opacity, patch_if_needed, patch_liquid_glass_if_needed};
15
16#[pax]
18#[engine_import_path("pax_engine")]
19#[primitive("pax_std::forms::slider::SliderInstance")]
20#[custom(Default)]
21pub struct Slider {
22 pub background: Property<Color>,
24 pub accent: Property<Color>,
26 pub corner_radius: Property<f64>,
28 pub value: Property<f64>,
30 pub step: Property<f64>,
32 pub min: Property<f64>,
34 pub max: Property<f64>,
36}
37
38impl Default for Slider {
39 fn default() -> Self {
40 Self {
41 value: Property::new(0.0),
42 step: Property::new(1.0),
43 min: Property::new(0.0),
44 max: Property::new(100.0),
45 accent: Property::new(Color::rgb(27.into(), 100.into(), 242.into())),
46 corner_radius: Property::new(5.0),
47 background: Property::new(Color::rgb(229.into(), 231.into(), 235.into())),
48 }
49 }
50}
51
52pub struct SliderInstance {
54 base: BaseInstance,
55 native_value_to_ack: Rc<RefCell<Option<f64>>>,
56}
57
58fn slider_values_equal(lhs: f64, rhs: f64) -> bool {
59 (lhs - rhs).abs() <= 0.000001
60}
61
62impl InstanceNode for SliderInstance {
63 fn instantiate(args: InstantiationArgs) -> Rc<Self>
64 where
65 Self: Sized,
66 {
67 Rc::new(Self {
68 base: BaseInstance::new(
69 args,
70 InstanceFlags {
71 invisible_to_slot: false,
72 invisible_to_raycasting: false,
73 layer: Layer::Native,
74 is_component: false,
75 is_slot: false,
76 },
77 ),
78 native_value_to_ack: Rc::new(RefCell::new(None)),
79 })
80 }
81
82 fn handle_mount(
83 self: Rc<Self>,
84 expanded_node: &Rc<ExpandedNode>,
85 context: &Rc<RuntimeContext>,
86 ) {
87 let id = expanded_node.id.clone();
89 context.enqueue_native_message(pax_message::NativeMessage::SliderCreate(AnyCreatePatch {
90 id: id.to_u32(),
91 parent_frame: expanded_node.parent_frame.get().map(|v| v.to_u32()),
92 render_layer_id: 0,
93 }));
94
95 let weak_self_ref = Rc::downgrade(&expanded_node);
97 let context = Rc::clone(context);
98 let last_patch = Rc::new(RefCell::new(SliderPatch {
99 id: id.to_u32(),
100 ..Default::default()
101 }));
102 let native_value_to_ack = self.native_value_to_ack.clone();
103
104 let deps: Vec<_> = borrow_mut!(expanded_node.properties_scope)
105 .values()
106 .cloned()
107 .map(|v| v.get_untyped_property().clone())
108 .chain([
109 expanded_node.transform_and_bounds.untyped(),
110 expanded_node.computed_opacity.untyped(),
111 expanded_node.occlusion.untyped(),
112 expanded_node.liquid_glass_scope.untyped(),
113 ])
114 .collect();
115 expanded_node
116 .changed_listener
117 .replace_with(LocalProperty::computed(
118 move || {
119 let Some(expanded_node) = weak_self_ref.upgrade() else {
120 return;
121 };
122 let id = expanded_node.id.clone();
123 let mut old_state = borrow_mut!(last_patch);
124
125 let mut patch = SliderPatch {
126 id: id.to_u32(),
127 ..Default::default()
128 };
129 expanded_node.with_properties_unwrapped(|properties: &mut Slider| {
130 let computed_tab = expanded_node.transform_and_bounds.get();
131 let (width, height) = computed_tab.bounds;
132 let current_value = properties.value.local().get();
133 let pending_native_value = *native_value_to_ack.borrow();
134 let value_updated = if pending_native_value
135 .is_some_and(|value| slider_values_equal(value, current_value))
136 {
137 old_state.value = Some(current_value);
138 *borrow_mut!(native_value_to_ack) = None;
139 false
140 } else {
141 patch_if_needed(&mut old_state.value, &mut patch.value, current_value)
142 };
143 let updates = [
144 patch_if_needed(&mut old_state.size_x, &mut patch.size_x, width),
145 patch_if_needed(&mut old_state.size_y, &mut patch.size_y, height),
146 patch_if_needed(
147 &mut old_state.parent_frame,
148 &mut patch.parent_frame,
149 expanded_node.parent_frame.get().map(|v| v.to_u32()),
150 ),
151 patch_if_needed(
152 &mut old_state.z_index,
153 &mut patch.z_index,
154 expanded_node.occlusion.get().z_index,
155 ),
156 patch_if_needed(
157 &mut old_state.transform,
158 &mut patch.transform,
159 computed_tab.transform.coeffs().to_vec(),
160 ),
161 patch_if_needed(
162 &mut old_state.accent,
163 &mut patch.accent,
164 (&properties.accent.local().get()).into(),
165 ),
166 value_updated,
167 patch_if_needed(
168 &mut old_state.step,
169 &mut patch.step,
170 properties.step.local().get(),
171 ),
172 patch_if_needed(
173 &mut old_state.min,
174 &mut patch.min,
175 properties.min.local().get(),
176 ),
177 patch_if_needed(
178 &mut old_state.max,
179 &mut patch.max,
180 properties.max.local().get(),
181 ),
182 patch_if_needed(
183 &mut old_state.corner_radius,
184 &mut patch.corner_radius,
185 properties.corner_radius.local().get(),
186 ),
187 patch_if_needed(
188 &mut old_state.background,
189 &mut patch.background,
190 (&properties.background.local().get()).into(),
191 ),
192 patch_if_needed(
193 &mut old_state.opacity,
194 &mut patch.opacity,
195 native_surface_opacity(&expanded_node, &context),
196 ),
197 patch_liquid_glass_if_needed(
198 &mut old_state.liquid_glass,
199 &mut patch.liquid_glass,
200 &expanded_node,
201 ),
202 ];
203 if updates.into_iter().any(|v| v == true) {
204 context.enqueue_native_message(
205 pax_message::NativeMessage::SliderUpdate(patch),
206 );
207 }
208 });
209 ()
210 },
211 &deps,
212 ));
213 }
214
215 fn handle_unmount(&self, expanded_node: &Rc<ExpandedNode>, context: &Rc<RuntimeContext>) {
216 let id = expanded_node.id.clone();
217 expanded_node
218 .changed_listener
219 .replace_with(LocalProperty::default());
220 context.enqueue_native_message(pax_message::NativeMessage::SliderDelete(id.to_u32()));
221 }
222
223 fn base(&self) -> &BaseInstance {
224 &self.base
225 }
226
227 fn resolve_debug(
228 &self,
229 f: &mut std::fmt::Formatter,
230 _expanded_node: Option<&ExpandedNode>,
231 ) -> std::fmt::Result {
232 f.debug_struct("Slider").finish_non_exhaustive()
233 }
234
235 fn property_requires_occlusion_recompute(&self, _property_name: &str) -> bool {
236 false
237 }
238
239 fn handle_native_interrupt(
240 &self,
241 expanded_node: &Rc<ExpandedNode>,
242 interrupt: &NativeInterrupt,
243 ) {
244 if let NativeInterrupt::FormSliderChange(args) = interrupt {
245 *borrow_mut!(self.native_value_to_ack) = Some(args.value);
246 expanded_node
247 .with_properties_unwrapped(|props: &mut Slider| props.value.set(args.value));
248 }
249 }
250}