pax-std 0.39.0

Standard library for Pax, including layouts, drawing primitives, and form controls
Documentation
use pax_engine::pax;
use pax_message::borrow_mut;
use pax_runtime::{
    BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstantiationArgs, RuntimeContext,
};
use std::rc::Rc;

pax_message::use_RefCell!();

use pax_runtime::api::Layer;

/// Allows embedding another Pax #[main] component with
/// a separate manifest via a separate DefinitionToInstanceTraverser
/// Useful for designtime hosts and other nested-program integrations.
#[pax]
#[engine_import_path("pax_engine")]
#[primitive("pax_std::core::inline_frame::InlineFrameInstance")]
pub struct InlineFrame {}

// Runtime instance backing `<InlineFrame>`.
pub struct InlineFrameInstance {
    base: BaseInstance,
}

impl InstanceNode for InlineFrameInstance {
    fn instantiate(args: InstantiationArgs) -> Rc<Self>
    where
        Self: Sized,
    {
        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 resolve_debug(
        &self,
        f: &mut std::fmt::Formatter,
        expanded_node: Option<&pax_runtime::ExpandedNode>,
    ) -> std::fmt::Result {
        match expanded_node {
            Some(expanded_node) => {
                expanded_node.with_properties_unwrapped(|_g: &mut InlineFrame| {
                    f.debug_struct("InlineFrame").finish()
                })
            }
            None => f.debug_struct("InlineFrame").finish_non_exhaustive(),
        }
    }

    fn base(&self) -> &BaseInstance {
        &self.base
    }

    fn handle_mount(self: Rc<Self>, expanded_node: &Rc<ExpandedNode>, ctx: &Rc<RuntimeContext>) {
        let Some(instance_node) = ctx.get_userland_root_instance_node() else {
            log::warn!("inline frame mounted without a userland instance node");
            expanded_node.children.set(Vec::new());
            *borrow_mut!(ctx.userland_root_expanded_node) = None;
            return;
        };
        let children_with_envs = vec![(instance_node, ctx.globals().stack_frame())];
        let new_children = expanded_node.generate_children(
            children_with_envs,
            ctx,
            &expanded_node.parent_frame,
            true,
        );
        *borrow_mut!(ctx.userland_root_expanded_node) = new_children.first().cloned();
        expanded_node.children.set(new_children);
    }
}