aura-anim 0.3.0

Convenience facade for aura animation core and Iced integration.
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# aura-anim

Typed animation primitives and Iced integration for Rust desktop interfaces.

The application stores lightweight `Motion<T>` handles while `MotionRuntime`
owns and advances the actual animation sources. Animated values remain ordinary
Rust structs, and `#[derive(Animatable)]` generates field-by-field
interpolation.

```text
Application
├── explicit UI state
├── Motion<T> handles
└── event-driven transition_to / play calls

MotionRuntime
├── owns type-erased animation slots
├── ticks active slots only
├── per-motion and batch pause / resume / seek / cancel / finish
├── generation-checked handle reuse
└── completion compaction and optional auto-removal

Animation<T>
├── Tween<T>
├── Spring<T>
├── Keyframes<T>
├── Sequence<T>
├── Parallel<T>
└── Hold<T>
```

## Workspace Crates

- `aura-anim-core`: runtime, handles, interpolation, animation sources and
  timeline composition.
- `aura-anim-iced`: Iced value integration and frame subscriptions.
- `aura-anim`: convenience facade re-exporting core and Iced APIs.
- `aura-anim-macros`: `Animatable` derive implementation.

## Installation

For Iced applications:

```toml
[dependencies]
aura-anim = "0.3.0"
iced = "0.14"
```

Use `aura-anim-core` directly when no Iced integration is required.

## Typed Motion

```rust
use aura_anim::prelude::*;

#[derive(Clone, Debug, Animatable)]
struct ButtonMotion {
    opacity: f32,
    scale: f32,
}

let mut runtime = MotionRuntime::new();
let button = runtime.motion_with(
    ButtonMotion {
        opacity: 0.5,
        scale: 0.95,
    },
    Timing::ease_out(160.0),
);

button.transition_to(
    ButtonMotion {
        opacity: 1.0,
        scale: 1.0,
    },
    &mut runtime,
)
.unwrap();

runtime.tick(std::time::Duration::from_millis(80));
let visual = button.value(&runtime).unwrap();
```

`transition_to` retargets from the currently sampled value, so interrupted
hover, press, menu and route animations do not jump back to a stale origin.
Motion access and mutation return `Result<_, MotionError>` so removed, stale,
out-of-bounds, and type-mismatched handles remain distinguishable.

`Timing::linear`, `Timing::ease_in`, `Timing::ease_out`, and
`Timing::ease_in_out` cover the common duration/easing combinations while
remaining normal `Timing` values that can be extended with delay, iterations,
or direction.

Use a deferred target factory when replacing the current animation:

```rust
button.play(
    tween_to(
        ButtonMotion {
            opacity: 0.0,
            scale: 0.9,
        },
        Timing::ease_in(120.0),
    ),
    &mut runtime,
)?;
# Ok::<(), MotionError>(())
```

`tween_to` and `spring_to` sample the motion's current value when playback
starts, so callers do not need to read or clone it manually.

Runtime-wide commands are available for application lifecycle and accessibility
policies:

```rust
runtime.command_all(AnimationCommand::Pause);
runtime.command_all(AnimationCommand::Resume);
runtime.command_all(AnimationCommand::Finish);
```

`command_all` applies to every stored motion, including paused and idle
motions. Completion, cancellation, and `DropWhenSettled` removal events use the
same semantics as commands sent through an individual `Motion<T>`.

## Independent Field Animations

`#[derive(Animatable)]` also generates typed field descriptors. A struct can
remain one `Motion<T>` while each selected field uses a different animation:

```rust
#[derive(Clone, Debug, Animatable)]
struct Position {
    x: f32,
    y: f32,
}

let mut runtime = MotionRuntime::new();
let position = runtime.motion(Position { x: 0.0, y: 0.0 });

position.play(
    fields()
        .animate(
            field!(Position::x),
            tween_to(100.0, Timing::ease_in(100.0)),
        )
        .animate(
            PositionFields::y,
            spring_to(200.0, SpringConfig::snappy()),
        ),
    &mut runtime,
)?;
# Ok::<(), MotionError>(())
```

