use pax_message::{AnyCreatePatch, NativeInterrupt, SliderPatch};
use pax_runtime::api::{use_RefCell, Layer, Property};
use pax_runtime::{
BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstantiationArgs, RuntimeContext,
};
use_RefCell!();
use pax_runtime::api::*;
use pax_engine::pax;
use std::rc::Rc;
use crate::common::{native_surface_opacity, patch_if_needed, patch_liquid_glass_if_needed};
#[pax]
#[engine_import_path("pax_engine")]
#[primitive("pax_std::forms::slider::SliderInstance")]
#[custom(Default)]
pub struct Slider {
pub background: Property<Color>,
pub accent: Property<Color>,
pub corner_radius: Property<f64>,
pub value: Property<f64>,
pub step: Property<f64>,
pub min: Property<f64>,
pub max: Property<f64>,
}
impl Default for Slider {
fn default() -> Self {
Self {
value: Property::new(0.0),
step: Property::new(1.0),
min: Property::new(0.0),
max: Property::new(100.0),
accent: Property::new(Color::rgb(27.into(), 100.into(), 242.into())),
corner_radius: Property::new(5.0),
background: Property::new(Color::rgb(229.into(), 231.into(), 235.into())),
}
}
}
pub struct SliderInstance {
base: BaseInstance,
native_value_to_ack: Rc<RefCell<Option<f64>>>,
}
fn slider_values_equal(lhs: f64, rhs: f64) -> bool {
(lhs - rhs).abs() <= 0.000001
}
impl InstanceNode for SliderInstance {
fn instantiate(args: InstantiationArgs) -> Rc<Self>
where
Self: Sized,
{
Rc::new(Self {
base: BaseInstance::new(
args,
InstanceFlags {
invisible_to_slot: false,
invisible_to_raycasting: false,
layer: Layer::Native,
is_component: false,
is_slot: false,
},
),
native_value_to_ack: Rc::new(RefCell::new(None)),
})
}
fn handle_mount(
self: Rc<Self>,
expanded_node: &Rc<ExpandedNode>,
context: &Rc<RuntimeContext>,
) {
let id = expanded_node.id.clone();
context.enqueue_native_message(pax_message::NativeMessage::SliderCreate(AnyCreatePatch {
id: id.to_u32(),
parent_frame: expanded_node.parent_frame.get().map(|v| v.to_u32()),
render_layer_id: 0,
}));
let weak_self_ref = Rc::downgrade(&expanded_node);
let context = Rc::clone(context);
let last_patch = Rc::new(RefCell::new(SliderPatch {
id: id.to_u32(),
..Default::default()
}));
let native_value_to_ack = self.native_value_to_ack.clone();
let deps: Vec<_> = borrow_mut!(expanded_node.properties_scope)
.values()
.cloned()
.map(|v| v.get_untyped_property().clone())
.chain([
expanded_node.transform_and_bounds.untyped(),
expanded_node.computed_opacity.untyped(),
expanded_node.occlusion.untyped(),
expanded_node.liquid_glass_scope.untyped(),
])
.collect();
expanded_node
.changed_listener
.replace_with(Property::computed(
move || {
let Some(expanded_node) = weak_self_ref.upgrade() else {
return;
};
let id = expanded_node.id.clone();
let mut old_state = borrow_mut!(last_patch);
let mut patch = SliderPatch {
id: id.to_u32(),
..Default::default()
};
expanded_node.with_properties_unwrapped(|properties: &mut Slider| {
let computed_tab = expanded_node.transform_and_bounds.get();
let (width, height) = computed_tab.bounds;
let current_value = properties.value.get();
let pending_native_value = *native_value_to_ack.borrow();
let value_updated = if pending_native_value
.is_some_and(|value| slider_values_equal(value, current_value))
{
old_state.value = Some(current_value);
*borrow_mut!(native_value_to_ack) = None;
false
} else {
patch_if_needed(&mut old_state.value, &mut patch.value, current_value)
};
let updates = [
patch_if_needed(&mut old_state.size_x, &mut patch.size_x, width),
patch_if_needed(&mut old_state.size_y, &mut patch.size_y, height),
patch_if_needed(
&mut old_state.parent_frame,
&mut patch.parent_frame,
expanded_node.parent_frame.get().map(|v| v.to_u32()),
),
patch_if_needed(
&mut old_state.z_index,
&mut patch.z_index,
expanded_node.occlusion.get().z_index,
),
patch_if_needed(
&mut old_state.transform,
&mut patch.transform,
computed_tab.transform.coeffs().to_vec(),
),
patch_if_needed(
&mut old_state.accent,
&mut patch.accent,
(&properties.accent.get()).into(),
),
value_updated,
patch_if_needed(
&mut old_state.step,
&mut patch.step,
properties.step.get(),
),
patch_if_needed(
&mut old_state.min,
&mut patch.min,
properties.min.get(),
),
patch_if_needed(
&mut old_state.max,
&mut patch.max,
properties.max.get(),
),
patch_if_needed(
&mut old_state.corner_radius,
&mut patch.corner_radius,
properties.corner_radius.get(),
),
patch_if_needed(
&mut old_state.background,
&mut patch.background,
(&properties.background.get()).into(),
),
patch_if_needed(
&mut old_state.opacity,
&mut patch.opacity,
native_surface_opacity(&expanded_node, &context),
),
patch_liquid_glass_if_needed(
&mut old_state.liquid_glass,
&mut patch.liquid_glass,
&expanded_node,
),
];
if updates.into_iter().any(|v| v == true) {
context.enqueue_native_message(
pax_message::NativeMessage::SliderUpdate(patch),
);
}
});
()
},
&deps,
));
}
fn handle_unmount(&self, expanded_node: &Rc<ExpandedNode>, context: &Rc<RuntimeContext>) {
let id = expanded_node.id.clone();
expanded_node
.changed_listener
.replace_with(Property::default());
context.enqueue_native_message(pax_message::NativeMessage::SliderDelete(id.to_u32()));
}
fn base(&self) -> &BaseInstance {
&self.base
}
fn resolve_debug(
&self,
f: &mut std::fmt::Formatter,
_expanded_node: Option<&ExpandedNode>,
) -> std::fmt::Result {
f.debug_struct("Slider").finish_non_exhaustive()
}
fn property_requires_occlusion_recompute(&self, _property_name: &str) -> bool {
false
}
fn handle_native_interrupt(
&self,
expanded_node: &Rc<ExpandedNode>,
interrupt: &NativeInterrupt,
) {
if let NativeInterrupt::FormSliderChange(args) = interrupt {
*borrow_mut!(self.native_value_to_ack) = Some(args.value);
expanded_node
.with_properties_unwrapped(|props: &mut Slider| props.value.set(args.value));
}
}
}