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
use super::Context;
use crate::time::Instant;
use crate::{
animation::{
state::{Control, Pass},
Animated, Animation, AnimationOptions, AnimationStatus, Interpolate, TweenOptions,
},
Id,
};
impl Context {
/// First appearance snaps; retarget preserves sampled position AND velocity.
pub fn spring_transition<T: crate::SpringValue>(
&mut self,
id: Id,
target: T,
options: crate::SpringOptions,
) -> Animated<crate::SpringState<T>> {
self.animations
.spring(id, None, target, options, self.animation_pass(true))
}
pub fn spring_transition_from<T: crate::SpringValue>(
&mut self,
id: Id,
initial: crate::SpringState<T>,
target: T,
options: crate::SpringOptions,
) -> Animated<crate::SpringState<T>> {
self.animations.spring(
id,
Some(initial),
target,
options,
self.animation_pass(true),
)
}
/// Remove a channel now, including its deadline. Reappearance starts fresh.
pub fn remove_animation(&mut self, id: Id) {
self.animations.remove(id);
}
/// Monotonic time fixed at the start of the current (or latest) UI pass.
/// Animation controls called outside a pass take effect at this time, too.
pub fn frame_time(&self) -> Instant {
self.frame_time
}
pub(crate) fn animation_pass(&self, visible: bool) -> Pass {
Pass {
now: self.frame_time,
frame: self.frame,
visible,
reduced: self.style.motion.reduced_motion,
interval: self.style.motion.frame_interval,
scale: {
let scale = f64::from(self.style.motion.time_scale);
if scale.is_finite() && scale > 0.0 {
scale
} else {
1.0
}
},
}
}
/// First appearance snaps to `target`. Changing the target retweens from the
/// current value; equal targets (including completed/cancelled) never restart.
/// Use property channels such as `widget_id.with("color")`. Call only while
/// visible; Ui helpers additionally check clipping. Options apply on retarget.
pub fn transition<T: Interpolate>(
&mut self,
id: Id,
target: T,
options: TweenOptions,
) -> Animated<T> {
self.transition_visible(id, None, target, options, true)
}
/// Like `transition`, but the first appearance animates from `initial`.
pub fn transition_from<T: Interpolate>(
&mut self,
id: Id,
initial: T,
target: T,
options: TweenOptions,
) -> Animated<T> {
self.transition_visible(id, Some(initial), target, options, true)
}
/// Like `transition`, but retargeting a running transition keeps its current
/// velocity instead of restarting the easing curve from zero speed, so
/// repeated changes of mind never produce a visible hitch. The retargeted
/// leg lasts `options.duration` and ignores easing; from rest the options
/// apply unchanged. Needs vector arithmetic: `f32`, `f64`, `Vec2` or an
/// application `SpringValue`.
pub fn transition_smooth<T: crate::SpringValue + Interpolate>(
&mut self,
id: Id,
target: T,
options: TweenOptions,
) -> Animated<T> {
self.transition_smooth_visible(id, target, options, true)
}
pub(crate) fn transition_smooth_visible<T: crate::SpringValue + Interpolate>(
&mut self,
id: Id,
target: T,
options: TweenOptions,
visible: bool,
) -> Animated<T> {
self.animations
.transition_smooth(id, None, target, options, self.animation_pass(visible))
}
pub(crate) fn transition_visible<T: Interpolate>(
&mut self,
id: Id,
initial: Option<T>,
target: T,
options: TweenOptions,
visible: bool,
) -> Animated<T> {
self.animations
.transition(id, initial, target, options, self.animation_pass(visible))
}
/// Create once per visible lifetime. The factory is not called again on
/// subsequent passes, even after completion or cancellation. Restart explicitly.
pub fn animate<T: Clone + 'static, A: Animation<T>>(
&mut self,
id: Id,
create: impl FnOnce() -> A,
) -> Animated<T> {
self.animate_with(id, AnimationOptions::default(), create)
}
pub fn animate_with<T: Clone + 'static, A: Animation<T>>(
&mut self,
id: Id,
options: AnimationOptions,
create: impl FnOnce() -> A,
) -> Animated<T> {
self.animations
.animate(id, options, create, self.animation_pass(true))
}
/// Replace a channel and reset its clock to zero, clearing pause/cancel/events.
/// This may deliberately jump to the track's initial value; use transition for
/// continuity. A subsequent animate/sample call marks the channel visible.
pub fn restart_animation<T: Clone + 'static>(&mut self, id: Id, animation: impl Animation<T>) {
self.restart_animation_with(id, animation, AnimationOptions::default());
}
pub fn restart_animation_with<T: Clone + 'static>(
&mut self,
id: Id,
animation: impl Animation<T>,
options: AnimationOptions,
) {
self.animations
.restart(id, animation, options, self.animation_pass(true));
}
/// Read an existing channel and keep it alive for this visible pass.
pub fn sample_animation<T: Clone + 'static>(&mut self, id: Id) -> Option<Animated<T>> {
self.animations.read(id, self.animation_pass(true))
}
pub fn animation_status(&self, id: Id) -> Option<AnimationStatus> {
self.animations.status(id)
}
/// Freeze value and elapsed time, including any remaining delay. Idempotent.
pub fn pause_animation(&mut self, id: Id) -> bool {
self.animations.control(id, Control::Pause, self.frame_time)
}
/// Continue from frozen time. Has no effect on completed/cancelled channels.
pub fn resume_animation(&mut self, id: Id) -> bool {
self.animations
.control(id, Control::Resume, self.frame_time)
}
/// Freeze the sampled value, discard the track, and suppress completion.
pub fn cancel_animation(&mut self, id: Id) -> bool {
self.animations
.control(id, Control::Cancel, self.frame_time)
}
/// Jump a running or paused channel to `elapsed` track time. A paused
/// channel shows the scrubbed pose immediately and stays paused; a position
/// past the end completes the channel. Completed and cancelled channels
/// hold no track, so they cannot be seeked: restart them instead.
pub fn seek_animation(&mut self, id: Id, elapsed: std::time::Duration) -> bool {
self.animations
.control(id, Control::Seek(elapsed), self.frame_time)
}
/// Playback rate of one channel (1.0 by default, 2.0 twice as fast,
/// negative backwards). Playing backwards completes at the start pose.
/// Position is preserved; restarting resets the rate. Zero or non-finite
/// rates panic: use `pause_animation` to stop a channel.
pub fn set_animation_rate(&mut self, id: Id, rate: f64) -> bool {
assert!(
rate.is_finite() && rate != 0.0,
"animation rate must be finite and nonzero"
);
self.animations
.control(id, Control::Rate(rate), self.frame_time)
}
/// Flip the direction of a running or paused channel from its current pose.
pub fn reverse_animation(&mut self, id: Id) -> bool {
self.animations
.control(id, Control::Reverse, self.frame_time)
}
/// Track time reached by the channel, including the effect of rate and
/// `MotionStyle::time_scale`. Useful to drive a scrubber.
pub fn animation_elapsed(&self, id: Id) -> Option<std::time::Duration> {
self.animations.elapsed(id, self.frame_time)
}
pub fn animation_rate(&self, id: Id) -> Option<f64> {
self.animations.rate(id)
}
/// Exact length of the active track, when it has one.
pub fn animation_duration(&self, id: Id) -> Option<std::time::Duration> {
self.animations.duration(id)
}
/// Apply the exact final value and emit completion once, including when paused.
pub fn finish_animation(&mut self, id: Id) -> bool {
self.animations
.control(id, Control::Finish, self.frame_time)
}
}