use pax_engine::*;
use pax_runtime::api::{Layer, LocalProperty, Platform, Property, SafeAreaInsets};
use pax_runtime::{
BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstantiationArgs, RuntimeContext,
};
use std::rc::Rc;
#[pax]
#[engine_import_path("pax_engine")]
#[primitive("pax_std::layout::dynamic_island_spacer::DynamicIslandSpacerInstance")]
pub struct DynamicIslandSpacer {
pub edge: Property<SafeAreaEdge>,
pub inset: Property<f64>,
}
#[pax]
#[engine_import_path("pax_engine")]
pub enum SafeAreaEdge {
#[default]
Top,
Right,
Bottom,
Left,
}
impl SafeAreaEdge {
fn measurement(&self, insets: SafeAreaInsets, parent: (f64, f64)) -> (f64, (f64, f64)) {
match self {
Self::Top => (insets.top, (parent.0, insets.top)),
Self::Right => (insets.right, (insets.right, parent.1)),
Self::Bottom => (insets.bottom, (parent.0, insets.bottom)),
Self::Left => (insets.left, (insets.left, parent.1)),
}
}
}
pub struct DynamicIslandSpacerInstance {
base: BaseInstance,
}
#[cfg(test)]
mod tests;
impl InstanceNode for DynamicIslandSpacerInstance {
fn instantiate(args: InstantiationArgs) -> Rc<Self> {
Rc::new(Self {
base: BaseInstance::new(
args,
InstanceFlags {
invisible_to_slot: false,
invisible_to_raycasting: true,
layer: Layer::DontCare,
is_component: false,
is_slot: false,
},
),
})
}
fn base(&self) -> &BaseInstance {
&self.base
}
fn resolve_debug(
&self,
f: &mut std::fmt::Formatter,
_: Option<&ExpandedNode>,
) -> std::fmt::Result {
f.debug_struct("DynamicIslandSpacer").finish()
}
fn handle_mount(self: Rc<Self>, node: &Rc<ExpandedNode>, context: &Rc<RuntimeContext>) {
let globals = context.globals();
let supported = globals.platform == Platform::Native && globals.os.is_ios();
let insets = context.safe_area_insets();
let parent = node.get_node_context(context).bounds_parent;
let (edge, inset) = node.with_properties_unwrapped(|p: &mut DynamicIslandSpacer| {
(p.edge.local(), p.inset.local())
});
let deps = [insets.untyped(), parent.untyped(), edge.untyped()];
let weak = Rc::downgrade(node);
node.changed_listener
.replace_with(LocalProperty::computed_with_name(
move || {
let Some(node) = weak.upgrade() else {
return;
};
let insets = if supported {
insets.get()
} else {
SafeAreaInsets::default()
};
let (extent, measured) = edge.get().measurement(insets, parent.get());
inset.set_if_neq(extent);
node.measured_size.set_if_neq(Some(measured));
},
&deps,
"dynamic island spacer",
));
}
fn handle_unmount(&self, node: &Rc<ExpandedNode>, _: &Rc<RuntimeContext>) {
node.changed_listener.replace_with(LocalProperty::default());
}
}