1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#![warn(missing_docs)]
use alloc::boxed::Box;
use core::cell::Cell;
#[cfg(feature = "std")]
use lyon_algorithms::geom::cubic_bezier;
#[cfg(not(feature = "std"))]
mod cubic_bezier {
type S = f32;
use euclid::default::Point2D as Point;
use num_traits::Float;
trait Scalar {
const ONE: f32 = 1.;
const THREE: f32 = 3.;
const HALF: f32 = 0.5;
const SIX: f32 = 6.;
const NINE: f32 = 9.;
fn value(v: f32) -> f32 {
v
}
}
impl Scalar for f32 {}
pub struct CubicBezierSegment {
pub from: Point<S>,
pub ctrl1: Point<S>,
pub ctrl2: Point<S>,
pub to: Point<S>,
}
impl CubicBezierSegment {
pub fn x(&self, t: S) -> S {
let t2 = t * t;
let t3 = t2 * t;
let one_t = S::ONE - t;
let one_t2 = one_t * one_t;
let one_t3 = one_t2 * one_t;
self.from.x * one_t3
+ self.ctrl1.x * S::THREE * one_t2 * t
+ self.ctrl2.x * S::THREE * one_t * t2
+ self.to.x * t3
}
pub fn y(&self, t: S) -> S {
let t2 = t * t;
let t3 = t2 * t;
let one_t = S::ONE - t;
let one_t2 = one_t * one_t;
let one_t3 = one_t2 * one_t;
self.from.y * one_t3
+ self.ctrl1.y * S::THREE * one_t2 * t
+ self.ctrl2.y * S::THREE * one_t * t2
+ self.to.y * t3
}
#[inline]
fn derivative_coefficients(&self, t: S) -> (S, S, S, S) {
let t2 = t * t;
(
-S::THREE * t2 + S::SIX * t - S::THREE,
S::NINE * t2 - S::value(12.0) * t + S::THREE,
-S::NINE * t2 + S::SIX * t,
S::THREE * t2,
)
}
pub fn dx(&self, t: S) -> S {
let (c0, c1, c2, c3) = self.derivative_coefficients(t);
self.from.x * c0 + self.ctrl1.x * c1 + self.ctrl2.x * c2 + self.to.x * c3
}
}
impl CubicBezierSegment {
pub fn solve_t_for_x(&self, x: S, t_range: core::ops::Range<S>, tolerance: S) -> S {
debug_assert!(t_range.start <= t_range.end);
let from = self.x(t_range.start);
let to = self.x(t_range.end);
if x <= from {
return t_range.start;
}
if x >= to {
return t_range.end;
}
let mut t = x - from / (to - from);
for _ in 0..8 {
let x2 = self.x(t);
if S::abs(x2 - x) <= tolerance {
return t;
}
let dx = self.dx(t);
if dx <= S::EPSILON {
break;
}
t -= (x2 - x) / dx;
}
let mut min = t_range.start;
let mut max = t_range.end;
let mut t = S::HALF;
while min < max {
let x2 = self.x(t);
if S::abs(x2 - x) < tolerance {
return t;
}
if x > x2 {
min = t;
} else {
max = t;
}
t = (max - min) * S::HALF + min;
}
t
}
}
}
#[repr(C, u32)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum EasingCurve {
Linear,
CubicBezier([f32; 4]),
}
impl Default for EasingCurve {
fn default() -> Self {
Self::Linear
}
}
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Ord, PartialOrd, Eq)]
pub struct Instant(pub u64);
impl core::ops::Sub<Instant> for Instant {
type Output = core::time::Duration;
fn sub(self, other: Self) -> core::time::Duration {
core::time::Duration::from_millis(self.0 - other.0)
}
}
impl core::ops::Sub<core::time::Duration> for Instant {
type Output = Instant;
fn sub(self, other: core::time::Duration) -> Instant {
Self(self.0 - other.as_millis() as u64)
}
}
impl core::ops::Add<core::time::Duration> for Instant {
type Output = Instant;
fn add(self, other: core::time::Duration) -> Instant {
Self(self.0 + other.as_millis() as u64)
}
}
impl core::ops::AddAssign<core::time::Duration> for Instant {
fn add_assign(&mut self, other: core::time::Duration) {
self.0 += other.as_millis() as u64;
}
}
impl core::ops::SubAssign<core::time::Duration> for Instant {
fn sub_assign(&mut self, other: core::time::Duration) {
self.0 -= other.as_millis() as u64;
}
}
impl Instant {
pub fn duration_since(self, earlier: Instant) -> core::time::Duration {
self - earlier
}
pub fn now() -> Self {
Self(Self::duration_since_start().as_millis() as u64)
}
fn duration_since_start() -> core::time::Duration {
crate::backend::instance().map(|backend| backend.duration_since_start()).unwrap_or_default()
}
}
pub struct AnimationDriver {
active_animations: Cell<bool>,
global_instant: core::pin::Pin<Box<crate::Property<Instant>>>,
}
impl Default for AnimationDriver {
fn default() -> Self {
AnimationDriver {
active_animations: Cell::default(),
global_instant: Box::pin(crate::Property::new_named(
Instant::default(),
"i_slint_core::AnimationDriver::global_instant",
)),
}
}
}
impl AnimationDriver {
pub fn update_animations(&self, new_tick: Instant) {
if self.global_instant.as_ref().get_untracked() != new_tick {
self.active_animations.set(false);
self.global_instant.as_ref().set(new_tick);
}
}
pub fn has_active_animations(&self) -> bool {
self.active_animations.get()
}
pub fn set_has_active_animations(&self) {
self.active_animations.set(true);
}
pub fn current_tick(&self) -> Instant {
self.global_instant.as_ref().get()
}
}
#[cfg(all(not(feature = "std"), feature = "unsafe_single_core"))]
use crate::unsafe_single_core::thread_local;
thread_local!(
pub static CURRENT_ANIMATION_DRIVER : AnimationDriver = AnimationDriver::default()
);
pub fn current_tick() -> Instant {
CURRENT_ANIMATION_DRIVER.with(|driver| driver.current_tick())
}
pub fn easing_curve(curve: &EasingCurve, value: f32) -> f32 {
match curve {
EasingCurve::Linear => value,
EasingCurve::CubicBezier([a, b, c, d]) => {
if !(0.0..=1.0).contains(a) && !(0.0..=1.0).contains(c) {
return value;
};
let curve = cubic_bezier::CubicBezierSegment {
from: (0., 0.).into(),
ctrl1: (*a, *b).into(),
ctrl2: (*c, *d).into(),
to: (1., 1.).into(),
};
#[cfg(feature = "std")]
let curve = curve.assume_monotonic();
curve.y(curve.solve_t_for_x(value, 0.0..1.0, 0.01))
}
}
}
pub fn update_animations() {
CURRENT_ANIMATION_DRIVER.with(|driver| {
#[allow(unused_mut)]
let mut duration = Instant::duration_since_start().as_millis() as u64;
#[cfg(feature = "std")]
if let Ok(val) = std::env::var("SLINT_SLOW_ANIMATIONS") {
let factor = val.parse().unwrap_or(2);
duration /= factor;
};
driver.update_animations(Instant(duration))
});
}