nsh-core 0.3.0

Platform-independent core: shadow tree, styles, layout and mount instructions.
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
//! The graph on a desktop backend's main thread, without the views: the
//! backend's frame clock ticks it, drags and scroll offsets update it, and
//! [`Animator::write`] hands each bound view its [`AnimValues`] (the
//! Android animator's design, `nsh-platform-android/src/anim.rs`). The
//! backends store the values in their views and redraw them.

use std::sync::Arc;

use rustc_hash::FxHashMap;

use super::{AnimatedPropValue, AnimationProp, AnimationValue, Finished, Graph, NodeId};
use crate::{
    Color, GestureValues, MAX_TRANSFORMS, MountItem,
    geometry::{Point, Points},
};

/// Values native animations set on a view; they win over the style's.
#[non_exhaustive]
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct AnimValues {
    /// Animated opacity.
    pub opacity: Option<f32>,
    /// Animated background color.
    pub background: Option<Color>,
    /// Animated entries of the transform list (by position).
    pub transform: [Option<f32>; MAX_TRANSFORMS],
    /// Animated border color (every side).
    pub border_color: Option<Color>,
    /// Animated text color (a text view's runs, an input's text).
    pub color: Option<Color>,
    /// Animated shadow color.
    pub shadow_color: Option<Color>,
    /// Animated shadow opacity.
    pub shadow_opacity: Option<f32>,
    /// Animated shadow blur radius.
    pub shadow_radius: Option<Points>,
}

#[derive(Default)]
struct Gestures {
    values: GestureValues,
    /// Values when the gesture began: pan x, pan y, pinch, rotation.
    start: [f32; 4],
}

/// The graph of one surface, with what binds it to views, gestures and
/// scroll views.
#[non_exhaustive]
#[derive(Default)]
pub struct Animator {
    graph: Graph,
    /// Animations that finished, for the backend to report to JS.
    pub finished: Vec<Finished>,
    /// View id -> bound properties.
    bindings: FxHashMap<u32, Vec<(AnimationProp, NodeId)>>,
    gestures: FxHashMap<u32, Gestures>,
    /// Scroll view id -> (value node, horizontal).
    scrolls: FxHashMap<u32, (NodeId, bool)>,
    /// Vertical scroll view id -> pull value node.
    pulls: FxHashMap<u32, NodeId>,
    /// Scroll views with sticky cells that stick an animated value further
    /// in (`StickyCell::offset`): pinned again on the graph's frames.
    sticky: Vec<u32>,
    /// A view's canvas slot values while they are written (reused: written
    /// on every frame).
    canvas_args: Vec<(u16, AnimationValue)>,
    /// Native component view id -> the names of its animated prop slots.
    component_slots: FxHashMap<u32, Vec<Arc<str>>>,
    /// (view, slot) -> the value last written to the component.
    component_values: FxHashMap<(u32, u16), AnimatedPropValue>,
}

impl Animator {
    /// Looks at one item of a batch: animation ops and bindings, gesture
    /// and scroll values, and deletions (which drop the view's entries).
    pub fn apply(&mut self, item: &MountItem) {
        match item {
            MountItem::Animation(op) => self.graph.apply(op.clone(), &mut self.finished),
            MountItem::ComponentPropSlots { id, names } => {
                self.component_slots.insert(*id, names.clone());
            }
            MountItem::AnimationBindings { id, bindings } => {
                // An unbound slot is written again once bound again.
                self.component_values.retain(|&(view, slot), _| {
                    view != *id
                        || bindings
                            .iter()
                            .any(|(p, _)| *p == AnimationProp::ComponentSlot(slot))
                });
                if bindings.is_empty() {
                    self.bindings.remove(id);
                } else {
                    self.bindings.insert(*id, bindings.clone());
                }
            }
            MountItem::Gestures { id, values } => {
                if values.is_empty() {
                    self.gestures.remove(id);
                } else {
                    self.gestures.entry(*id).or_default().values = *values;
                }
            }
            MountItem::ScrollConfig {
                id,
                horizontal,
                scroll_value,
                pull_value,
                ..
            } => {
                match scroll_value {
                    Some(node) => {
                        self.scrolls.insert(*id, (*node, *horizontal));
                    }
                    None => {
                        self.scrolls.remove(id);
                    }
                }
                match pull_value.filter(|_| !*horizontal) {
                    Some(node) => {
                        self.pulls.insert(*id, node);
                    }
                    None => {
                        self.pulls.remove(id);
                    }
                }
            }
            MountItem::StickyCells { id, cells } => {
                let animated = cells.iter().any(|c| c.offset.is_some());
                let at = self.sticky.iter().position(|v| v == id);
                match (animated, at) {
                    (true, None) => self.sticky.push(*id),
                    (false, Some(i)) => {
                        self.sticky.swap_remove(i);
                    }
                    _ => {}
                }
            }
            MountItem::Delete { id } => {
                self.component_slots.remove(id);
                self.component_values.retain(|&(view, _), _| view != *id);
                self.bindings.remove(id);
                self.gestures.remove(id);
                self.scrolls.remove(id);
                self.pulls.remove(id);
                self.sticky.retain(|v| v != id);
            }
            _ => {}
        }
    }

