pax_std/layout/
dynamic_island_spacer.rs1use pax_engine::*;
2use pax_runtime::api::{Layer, LocalProperty, Platform, Property, SafeAreaInsets};
3use pax_runtime::{
4 BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstantiationArgs, RuntimeContext,
5};
6use std::rc::Rc;
7
8#[pax]
20#[engine_import_path("pax_engine")]
21#[primitive("pax_std::layout::dynamic_island_spacer::DynamicIslandSpacerInstance")]
22pub struct DynamicIslandSpacer {
23 pub edge: Property<SafeAreaEdge>,
26 pub inset: Property<f64>,
29}
30
31#[pax]
33#[engine_import_path("pax_engine")]
34pub enum SafeAreaEdge {
35 #[default]
37 Top,
38 Right,
40 Bottom,
42 Left,
44}
45
46impl SafeAreaEdge {
47 fn measurement(&self, insets: SafeAreaInsets, parent: (f64, f64)) -> (f64, (f64, f64)) {
48 match self {
49 Self::Top => (insets.top, (parent.0, insets.top)),
50 Self::Right => (insets.right, (insets.right, parent.1)),
51 Self::Bottom => (insets.bottom, (parent.0, insets.bottom)),
52 Self::Left => (insets.left, (insets.left, parent.1)),
53 }
54 }
55}
56
57pub struct DynamicIslandSpacerInstance {
58 base: BaseInstance,
59}
60
61#[cfg(test)]
62mod tests;
63
64impl InstanceNode for DynamicIslandSpacerInstance {
65 fn instantiate(args: InstantiationArgs) -> Rc<Self> {
66 Rc::new(Self {
67 base: BaseInstance::new(
68 args,
69 InstanceFlags {
70 invisible_to_slot: false,
71 invisible_to_raycasting: true,
72 layer: Layer::DontCare,
73 is_component: false,
74 is_slot: false,
75 },
76 ),
77 })
78 }
79
80 fn base(&self) -> &BaseInstance {
81 &self.base
82 }
83
84 fn resolve_debug(
85 &self,
86 f: &mut std::fmt::Formatter,
87 _: Option<&ExpandedNode>,
88 ) -> std::fmt::Result {
89 f.debug_struct("DynamicIslandSpacer").finish()
90 }
91
92 fn handle_mount(self: Rc<Self>, node: &Rc<ExpandedNode>, context: &Rc<RuntimeContext>) {
93 let globals = context.globals();
94 let supported = globals.platform == Platform::Native && globals.os.is_ios();
95 let insets = context.safe_area_insets();
96 let parent = node.get_node_context(context).bounds_parent;
97 let (edge, inset) = node.with_properties_unwrapped(|p: &mut DynamicIslandSpacer| {
98 (p.edge.local(), p.inset.local())
99 });
100 let deps = [insets.untyped(), parent.untyped(), edge.untyped()];
101 let weak = Rc::downgrade(node);
102 node.changed_listener
103 .replace_with(LocalProperty::computed_with_name(
104 move || {
105 let Some(node) = weak.upgrade() else {
106 return;
107 };
108 let insets = if supported {
109 insets.get()
110 } else {
111 SafeAreaInsets::default()
112 };
113 let (extent, measured) = edge.get().measurement(insets, parent.get());
114 inset.set_if_neq(extent);
115 node.measured_size.set_if_neq(Some(measured));
116 },
117 &deps,
118 "dynamic island spacer",
119 ));
120 }
121
122 fn handle_unmount(&self, node: &Rc<ExpandedNode>, _: &Rc<RuntimeContext>) {
123 node.changed_listener.replace_with(LocalProperty::default());
124 }
125}