nativeview-layout 0.1.0-rc.1

Shared NodeStore layout layer: StoreLayoutEngine / TextMeasurer traits and the default direct Taffy engine
Documentation
//! The single commit pipeline of a [`LaidOutScene`]: every backend and runtime applies a
//! committed `ChangeSet` through [`LaidOutScene::commit_frame`] /
//! [`LaidOutScene::commit_frame_with`], so sync → layout → scroll → overlays → focus (→ scroll,
//! overlays again after a focus-driven re-layout) runs in exactly one order everywhere.

use nativeview_engine::FocusRecoveryResult;
use nativeview_store::{ChangeSet, NodeId, NodeStore};
use nativeview_tables::transform::Transform2D;

use crate::StoreLayoutEngine;

use super::LaidOutScene;

/// Backend extensions threaded through [`LaidOutScene::commit_frame_with`].
pub trait CommitHooks<L: StoreLayoutEngine> {
    /// Runs once layout and scroll offsets are current, before overlays are reconciled. Returns
    /// `true` when overlay geometry must be re-resolved even if layout and scroll did not move
    /// (e.g. a frame hook now animates an anchor's transform).
    fn after_layout(
        &mut self,
        _scene: &LaidOutScene<L>,
        _store: &NodeStore,
        _changes: &ChangeSet,
    ) -> bool {
        false
    }

    /// Effective (e.g. animated) transform of `id`, given its committed `base`, used to place
    /// anchored overlays.
    fn resolve_transform(&self, _id: NodeId, base: Option<Transform2D>) -> Option<Transform2D> {
        base
    }
}

/// [`CommitHooks`] that add nothing (store-only backends, the runtime).
#[derive(Debug, Clone, Copy, Default)]
pub struct NoCommitHooks;

impl<L: StoreLayoutEngine> CommitHooks<L> for NoCommitHooks {}

/// What one [`LaidOutScene::commit_frame`] did.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CommitOutcome {
    /// A layout pass ran (rects may have changed), including a focus-driven re-layout.
    pub layout_ran: bool,
    /// Deterministic focus recovery applied after overlays were reconciled.
    pub focus: FocusRecoveryResult,
    /// Focus recovery changed `StateRules` / paragraphs and re-ran layout.
    pub focus_layout_ran: bool,
}

impl<L: StoreLayoutEngine> LaidOutScene<L> {
    /// Applies one committed `ChangeSet` (empty on clean ticks): the only commit entry point.
    #[inline]
    pub fn commit_frame(&mut self, store: &NodeStore, changes: &ChangeSet) -> CommitOutcome {
        self.commit_frame_with(store, changes, &mut NoCommitHooks)
    }

    /// [`Self::commit_frame`] with backend `hooks` (animated transforms, frame hooks).
    pub fn commit_frame_with<H: CommitHooks<L>>(
        &mut self,
        store: &NodeStore,
        changes: &ChangeSet,
        hooks: &mut H,
    ) -> CommitOutcome {
        self.commit_changed_override = None;
        self.sync_changes(store, changes);
        let ran = self.recompute_layout(store);
        let _ = self.reconcile_scroll_offsets_if_needed(store, changes, ran);
        let hooks_dirty = hooks.after_layout(self, store, changes);
        // Offsets moved by layout clamping above or by wheel / physics input since the last
        // reconcile: anchored overlays follow them here, once.
        let scrolled = self.scroll.take_moved();
        self.reconcile_overlays_if_needed(
            store,
            changes,
            ran || scrolled || hooks_dirty,
            |id, b| hooks.resolve_transform(id, b),
        );
        let (focus, focus_layout_ran) = self.reconcile_focus_after_commit(store, ran);
        if focus_layout_ran {
            // `last_reconciled_scroll_nodes` reports this commit's union of both scroll passes.
            let scroll = &mut self.scroll;
            scroll.commit_changed.clear();
            scroll
                .commit_changed
                .extend_from_slice(&scroll.changed_scratch);
            let _ = self.reconcile_scroll_offsets_if_needed(store, changes, true);
            let scroll = &mut self.scroll;
            for &id in &scroll.commit_changed {
                if !scroll.changed_scratch.contains(&id) {
                    scroll.changed_scratch.push(id);
                }
            }
            self.reconcile_overlays_if_needed(store, changes, true, |id, b| {
                hooks.resolve_transform(id, b)
            });
        }
        self.focus_policy.on_scene_commit(store);
        CommitOutcome {
            layout_ran: ran || focus_layout_ran,
            focus,
            focus_layout_ran,
        }
    }
}