    /// Advances the graph to `now` (the graph's clock); true while it
    /// animates.
    pub fn tick(&mut self, now: f64) -> bool {
        self.graph.tick(now, &mut self.finished)
    }

    /// The scroll views whose sticky cells stick an animated value further
    /// from the viewport's start: pin them again after writing a frame.
    pub fn sticky_views(&self) -> &[u32] {
        &self.sticky
    }

    /// The value of node `node` now (a sticky cell's offset; 0 for a node
    /// it does not know).
    pub fn value(&self, node: NodeId) -> f32 {
        self.graph.eval(node).number()
    }

    /// True while a driver of the graph runs.
    pub fn animating(&self) -> bool {
        self.graph.is_animating()
    }

    /// A scroll view moved to `offset`: feeds its scroll value. False if no
    /// value is bound to it.
    pub fn scrolled(&mut self, id: u32, offset: Point<Points>) -> bool {
        let Some(&(node, horizontal)) = self.scrolls.get(&id) else {
            return false;
        };
        let value = if horizontal { offset.x } else { offset.y };
        self.graph.set(node, value.0, &mut self.finished);
        true
    }

    /// A vertical scroll view is pulled `distance` past its top (0 when
    /// not): feeds its pull value. False if no value is bound to it.
    pub fn pulled(&mut self, id: u32, distance: Points) -> bool {
        let Some(&node) = self.pulls.get(&id) else {
            return false;
        };
        self.graph
            .set(node, distance.0.max(0.0), &mut self.finished);
        true
    }

    /// Scroll view `id` came to rest (its drag and momentum ended): the
    /// snapping `diffClamp`s that follow its scroll value settle
    /// ([`super::Graph::settle`]). True if one starts moving: the platform
    /// runs its frames.
    pub fn scroll_ended(&mut self, id: u32) -> bool {
        let Some(&(node, _)) = self.scrolls.get(&id) else {
            return false;
        };
        self.graph.settle(node)
    }

    /// A drag began: grabbing an animating value stops it where it is.
    pub fn pan_began(&mut self, id: u32) {
        let graph = &self.graph;
        if let Some(g) = self.gestures.get_mut(&id) {
            g.start[0] = g.values.pan_x.and_then(|n| graph.value(n)).unwrap_or(0.0);
            g.start[1] = g.values.pan_y.and_then(|n| graph.value(n)).unwrap_or(0.0);
        }
    }

    /// The drag moved by `translation` since it began.
    pub fn pan_moved(&mut self, id: u32, translation: Point<Points>) {
        let Some(g) = self.gestures.get(&id) else {
            return;
        };
        let (x, y, start) = (g.values.pan_x, g.values.pan_y, g.start);
        if let Some(n) = x {
            self.graph
                .set(n, start[0] + translation.x.0, &mut self.finished);
        }
        if let Some(n) = y {
            self.graph
                .set(n, start[1] + translation.y.0, &mut self.finished);
        }
    }

    /// The values the drag on `id` moves (`pan.x`, `pan.y`).
    pub fn pan_values(&self, id: u32) -> (Option<f32>, Option<f32>) {
        let Some(g) = self.gestures.get(&id) else {
            return (None, None);
        };
        (
            g.values.pan_x.and_then(|n| self.graph.value(n)),
            g.values.pan_y.and_then(|n| self.graph.value(n)),
        )
    }

    /// A pinch (`rotation` false) or two-finger rotation began.
    pub fn transform_began(&mut self, id: u32, rotation: bool) {
        let graph = &self.graph;
        if let Some(g) = self.gestures.get_mut(&id) {
            let (node, slot, default) = if rotation {
                (g.values.rotation, 3, 0.0)
            } else {
                (g.values.pinch, 2, 1.0)
            };
            g.start[slot] = node.and_then(|n| graph.value(n)).unwrap_or(default);
        }
    }

