euv-core 0.45.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::*;

/// Queries browser dark-mode preference using `window.matchMedia("(prefers-color-scheme: dark)")`.
///
/// # Returns
///
/// - `ThemeMode` - Detected browser appearance mode.
#[must_use]
pub fn web_system_theme() -> ThemeMode {
    let Some(win) = window() else {
        return ThemeMode::Light;
    };
    let matches_dark: bool = win
        .match_media(WEB_DARK_MEDIA_QUERY)
        .ok()
        .flatten()
        .map(|mql: MediaQueryList| mql.matches())
        .unwrap_or(false);
    if matches_dark {
        ThemeMode::Dark
    } else {
        ThemeMode::Light
    }
}

/// Queries the browser's `window.devicePixelRatio`.
///
/// # Returns
///
/// - `f32` - Device pixel ratio clamped to at least `0.5`.
#[must_use]
pub fn web_device_pixel_ratio() -> f32 {
    window()
        .map(|w: Window| (w.device_pixel_ratio() as f32).max(0.5))
        .unwrap_or(1.0)
}

/// Writes `text` to the browser's `navigator.clipboard` API when available.
///
/// # Arguments
///
/// - `&str` - UTF-8 text string to copy.
///
/// # Returns
///
/// - `bool` - `true` when `navigator.clipboard.writeText` was dispatched.
pub(crate) fn web_write_clipboard(text: &str) -> bool {
    let Some(win) = window() else {
        return false;
    };
    let nav: Navigator = win.navigator();
    let clip: Clipboard = nav.clipboard();
    let _: Promise = clip.write_text(text);
    true
}

/// Queries whether the active browser tab is currently hidden (`document.hidden`).
///
/// # Returns
///
/// - `bool` - `true` when the page is in a background tab or minimized.
#[must_use]
pub(crate) fn web_document_hidden() -> bool {
    window()
        .and_then(|w: Window| w.document())
        .map(|d: Document| d.hidden())
        .unwrap_or(false)
}

/// Queries live browser online connectivity via `navigator.onLine`.
///
/// # Returns
///
/// - `bool` - `true` when the browser reports network availability.
#[must_use]
pub(crate) fn web_navigator_online() -> bool {
    window()
        .map(|w: Window| w.navigator().on_line())
        .unwrap_or(true)
}

/// Queries the browser's primary BCP-47 locale tag via `navigator.language`.
///
/// # Returns
///
/// - `String` - Locale identifier (fallback `"en-US"`).
#[must_use]
pub(crate) fn web_navigator_locale() -> String {
    window()
        .and_then(|w: Window| w.navigator().language())
        .filter(|s: &String| !s.trim().is_empty())
        .unwrap_or_else(|| DEFAULT_SYSTEM_LOCALE.to_string())
}

/// Measures a `(width, height)` CSS `env(safe-area-inset-*)` pair using a transient hidden DOM probe attached to `parent`.
///
/// # Arguments
///
/// - `&Document` - Active browser DOM document.
/// - `&Element` - Root element receiving the transient measurement probe.
/// - `&str` - Inline CSS style binding `width` and `height` to `env(safe-area-inset-*)`.
///
/// # Returns
///
/// - `(f32, f32)` - Sanitized non-negative `(width, height)` measurements in logical CSS pixels.
#[must_use]
fn web_measure_safe_area_pair(doc: &Document, parent: &Element, style: &str) -> (f32, f32) {
    let Ok(el) = doc.create_element(WEB_DIV_TAG) else {
        return (0.0, 0.0);
    };
    drop(el.set_attribute(WEB_ATTR_STYLE, style));
    if parent.append_child(&el).is_err() {
        return (0.0, 0.0);
    }
    let rect: DomRect = el.get_bounding_client_rect();
    el.remove();
    let w: f32 = rect.width() as f32;
    let h: f32 = rect.height() as f32;
    (
        if w.is_finite() && w > 0.0 { w } else { 0.0 },
        if h.is_finite() && h > 0.0 { h } else { 0.0 },
    )
}

