Skip to main content

nativeview_layout/scene/
commit.rs

1//! The single commit pipeline of a [`LaidOutScene`]: every backend and runtime applies a
2//! committed `ChangeSet` through [`LaidOutScene::commit_frame`] /
3//! [`LaidOutScene::commit_frame_with`], so sync → layout → scroll → overlays → focus (→ scroll,
4//! overlays again after a focus-driven re-layout) runs in exactly one order everywhere.
5
6use nativeview_engine::FocusRecoveryResult;
7use nativeview_store::{ChangeSet, NodeId, NodeStore};
8use nativeview_tables::transform::Transform2D;
9
10use crate::StoreLayoutEngine;
11
12use super::LaidOutScene;
13
14/// Backend extensions threaded through [`LaidOutScene::commit_frame_with`].
15pub trait CommitHooks<L: StoreLayoutEngine> {
16    /// Runs once layout and scroll offsets are current, before overlays are reconciled. Returns
17    /// `true` when overlay geometry must be re-resolved even if layout and scroll did not move
18    /// (e.g. a frame hook now animates an anchor's transform).
19    fn after_layout(
20        &mut self,
21        _scene: &LaidOutScene<L>,
22        _store: &NodeStore,
23        _changes: &ChangeSet,
24    ) -> bool {
25        false
26    }
27
28    /// Effective (e.g. animated) transform of `id`, given its committed `base`, used to place
29    /// anchored overlays.
30    fn resolve_transform(&self, _id: NodeId, base: Option<Transform2D>) -> Option<Transform2D> {
31        base
32    }
33}
34
35/// [`CommitHooks`] that add nothing (store-only backends, the runtime).
36#[derive(Debug, Clone, Copy, Default)]
37pub struct NoCommitHooks;
38
39impl<L: StoreLayoutEngine> CommitHooks<L> for NoCommitHooks {}
40
41/// What one [`LaidOutScene::commit_frame`] did.
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub struct CommitOutcome {
44    /// A layout pass ran (rects may have changed), including a focus-driven re-layout.
45    pub layout_ran: bool,
46    /// Deterministic focus recovery applied after overlays were reconciled.
47    pub focus: FocusRecoveryResult,
48    /// Focus recovery changed `StateRules` / paragraphs and re-ran layout.
49    pub focus_layout_ran: bool,
50}
51
52impl<L: StoreLayoutEngine> LaidOutScene<L> {
53    /// Applies one committed `ChangeSet` (empty on clean ticks): the only commit entry point.
54    #[inline]
55    pub fn commit_frame(&mut self, store: &NodeStore, changes: &ChangeSet) -> CommitOutcome {
56        self.commit_frame_with(store, changes, &mut NoCommitHooks)
57    }
58
59    /// [`Self::commit_frame`] with backend `hooks` (animated transforms, frame hooks).
60    pub fn commit_frame_with<H: CommitHooks<L>>(
61        &mut self,
62        store: &NodeStore,
63        changes: &ChangeSet,
64        hooks: &mut H,
65    ) -> CommitOutcome {
66        self.commit_changed_override = None;
67        self.sync_changes(store, changes);
68        let ran = self.recompute_layout(store);
69        let _ = self.reconcile_scroll_offsets_if_needed(store, changes, ran);
70        let hooks_dirty = hooks.after_layout(self, store, changes);
71        // Offsets moved by layout clamping above or by wheel / physics input since the last
72        // reconcile: anchored overlays follow them here, once.
73        let scrolled = self.scroll.take_moved();
74        self.reconcile_overlays_if_needed(
75            store,
76            changes,
77            ran || scrolled || hooks_dirty,
78            |id, b| hooks.resolve_transform(id, b),
79        );
80        let (focus, focus_layout_ran) = self.reconcile_focus_after_commit(store, ran);
81        if focus_layout_ran {
82            // `last_reconciled_scroll_nodes` reports this commit's union of both scroll passes.
83            let scroll = &mut self.scroll;
84            scroll.commit_changed.clear();
85            scroll
86                .commit_changed
87                .extend_from_slice(&scroll.changed_scratch);
88            let _ = self.reconcile_scroll_offsets_if_needed(store, changes, true);
89            let scroll = &mut self.scroll;
90            for &id in &scroll.commit_changed {
91                if !scroll.changed_scratch.contains(&id) {
92                    scroll.changed_scratch.push(id);
93                }
94            }
95            self.reconcile_overlays_if_needed(store, changes, true, |id, b| {
96                hooks.resolve_transform(id, b)
97            });
98        }
99        self.focus_policy.on_scene_commit(store);
100        CommitOutcome {
101            layout_ran: ran || focus_layout_ran,
102            focus,
103            focus_layout_ran,
104        }
105    }
106}