Skip to main content

pax_std/layout/
dynamic_island_spacer.rs

1use 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/// Opt-in space for UIKit's current safe area; does not draw or intercept input.
9///
10/// `<DynamicIslandSpacer />` fills its container's width and measures its height
11/// from the top safe-area inset. Use it in content-sized layout, or bind `inset`
12/// when positioning other content explicitly. `edge` also supports the home
13/// indicator and landscape side insets. Insets update on rotation and window
14/// changes, in debug and release. Other chassis measure zero on the inset axis.
15///
16/// This reserves the platform's entire safe inset, including status bars on
17/// devices without a Dynamic Island. It does not automatically move siblings,
18/// inset ancestors, or detect an ancestor that already avoids the safe area.
19#[pax]
20#[engine_import_path("pax_engine")]
21#[primitive("pax_std::layout::dynamic_island_spacer::DynamicIslandSpacerInstance")]
22pub struct DynamicIslandSpacer {
23    /// Window edge to reserve. Defaults to Top. Left/Right measure width and
24    /// fill the container's height; Top/Bottom measure height and fill width.
25    pub edge: Property<SafeAreaEdge>,
26    /// Computed inset in logical pixels. Bind to observe it; the spacer owns
27    /// this output and overwrites authored values when its measurement changes.
28    pub inset: Property<f64>,
29}
30
31/// The window safe-area edge reserved by a DynamicIslandSpacer.
32#[pax]
33#[engine_import_path("pax_engine")]
34pub enum SafeAreaEdge {
35    /// Space below the status bar or top cutout.
36    #[default]
37    Top,
38    /// Space left of a right-side cutout in landscape.
39    Right,
40    /// Space above the home indicator.
41    Bottom,
42    /// Space right of a left-side cutout in landscape.
43    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}