    /// The pinch scale (or rotation in degrees) since the gesture began.
    pub fn transform_moved(&mut self, id: u32, rotation: bool, amount: f32) {
        let Some(g) = self.gestures.get(&id) else {
            return;
        };
        if rotation {
            if let Some(n) = g.values.rotation {
                let v = g.start[3] + amount;
                self.graph.set(n, v, &mut self.finished);
            }
        } else if let Some(n) = g.values.pinch {
            let v = g.start[2] * amount;
            self.graph.set(n, v, &mut self.finished);
        }
    }

    /// Evaluates every bound property: for each view `find` knows, `set`
    /// gets its values and the values of its canvas slots.
    pub fn write<V>(
        &mut self,
        mut find: impl FnMut(u32) -> Option<V>,
        mut set: impl FnMut(V, AnimValues, &[(u16, AnimationValue)]),
    ) {
        let canvas = &mut self.canvas_args;
        for (&id, bindings) in &self.bindings {
            let Some(view) = find(id) else {
                continue;
            };
            let mut values = AnimValues::default();
            canvas.clear();
            for &(prop, node) in bindings {
                let value = self.graph.eval(node);
                match prop {
                    AnimationProp::Opacity => values.opacity = Some(value.number()),
                    AnimationProp::BackgroundColor => values.background = Some(color(value)),
                    AnimationProp::BorderColor => values.border_color = Some(color(value)),
                    AnimationProp::Color => values.color = Some(color(value)),
                    AnimationProp::ShadowColor => values.shadow_color = Some(color(value)),
                    AnimationProp::ShadowOpacity => values.shadow_opacity = Some(value.number()),
                    AnimationProp::ShadowRadius => {
                        values.shadow_radius = Some(Points(value.number()))
                    }
                    AnimationProp::Transform(i) => {
                        if let Some(t) = values.transform.get_mut(i as usize) {
                            *t = Some(value.number());
                        }
                    }
                    AnimationProp::CanvasArg(k) => canvas.push((k, value)),
                    // The scroll view's (`sticky_views`), never bound.
                    AnimationProp::StickyTop | AnimationProp::StickyLeft => {}
                    // The component's (`write_components`).
                    AnimationProp::ComponentSlot(_) => {}
                }
            }
            set(view, values, canvas);
        }
    }
}

impl Animator {
    /// Evaluates every bound native component prop: for each view `find`
    /// knows, `set` gets the slot's name and value, only when it changed
    /// since the last call (the components see one call per change).
    pub fn write_components<V: Copy>(
        &mut self,
        mut find: impl FnMut(u32) -> Option<V>,
        mut set: impl FnMut(V, &str, AnimatedPropValue),
    ) {
        for (&id, bindings) in &self.bindings {
            let Some(names) = self.component_slots.get(&id) else {
                continue;
            };
            for &(prop, node) in bindings {
                let AnimationProp::ComponentSlot(slot) = prop else {
                    continue;
                };
                let Some(name) = names.get(slot as usize) else {
                    continue;
                };
                let value = AnimatedPropValue::from(self.graph.eval(node));
                if self.component_values.get(&(id, slot)) == Some(&value) {
                    continue;
                }
                let Some(view) = find(id) else {
                    continue;
                };
                self.component_values.insert((id, slot), value);
                set(view, name, value);
            }
        }
    }
}

