aetna-core 0.3.3

Aetna — backend-agnostic UI library core
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
use super::support::*;

#[test]
fn settled_mode_snaps_hover_envelope_to_one() {
    // Headless contract: Settled mode must produce the post-hover
    // envelope on a single prepare. A windowed runner (Live mode)
    // would ease over many frames; the fixture path can't wait.
    let (mut tree, mut state) = lay_out_counter();
    state.set_animation_mode(AnimationMode::Settled);
    state.hovered = Some(target(&tree, &state, "inc"));
    state.apply_to_state();

    let needs_redraw = state.tick_visual_animations(&mut tree, Instant::now());

    assert!(!needs_redraw, "Settled mode should never report in flight");
    assert_eq!(
        envelope_for(&tree, &state, "inc", EnvelopeKind::Hover),
        Some(1.0)
    );
    assert_eq!(
        envelope_for(&tree, &state, "inc", EnvelopeKind::Press),
        Some(0.0)
    );
    // The build fill stays untouched — the lightening happens in
    // apply_state at draw time, mixing by hover_amount.
}

#[test]
fn live_mode_eases_hover_envelope_over_multiple_ticks() {
    // After a single 8 ms tick the hover envelope should be
    // strictly between 0 and 1 — neither snapped to either end.
    let (mut tree, mut state) = lay_out_counter();
    let t0 = Instant::now();
    state.tick_visual_animations(&mut tree, t0);

    state.hovered = Some(target(&tree, &state, "inc"));
    state.apply_to_state();
    let needs_redraw =
        state.tick_visual_animations(&mut tree, t0 + std::time::Duration::from_millis(8));
    let mid = envelope_for(&tree, &state, "inc", EnvelopeKind::Hover).expect("hover envelope");

    assert!(
        needs_redraw,
        "spring should still be in flight after one 8 ms tick"
    );
    assert!(
        mid > 0.0 && mid < 1.0,
        "expected envelope mid-flight, got {mid}",
    );
}

