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
use std::cell::RefCell;
use std::rc::Rc;
use repose_core::{
Color, Rect, Size, Vec2,
animation::{AnimatedValue, AnimationSpec, KeyframesSpec, RepeatableSpec, SplineKeyframes},
animation_driver, remember_state_with_key, request_frame,
};
/// Animate f32 from an explicit initial value to a target.
/// - On first creation for this key, starts at `initial` then animates toward `target`.
/// - On later calls, animates from the current value to the new target.
pub fn animate_f32_from(
key: impl Into<String>,
initial: f32,
target: f32,
spec: AnimationSpec,
) -> f32 {
let key = key.into();
let anim_key = format!("anim:f32:{key}");
let anim = remember_state_with_key(&anim_key, || AnimatedValue::new(initial, spec));
let last = remember_state_with_key(format!("anim:f32_last:{key}"), || f32::NAN);
let mut a = anim.borrow_mut();
let mut lt = last.borrow_mut();
let should_set_target = lt.is_nan() || (*lt - target).abs() > 1e-6;
if should_set_target {
a.set_spec(spec);
a.set_target(target);
*lt = target;
drop(lt);
// Register with AnimationDriver for pre-composition advancement.
let reg_key = anim_key;
let reg_anim = anim.clone();
animation_driver::register(
reg_key,
Rc::new(RefCell::new(move || reg_anim.borrow_mut().update())),
);
request_frame();
*a.get()
} else {
drop(lt);
*a.get()
}
}
/// Animate f32 to the given target; starts at the target on first mount (legacy behavior).
pub fn animate_f32(key: impl Into<String>, target: f32, spec: AnimationSpec) -> f32 {
animate_f32_from(key, target, target, spec)
}
macro_rules! animate_from_impl {
($name:ident, $type:ty, $prefix:expr) => {
pub fn $name(
key: impl Into<String>,
initial: $type,
target: $type,
spec: AnimationSpec,
) -> $type {
let key = key.into();
let anim_key = format!("anim:{}:{}", $prefix, key);
let anim = remember_state_with_key(&anim_key, || AnimatedValue::new(initial, spec));
let last =
remember_state_with_key(format!("anim:{}_last:{}", $prefix, key), || None::<$type>);
let mut a = anim.borrow_mut();
let mut lt = last.borrow_mut();
if lt.is_none() || lt.as_ref().unwrap() != &target {
a.set_spec(spec);
a.set_target(target);
*lt = Some(target);
drop(lt);
// Register with AnimationDriver
let reg_key = anim_key.clone();
let reg_anim = anim.clone();
repose_core::animation_driver::register(
reg_key,
std::rc::Rc::new(std::cell::RefCell::new(move || {
reg_anim.borrow_mut().update()
})),
);
repose_core::request_frame();
*a.get()
} else {
drop(lt);
*a.get()
}
}
};
}
animate_from_impl!(animate_color_from, Color, "color");
animate_from_impl!(animate_vec2_from, Vec2, "vec2");
animate_from_impl!(animate_size_from, Size, "size");
animate_from_impl!(animate_rect_from, Rect, "rect");
/// Animate Color to the given target; starts at the target on first mount (legacy behavior).
pub fn animate_color(key: impl Into<String>, target: Color, spec: AnimationSpec) -> Color {
animate_color_from(key, target, target, spec)
}
/// Animate Vec2 to the given target; starts at the target on first mount.
pub fn animate_vec2(key: impl Into<String>, target: Vec2, spec: AnimationSpec) -> Vec2 {
animate_vec2_from(key, target, target, spec)
}
/// Animate Size to the given target; starts at the target on first mount.
pub fn animate_size(key: impl Into<String>, target: Size, spec: AnimationSpec) -> Size {
animate_size_from(key, target, target, spec)
}
/// Animate Rect to the given target; starts at the target on first mount.
pub fn animate_rect(key: impl Into<String>, target: Rect, spec: AnimationSpec) -> Rect {
animate_rect_from(key, target, target, spec)
}
/// Animate f32 through a sequence of keyframes.
///
/// The keyframe timestamps range from 0.0 to 1.0, mapped across `spec`'s duration.
/// The animation loops if `repeat` is set on the spec.
///
/// Example:
/// ```ignore
/// let val = animate_keyframes("bounce", KeyframesSpec::new(vec![
/// (0.0, 0.0),
/// (0.3, 100.0),
/// (0.6, 80.0),
/// (1.0, 100.0),
/// ]), AnimationSpec::tween(Duration::from_millis(600), Easing::EaseOut));
/// ```
pub fn animate_keyframes(
key: impl Into<String>,
keyframes: KeyframesSpec<f32>,
spec: AnimationSpec,
) -> f32 {
let key = key.into();
let anim = remember_state_with_key(format!("anim:kf:{key}"), || AnimatedValue::new(0.0, spec));
let mut a = anim.borrow_mut();
if !a.has_keyframes() {
a.set_keyframes(keyframes);
}
a.update();
*a.get()
}
/// Animate f32 through a sequence of keyframes using a smooth cubic Hermite spline.
///
/// Produces C1-continuous animation (smooth derivatives at keyframe boundaries),
/// unlike `animate_keyframes` which uses C0 linear interpolation.
///
/// Example:
/// ```ignore
/// let val = animate_spline_keyframes("bounce", SplineKeyframes::new(vec![
/// (0.0, 0.0),
/// (0.3, 100.0),
/// (0.6, 80.0),
/// (1.0, 100.0),
/// ]), AnimationSpec::tween(Duration::from_millis(600), Easing::EaseInOut));
/// ```
pub fn animate_spline_keyframes(
key: impl Into<String>,
keyframes: SplineKeyframes,
spec: AnimationSpec,
) -> f32 {
let key = key.into();
// Animate progress 0..1 using standard AnimatedValue (handles easing, repeat, frame clock)
let anim = remember_state_with_key(format!("anim:spkf_progress:{key}"), || {
AnimatedValue::new(0.0, spec)
});
let spline = remember_state_with_key(format!("anim:spkf:{key}"), || keyframes);
let mut a = anim.borrow_mut();
let s = spline.borrow();
a.set_target(1.0);
a.update();
let progress = *a.get();
s.evaluate(progress)
}
fn with_infinite_repeat(spec: AnimationSpec) -> AnimationSpec {
if spec.repeat.is_none() {
spec.repeated(RepeatableSpec::infinite().reverse())
} else {
spec
}
}
/// A scoped API for continuous/repeating animations.
///
/// All child animations created via this transition loop indefinitely
/// between the given initial and target values (ping-pong with `reverse`).
///
/// If you need a custom repeat configuration, pass an `AnimationSpec` that
/// already has `.repeated(...)` set - the transition will respect it instead
/// of applying the default infinite reverse.
pub struct InfiniteTransition {
_private: (),
}
/// Creates an `InfiniteTransition` scoped to the current composition slot.
///
/// Use its `animate_float`, `animate_color`, `animate_vec2`, `animate_size`,
/// and `animate_rect` methods for continuous looping animations.
///
/// # Example
///
/// ```ignore
/// let t = remember_infinite_transition();
/// let pulse = t.animate_float("pulse", 0.0, 1.0,
/// AnimationSpec::tween(Duration::from_millis(600), Easing::EaseInOut));
/// ```
pub fn remember_infinite_transition() -> InfiniteTransition {
InfiniteTransition { _private: () }
}
macro_rules! inf_method {
($method:ident, $from_fn:ident, $type:ty) => {
pub fn $method(
&self,
key: impl Into<String>,
initial: $type,
target: $type,
spec: AnimationSpec,
) -> $type {
let key = key.into();
$from_fn(
format!("inf:{}", key),
initial,
target,
with_infinite_repeat(spec),
)
}
};
}
impl InfiniteTransition {
inf_method!(animate_float, animate_f32_from, f32);
inf_method!(animate_color, animate_color_from, Color);
inf_method!(animate_vec2, animate_vec2_from, Vec2);
inf_method!(animate_size, animate_size_from, Size);
inf_method!(animate_rect, animate_rect_from, Rect);
}
/// A scope for multi-target animations driven by a changing state.
///
/// When the target state changes (detected via `PartialEq`), all child
/// animations registered via `animate_float`, `animate_color`, etc.
/// automatically animate from their current value toward the new mapped
/// target value.
///
/// # Example
///
/// ```ignore
/// let t = update_transition("panel", is_expanded, AnimationSpec::spring_gentle());
/// let h = t.animate_float("height", |e| if *e { 200.0 } else { 48.0 });
/// let bg = t.animate_color("bg", |e| if *e { theme().primary } else { theme().surface });
/// ```
pub struct TransitionScope<T> {
key: String,
spec: AnimationSpec,
state: Rc<RefCell<T>>,
}
/// Creates a `TransitionScope` keyed to the given state.
///
/// Whenever `target_state` differs from the previous call (via `PartialEq`),
/// all child animation targets are updated and the transition animates toward
/// the new values.
pub fn update_transition<T>(
key: impl Into<String>,
target_state: T,
spec: AnimationSpec,
) -> TransitionScope<T>
where
T: PartialEq + Clone + 'static,
{
let key = key.into();
let state: Rc<RefCell<T>> =
remember_state_with_key(format!("tr_state:{key}"), || target_state.clone());
if *state.borrow() != target_state {
*state.borrow_mut() = target_state;
}
TransitionScope { key, spec, state }
}
macro_rules! tr_method {
($method:ident, $fn:ident, $type:ty) => {
pub fn $method<F>(&self, child_key: impl Into<String>, map: F) -> $type
where
F: Fn(&T) -> $type,
{
let target = map(&*self.state.borrow());
$fn(
format!("tr:{}:{}", self.key, child_key.into()),
target,
self.spec,
)
}
};
}
impl<T> TransitionScope<T> {
tr_method!(animate_float, animate_f32, f32);
tr_method!(animate_color, animate_color, Color);
tr_method!(animate_vec2, animate_vec2, Vec2);
tr_method!(animate_size, animate_size, Size);
tr_method!(animate_rect, animate_rect, Rect);
}