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

/// Flat generation-indexed arena holding the entire retained UI scene graph with dense SoA metadata and intrusive child topology.
#[derive(CustomDebug, Data)]
pub struct SceneTree {
    /// Contiguous node storage vector.
    pub(crate) nodes: Vec<SceneNode>,
    /// Dense 8-byte SoA metadata parallel to `nodes` (generation, structural flags, dirty mask).
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) meta: Vec<NodeMeta>,
    /// Dense 24-byte SoA intrusive doubly-linked tree topology parallel to `nodes` (0 `Vec<NodeId>` heap allocations).
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) topology: Vec<NodeTopology>,
    /// Dense 32-byte SoA compact paint style array parallel to `nodes`.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) compact_paints: Vec<CompactPaintStyle>,
    /// Sparse slot-recycled table of rare paint effects (`Shadow`, `LinearGradient`, `Transform2D`, `focus_ring`, `backdrop_blur`).
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) effect_table: EffectTable,
    /// Recycled slot indices for O(1) node allocation.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) free_list: Vec<u32>,
    /// Incrementally maintained count of live nodes for O(1) `live_node_count()`.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) live_count: usize,
    /// Incrementally maintained count of attached `Lifecycle` observers for O(1) zero-lifecycle traversal skipping.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) live_lifecycle_count: usize,
    /// Root scene node handle.
    #[get(type(copy))]
    pub(crate) root: NodeId,
    /// Focus, hover, and pointer-press tracking state.
    #[get(type(copy))]
    pub(crate) focus: FocusManager,
    /// Monotonic structural/visual mutation counter.
    #[get(type(copy))]
    pub(crate) revision: u64,
    /// Active paint/transform node transitions ticked on every frame until settled.
    pub(crate) transitions: Vec<NodeTransition>,
    /// Blinking visibility state of the focused text input caret (`true` = visible phase).
    #[get(type(copy))]
    pub(crate) caret_visible: bool,
    /// Incrementally maintained registry of live `NodeKind::Portal` and `Position::Fixed` nodes.
    #[get(skip)]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) portal_nodes: Vec<NodeId>,
    /// Incrementally maintained registry of live `Overflow::Scroll` container nodes.
    #[get(skip)]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) scroll_nodes: Vec<NodeId>,
    /// Bounded list of nodes marked paint/scroll dirty since the last `collect_paint_damage` pass.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) dirty_paint_nodes: Vec<NodeId>,
    /// Flag raised when structural/layout mutations or >64 dirty nodes require full viewport damage.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) full_damage_pending: bool,
    /// Flag raised when one or more layout/children/text mutations occurred since the last damage collection.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) layout_damage_pending: bool,
    /// Flag raised when `dirty_paint_nodes` exceeded `DIRTY_PAINT_FULL_THRESHOLD` since the last collection.
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) full_damage_overflow: bool,
    /// Number of newly allocated nodes created since the last damage collection (promotes to overflow above threshold).
    #[get(pub(crate), type(copy))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) created_since_damage: usize,
    /// Pre-mutation conservative logical screen rectangles for disposed, reparented, or shrinking-halo nodes.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) extra_logical_damage: Vec<Rect>,
    /// Active native text selection state on a selectable `NodeKind::Text` node, if any.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) text_selection: Option<SelectionState>,
    /// Registry of live nodes with active backdrop-blur radii `(node_id, blur_radius)` for effect-dependency damage propagation.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) backdrop_blur_nodes: Vec<(NodeId, f32)>,
    /// Cumulative count of genuine layout-invalidating mutations (`DIRTY_LAYOUT`, `DIRTY_CHILDREN`, `DIRTY_TEXT`, node creation).
    #[get(type(copy))]
    #[get_mut(skip)]
    #[set(pub(crate))]
    pub(crate) layout_invalidations: u64,
    /// Whether the most recent `collect_surgical_paint_damage` pass observed only composite/scroll changes with zero layout or paint mutations.
    #[get(pub(crate), type(copy))]
    #[get_mut(skip)]
    #[set(pub(crate))]
    pub(crate) last_collect_composite_only: bool,
    /// Sparse generation-guarded theme ownership and origin snapshots keyed by arena slot index.
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) theme_entries: HashMap<u32, ThemeNodeEntry>,
    /// Dense per-node monotonic paint epoch parallel to `nodes` for O(1) `PaintFragmentCache` invalidation.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) paint_epochs: Vec<u32>,
    /// Flag raised when `LayoutEngine` skips a `Display::None` node carrying `DIRTY_SUBTREE_LAYOUT` so `mark_dirty` never breaks early on stale descendant bits.
    #[get(pub(crate), type(copy))]
    #[get_mut(skip)]
    #[set(pub(crate))]
    pub(crate) has_stale_hidden_dirty: bool,
}