/// Measures browser CSS `env(safe-area-inset-*)` values in logical CSS pixels via transient hidden DOM probes.
///
/// # Returns
///
/// - `SafeAreaInsets` - Sanitized browser safe-area insets (`0.0` when unsupported or non-notched).
#[must_use]
pub fn web_query_safe_area_insets() -> SafeAreaInsets {
    let Some(doc) = window().and_then(|w: Window| w.document()) else {
        return SafeAreaInsets::default();
    };
    let Some(parent): Option<Element> = doc.document_element() else {
        return SafeAreaInsets::default();
    };
    let (left, top): (f32, f32) =
        web_measure_safe_area_pair(&doc, &parent, WEB_SAFE_AREA_TOP_LEFT_STYLE);
    let (right, bottom): (f32, f32) =
        web_measure_safe_area_pair(&doc, &parent, WEB_SAFE_AREA_BOTTOM_RIGHT_STYLE);
    SafeAreaInsets::new(top, right, bottom, left)
}

/// Invokes a zero-argument DOM method that may return a JS `Promise`, swallowing rejections so browser consoles stay clean.
///
/// # Arguments
///
/// - `&JsValue` - Target DOM receiver (`Element` or `Document`).
/// - `&str` - Method property name to invoke.
fn web_invoke_promise_method(target: &JsValue, method: &str) {
    let Ok(func_val) = Reflect::get(target, &JsValue::from_str(method)) else {
        return;
    };
    let Ok(func) = func_val.dyn_into::<Function>() else {
        return;
    };
    if let Ok(ret) = func.call0(target)
        && let Ok(promise) = ret.dyn_into::<Promise>()
    {
        WEB_PROMISE_NOOP_REJECT.with(|cb: &Closure<dyn FnMut(JsValue)>| {
            let _: Promise = promise.catch(cb);
        });
    }
}

/// Requests or exits browser Fullscreen API cleanly on `document.documentElement`.
///
/// # Arguments
///
/// - `bool` - `true` to enter fullscreen; `false` to exit fullscreen.
pub fn web_set_fullscreen(enabled: bool) {
    let Some(doc) = window().and_then(|w: Window| w.document()) else {
        return;
    };
    if enabled {
        if doc.fullscreen_element().is_none()
            && let Some(root) = doc.document_element()
        {
            web_invoke_promise_method(root.as_ref(), WEB_METHOD_REQUEST_FULLSCREEN);
        }
    } else if doc.fullscreen_element().is_some() {
        web_invoke_promise_method(doc.as_ref(), WEB_METHOD_EXIT_FULLSCREEN);
    }
}

/// Creates and attaches an `HTMLCanvasElement` and its 2D context directly via `web-sys`.
///
/// # Arguments
///
/// - `&WindowConfig` - Declarative window configuration.
/// - `f32` - Browser device pixel ratio.
///
/// # Returns
///
/// - `Result<(HtmlCanvasElement, CanvasRenderingContext2d), PlatformError>` - Canvas and 2D context.
pub(crate) fn web_create_canvas(
    config: &WindowConfig,
    dpr: f32,
) -> Result<(HtmlCanvasElement, CanvasRenderingContext2d), PlatformError> {
    let win: Window = window()
        .ok_or_else(|| PlatformError::InitializationFailed(ERR_WEB_WINDOW_MISSING.to_string()))?;
    let doc: Document = win
        .document()
        .ok_or_else(|| PlatformError::InitializationFailed(ERR_WEB_WINDOW_MISSING.to_string()))?;
    doc.set_title(config.get_title());
    let el: Element = doc.create_element(WEB_CANVAS_TAG).map_err(|_: JsValue| {
        PlatformError::InitializationFailed(ERR_WEB_CANVAS_CREATE.to_string())
    })?;
    let canvas: HtmlCanvasElement = el.dyn_into::<HtmlCanvasElement>().map_err(|_: Element| {
        PlatformError::InitializationFailed(ERR_WEB_CANVAS_CREATE.to_string())
    })?;
    let win_w: f64 = win
        .inner_width()
        .ok()
        .and_then(|v: JsValue| v.as_f64())
        .filter(|w: &f64| *w >= 320.0)
        .unwrap_or_else(|| config.get_width() as f64);
    let win_h: f64 = win
        .inner_height()
        .ok()
        .and_then(|v: JsValue| v.as_f64())
        .filter(|h: &f64| *h >= 320.0)
        .unwrap_or_else(|| config.get_height() as f64);
    let phys_w: u32 = (win_w * (dpr as f64)).round().max(1.0) as u32;
    let phys_h: u32 = (win_h * (dpr as f64)).round().max(1.0) as u32;
    canvas.set_width(phys_w);
    canvas.set_height(phys_h);
    let css_style: String =
        format!("{WEB_CANVAS_STYLE_PREFIX}{WEB_CURSOR_POINTER}{WEB_CANVAS_STYLE_SUFFIX}");
    drop(canvas.set_attribute(WEB_ATTR_STYLE, &css_style));
    drop(canvas.set_attribute(WEB_ATTR_TABINDEX, WEB_ATTR_TABINDEX_ZERO));
    if let Some(body) = doc.body() {
        drop(body.set_attribute(WEB_ATTR_STYLE, WEB_BODY_STYLE));
        drop(body.append_child(&canvas));
    }
    let raw_ctx: Object = canvas
        .get_context(WEB_CONTEXT_2D)
        .map_err(|_: JsValue| PlatformError::SurfaceError(ERR_WEB_CTX2D_MISSING.to_string()))?
        .ok_or_else(|| PlatformError::SurfaceError(ERR_WEB_CTX2D_MISSING.to_string()))?;
    let ctx2d: CanvasRenderingContext2d = raw_ctx
        .dyn_into::<CanvasRenderingContext2d>()
        .map_err(|_: Object| PlatformError::SurfaceError(ERR_WEB_CTX2D_MISSING.to_string()))?;
    Ok((canvas, ctx2d))
}

