Skip to main content

nativeview_scroll_physics/
profile.rs

1//! Platform-tuned scroll physics presets ([`ScrollPhysicsProfile`]) and [`PlatformScrollPhysics`]
2//! engine constructors.
3
4use nativeview_physics::{DecaySpec, RubberBandSpec, SmoothWheelSpec, SpringSpec};
5
6use crate::behavior::{ScrollBehavior, SnapQuery, SpringReturn};
7use crate::pool::PooledScrollPhysics;
8
9/// Apple asymptote rubber-band drag resistance: `sign(raw) * c*|raw|*D / (D + c*|raw|)`.
10#[inline]
11pub fn rubber_band_forward(raw: f32, viewport: f32) -> f32 {
12    RubberBandSpec::APPLE.forward(raw, viewport)
13}
14
15/// Exact inverse of [`rubber_band_forward`] for smooth drag unwinding.
16#[inline]
17pub fn rubber_band_inverse(visual: f32, viewport: f32) -> f32 {
18    RubberBandSpec::APPLE.inverse(visual, viewport)
19}
20
21/// Platform-tuned scroll physics profile (`deceleration`, spring frequency, wheel line step,
22/// smooth-wheel time constant, and default rubber-band elasticity).
23#[repr(u8)]
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25pub enum ScrollPhysicsProfile {
26    Apple = 0,
27    Android = 1,
28    Windows = 2,
29    Linux = 3,
30}
31
32impl Default for ScrollPhysicsProfile {
33    #[inline]
34    fn default() -> Self {
35        Self::platform_default()
36    }
37}
38
39impl ScrollPhysicsProfile {
40    #[inline]
41    pub const fn platform_default() -> Self {
42        #[cfg(any(target_os = "macos", target_os = "ios"))]
43        {
44            Self::Apple
45        }
46        #[cfg(target_os = "android")]
47        {
48            Self::Android
49        }
50        #[cfg(target_os = "windows")]
51        {
52            Self::Windows
53        }
54        #[cfg(not(any(
55            target_os = "macos",
56            target_os = "ios",
57            target_os = "android",
58            target_os = "windows"
59        )))]
60        {
61            Self::Linux
62        }
63    }
64
65    #[inline]
66    pub const fn deceleration_per_ms(self) -> f32 {
67        self.decay_spec().deceleration_per_ms.0
68    }
69
70    #[inline]
71    pub const fn spring_omega(self) -> f32 {
72        match self {
73            Self::Apple => 18.0,
74            Self::Android | Self::Windows | Self::Linux => 20.0,
75        }
76    }
77
78    #[inline]
79    pub const fn wheel_smooth_tau(self) -> f32 {
80        self.smooth_wheel_spec().tau_s
81    }
82
83    #[inline]
84    pub const fn px_per_line(self) -> f32 {
85        match self {
86            Self::Windows => 48.0,
87            Self::Apple | Self::Android | Self::Linux => 40.0,
88        }
89    }
90
91    #[inline]
92    pub const fn rubber_band_default(self) -> bool {
93        matches!(self, Self::Apple | Self::Android)
94    }
95
96    #[inline]
97    pub const fn decay_spec(self) -> DecaySpec {
98        match self {
99            Self::Apple => DecaySpec::apple(),
100            Self::Android => DecaySpec::android(),
101            Self::Windows | Self::Linux => DecaySpec::desktop(),
102        }
103    }
104
105    #[inline]
106    pub fn spring_spec(self) -> SpringSpec {
107        SpringReturn::from_omega(self.spring_omega()).0
108    }
109
110    #[inline]
111    pub const fn smooth_wheel_spec(self) -> SmoothWheelSpec {
112        match self {
113            Self::Apple => SmoothWheelSpec::apple(),
114            Self::Windows => SmoothWheelSpec::windows(),
115            Self::Android | Self::Linux => SmoothWheelSpec::default_smooth(),
116        }
117    }
118}
119
120impl ScrollBehavior for ScrollPhysicsProfile {
121    #[inline]
122    fn bounce_by_default(&self) -> bool {
123        self.rubber_band_default()
124    }
125
126    #[inline]
127    fn rubber_band_spec(&self) -> Option<RubberBandSpec> {
128        Some(RubberBandSpec::APPLE)
129    }
130
131    #[inline]
132    fn decay_spec(&self) -> Option<DecaySpec> {
133        Some((*self).decay_spec())
134    }
135
136    #[inline]
137    fn snap_target(&self, _query: SnapQuery) -> Option<f32> {
138        None
139    }
140
141    #[inline]
142    fn return_spring(&self) -> SpringSpec {
143        (*self).spring_spec()
144    }
145
146    #[inline]
147    fn smooth_wheel_spec(&self) -> Option<SmoothWheelSpec> {
148        Some((*self).smooth_wheel_spec())
149    }
150
151    #[inline]
152    fn px_per_line(&self) -> f32 {
153        (*self).px_per_line()
154    }
155}
156
157/// Platform-preset [`PooledScrollPhysics`] engine ready to install onto `LaidOutScene`,
158/// `SkiaBackend`, or `DesktopSession`.
159pub type PlatformScrollPhysics = PooledScrollPhysics<ScrollPhysicsProfile>;
160
161impl PlatformScrollPhysics {
162    #[inline]
163    pub fn platform_default() -> Self {
164        Self::new(ScrollPhysicsProfile::platform_default())
165    }
166
167    #[inline]
168    pub fn apple() -> Self {
169        Self::new(ScrollPhysicsProfile::Apple)
170    }
171
172    #[inline]
173    pub fn android() -> Self {
174        Self::new(ScrollPhysicsProfile::Android)
175    }
176
177    #[inline]
178    pub fn windows() -> Self {
179        Self::new(ScrollPhysicsProfile::Windows)
180    }
181
182    #[inline]
183    pub fn linux() -> Self {
184        Self::new(ScrollPhysicsProfile::Linux)
185    }
186
187    #[inline]
188    pub fn profile(&self) -> ScrollPhysicsProfile {
189        *self.behavior()
190    }
191
192    #[inline]
193    pub fn set_profile(&mut self, profile: ScrollPhysicsProfile) {
194        *self.behavior_mut() = profile;
195    }
196}