Skip to main content

openlogi_core/
scroll.rs

1//! Platform-neutral scroll distances.
2
3/// A signed two-axis scroll distance with an explicit unit.
4///
5/// Positive horizontal values scroll right; positive vertical values scroll
6/// up. Keeping pixels distinct from standard wheel ticks prevents a smooth
7/// scrolling runtime from accidentally interpolating or accumulating unlike
8/// quantities.
9#[derive(Clone, Copy, Debug, PartialEq)]
10pub enum ScrollDelta {
11    /// Pixel-precise scrolling, as reported by continuous macOS wheel events.
12    Pixels {
13        /// Horizontal distance; positive scrolls right.
14        x: f64,
15        /// Vertical distance; positive scrolls up.
16        y: f64,
17    },
18    /// Standard wheel ticks. One tick is one detent, represented by 120
19    /// high-resolution wheel units on Linux and Windows.
20    WheelTicks {
21        /// Horizontal distance; positive scrolls right.
22        x: f64,
23        /// Vertical distance; positive scrolls up.
24        y: f64,
25    },
26}
27
28impl ScrollDelta {
29    /// Construct a pixel-precise scroll distance.
30    #[must_use]
31    pub const fn pixels(x: f64, y: f64) -> Self {
32        Self::Pixels { x, y }
33    }
34
35    /// Construct a scroll distance in standard wheel ticks.
36    #[must_use]
37    pub const fn wheel_ticks(x: f64, y: f64) -> Self {
38        Self::WheelTicks { x, y }
39    }
40
41    /// Return the signed horizontal distance in this value's unit.
42    #[must_use]
43    pub const fn x(self) -> f64 {
44        match self {
45            Self::Pixels { x, .. } | Self::WheelTicks { x, .. } => x,
46        }
47    }
48
49    /// Return the signed vertical distance in this value's unit.
50    #[must_use]
51    pub const fn y(self) -> f64 {
52        match self {
53            Self::Pixels { y, .. } | Self::WheelTicks { y, .. } => y,
54        }
55    }
56
57    /// Whether both components are finite numbers suitable for interpolation.
58    #[must_use]
59    pub fn is_finite(self) -> bool {
60        self.x().is_finite() && self.y().is_finite()
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::ScrollDelta;
67
68    #[test]
69    fn units_remain_distinct() {
70        assert_ne!(
71            ScrollDelta::pixels(1.0, -2.0),
72            ScrollDelta::wheel_ticks(1.0, -2.0)
73        );
74    }
75
76    #[test]
77    fn rejects_non_finite_components() {
78        assert!(!ScrollDelta::pixels(f64::NAN, 0.0).is_finite());
79        assert!(!ScrollDelta::wheel_ticks(0.0, f64::INFINITY).is_finite());
80        assert!(ScrollDelta::wheel_ticks(0.25, -1.0).is_finite());
81    }
82}