Skip to main content

glory_ui/list/
scroll.rs

1use super::{Error, nonnegative, positive, valid_extent};
2
3/// A bounded physical scroll surface with logical coordinates. Re-centering
4/// preserves pixel-sized wheel movement instead of multiplying every wheel
5/// delta by the ratio of a huge data set to a browser's safe scroll extent.
6#[derive(Debug)]
7pub struct ScrollSpace {
8    total: f64,
9    viewport: f64,
10    limit: f64,
11    origin: f64,
12}
13
14#[derive(Clone, Copy, Debug, PartialEq)]
15pub struct ScrollUpdate {
16    pub logical: f64,
17    pub physical: f64,
18    /// Host must set scrollTop and reposition rows together when true.
19    pub rebased: bool,
20}
21
22impl ScrollSpace {
23    pub fn new(total: f64, viewport: f64, limit: f64) -> Result<Self, Error> {
24        if !valid_extent(total)
25            || !nonnegative(viewport)
26            || !positive(limit)
27            || (total > limit && viewport >= limit)
28        {
29            return Err(Error::InvalidSize);
30        }
31        Ok(Self {
32            total,
33            viewport,
34            limit,
35            origin: 0.0,
36        })
37    }
38
39    pub fn extent(&self) -> f64 {
40        self.total.min(self.limit)
41    }
42    pub fn origin(&self) -> f64 {
43        self.origin
44    }
45    pub fn project(&self, logical: f64) -> f64 {
46        logical - self.origin
47    }
48
49    /// Programmatic index/end/home navigation uses a logical target. The native
50    /// scrollbar represents the current segment, not a misleading scaled total.
51    pub fn seek(&mut self, logical: f64) -> Result<ScrollUpdate, Error> {
52        if !nonnegative(logical) {
53            return Err(Error::InvalidSize);
54        }
55        let logical = logical.min((self.total - self.viewport).max(0.0));
56        let previous = self.origin;
57        let span = (self.extent() - self.viewport).max(0.0);
58        self.origin = (logical - span / 2.0).clamp(0.0, (self.total - self.extent()).max(0.0));
59        Ok(ScrollUpdate {
60            logical,
61            physical: logical - self.origin,
62            rebased: self.origin != previous,
63        })
64    }
65
66    /// Process one coalesced host scroll position. Rebase near the segment edges
67    /// before the physical end prevents further wheel/key movement.
68    pub fn scrolled(&mut self, physical: f64) -> Result<ScrollUpdate, Error> {
69        if !physical.is_finite() {
70            return Err(Error::InvalidSize);
71        }
72        let span = (self.extent() - self.viewport).max(0.0);
73        let physical = physical.clamp(0.0, span);
74        let logical = (self.origin + physical).min((self.total - self.viewport).max(0.0));
75        if self.total > self.limit && (physical < span * 0.2 || physical > span * 0.8) {
76            self.seek(logical)
77        } else {
78            Ok(ScrollUpdate {
79                logical,
80                physical,
81                rebased: false,
82            })
83        }
84    }
85
86    /// Preserve the old logical anchor when measurements or viewport change.
87    pub fn resize(
88        &mut self,
89        total: f64,
90        viewport: f64,
91        logical: f64,
92    ) -> Result<ScrollUpdate, Error> {
93        let replacement = Self::new(total, viewport, self.limit)?;
94        if !nonnegative(logical) {
95            return Err(Error::InvalidSize);
96        }
97        let previous = self.origin;
98        *self = replacement;
99        let mut update = self.seek(logical)?;
100        update.rebased = self.origin != previous;
101        Ok(update)
102    }
103}