Target factories receive the field's current sampled value when `play` is
called. Custom `|from| ...` factories remain supported. Interrupted field
animations therefore continue from the visible value, while fields not
included in the plan retain their current values.

For a named struct, the derive generates `PositionFields::x`,
`PositionFields::y`, and equivalent `field!(Position::x)` descriptors. Tuple
struct descriptors use `_0`, `_1`, and so on, while `field!(Offset::0)` uses
the tuple index directly. A generated descriptor type can be renamed when
necessary:

```rust
#[derive(Clone, Animatable)]
#[animatable(fields = PositionAnimationFields)]
struct Position {
    x: f32,
    y: f32,
}
```

## Animation Events

`MotionRuntime` queues structured lifecycle events when playbacks complete,
cancel, are interrupted, or leave runtime storage. Events are emitted once per
state transition and remain queued until the application takes or clears them:

```rust
aura_anim::iced::frame(&mut runtime, now);

for event in runtime.take_events() {
    if event.is_completed_for(motion) {
        // Run one-time completion logic.
    }
}
```

Matching a `Motion<T>` is sufficient for simple cleanup. Multi-stage flows
should track the exact playback so an older queued event cannot complete a
newer animation on the same motion:

```rust
let exit = motion.play_tracked(
    Tween::between(current, hidden, Timing::new(150.0)),
    &mut runtime,
)?;

// In a later frame update:
for event in runtime.take_events() {
    if event.is_completed_for(exit) {
        let enter = motion.play_tracked(
            Tween::between(hidden, visible, Timing::new(220.0)),
            &mut runtime,
        )?;
        # let _ = enter;
    }
}
# Ok::<(), MotionError>(())
```

`play_tracked` and `transition_to_tracked` return a `PlaybackId`. Existing
`play` and `transition_to` calls remain unchanged when playback identity is not
needed.

Event kinds include:

- `Completed`
- `Canceled`
- `Interrupted(Replaced | Retargeted | Removed)`
- `Removed(Explicit | Settled)`

`DropWhenSettled` motions emit their terminal event before their removal event,
so completion remains observable after the handle becomes invalid.

`Presence::handle_event` uses the current exit playback ID to avoid stale exit
events unmounting content that has already been shown again:

```rust
for event in runtime.take_events() {
    menu.handle_event(&event);
    toast.handle_event(&event);
}
```

`Presence::sync` remains available for polling-based integrations.

For boolean application state, `set_visible` avoids restarting an animation
when the requested target is unchanged:

```rust
menu.set_visible(is_open, &mut runtime)?;
menu.toggle(&mut runtime)?;
# Ok::<(), MotionError>(())
```

Both methods return whether a new transition was started. Explicit
`show`/`hide` and custom `show_with`/`hide_with` calls remain available when a
transition should be replayed intentionally.

## Iced Integration

Store the runtime and typed handles in application state:

```rust
use std::time::Instant;

use aura_anim::prelude::*;
use iced::{Subscription, Vector};

#[derive(Clone, Debug, Animatable)]
struct PanelMotion {
    opacity: f32,
    offset: Vector,
}

struct App {
    runtime: MotionRuntime,
    panel: Motion<PanelMotion>,
}

#[derive(Clone, Debug)]
enum Message {
    Frame(Instant),
    Open,
}

impl App {
    fn update(&mut self, message: Message) {
        match message {
            Message::Frame(now) => aura_anim::iced::frame(&mut self.runtime, now),
            Message::Open => {
                if let Err(error) = self.panel.transition_to(
                    PanelMotion {
                        opacity: 1.0,
                        offset: Vector::ZERO,
                    },
                    &mut self.runtime,
                ) {
                    eprintln!("panel transition failed: {error}");
                }
            }
        }
    }

    fn subscription(&self) -> Subscription<Message> {
        aura_anim::iced::subscription_with_policy(
            &self.runtime,
            TickPolicy::fps(60),
        )
        .map(Message::Frame)
    }
}
```