/// Zero-allocation double-ended iterator over a node's direct children in `SceneTree`'s intrusive SoA topology.
#[derive(Clone, Debug)]
pub struct SceneChildIter<'a> {
    /// Shared slice of scene metadata carrying slot generations.
    pub(crate) meta: &'a [NodeMeta],
    /// Shared slice of intrusive sibling links.
    pub(crate) topology: &'a [NodeTopology],
    /// Current forward cursor slot index (`NODE_INDEX_NONE` when exhausted).
    pub(crate) head: u32,
    /// Current backward cursor slot index (`NODE_INDEX_NONE` when exhausted).
    pub(crate) tail: u32,
    /// Remaining unyielded direct children count.
    pub(crate) remaining: usize,
}

/// Shared scene storage pairing interior-mutable `SceneTree` access with a zero-steady-state-allocation deferred write queue.
#[derive(CustomDebug)]
pub(crate) struct SceneHandleInner {
    /// Interior-mutable retained UI scene tree.
    pub(crate) tree: RefCell<SceneTree>,
    /// Fast-path flag indicating at least one deferred mutation is queued (avoids `RefCell` touch in steady state).
    pub(crate) has_deferred: Cell<bool>,
    /// Lazily populated queue of re-entrant scene mutations deferred while `tree` is actively borrowed.
    #[debug(skip)]
    pub(crate) deferred_writes: RefCell<Vec<DeferredSceneWrite>>,
}

/// RAII guard tracking active `SceneTree` borrow nesting depth on the current thread.
#[derive(Debug)]
pub(crate) struct SceneBorrowGuard;

/// Shared handle wrapping a `SceneTree` so reactive `Effect` closures can patch nodes directly.
#[derive(Clone, CustomDebug)]
pub struct SceneHandle {
    /// Shared interior-mutable scene storage and deferred mutation queue.
    pub(crate) inner: Rc<SceneHandleInner>,
}

