euv-core 0.46.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Detects the compile-time target `PlatformKind` for the current binary.
///
/// # Returns
///
/// - `PlatformKind` - Active platform kind (`iPadOS` is refined dynamically on iOS via `UIDevice`).
#[must_use]
pub fn detect_current_platform() -> PlatformKind {
    #[cfg(target_arch = "wasm32")]
    {
        PlatformKind::Web
    }
    #[cfg(target_env = "ohos")]
    {
        PlatformKind::HarmonyOs
    }
    #[cfg(target_os = "android")]
    {
        PlatformKind::Android
    }
    #[cfg(target_os = "ios")]
    {
        ios_detect_platform_kind()
    }
    #[cfg(target_os = "macos")]
    {
        PlatformKind::MacOs
    }
    #[cfg(target_os = "windows")]
    {
        PlatformKind::Windows
    }
    #[cfg(all(target_os = "linux", not(target_env = "ohos")))]
    {
        PlatformKind::Linux
    }
    #[cfg(not(any(target_arch = "wasm32", unix, windows)))]
    {
        PlatformKind::Headless
    }
}

/// Instantiates the native `PlatformBackend` for the active compile target.
///
/// # Returns
///
/// - `Box<dyn PlatformBackend>` - Ready-to-use native OS or web platform backend.
#[must_use]
pub fn create_native_platform() -> Box<dyn PlatformBackend> {
    #[cfg(target_arch = "wasm32")]
    {
        Box::new(WebPlatform::default())
    }
    #[cfg(target_env = "ohos")]
    {
        Box::new(HarmonyPlatform::default())
    }
    #[cfg(target_os = "android")]
    {
        Box::new(AndroidPlatform::default())
    }
    #[cfg(target_os = "ios")]
    {
        Box::new(IosPlatform::default())
    }
    #[cfg(target_os = "macos")]
    {
        Box::new(MacOsPlatform::default())
    }
    #[cfg(target_os = "windows")]
    {
        Box::new(WindowsPlatform::default())
    }
    #[cfg(all(target_os = "linux", not(target_env = "ohos")))]
    {
        Box::new(LinuxPlatform::default())
    }
    #[cfg(not(any(target_arch = "wasm32", unix, windows)))]
    {
        Box::new(HeadlessPlatform::default())
    }
}

/// Instantiates a ready-to-use `PlatformBackend` for any explicit `PlatformKind` uniformly across all hosts.
///
/// # Arguments
///
/// - `PlatformKind` - Target execution platform kind.
///
/// # Returns
///
/// - `Box<dyn PlatformBackend>` - Concrete native, adaptive, or emulated platform backend.
#[must_use]
pub fn create_platform_for_kind(kind: PlatformKind) -> Box<dyn PlatformBackend> {
    match kind {
        PlatformKind::Terminal => Box::new(TerminalPlatform::with_mode(
            TerminalHostMode::InteractiveTui,
        )),
        PlatformKind::TerminalCli => {
            Box::new(TerminalPlatform::with_mode(TerminalHostMode::StaticCli))
        }
        PlatformKind::Wearable => Box::new(WearablePlatform::default()),
        PlatformKind::HarmonyOs => Box::new(HarmonyPlatform::default()),
        PlatformKind::MiniApp => Box::new(MiniAppPlatform::default()),
        PlatformKind::QuickApp => Box::new(QuickAppPlatform::default()),
        PlatformKind::Headless => Box::new(HeadlessPlatform::default()),
        other => {
            if other == detect_current_platform() {
                create_native_platform()
            } else {
                Box::new(HeadlessPlatform::emulating(
                    other,
                    1.0,
                    SafeAreaInsets::default(),
                ))
            }
        }
    }
}