Skip to main content

strata/
view_transition.rs

1//! CSS View Transitions Module Level 1 & Level 2 snapshot and morphing engine.
2
3use std::collections::HashMap;
4use strata_primitives::geom::Rect;
5
6/// A captured snapshot pairing old and new layout bounds for an element with `view-transition-name`.
7#[derive(Debug, Clone, PartialEq)]
8pub struct ViewTransitionSnapshot {
9    pub name: String,
10    pub old_bounds: Rect,
11    pub new_bounds: Rect,
12    pub start_time_ms: f64,
13    pub duration_ms: f32,
14    pub is_finished: bool,
15}
16
17/// Coordinates same-document view transitions, cross-fading and interpolating geometry between layout states.
18#[derive(Debug, Clone, Default)]
19pub struct ViewTransitionManager {
20    pub active_transitions: HashMap<String, ViewTransitionSnapshot>,
21    pub pending_old_snapshots: HashMap<String, Rect>,
22}
23
24impl ViewTransitionManager {
25    /// Creates a new, empty `ViewTransitionManager`.
26    pub fn new() -> Self {
27        Self::default()
28    }
29
30    /// Captures the current bounding boxes of all named view-transition elements before a DOM mutation.
31    pub fn capture_old_state(&mut self, named_elements: &[(String, Rect)]) {
32        for (name, rect) in named_elements {
33            if name != "none" {
34                self.pending_old_snapshots.insert(name.clone(), *rect);
35            }
36        }
37    }
38
39    /// Starts view transitions by pairing previously captured old bounds with newly computed layout rects.
40    pub fn start_transition(
41        &mut self,
42        new_elements: &[(String, Rect)],
43        now_ms: f64,
44        duration_ms: f32,
45    ) {
46        for (name, new_rect) in new_elements {
47            if let Some(old_rect) = self.pending_old_snapshots.remove(name) {
48                self.active_transitions.insert(
49                    name.clone(),
50                    ViewTransitionSnapshot {
51                        name: name.clone(),
52                        old_bounds: old_rect,
53                        new_bounds: *new_rect,
54                        start_time_ms: now_ms,
55                        duration_ms: duration_ms.max(10.0),
56                        is_finished: false,
57                    },
58                );
59            }
60        }
61        self.pending_old_snapshots.clear();
62    }
63
64    /// Advances active view transitions at timestamp `now_ms`. Returns true if any transition remains active.
65    pub fn step(&mut self, now_ms: f64) -> bool {
66        let mut active = false;
67        for trans in self.active_transitions.values_mut() {
68            let elapsed = (now_ms - trans.start_time_ms) as f32;
69            if elapsed >= trans.duration_ms {
70                trans.is_finished = true;
71            } else {
72                active = true;
73            }
74        }
75        self.active_transitions.retain(|_, t| !t.is_finished);
76        active
77    }
78
79    /// Samples current interpolated bounding rect and progress `t` in [0.0, 1.0] for a named transition.
80    pub fn sample(&self, name: &str, now_ms: f64) -> Option<(Rect, f32)> {
81        let trans = self.active_transitions.get(name)?;
82        let elapsed = (now_ms - trans.start_time_ms).max(0.0) as f32;
83        let t = (elapsed / trans.duration_ms).clamp(0.0, 1.0);
84        let old_r = &trans.old_bounds;
85        let new_r = &trans.new_bounds;
86        let current_rect = Rect::from_xywh(
87            old_r.origin.x + (new_r.origin.x - old_r.origin.x) * t,
88            old_r.origin.y + (new_r.origin.y - old_r.origin.y) * t,
89            old_r.size.width + (new_r.size.width - old_r.size.width) * t,
90            old_r.size.height + (new_r.size.height - old_r.size.height) * t,
91        );
92        Some((current_rect, t))
93    }
94
95    /// Returns true if there are any active view transitions in progress.
96    pub fn has_active_transitions(&self) -> bool {
97        !self.active_transitions.is_empty()
98    }
99}