When no animation is active, the subscription returns `Subscription::none()`
and does not continue waking the application.

`TickPolicy` supports:

```rust
TickPolicy::Frames
TickPolicy::fps(60)
TickPolicy::interval(std::time::Duration::from_millis(32))
```

## Motion Binding

`MotionBinding<S, T>` maps reusable business states to visual targets and
transition factories. The binding is immutable configuration; each button,
menu item, or route owns a small `MotionBindingState<S>` that records its last
successfully applied state.

```rust
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ButtonState {
    Idle,
    Hovered,
    Pressed,
}

let button_binding = MotionBinding::new(ButtonState::Idle, idle)
    .when(ButtonState::Hovered, hovered)
    .when(ButtonState::Pressed, pressed)
    .transition(ButtonState::Idle, ButtonState::Hovered, |ctx| {
        ctx.tween(Timing::new(120.0))
    })
    .transition(ButtonState::Hovered, ButtonState::Pressed, |ctx| {
        ctx.spring(SpringConfig::snappy())
    })
    .fallback(|ctx| ctx.tween(Timing::new(100.0)));

let motion = runtime.motion(idle);
let mut binding_state = button_binding.state();

let playback = button_binding
    .set_state_tracked(
        &mut binding_state,
        ButtonState::Hovered,
        motion,
        &mut runtime,
    )?
    .expect("state changed");

// In a later update, after the runtime has been ticked:
for event in runtime.take_events() {
    if event.is_completed_for(playback) {
        // The Hovered transition completed.
    }
}
```

On each state change the binding resolves the target, samples the motion's
current value, selects the exact transition or fallback factory, constructs
the animation, calls `motion.play(...)`, and records the new business state
only after playback succeeds. Factories can return concrete Tween, Spring,
Keyframes, Timeline, or any custom `Animation<T>`; the binding handles type
erasure internally.

`set_state` returns whether a transition was started.
`set_state_tracked` returns `None` for an unchanged state or the exact
`PlaybackId` for a newly started transition, allowing completion and
interruption events to be matched without polling the motion.

One binding configuration can be cloned or shared and reused with independent
`MotionBindingState` values.

## Iced Animatable Types

With the core `iced` integration enabled, these types can be fields in an
`Animatable` struct:

- `iced::Vector<T>`
- `iced::Point<T>`
- `iced::Size<T>`
- `iced::Rectangle<T>`
- `iced::Padding`
- `iced::border::Radius`

The active `rgba` or `oklaba` color feature additionally enables:

- `iced::Color`
- `iced::Shadow`
- `iced::Border`

## Color Interpolation

RGBA component interpolation is enabled by default:

```toml
aura-anim = "0.3.0"
```

For Oklab RGB interpolation with independently interpolated alpha:

```toml
aura-anim = {
    version = "0.3.0",
    default-features = false,
    features = ["oklaba"]
}
```

`rgba` and `oklaba` are mutually exclusive. Oklaba conversion follows:

```text
Iced sRGB
→ palette sRGB
→ Oklab interpolation
→ display sRGB
```

## Tracing

Enable the optional `tracing` feature to emit runtime diagnostics without
installing or configuring a subscriber inside the library:

```toml
aura-anim = {
    version = "0.3.0",
    features = ["tracing"]
}
```

The `aura_anim::runtime`, `aura_anim::binding`, and `aura_anim::presence`
targets report motion allocation and reuse, playback commands, lifecycle
changes, invalid handles, binding transition selection, and presence mounting.
Per-tick diagnostics use the `TRACE` level; lifecycle and error diagnostics use
`DEBUG`. Applications remain responsible for installing a compatible
`tracing` subscriber.

## Animation Sources

### Tween

```rust
motion.play(
    Tween::between(current, target, Timing::new(180.0)).rate(2.0),
    &mut runtime,
);
```

