use nativeview_engine::FocusRecoveryResult;
use nativeview_store::{ChangeSet, NodeId, NodeStore};
use nativeview_tables::transform::Transform2D;
use crate::StoreLayoutEngine;
use super::LaidOutScene;
pub trait CommitHooks<L: StoreLayoutEngine> {
fn after_layout(
&mut self,
_scene: &LaidOutScene<L>,
_store: &NodeStore,
_changes: &ChangeSet,
) -> bool {
false
}
fn resolve_transform(&self, _id: NodeId, base: Option<Transform2D>) -> Option<Transform2D> {
base
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct NoCommitHooks;
impl<L: StoreLayoutEngine> CommitHooks<L> for NoCommitHooks {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CommitOutcome {
pub layout_ran: bool,
pub focus: FocusRecoveryResult,
pub focus_layout_ran: bool,
}
impl<L: StoreLayoutEngine> LaidOutScene<L> {
#[inline]
pub fn commit_frame(&mut self, store: &NodeStore, changes: &ChangeSet) -> CommitOutcome {
self.commit_frame_with(store, changes, &mut NoCommitHooks)
}
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);
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 {
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,
}
}
}