fc_ui/capabilities/motion/
scroll_physics.rs1#[derive(Clone, Debug)]
2pub struct ScrollPhysics {
3 velocity: f32,
4 position: f32,
5 min_bound: f32,
6 max_bound: f32,
7 deceleration: f32,
8 overscroll_resistance: f32,
9 momentum_enabled: bool,
10 overscroll_enabled: bool,
11}
12
13impl Default for ScrollPhysics {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl ScrollPhysics {
20 pub fn new() -> Self {
21 Self {
22 velocity: 0.0,
23 position: 0.0,
24 min_bound: 0.0,
25 max_bound: f32::MAX,
26 deceleration: 0.95,
27 overscroll_resistance: 0.3,
28 momentum_enabled: true,
29 overscroll_enabled: true,
30 }
31 }
32
33 pub fn with_bounds(mut self, min: f32, max: f32) -> Self {
34 self.min_bound = min;
35 self.max_bound = max;
36 self
37 }
38
39 pub fn with_deceleration(mut self, deceleration: f32) -> Self {
40 self.deceleration = deceleration.clamp(0.8, 0.99);
41 self
42 }
43
44 pub fn with_overscroll_resistance(mut self, resistance: f32) -> Self {
45 self.overscroll_resistance = resistance.clamp(0.0, 1.0);
46 self
47 }
48
49 pub fn momentum(mut self, enabled: bool) -> Self {
50 self.momentum_enabled = enabled;
51 self
52 }
53
54 pub fn overscroll(mut self, enabled: bool) -> Self {
55 self.overscroll_enabled = enabled;
56 self
57 }
58
59 pub fn set_bounds(&mut self, min: f32, max: f32) {
60 self.min_bound = min;
61 self.max_bound = max;
62 }
63
64 pub fn apply_delta(&mut self, delta: f32) {
65 if self.momentum_enabled {
66 self.velocity = delta * 0.8 + self.velocity * 0.2;
67 } else {
68 self.velocity = 0.0;
69 }
70 self.position += delta;
71
72 if !self.overscroll_enabled {
73 self.position = self.position.clamp(self.min_bound, self.max_bound);
74 }
75 }
76
77 pub fn tick(&mut self, dt: f32) -> bool {
83 if !self.momentum_enabled && !self.is_overscrolled() {
84 return false;
85 }
86
87 self.velocity *= self.deceleration.powf(dt * 60.0);
88 self.position += self.velocity * dt * 60.0;
89
90 if self.overscroll_enabled {
91 if self.position < self.min_bound {
92 let overshoot = self.min_bound - self.position;
93 self.position += overshoot * self.overscroll_resistance;
94 self.velocity *= 0.5;
95 }
96 if self.position > self.max_bound {
97 let overshoot = self.position - self.max_bound;
98 self.position -= overshoot * self.overscroll_resistance;
99 self.velocity *= 0.5;
100 }
101 } else {
102 self.position = self.position.clamp(self.min_bound, self.max_bound);
103 if self.position <= self.min_bound || self.position >= self.max_bound {
104 self.velocity = 0.0;
105 }
106 }
107
108 self.velocity.abs() > 0.5 || self.is_overscrolled()
109 }
110
111 pub fn position(&self) -> f32 {
112 self.position
113 }
114
115 pub fn velocity(&self) -> f32 {
116 self.velocity
117 }
118
119 pub fn is_moving(&self) -> bool {
120 self.velocity.abs() > 0.5
121 }
122
123 pub fn is_overscrolled(&self) -> bool {
124 self.position < self.min_bound || self.position > self.max_bound
125 }
126
127 pub fn stop(&mut self) {
128 self.velocity = 0.0;
129 }
130
131 pub fn reset(&mut self) {
132 self.velocity = 0.0;
133 self.position = self.min_bound;
134 }
135
136 pub fn set_position(&mut self, position: f32) {
137 self.position = position;
138 }
139
140 pub fn scroll_to(&mut self, position: f32) {
141 self.position = position.clamp(self.min_bound, self.max_bound);
142 self.velocity = 0.0;
143 }
144
145 pub fn fling(&mut self, velocity: f32) {
146 self.velocity = velocity;
147 }
148}