Timing supports delay, easing, finite or infinite iterations, and playback
direction. `Animation::rate` directly scales stored durations: `2.0` halves
duration and `0.5` doubles it. It recursively updates existing Timeline
children, while Spring ignores rate because its motion is physics-based.

### Keyframes

```rust
motion.play(
    Keyframes::new(start)
        .push_eased(180.0, overshoot, Easing::EaseOut)
        .push_eased(280.0, settled, Easing::EaseInOut),
    &mut runtime,
);
```

### Spring

```rust
motion.play(
    Spring::new(current, target, SpringConfig::default()),
    &mut runtime,
);
```

Spring interpolation may overshoot and can be retargeted while active.

For values whose fields need different physical responses, create independent
spring channels and explicitly compose their outputs:

```rust
#[derive(Clone, Debug, Animatable)]
struct PanelMotion {
    offset: f32,
    opacity: f32,
}

let movement = SpringConfig::new(180.0, 20.0);
let fade = SpringConfig::new(420.0, 32.0)
    .with_mass(1.2)
    .with_epsilon(0.001);

let spring = Spring::with_channels(
    PanelMotion {
        offset: 24.0,
        opacity: 0.0,
    },
    PanelMotion {
        offset: 0.0,
        opacity: 1.0,
    },
    [movement, fade],
    |outputs| PanelMotion {
        offset: outputs[0].offset,
        opacity: outputs[1].opacity,
    },
);
```

Each channel owns its own position, velocity and `SpringConfig`. Spring
advancement uses the analytic damped-oscillator solution, so long frame
intervals are fully consumed instead of being truncated.

## Timeline Composition

`Sequence`, `Parallel` and `Hold` all implement `Animation<T>`, so composition
is recursive:

```text
Sequence(
    Parallel(
        Sequence(Hold, Tween),
        Sequence(Tween, Tween),
    ),
    Tween,
)
```

Parallel branches produce complete `T` values. A compositor explicitly selects
which fields each branch owns:

```rust
let parallel = Parallel::new(start.clone(), |outputs: &[Position]| Position {
    x: outputs[0].x,
    y: outputs[1].y,
})
.with(x_sequence)
.with(y_sequence);
```

Sequence propagates unused frame time into following children. Parallel
completes when its longest branch completes.

Concrete animations can start a sequence directly through `AnimationExt`:

```rust
let timeline = Tween::between(hidden, visible, Timing::ease_out(180.0))
    .delay(80.0)
    .then(Hold::new(visible.clone(), 240.0))
    .then(Tween::between(visible, hidden, Timing::ease_in(120.0)));
```

`delay` inserts a `Hold` before the animation. Both combinators return the
existing `Sequence<T>` type, so lifecycle, seeking, rate changes, and overflow
propagation retain the same behavior as manually constructing a sequence.

## Lifecycle

Normal motions retain their final value:

```rust
let motion = runtime.motion(initial);
```

Completed sources are compacted to the final value, releasing keyframe and
timeline trees while keeping the handle valid.

Transient animations can remove their slot automatically:

```rust
let transient = runtime.play_once(animation);
```

Slots are reused with generation counters, preventing stale handles from
accessing a newly allocated motion.

## Examples

Run the command-line architecture example:

```sh
cargo run -p aura-anim --example runtime
```

Run the Iced showcase:

```sh
cargo run -p aura-anim-iced --example showcase
```

Run the focused visual examples:

```sh
cargo run -p aura-anim-iced --example tween
cargo run -p aura-anim-iced --example keyframes
cargo run -p aura-anim-iced --example timeline
cargo run -p aura-anim-iced --example spring
```

Run the interactive UI examples:

```sh
cargo run -p aura-anim-iced --example button
cargo run -p aura-anim-iced --example menu
cargo run -p aura-anim-iced --example notification
cargo run -p aura-anim-iced --example route_transition
```

Run the showcase with perceptual color interpolation:

```sh
cargo run -p aura-anim-iced \
    --no-default-features \
    --features oklaba \
    --example showcase
```