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