/// Ambient framework-managed UI context binding the active `SceneHandle`, responsive signals, and platform metadata.
#[derive(Clone, CustomDebug, Data)]
pub struct Context {
    /// Shared scene tree handle for creating and binding nodes.
    pub(crate) scene: SceneHandle,
    /// Current viewport logical breakpoint signal for responsive layouts.
    #[get(type(copy))]
    pub(crate) breakpoint: Signal<Breakpoint>,
    /// Current logical viewport dimensions signal updated immediately on window resize.
    #[get(type(copy))]
    pub(crate) viewport_size: Signal<Size>,
    /// Current OS/app theme mode signal (`Light` / `Dark`).
    #[get(type(copy))]
    pub(crate) theme_mode: Signal<ThemeMode>,
    /// Active platform kind for capability-aware adaptation.
    #[get(type(copy))]
    pub(crate) platform: PlatformKind,
    /// Active safe-area insets signal.
    #[get(type(copy))]
    pub(crate) safe_area: Signal<SafeAreaInsets>,
    /// Reactive stream of recognized cross-platform gestures (`Tap`, `DoubleTap`, `LongPress`, `Swipe*`, `Pan`, `Pinch`).
    #[get(type(copy))]
    pub(crate) gesture: Signal<Option<GestureEvent>>,
    /// Live horizontal touch-swipe drag displacement in logical pixels (`0.0` when idle) for interactive page-swipe feedback.
    #[get(type(copy))]
    pub(crate) swipe_drag_offset: Signal<f32>,
    /// Monotonic counter incremented on OS Back button / Escape / edge-back triggers for unified overlay & router stack dismissal.
    #[get(type(copy))]
    pub(crate) system_back: Signal<u64>,
    /// Shared dynamic update hub enabling ambient component-level and page-level live updates.
    pub(crate) dynamic_hub: DynamicUpdateHub,
    /// Optional target FPS cap across all platforms (`None` = unlimited maximum FPS by default).
    #[get(type(copy))]
    pub(crate) fps_limit: Signal<Option<u32>>,
    /// Smoothed live rendered frames-per-second telemetry signal.
    #[get(type(copy))]
    pub(crate) live_fps: Signal<f32>,
    /// Idle-aware active presented frames-per-second signal (`0.0` while idle).
    #[get(type(copy))]
    pub(crate) present_fps: Signal<f32>,
    /// Reactive flag indicating whether the frame loop is currently idle (`true` when skipping clean frames).
    #[get(type(copy))]
    pub(crate) is_idle: Signal<bool>,
    /// Native hardware display refresh rate in Hertz reported by the active OS/browser host.
    #[get(type(copy))]
    pub(crate) display_refresh_rate: Signal<f32>,
    /// Live snapshot of all currently active touch/pointer contacts `(pointer_id, position)` for multi-touch UIs.
    #[get(type(copy))]
    pub(crate) active_touches: Signal<Vec<(u64, Point)>>,
    /// Reactive flag raised when the application requests graceful termination (e.g. double-back-to-exit at root).
    #[get(type(copy))]
    pub(crate) exit_requested: Signal<bool>,
    /// Canonical cross-platform application lifecycle state signal (`Active`, `Inactive`, `Background`, `Terminated`).
    #[get(type(copy))]
    pub(crate) app_state: Signal<AppState>,
    /// Live network reachability status signal (`Online`, `Offline`, `Wifi`, `Cellular`, `Unknown`).
    #[get(type(copy))]
    pub(crate) network_status: Signal<NetworkStatus>,
    /// Whether the soft virtual keyboard / IME is currently presented.
    #[get(type(copy))]
    pub(crate) keyboard_visible: Signal<bool>,
    /// Current soft virtual keyboard height in logical pixels (`0.0` when hidden).
    #[get(type(copy))]
    pub(crate) keyboard_height: Signal<f32>,
    /// Current screen or window orientation signal.
    #[get(type(copy))]
    pub(crate) orientation: Signal<ScreenOrientation>,
    /// Whether OS battery saver / low-power mode is currently enabled.
    #[get(type(copy))]
    pub(crate) low_power_mode: Signal<bool>,
    /// Active OS/browser preferred BCP-47 locale string signal.
    #[get(type(copy))]
    pub(crate) locale: Signal<String>,
    /// Whether the primary application window currently holds keyboard/input focus.
    #[get(type(copy))]
    pub(crate) window_focused: Signal<bool>,
    /// Reactive flag indicating whether immersive fullscreen mode is active across app chrome and OS system bars.
    #[get(type(copy))]
    #[set(skip)]
    pub(crate) immersive_fullscreen: Signal<bool>,
    /// Live screen-aligned 6-DOF gravity, linear acceleration, and gyroscope sensor sample signal.
    #[get(type(copy))]
    pub(crate) device_motion: Signal<DeviceMotionSample>,
    /// Shared reference-counted context lifetime owner storing clock, fullscreen depth, and owned signals disposed on final drop.
    #[debug(skip)]
    #[get(skip)]
    #[get_mut(skip)]
    #[set(skip)]
    pub(crate) shared: Rc<ContextShared>,
}

/// Shared interior-mutable runtime metadata and signal ownership table disposed automatically when the last `Context` clone drops.
#[derive(Debug)]
pub(crate) struct ContextShared {
    /// Nested immersive fullscreen entry counter enabling balanced RAII enter/exit calls.
    pub(crate) fullscreen_depth: Cell<u32>,
    /// Monotonic runtime clock in milliseconds advanced deterministically by `App` frame steps.
    pub(crate) clock_ms: Cell<u64>,
    /// Detached reactive signals created by `Context::new` and disposed on final drop to guarantee zero lifecycle leak.
    pub(crate) owned_signals: [SignalId; 24],
    /// Reactive runtime epoch active when `owned_signals` were allocated.
    pub(crate) runtime_epoch: u64,
}

/// RAII scope guard restoring the previous ambient `Context` on drop.
#[derive(CustomDebug, New)]
pub struct ContextGuard {
    /// Previously bound ambient context to restore when leaving scope.
    pub(crate) previous: Option<Context>,
}