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

/// Unified system abstraction implemented directly over each OS native API (Web, Android, iOS, iPadOS, macOS, Windows, Linux, Headless).
pub trait PlatformBackend: WindowController {
    /// Returns the concrete `PlatformKind` served by this backend.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Backend reference.
    ///
    /// # Returns
    ///
    /// - `PlatformKind` - Active platform kind.
    fn kind(&self) -> PlatformKind;

    /// Returns the normalized capability matrix for this backend.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Backend reference.
    ///
    /// # Returns
    ///
    /// - `PlatformCapabilities` - Hardware and OS capability flags.
    fn capabilities(&self) -> PlatformCapabilities;

    /// Creates a native or web window configured by `config`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable platform backend.
    /// - `&WindowConfig` - Declarative window configuration.
    ///
    /// # Returns
    ///
    /// - `Result<WindowState, PlatformError>` - Initial live window state on success.
    fn create_window(&mut self, config: &WindowConfig) -> Result<WindowState, PlatformError>;

    /// Returns the latest `WindowState` for `id`, if alive.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Backend reference.
    /// - `WindowId` - Window identifier.
    ///
    /// # Returns
    ///
    /// - `Option<WindowState>` - Live window state.
    fn window_state(&self, id: WindowId) -> Option<WindowState>;

    /// Returns the ABI-stable `RawWindowHandle` for hardware GPU surface binding.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Backend reference.
    /// - `WindowId` - Window identifier.
    ///
    /// # Returns
    ///
    /// - `RawWindowHandle` - Native OS surface pointer bundle.
    fn raw_window_handle(&self, id: WindowId) -> RawWindowHandle;

    /// Drains all pending OS window, input, and lifecycle events since the last poll.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable platform backend.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Ordered pending events.
    fn poll_events(&mut self) -> Vec<WindowEvent>;

    /// Blocks with zero CPU usage until at least one OS or lifecycle event arrives, then drains all pending events.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable platform backend.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Ordered events received upon wake.
    fn wait_for_events(&mut self) -> Vec<WindowEvent> {
        self.poll_events()
    }

    /// Pauses or resumes the native host frame pump when entering or leaving background-frozen state.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable platform backend.
    /// - `bool` - `true` to pause frame pumping for background freeze; `false` to resume.
    fn set_frame_pump_paused(&mut self, _paused: bool) {}

    /// Returns whether the platform backend's frame pump is currently paused for background freeze.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Backend reference.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when frame pumping is paused.
    fn is_frame_pump_paused(&self) -> bool {
        false
    }

    /// Presents a CPU-rendered RGBA8 `Framebuffer` directly onto the native window surface.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable platform backend.
    /// - `WindowId` - Target window handle.
    /// - `&Framebuffer` - Rendered RGBA8 framebuffer.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Presentation status.
    fn present_framebuffer(
        &mut self,
        id: WindowId,
        framebuffer: &Framebuffer,
    ) -> Result<(), PlatformError>;

    /// Presents a damaged sub-region of a CPU-rendered RGBA8 `Framebuffer` when supported by the backend.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable platform backend.
    /// - `WindowId` - Target window handle.
    /// - `&Framebuffer` - Rendered RGBA8 framebuffer.
    /// - `DamageRegion` - Damaged pixel region updated this frame.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Presentation status.
    fn present_framebuffer_region(
        &mut self,
        id: WindowId,
        framebuffer: &Framebuffer,
        _damage: DamageRegion,
    ) -> Result<(), PlatformError> {
        self.present_framebuffer(id, framebuffer)
    }

    /// Reads the current system clipboard text if available.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Backend reference.
    ///
    /// # Returns
    ///
    /// - `Option<String>` - Clipboard UTF-8 contents.
    fn read_clipboard(&self) -> Option<String>;

    /// Writes `text` into the system clipboard.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable platform backend.
    /// - `&str` - UTF-8 text to store.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Clipboard write status.
    fn write_clipboard(&mut self, text: &str) -> Result<(), PlatformError>;

    /// Queries the current operating system theme mode (`Light` / `Dark`).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Backend reference.
    ///
    /// # Returns
    ///
    /// - `ThemeMode` - Detected OS appearance mode.
    fn system_theme(&self) -> ThemeMode;

