Skip to main content

yew_virtual/core/
scroll_state.rs

1use crate::core::scroll_alignment::ScrollAlignment;
2use crate::core::scroll_behavior::ScrollBehavior;
3
4/// Internal state for an active programmatic scroll operation.
5///
6/// Tracks the target, alignment, behavior, and settling progress
7/// of an in-flight scroll reconciliation loop. Used by the virtualizer
8/// to determine when a smooth scroll has reached its destination.
9#[derive(Debug, Clone, PartialEq)]
10pub struct ScrollState {
11    /// The target item index, if scrolling to a specific item.
12    pub index: Option<usize>,
13
14    /// The alignment used for this scroll operation.
15    pub align: ScrollAlignment,
16
17    /// The animation behavior for the scroll.
18    pub behavior: ScrollBehavior,
19
20    /// Timestamp (ms) when this scroll operation started.
21    pub started_at: f64,
22
23    /// The last computed target scroll offset.
24    pub last_target_offset: f64,
25
26    /// Number of consecutive frames where the scroll position
27    /// matched the target (used for settling detection).
28    pub stable_frames: u32,
29}
30
31impl ScrollState {
32    /// Creates a new scroll state for a programmatic scroll operation.
33    ///
34    /// # Parameters
35    ///
36    /// - `index`: Optional target item index.
37    /// - `align`: The alignment for the scroll.
38    /// - `behavior`: The animation behavior.
39    /// - `started_at`: Timestamp when the scroll started.
40    /// - `last_target_offset`: The initial target scroll offset.
41    pub fn new(
42        index: Option<usize>,
43        align: ScrollAlignment,
44        behavior: ScrollBehavior,
45        started_at: f64,
46        last_target_offset: f64,
47    ) -> Self {
48        // Construct the initial scroll state with zero stable frames.
49        Self {
50            index,
51            align,
52            behavior,
53            started_at,
54            last_target_offset,
55            stable_frames: 0,
56        }
57    }
58}