#[test]
fn build_value_change_survives_hover_envelope() {
    // The point of envelopes: when the author swaps a button's fill
    // mid-hover, n.fill must reflect the new build value
    // immediately. The envelope keeps easing independently. This is
    // what avoids the AppFill / StateFill fight of an earlier draft.
    let mut tree_a =
        column([row([button("X").key("x").fill(Color::rgb(255, 0, 0))])]).padding(20.0);
    let mut state = UiState::new();
    layout(&mut tree_a, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.set_animation_mode(AnimationMode::Settled);
    state.hovered = Some(target(&tree_a, &state, "x"));
    state.apply_to_state();
    state.tick_visual_animations(&mut tree_a, Instant::now());
    assert_eq!(
        envelope_for(&tree_a, &state, "x", EnvelopeKind::Hover),
        Some(1.0)
    );

    // Rebuild: same button, fill swapped to blue.
    let mut tree_b =
        column([row([button("X").key("x").fill(Color::rgb(0, 0, 255))])]).padding(20.0);
    layout(&mut tree_b, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.apply_to_state();
    state.tick_visual_animations(&mut tree_b, Instant::now());

    let observed = find_fill(&tree_b, "x").expect("x fill");
    assert_eq!(
        (observed.r, observed.g, observed.b),
        (0, 0, 255),
        "build fill should pass through unchanged — envelope handles state delta separately",
    );
    assert_eq!(
        envelope_for(&tree_b, &state, "x", EnvelopeKind::Hover),
        Some(1.0)
    );
}

#[test]
fn focus_ring_alpha_eases_in_and_out() {
    let (mut tree, mut state) = lay_out_counter();
    state.set_animation_mode(AnimationMode::Settled);

    // No focus → alpha settled at 0.
    state.tick_visual_animations(&mut tree, Instant::now());
    assert_eq!(
        envelope_for(&tree, &state, "inc", EnvelopeKind::FocusRing),
        Some(0.0)
    );

    // Focus on inc + focus_visible (Tab semantics) → alpha settles at 1.0.
    let (mut tree, _) = lay_out_counter();
    // Re-layout against the existing state so the rect map is fresh.
    layout(&mut tree, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.focused = Some(target(&tree, &state, "inc"));
    state.set_focus_visible(true);
    state.apply_to_state();
    state.tick_visual_animations(&mut tree, Instant::now());
    assert_eq!(
        envelope_for(&tree, &state, "inc", EnvelopeKind::FocusRing),
        Some(1.0)
    );

    // Lose focus → alpha settles back to 0.
    let (mut tree, _) = lay_out_counter();
    layout(&mut tree, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.focused = None;
    state.apply_to_state();
    state.tick_visual_animations(&mut tree, Instant::now());
    assert_eq!(
        envelope_for(&tree, &state, "inc", EnvelopeKind::FocusRing),
        Some(0.0)
    );
}

#[test]
fn focus_ring_stays_dim_when_focus_is_not_visible() {
    // Pointer-driven focus (`focus_visible == false`) keeps the ring
    // off — clicking a button shouldn't paint the keyboard
    // affordance. Mirrors the web `:focus-visible` heuristic.
    let (mut tree, mut state) = lay_out_counter();
    state.set_animation_mode(AnimationMode::Settled);
    state.focused = Some(target(&tree, &state, "inc"));
    // Default focus_visible is false (the runtime sets it explicitly
    // on Tab / pointer-down). Don't raise it here.
    assert!(!state.focus_visible);
    state.apply_to_state();
    state.tick_visual_animations(&mut tree, Instant::now());
    assert_eq!(
        envelope_for(&tree, &state, "inc", EnvelopeKind::FocusRing),
        Some(0.0),
        "ring must stay off until focus_visible is raised",
    );
}

#[test]
fn focus_ring_lights_up_on_always_show_focus_ring_widgets_even_without_focus_visible() {
    // Text inputs / text areas opt in via `.always_show_focus_ring()`
    // because the ring on click is a meaningful "now editable"
    // affordance. Verify the per-element flag overrides the global
    // `focus_visible == false`.
    let selection = crate::selection::Selection::default();
    let mut tree = column([row([crate::widgets::text_input::text_input(
        "", &selection, "field",
    )])])
    .padding(20.0);
    let mut state = UiState::new();
    layout(&mut tree, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.set_animation_mode(AnimationMode::Settled);
    state.focused = Some(target(&tree, &state, "field"));
    assert!(!state.focus_visible);
    state.apply_to_state();
    state.tick_visual_animations(&mut tree, Instant::now());
    assert_eq!(
        envelope_for(&tree, &state, "field", EnvelopeKind::FocusRing),
        Some(1.0),
        "always_show_focus_ring widgets ring on click too",
    );
}

#[test]
fn focus_ring_stays_on_when_focused_node_is_also_hovered() {
    // Regression: hovering a focused element used to overwrite its
    // resolved `state` from Focus to Hover (Hover wins in
    // `apply_to_state`), and `FocusRingAlpha`'s target gated on
    // `state == Focus`, so the ring fell off the moment the cursor
    // entered. The `alpha_follows_focused_ancestor` chain dragged
    // the text-input caret to invisible with it. Focus envelope must
    // be independent of hover/press.
    let selection = crate::selection::Selection::default();
    let mut tree = column([row([crate::widgets::text_input::text_input(
        "", &selection, "field",
    )])])
    .padding(20.0);
    let mut state = UiState::new();
    layout(&mut tree, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.set_animation_mode(AnimationMode::Settled);

    let field = target(&tree, &state, "field");
    state.focused = Some(field.clone());
    state.hovered = Some(field);
    state.apply_to_state();
    state.tick_visual_animations(&mut tree, Instant::now());

    assert_eq!(
        envelope_for(&tree, &state, "field", EnvelopeKind::FocusRing),
        Some(1.0),
        "focus ring must stay on while the focused node is also the hover target",
    );
}

#[test]
fn app_fill_settles_to_new_value_in_settled_mode() {
    // .animate(SPRING_STANDARD) on a node whose fill changes
    // between rebuilds. Settled mode should produce the new fill
    // on the very first tick after the change.
    use crate::anim::Timing;
    let mut tree_a = column([
        crate::text("0"),
        row([button("X")
            .key("x")
            .fill(Color::rgb(255, 0, 0))
            .animate(Timing::SPRING_STANDARD)]),
    ])
    .padding(20.0);
    let mut state = UiState::new();
    layout(&mut tree_a, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));

    state.set_animation_mode(AnimationMode::Settled);
    state.tick_visual_animations(&mut tree_a, Instant::now());
    assert_eq!(
        find_fill(&tree_a, "x").map(|c| (c.r, c.g, c.b)),
        Some((255, 0, 0))
    );

    // Rebuild with a different fill; tracker eases through.
    let mut tree_b = column([
        crate::text("0"),
        row([button("X")
            .key("x")
            .fill(Color::rgb(0, 0, 255))
            .animate(Timing::SPRING_STANDARD)]),
    ])
    .padding(20.0);
    layout(&mut tree_b, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.tick_visual_animations(&mut tree_b, Instant::now());

    assert_eq!(
        find_fill(&tree_b, "x").map(|c| (c.r, c.g, c.b)),
        Some((0, 0, 255)),
        "Settled mode should snap to the new build value",
    );
}

#[test]
fn app_fill_eases_in_live_mode() {
    // Same setup as above but in Live mode: after a small dt the
    // colour should be partway between red and blue, not at either.
    use crate::anim::Timing;
    let mut tree_a = column([row([button("X")
        .key("x")
        .fill(Color::rgb(255, 0, 0))
        .animate(Timing::SPRING_STANDARD)])])
    .padding(20.0);
    let mut state = UiState::new();
    layout(&mut tree_a, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));

    let t0 = Instant::now();
    state.tick_visual_animations(&mut tree_a, t0);

    let mut tree_b = column([row([button("X")
        .key("x")
        .fill(Color::rgb(0, 0, 255))
        .animate(Timing::SPRING_STANDARD)])])
    .padding(20.0);
    layout(&mut tree_b, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    let needs_redraw =
        state.tick_visual_animations(&mut tree_b, t0 + std::time::Duration::from_millis(8));
    let mid = find_fill(&tree_b, "x").expect("mid fill");

    assert!(
        needs_redraw,
        "spring should still be in flight after one tick"
    );
    assert!(
        mid.r < 255 && mid.b < 255,
        "expected mid-flight, got {mid:?}",
    );
    assert!(mid.r > 0 || mid.b > 0, "should have moved off the start",);
}

#[test]
fn token_tagged_fill_eases_through_draw_ops_without_snapping() {
    // Regression: when a checkbox / switch / button toggles its
    // fill from one token to another, the animator interpolates rgba
    // between them. Before the fix, `Animation::from_channels`
    // preserved the source token on the in-flight value, and
    // `apply_state.palette.resolve(c)` snapped the rgb back to that
    // token's palette value — so the transition rendered as an
    // instant flip. Verify the eased fill survives draw_ops.
    use crate::anim::Timing;
    use crate::draw_ops::draw_ops_with_theme;
    use crate::ir::DrawOp;
    use crate::shader::UniformValue;
    use crate::theme::Theme;
    use crate::tokens;

    let mut tree_a = column([row([button("X")
        .key("x")
        .fill(tokens::PRIMARY)
        .animate(Timing::SPRING_STANDARD)])])
    .padding(20.0);
    let mut state = UiState::new();
    layout(&mut tree_a, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    let t0 = Instant::now();
    state.tick_visual_animations(&mut tree_a, t0);

    let mut tree_b = column([row([button("X")
        .key("x")
        .fill(tokens::CARD)
        .animate(Timing::SPRING_STANDARD)])])
    .padding(20.0);
    layout(&mut tree_b, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.tick_visual_animations(&mut tree_b, t0 + std::time::Duration::from_millis(8));

    // Drive the rendered output through draw_ops; the apply_state
    // path is what previously snapped the in-flight rgb.
    let theme = Theme::default();
    let ops = draw_ops_with_theme(&tree_b, &state, &theme);
    let quad_fill = ops
        .iter()
        .find_map(|op| match op {
            DrawOp::Quad { id, uniforms, .. } if id.contains("x") => {
                uniforms.get("fill").and_then(|v| match v {
                    UniformValue::Color(c) => Some(*c),
                    _ => None,
                })
            }
            _ => None,
        })
        .expect("button quad fill");

    // Mid-flight: rgb should be strictly between the source PRIMARY
    // and target CARD, not snapped to either.
    let p = tokens::PRIMARY;
    let c = tokens::CARD;
    assert_ne!(
        (quad_fill.r, quad_fill.g, quad_fill.b),
        (p.r, p.g, p.b),
        "rendered fill must not snap back to the source token's rgb",
    );
    assert_ne!(
        (quad_fill.r, quad_fill.g, quad_fill.b),
        (c.r, c.g, c.b),
        "rendered fill must not snap forward to the target token's rgb",
    );
}

#[test]
fn app_translate_eases_on_rebuild() {
    use crate::anim::Timing;
    let mut tree_a = column([row([button("slide")
        .key("s")
        .translate(0.0, 0.0)
        .animate(Timing::SPRING_STANDARD)])])
    .padding(20.0);
    let mut state = UiState::new();
    layout(&mut tree_a, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.set_animation_mode(AnimationMode::Settled);
    state.tick_visual_animations(&mut tree_a, Instant::now());

    // Rebuild with a different translate.
    let mut tree_b = column([row([button("slide")
        .key("s")
        .translate(100.0, 50.0)
        .animate(Timing::SPRING_STANDARD)])])
    .padding(20.0);
    layout(&mut tree_b, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.tick_visual_animations(&mut tree_b, Instant::now());

    let n = find_node(&tree_b, "s").expect("s node");
    assert!((n.translate.0 - 100.0).abs() < 0.5);
    assert!((n.translate.1 - 50.0).abs() < 0.5);
}

#[test]
fn state_envelope_composes_on_app_eased_fill() {
    // A keyed interactive node with .animate() AND being hovered.
    // After Settled tick: n.fill = (eased) build value, hover
    // envelope = 1. draw_ops in apply_state then mixes the build
    // colour toward its lightened version by the envelope amount.
    // Since the envelope is at 1, the emitted Quad's fill should
    // equal lighten(build_fill, HOVER_LIGHTEN).
    use crate::anim::Timing;
    let mut tree = column([row([button("X")
        .key("x")
        .fill(Color::rgb(100, 100, 100))
        .animate(Timing::SPRING_STANDARD)])])
    .padding(20.0);
    let mut state = UiState::new();
    layout(&mut tree, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));

    state.set_animation_mode(AnimationMode::Settled);
    state.hovered = Some(target(&tree, &state, "x"));
    state.apply_to_state();
    state.tick_visual_animations(&mut tree, Instant::now());

    // Build fill survives untouched (envelope handles the delta).
    let n_fill = find_fill(&tree, "x").expect("x fill");
    assert_eq!((n_fill.r, n_fill.g, n_fill.b), (100, 100, 100));
    assert_eq!(
        envelope_for(&tree, &state, "x", EnvelopeKind::Hover),
        Some(1.0)
    );
}

#[test]
fn app_animation_skipped_when_animate_not_set() {
    // Without .animate(), app props are not tracked — the node's
    // fill snaps to whatever the build produces, no easing.
    //
    // Note: many stock widgets (e.g. `button`) opt into animation
    // by default, so this negative test uses a plain `Kind::Custom`
    // node that genuinely has no animate field set.
    fn swatch(fill: Color) -> El {
        El::new(Kind::Custom("swatch"))
            .key("x")
            .fill(fill)
            .width(Size::Fixed(40.0))
            .height(Size::Fixed(20.0))
    }

    let mut tree_a = column([row([swatch(Color::rgb(255, 0, 0))])]).padding(20.0);
    let mut state = UiState::new();
    layout(&mut tree_a, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.tick_visual_animations(&mut tree_a, Instant::now());

    let mut tree_b = column([row([swatch(Color::rgb(0, 0, 255))])]).padding(20.0);
    layout(&mut tree_b, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.tick_visual_animations(&mut tree_b, Instant::now());

    let observed = find_fill(&tree_b, "x").expect("x fill");
    assert_eq!(
        (observed.r, observed.g, observed.b),
        (0, 0, 255),
        "no .animate() — value should snap",
    );
}

#[test]
fn animation_entries_gc_when_node_leaves_tree() {
    // Build a tree with two buttons; hover one to seed an entry.
    // Then build a different tree with only one button. The orphan's
    // animation entries should be trimmed.
    let (mut tree_a, mut state) = lay_out_counter();
    state.hovered = Some(target(&tree_a, &state, "inc"));
    state.apply_to_state();
    state.tick_visual_animations(&mut tree_a, Instant::now());
    let inc_id_a = find_id(&tree_a, "inc").expect("inc id");
    assert!(
        state
            .animation
            .animations
            .keys()
            .any(|(id, _)| id == &inc_id_a),
        "expected at least one entry for inc"
    );

    // Rebuild with only the dec button. inc entries should be gone.
    let mut tree_b = column([crate::text("0"), row([button("-").key("dec")])]).padding(20.0);
    layout(&mut tree_b, &mut state, Rect::new(0.0, 0.0, 400.0, 200.0));
    state.hovered = None;
    state.apply_to_state();
    state.tick_visual_animations(&mut tree_b, Instant::now());
    assert!(
        !state
            .animation
            .animations
            .keys()
            .any(|(id, _)| id == &inc_id_a),
        "stale entries for inc were not GC'd"
    );
}