repose-ui 0.17.3

UI widgets and libs for Repose
Documentation
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use std::cell::RefCell;
use std::rc::Rc;

use repose_core::Color;
use repose_core::{
    Rect, Size, Vec2,
    animation::{AnimatedValue, AnimationSpec, KeyframesSpec, RepeatableSpec},
    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 = remember_state_with_key(format!("anim:f32:{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);

    let still_animating = a.update();
    if still_animating {
        request_frame();
    }

    *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)
}

/// Animate Color from an explicit initial value to a target.
pub fn animate_color_from(
    key: impl Into<String>,
    initial: Color,
    target: Color,
    spec: AnimationSpec,
) -> Color {
    let key = key.into();
    let anim = remember_state_with_key(format!("anim:color:{key}"), || {
        AnimatedValue::new(initial, spec)
    });
    let last = remember_state_with_key(format!("anim:color_last:{key}"), || None::<Color>);

    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);

    let still_animating = a.update();
    if still_animating {
        request_frame();
    }

    *a.get()
}

/// 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 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);
    }
    let still = a.update();
    if still {
        request_frame();
    }
    *a.get()
}

/// Animate Vec2 from an explicit initial value to a target.
pub fn animate_vec2_from(
    key: impl Into<String>,
    initial: Vec2,
    target: Vec2,
    spec: AnimationSpec,
) -> Vec2 {
    let key = key.into();
    let anim = remember_state_with_key(format!("anim:vec2:{key}"), || {
        AnimatedValue::new(initial, spec)
    });
    let last = remember_state_with_key(format!("anim:vec2_last:{key}"), || None::<Vec2>);

    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);

    let still_animating = a.update();
    if still_animating {
        request_frame();
    }

    *a.get()
}

/// 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 from an explicit initial value to a target.
pub fn animate_size_from(
    key: impl Into<String>,
    initial: Size,
    target: Size,
    spec: AnimationSpec,
) -> Size {
    let key = key.into();
    let anim = remember_state_with_key(format!("anim:size:{key}"), || {
        AnimatedValue::new(initial, spec)
    });
    let last = remember_state_with_key(format!("anim:size_last:{key}"), || None::<Size>);

    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);

    let still_animating = a.update();
    if still_animating {
        request_frame();
    }

    *a.get()
}

/// 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 from an explicit initial value to a target.
pub fn animate_rect_from(
    key: impl Into<String>,
    initial: Rect,
    target: Rect,
    spec: AnimationSpec,
) -> Rect {
    let key = key.into();
    let anim = remember_state_with_key(format!("anim:rect:{key}"), || {
        AnimatedValue::new(initial, spec)
    });
    let last = remember_state_with_key(format!("anim:rect_last:{key}"), || None::<Rect>);

    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);

    let still_animating = a.update();
    if still_animating {
        request_frame();
    }

    *a.get()
}

/// 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)
}

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: () }
}

impl InfiniteTransition {
    /// Animate an f32 value continuously between `initial` and `target`.
    pub fn animate_float(
        &self,
        key: impl Into<String>,
        initial: f32,
        target: f32,
        spec: AnimationSpec,
    ) -> f32 {
        let key = key.into();
        animate_f32_from(
            format!("inf:{key}"),
            initial,
            target,
            with_infinite_repeat(spec),
        )
    }

    /// Animate a Color value continuously between `initial` and `target`.
    pub fn animate_color(
        &self,
        key: impl Into<String>,
        initial: Color,
        target: Color,
        spec: AnimationSpec,
    ) -> Color {
        let key = key.into();
        animate_color_from(
            format!("inf:{key}"),
            initial,
            target,
            with_infinite_repeat(spec),
        )
    }

    /// Animate a Vec2 value continuously between `initial` and `target`.
    pub fn animate_vec2(
        &self,
        key: impl Into<String>,
        initial: Vec2,
        target: Vec2,
        spec: AnimationSpec,
    ) -> Vec2 {
        let key = key.into();
        animate_vec2_from(
            format!("inf:{key}"),
            initial,
            target,
            with_infinite_repeat(spec),
        )
    }

    /// Animate a Size value continuously between `initial` and `target`.
    pub fn animate_size(
        &self,
        key: impl Into<String>,
        initial: Size,
        target: Size,
        spec: AnimationSpec,
    ) -> Size {
        let key = key.into();
        animate_size_from(
            format!("inf:{key}"),
            initial,
            target,
            with_infinite_repeat(spec),
        )
    }

    /// Animate a Rect value continuously between `initial` and `target`.
    pub fn animate_rect(
        &self,
        key: impl Into<String>,
        initial: Rect,
        target: Rect,
        spec: AnimationSpec,
    ) -> Rect {
        let key = key.into();
        animate_rect_from(
            format!("inf:{key}"),
            initial,
            target,
            with_infinite_repeat(spec),
        )
    }
}

/// 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 }
}

impl<T> TransitionScope<T> {
    /// Animate an f32 value derived from the current state.
    pub fn animate_float<F>(&self, child_key: impl Into<String>, map: F) -> f32
    where
        F: Fn(&T) -> f32,
    {
        let target = map(&*self.state.borrow());
        animate_f32(
            format!("tr:{}:{}", self.key, child_key.into()),
            target,
            self.spec,
        )
    }

    /// Animate a `Color` value derived from the current state.
    pub fn animate_color<F>(&self, child_key: impl Into<String>, map: F) -> Color
    where
        F: Fn(&T) -> Color,
    {
        let target = map(&*self.state.borrow());
        animate_color(
            format!("tr:{}:{}", self.key, child_key.into()),
            target,
            self.spec,
        )
    }

    /// Animate a `Vec2` value derived from the current state.
    pub fn animate_vec2<F>(&self, child_key: impl Into<String>, map: F) -> Vec2
    where
        F: Fn(&T) -> Vec2,
    {
        let target = map(&*self.state.borrow());
        animate_vec2(
            format!("tr:{}:{}", self.key, child_key.into()),
            target,
            self.spec,
        )
    }

    /// Animate a `Size` value derived from the current state.
    pub fn animate_size<F>(&self, child_key: impl Into<String>, map: F) -> Size
    where
        F: Fn(&T) -> Size,
    {
        let target = map(&*self.state.borrow());
        animate_size(
            format!("tr:{}:{}", self.key, child_key.into()),
            target,
            self.spec,
        )
    }

    /// Animate a `Rect` value derived from the current state.
    pub fn animate_rect<F>(&self, child_key: impl Into<String>, map: F) -> Rect
    where
        F: Fn(&T) -> Rect,
    {
        let target = map(&*self.state.borrow());
        animate_rect(
            format!("tr:{}:{}", self.key, child_key.into()),
            target,
            self.spec,
        )
    }
}