use crate::{Font, TextAlignHorizontal, TextAlignVertical};
use pax_engine::api::Paint;
use pax_engine::*;
use pax_message::{AnyCreatePatch, ButtonPatch};
use pax_runtime::api::{borrow, borrow_mut, use_RefCell, Color, Numeric, Size, Stroke};
use pax_runtime::api::{Layer, Property};
use pax_runtime::{
BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstantiationArgs, RuntimeContext,
};
use std::rc::Rc;
use_RefCell!();
use crate::common::{native_surface_opacity, patch_if_needed, patch_liquid_glass_if_needed};
use crate::TextStyle;
#[cfg(all(test, not(feature = "designtime")))]
mod publication_tests {
use super::*;
use pax_engine::api::pax_value::{PaxAny, ToFromPaxAny};
use pax_engine::api::{Platform, Variable, OS};
use pax_runtime::{CommonPropertiesInit, PaxEngine, PropertiesInit, PropertiesScopeInit};
use std::collections::HashMap;
#[test]
fn worker_result_reaches_native_patch_in_the_first_settlement() {
let mut engine = PaxEngine::new_empty(
(320.0, 240.0),
Platform::Web,
OS::Mac,
Box::new(|| 0),
Default::default(),
);
let _graph = engine.runtime_context.property_graph.enter();
let value = Property::new(0_u32);
let local = value.local();
let deps = [local.untyped()];
let button = Button::default();
button.label.replace_with(LocalProperty::computed(
move || format!("value {}", local.get()),
&deps,
));
let properties = Rc::new(RefCell::new(button.to_pax_any()));
let args = InstantiationArgs {
prototypical_common_properties: CommonPropertiesInit::Default,
prototypical_properties: PropertiesInit::Factory(Box::new(move |_, node| {
node.is_none().then(|| properties.clone())
})),
properties_scope: PropertiesScopeInit::Factory(Box::new(|properties| {
let properties = properties.borrow();
let button = Button::ref_from_pax_any(&properties).unwrap();
HashMap::from([(
"label".into(),
Variable::new_from_typed_property(button.label.clone()),
)])
})),
handler_registry: None,
children: None,
component_template: None,
component_settings: None,
template_node_identifier: None,
template_node_type_id: None,
template_node_selector_info: None,
transition_config: Default::default(),
};
let child = ButtonInstance::instantiate(args);
engine.mount_root_component(pax_runtime::ComponentInstance::instantiate(
InstantiationArgs {
prototypical_common_properties: CommonPropertiesInit::Default,
prototypical_properties: PropertiesInit::Factory(Box::new(|_, node| {
node.is_none()
.then(|| Rc::new(RefCell::new(PaxAny::Builtin(Default::default()))))
})),
properties_scope: PropertiesScopeInit::None,
handler_registry: None,
children: None,
component_template: Some(RefCell::new(vec![child])),
component_settings: None,
template_node_identifier: None,
template_node_type_id: None,
template_node_selector_info: None,
transition_config: Default::default(),
},
));
engine.tick();
for n in 1..=3 {
let value = value.clone();
std::thread::spawn(move || value.set(n)).join().unwrap();
let messages = engine.tick();
assert!(messages.iter().any(|message| matches!(message,
pax_message::NativeMessage::ButtonUpdate(patch) if patch.content.as_deref() == Some(format!("value {n}").as_str())
)), "first settlement did not publish value {n}");
}
}
}
#[pax]
#[engine_import_path("pax_engine")]
#[primitive("pax_std::forms::button::ButtonInstance")]
#[custom(Default)]
pub struct Button {
pub label: Property<String>,
pub color: Property<Color>,
pub hover_color: Property<Color>,
pub corner_radius: Property<f64>,
pub outline: Property<Stroke>,
pub style: Property<TextStyle>,
}
impl Default for Button {
fn default() -> Self {
Self {
color: Property::new(Color::GRAY),
hover_color: Property::new(Color::INDIGO),
corner_radius: Property::new(8.0),
label: Property::new(String::from("button")),
style: Property::new(TextStyle {
font: Property::new(Font::default()),
font_size: Property::new(Size::Pixels(Numeric::F64(20.0))),
fill: Property::new(Paint::Solid(Color::WHITE)),
underline: Property::new(false),
align_multiline: Property::new(TextAlignHorizontal::Center),
align_vertical: Property::new(TextAlignVertical::Center),
align_horizontal: Property::new(TextAlignHorizontal::Center),
}),
outline: Property::new(Stroke {
width: Property::new(Size::Pixels(0.into())),
..Stroke::default()
}),
}
}
}
pub struct ButtonInstance {
base: BaseInstance,
}
impl InstanceNode for ButtonInstance {
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,
},
),
})
}
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::ButtonCreate(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(ButtonPatch {
id: id.to_u32(),
..Default::default()
}));
let deps: Vec<_> = borrow!(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(LocalProperty::computed(
move || {
let Some(expanded_node) = weak_self_ref.upgrade() else {
return;
};
let mut old_state = borrow_mut!(last_patch);
let mut patch = ButtonPatch {
id: expanded_node.id.to_u32(),
..Default::default()
};
expanded_node.with_properties_unwrapped(|properties: &mut Button| {
let computed_tab = expanded_node.transform_and_bounds.get();
let (width, height) = computed_tab.bounds;
let updates = [
patch_if_needed(
&mut old_state.outline_stroke_color,
&mut patch.outline_stroke_color,
(&crate::common::native_stroke_color(
&properties.outline.local().get(),
))
.into(),
),
patch_if_needed(
&mut old_state.outline_stroke_width,
&mut patch.outline_stroke_width,
properties
.outline
.local()
.get()
.width
.local()
.get()
.expect_pixels()
.to_float(),
),
patch_if_needed(
&mut old_state.hover_color,
&mut patch.hover_color,
(&properties.hover_color.local().get()).into(),
),
patch_if_needed(
&mut old_state.corner_radius,
&mut patch.corner_radius,
properties.corner_radius.local().get(),
),
patch_if_needed(
&mut old_state.content,
&mut patch.content,
properties.label.local().get(),
),
patch_if_needed(
&mut old_state.color,
&mut patch.color,
(&properties.color.local().get()).into(),
),
patch_if_needed(
&mut old_state.style,
&mut patch.style,
properties.style.local().get().message(true),
),
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.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::ButtonUpdate(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(LocalProperty::default());
context.enqueue_native_message(pax_message::NativeMessage::ButtonDelete(id.to_u32()));
}
fn base(&self) -> &BaseInstance {
&self.base
}
fn property_requires_occlusion_recompute(&self, _property_name: &str) -> bool {
false
}
fn resolve_debug(
&self,
f: &mut std::fmt::Formatter,
_expanded_node: Option<&ExpandedNode>,
) -> std::fmt::Result {
f.debug_struct("Button").finish_non_exhaustive()
}
}