/// Blits an RGBA8 `Framebuffer` directly onto a browser `CanvasRenderingContext2d` via `ImageData`.
///
/// # Arguments
///
/// - `&CanvasRenderingContext2d` - Browser 2D rendering context.
/// - `&Framebuffer` - Rendered RGBA8 framebuffer.
///
/// # Returns
///
/// - `Result<(), PlatformError>` - Presentation status.
pub(crate) fn web_present_image_data(
    ctx2d: &CanvasRenderingContext2d,
    framebuffer: &Framebuffer,
) -> Result<(), PlatformError> {
    web_present_image_data_region(ctx2d, framebuffer, DamageRegion::full())
}

/// Blits only the damaged physical rectangle of an RGBA8 `Framebuffer` onto a browser `CanvasRenderingContext2d`.
///
/// # Arguments
///
/// - `&CanvasRenderingContext2d` - Browser 2D rendering context.
/// - `&Framebuffer` - Rendered RGBA8 framebuffer.
/// - `DamageRegion` - Damaged pixel region to copy into the canvas.
///
/// # Returns
///
/// - `Result<(), PlatformError>` - Presentation status.
pub(crate) fn web_present_image_data_region(
    ctx2d: &CanvasRenderingContext2d,
    framebuffer: &Framebuffer,
    damage: DamageRegion,
) -> Result<(), PlatformError> {
    let fb_w: u32 = framebuffer.get_width();
    let fb_h: u32 = framebuffer.get_height();
    let Some(rect) = damage.resolve(fb_w, fb_h) else {
        return Ok(());
    };
    let clamped: Clamped<&[u8]> = Clamped(framebuffer.get_pixels().as_slice());
    let img: ImageData = ImageData::new_with_u8_clamped_array_and_sh(clamped, fb_w, fb_h)
        .map_err(|_: JsValue| PlatformError::SurfaceError(ERR_WEB_PRESENT_FAILED.to_string()))?;
    if damage.get_full_damage() {
        ctx2d
            .put_image_data(&img, 0.0, 0.0)
            .map_err(|_: JsValue| PlatformError::SurfaceError(ERR_WEB_PRESENT_FAILED.to_string()))
    } else {
        let dx: f64 = rect.get_x().floor().clamp(0.0, fb_w as f32) as f64;
        let dy: f64 = rect.get_y().floor().clamp(0.0, fb_h as f32) as f64;
        let rx1: f64 = rect.right().ceil().clamp(0.0, fb_w as f32) as f64;
        let ry1: f64 = rect.bottom().ceil().clamp(0.0, fb_h as f32) as f64;
        let dw: f64 = (rx1 - dx).max(1.0);
        let dh: f64 = (ry1 - dy).max(1.0);
        ctx2d
            .put_image_data_with_dirty_x_and_dirty_y_and_dirty_width_and_dirty_height(
                &img, 0.0, 0.0, dx, dy, dw, dh,
            )
            .map_err(|_: JsValue| PlatformError::SurfaceError(ERR_WEB_PRESENT_FAILED.to_string()))
    }
}