    /// Queries the active display's native hardware refresh rate in Hertz (`60.0` fallback).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Backend reference.
    ///
    /// # Returns
    ///
    /// - `f32` - Native display refresh rate in Hertz (e.g. `120.0`, `144.0`, `180.0`).
    fn display_refresh_rate_hz(&self) -> f32 {
        60.0
    }
}

/// Unified key-value local storage contract shared across Web, Mobile, HarmonyOS, Wearables, Mini-Programs, and QuickApps.
pub trait PlatformStorage {
    /// Reads a UTF-8 value associated with `key` from sandboxed local storage.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Storage adapter reference.
    /// - `&str` - Storage key.
    ///
    /// # Returns
    ///
    /// - `Option<String>` - Stored value, if present.
    fn storage_get(&self, key: &str) -> Option<String>;

    /// Stores a UTF-8 `value` under `key` in sandboxed local storage.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable storage adapter.
    /// - `&str` - Storage key.
    /// - `&str` - UTF-8 value to persist.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Persistence status.
    fn storage_set(&mut self, key: &str, value: &str) -> Result<(), PlatformError>;

    /// Removes `key` from sandboxed local storage.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable storage adapter.
    /// - `&str` - Storage key to remove.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Removal status.
    fn storage_remove(&mut self, key: &str) -> Result<(), PlatformError>;

    /// Clears all keys from sandboxed local storage.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable storage adapter.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Clear status.
    fn storage_clear(&mut self) -> Result<(), PlatformError>;
}

/// Unified host-level navigation bridge abstracting OS routers, MiniApp page stacks, and QuickApp `@system.router`.
pub trait PlatformNavigation {
    /// Pushes or navigates to `route` on the host navigation stack.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable navigation adapter.
    /// - `&str` - Target logical route path.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Navigation dispatch status.
    fn navigate_to(&mut self, route: &str) -> Result<(), PlatformError>;

    /// Pops `delta` entries from the host navigation stack.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable navigation adapter.
    /// - `u32` - Number of pages to navigate back (`>= 1`).
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Back navigation status.
    fn navigate_back(&mut self, delta: u32) -> Result<(), PlatformError>;

    /// Returns the currently active logical route path.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Navigation adapter reference.
    ///
    /// # Returns
    ///
    /// - `&str` - Active route path string.
    fn current_route(&self) -> &str;
}

/// Unified Freeze / Resume / Destroy surface lifecycle contract smoothing platform differences across all targets.
pub trait PlatformLifecycleBridge {
    /// Suspends frame pumping, cancels active pointers, and emits canonical freeze/background events.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable lifecycle adapter.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Normalized freeze events to dispatch to `App`.
    fn freeze_surface(&mut self) -> Vec<WindowEvent>;

    /// Resumes frame pumping and emits canonical appear/foreground events.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable lifecycle adapter.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Normalized resume events to dispatch to `App`.
    fn resume_surface(&mut self) -> Vec<WindowEvent>;

    /// Tears down surface attachments, cancels pointers, and emits canonical terminate events.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable lifecycle adapter.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Normalized teardown events to dispatch to `App`.
    fn destroy_surface(&mut self) -> Vec<WindowEvent>;
}

/// Supertrait implemented by full cross-platform host adapters exposing uniform backend, storage, navigation, and lifecycle semantics.
pub trait PlatformHostAdapter:
    PlatformBackend + PlatformStorage + PlatformNavigation + PlatformLifecycleBridge
{
    /// Returns the concrete `DeviceFormFactor` of this running host instance.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Host adapter reference.
    ///
    /// # Returns
    ///
    /// - `DeviceFormFactor` - Active physical or virtual form factor.
    fn device_form_factor(&self) -> DeviceFormFactor;

    /// Returns the canonical lowercase vendor or runtime name for diagnostics (never required by app UI code).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Host adapter reference.
    ///
    /// # Returns
    ///
    /// - `&'static str` - Vendor or runtime identifier string.
    fn host_vendor_name(&self) -> &'static str;
}