/// A bound color: animated colors are `0xRRGGBBAA` values.
fn color(value: AnimationValue) -> Color {
    match value {
        AnimationValue::Color(c) => Color(c),
        other => Color(other.number() as u32),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        StickyCell,
        animation::AnimationOp,
        geometry::{Points, point, px},
    };

    fn component_writes(a: &mut Animator) -> Vec<(u32, String, AnimatedPropValue)> {
        let mut out = Vec::new();
        a.write_components(Some, |id, name, value| {
            out.push((id, name.to_string(), value))
        });
        out
    }

    fn bound_component(a: &mut Animator, value: f32) {
        a.apply(&MountItem::Animation(AnimationOp::CreateValue {
            node: 2,
            value,
        }));
        a.apply(&MountItem::ComponentPropSlots {
            id: 4,
            names: vec!["progress".into()],
        });
        a.apply(&MountItem::AnimationBindings {
            id: 4,
            bindings: vec![(AnimationProp::ComponentSlot(0), 2)],
        });
    }

    #[test]
    fn component_values_written_once_per_change() {
        let mut a = Animator::default();
        bound_component(&mut a, 0.25);
        assert_eq!(
            component_writes(&mut a),
            vec![(4, "progress".to_string(), AnimatedPropValue::Number(0.25))]
        );
        // Unchanged: nothing.
        assert!(component_writes(&mut a).is_empty());
        a.apply(&MountItem::Animation(AnimationOp::Set {
            node: 2,
            value: 0.5,
        }));
        assert_eq!(
            component_writes(&mut a),
            vec![(4, "progress".to_string(), AnimatedPropValue::Number(0.5))]
        );
    }

    #[test]
    fn component_color_is_argb() {
        assert_eq!(
            AnimatedPropValue::from(AnimationValue::Color(0x1122_3344)),
            AnimatedPropValue::Color(0x4411_2233)
        );
        assert_eq!(
            AnimatedPropValue::from(AnimationValue::Number(0.5)),
            AnimatedPropValue::Number(0.5)
        );
    }

    #[test]
    fn deleted_component_slots_are_dropped() {
        let mut a = Animator::default();
        bound_component(&mut a, 0.25);
        component_writes(&mut a);
        a.apply(&MountItem::Delete { id: 4 });
        assert!(component_writes(&mut a).is_empty());
        // A view made again with that id starts over.
        bound_component(&mut a, 0.25);
        assert_eq!(component_writes(&mut a).len(), 1);
    }

    #[test]
    fn rebound_component_prop_is_written_again() {
        let mut a = Animator::default();
        bound_component(&mut a, 0.25);
        component_writes(&mut a);
        a.apply(&MountItem::AnimationBindings {
            id: 4,
            bindings: vec![],
        });
        a.apply(&MountItem::AnimationBindings {
            id: 4,
            bindings: vec![(AnimationProp::ComponentSlot(0), 2)],
        });
        assert_eq!(component_writes(&mut a).len(), 1);
    }

    #[test]
    fn sticky_views_with_animated_offsets_are_pinned_on_frames() {
        let mut a = Animator::default();
        a.apply(&MountItem::Animation(AnimationOp::CreateValue {
            node: 2,
            value: 56.0,
        }));
        let cell = |offset| StickyCell {
            cell: 9,
            start: Points(0.0),
            end: Points(100.0),
            offset,
        };
        a.apply(&MountItem::StickyCells {
            id: 4,
            cells: vec![cell(Some(2))],
        });
        assert_eq!(a.sticky_views(), [4]);
        assert_eq!(a.value(2), 56.0);
        // Its scroll value moves the offset.
        a.apply(&MountItem::ScrollConfig {
            id: 4,
            horizontal: false,
            scrollbar_visible: true,
            scroll_value: Some(2),
            pull_value: None,
            keyboard_dismiss: Default::default(),
            mirrored: false,
        });
        a.scrolled(4, point(px(0.0), px(12.0)));
        assert_eq!(a.value(2), 12.0);
        // No animated offset any more.
        a.apply(&MountItem::StickyCells {
            id: 4,
            cells: vec![cell(None)],
        });
        assert!(a.sticky_views().is_empty());
    }

    #[test]
    fn a_scroll_view_at_rest_settles_the_values_that_follow_it() {
        let mut a = Animator::default();
        a.apply(&MountItem::Animation(AnimationOp::CreateValue {
            node: 2,
            value: 0.0,
        }));
        a.apply(&MountItem::Animation(AnimationOp::CreateDiffClamp {
            node: 3,
            input: 2,
            min: 0.0,
            max: 50.0,
            snap: true,
        }));
        a.apply(&MountItem::ScrollConfig {
            id: 4,
            horizontal: false,
            scrollbar_visible: true,
            scroll_value: Some(2),
            pull_value: None,
            keyboard_dismiss: Default::default(),
            mirrored: false,
        });
        for y in [0.0, 200.0, 190.0] {
            a.scrolled(
                4,
                crate::geometry::point(crate::Points(0.0), crate::Points(y)),
            );
            a.graph.eval(3);
        }
        // Another scroll view's end settles nothing.
        assert!(!a.scroll_ended(5));
        assert!(a.scroll_ended(4));
        assert!(a.animating());
        a.tick(1.0);
        a.tick(1.3);
        assert_eq!(a.graph.eval(3).number(), 50.0);
        assert!(!a.animating());
    }
}