euv-core 0.48.1

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::*;

/// Registers a platform-host native OS capability bridge (haptics, notifications, permissions, fullscreen, Dynamic Island).
///
/// # Arguments
///
/// - `fn(&str, &str, &str) -> bool` - Host callback handling `(kind, arg1, arg2)` and returning whether native OS execution succeeded.
pub fn register_native_action_bridge(bridge: fn(&str, &str, &str) -> bool) {
    EXTERNAL_NATIVE_ACTION_BRIDGE.store(bridge as *mut (), Ordering::Release);
}

/// Dispatches a native OS capability action `(kind, arg1, arg2)` to the registered host bridge, if any.
///
/// # Arguments
///
/// - `&str` - Action category (`"haptic"`, `"notify"`, `"island"`, `"permission"`, `"fullscreen"`, `"protocol"`).
/// - `&str` - Primary payload argument (e.g. title or permission key).
/// - `&str` - Secondary payload argument (e.g. body or rationale).
///
/// # Returns
///
/// - `bool` - `true` when handled by an active native OS host bridge.
pub fn dispatch_native_action(kind: &str, arg1: &str, arg2: &str) -> bool {
    let raw: *mut () = EXTERNAL_NATIVE_ACTION_BRIDGE.load(Ordering::Acquire);
    if raw.is_null() {
        return false;
    }
    // SAFETY: `raw` is stored exclusively via `register_native_action_bridge` from a `fn(&str, &str, &str) -> bool` pointer.
    let func: fn(&str, &str, &str) -> bool =
        unsafe { transmute::<*mut (), fn(&str, &str, &str) -> bool>(raw) };
    func(kind, arg1, arg2)
}

/// Copies `text` to the active OS or browser clipboard via direct platform hooks and any registered native action bridge.
///
/// # Arguments
///
/// - `&str` - UTF-8 text string to place on the system clipboard.
///
/// # Returns
///
/// - `bool` - `true` when at least one native or host clipboard sink accepted `text`.
pub fn write_system_clipboard(text: &str) -> bool {
    let bridge_ok: bool =
        dispatch_native_action(NATIVE_ACTION_CLIPBOARD, CLIPBOARD_ACTION_WRITE, text);
    #[cfg(target_os = "macos")]
    {
        macos_write_pasteboard(text) || bridge_ok
    }
    #[cfg(target_arch = "wasm32")]
    {
        web_write_clipboard(text) || bridge_ok
    }
    #[cfg(not(any(target_os = "macos", target_arch = "wasm32")))]
    {
        bridge_ok
    }
}

/// Validates that `url` is a safe external `http://` or `https://` URL without whitespace or control characters.
///
/// # Arguments
///
/// - `&str` - Candidate external URL string.
///
/// # Returns
///
/// - `bool` - `true` when `url` has a valid `http://` or `https://` prefix, a non-empty host authority, and no control or whitespace characters.
#[must_use]
pub fn is_valid_external_url(url: &str) -> bool {
    let trimmed: &str = url.trim();
    if trimmed.is_empty() {
        return false;
    }
    let rest: &str = if let Some(after_https) = trimmed.strip_prefix(URL_SCHEME_HTTPS) {
        after_https
    } else if let Some(after_http) = trimmed.strip_prefix(URL_SCHEME_HTTP) {
        after_http
    } else {
        return false;
    };
    if rest.is_empty() || rest.starts_with('/') {
        return false;
    }
    !trimmed
        .chars()
        .any(|ch: char| ch.is_control() || ch.is_whitespace())
}

/// Spawns the host desktop OS browser launcher for a validated URL outside test and headless runs.
///
/// # Arguments
///
/// - `&str` - Validated external URL string.
///
/// # Returns
///
/// - `bool` - `true` when the desktop launcher process spawned successfully.
#[cfg(not(target_arch = "wasm32"))]
fn open_external_url_desktop(target: &str) -> bool {
    if cfg!(test) || env::var_os(ENV_EUV_HEADLESS).is_some() {
        return false;
    }
    #[cfg(target_os = "macos")]
    {
        Command::new(OS_CMD_OPEN)
            .arg(target)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .is_ok()
    }
    #[cfg(target_os = "linux")]
    {
        Command::new(OS_CMD_XDG_OPEN)
            .arg(target)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .is_ok()
    }
    #[cfg(target_os = "windows")]
    {
        Command::new(OS_CMD_WIN_SHELL)
            .args([OS_CMD_WIN_EXEC, OS_CMD_WIN_START, "", target])
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .is_ok()
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
    {
        let _: &str = target;
        false
    }
}

/// Opens a validated `http://` or `https://` URL externally via the registered host bridge, browser `window.open`, or native desktop launcher.
///
/// # Arguments
///
/// - `&str` - External URL to open (`https://...` or `http://...`).
///
/// # Returns
///
/// - `bool` - `true` when the URL passed validation and was accepted by a host bridge or OS/browser launcher.
pub fn open_external_url(url: &str) -> bool {
    if !is_valid_external_url(url) {
        return false;
    }
    let target: &str = url.trim();
    let bridge_ok: bool = dispatch_native_action(NATIVE_ACTION_OPEN_URL, target, "");
    #[cfg(target_arch = "wasm32")]
    {
        bridge_ok || web_open_external_url(target)
    }
    #[cfg(not(target_arch = "wasm32"))]
    {
        bridge_ok || open_external_url_desktop(target)
    }
}