nsh-core 0.8.1

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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/// Identifier of an animation node, assigned by JS.
pub type NodeId = u32;

/// Identifier of one started animation, assigned by JS; reported back when
/// the animation finishes.
pub type AnimationId = u32;

/// A view property that an animation node can drive.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AnimationProp {
    /// `opacity`, 0..1.
    Opacity,
    /// `backgroundColor`, from a color interpolation.
    BackgroundColor,
    /// `borderColor`: the color of every side.
    BorderColor,
    /// `color` of a text: the whole text takes it.
    Color,
    /// The value of entry `n` of the view's `transform` list (translations
    /// in points, rotations and skews in degrees).
    Transform(u8),
    /// Animated value slot `n` of a canvas drawing, or of a shader's layer
    /// ([`crate::shader::ShaderLayer::slots`]): the canvas or the shader
    /// draws again with it (a number or a color).
    CanvasArg(u16),
    /// `top` of a `position: sticky` view: how far from the viewport's
    /// start of a vertical scroll view it sticks. The core hands it to the
    /// scroll view's sticky cells (`StickyCell::offset`), never to the
    /// view's bindings.
    StickyTop,
    /// `left`: the same in a horizontal scroll view.
    StickyLeft,
    /// Animated prop slot `n` of a registered native component: the platform
    /// hands the value to the component under the slot's name
    /// ([`crate::mount::MountItem::ComponentPropSlots`]), a number or a color.
    ComponentSlot(u16),
}

/// The value of a native component's animated prop
/// ([`AnimationProp::ComponentSlot`]), as the platform hands it over.
///
/// Values are equal when their bits are: a NaN that stays NaN is not a change
/// to hand over again.
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub enum AnimatedPropValue {
    /// A number.
    Number(f64),
    /// A color, `0xAARRGGBB` (Android's `Color` layout).
    Color(u32),
}

impl PartialEq for AnimatedPropValue {
    fn eq(&self, other: &AnimatedPropValue) -> bool {
        match (self, other) {
            (AnimatedPropValue::Number(a), AnimatedPropValue::Number(b)) => {
                a.to_bits() == b.to_bits()
            }
            (AnimatedPropValue::Color(a), AnimatedPropValue::Color(b)) => a == b,
            _ => false,
        }
    }
}

impl Eq for AnimatedPropValue {}

impl From<super::AnimationValue> for AnimatedPropValue {
    fn from(value: super::AnimationValue) -> AnimatedPropValue {
        match value {
            // The graph's colors are `0xRRGGBBAA`.
            super::AnimationValue::Color(rgba) => AnimatedPropValue::Color(rgba.rotate_right(8)),
            other => AnimatedPropValue::Number(f64::from(other.number())),
        }
    }
}

impl AnimationProp {
    /// Parses a style key (transform entries are addressed by position).
    pub fn from_name(name: &str) -> Option<AnimationProp> {
        Some(match name {
            "opacity" => AnimationProp::Opacity,
            "backgroundColor" => AnimationProp::BackgroundColor,
            "borderColor" => AnimationProp::BorderColor,
            "color" => AnimationProp::Color,
            "top" => AnimationProp::StickyTop,
            "left" => AnimationProp::StickyLeft,
            _ => return None,
        })
    }
}

/// Easing curve of a timing animation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Easing {
    /// Constant speed.
    Linear,
    /// CSS `ease-in`.
    EaseIn,
    /// CSS `ease-out`.
    EaseOut,
    /// CSS `ease-in-out`.
    EaseInOut,
    /// A cubic Bézier `(x1, y1, x2, y2)`, like CSS `cubic-bezier()`.
    Bezier(f32, f32, f32, f32),
    /// One of React Native's `Easing` functions with its `in` / `out` /
    /// `inOut` form.
    Curve(Curve, EaseMode),
    /// CSS `steps(count, position)`: jumps between `count` levels instead
    /// of moving (a frame-by-frame sprite, a ticking clock hand).
    Steps(u32, StepPosition),
}

impl Easing {
    /// CSS `ease`.
    pub const EASE: Easing = Easing::Bezier(0.25, 0.1, 0.25, 1.0);

    /// The curve played backwards (`1 - e(1 - t)`): what a keyframe segment
    /// follows when its animation runs in reverse, as in CSS.
    pub fn reversed(self) -> Easing {
        match self {
            Easing::Linear => Easing::Linear,
            Easing::EaseIn => Easing::EaseOut,
            Easing::EaseOut => Easing::EaseIn,
            Easing::EaseInOut => Easing::EaseInOut,
            Easing::Bezier(x1, y1, x2, y2) => {
                Easing::Bezier(1.0 - x2, 1.0 - y2, 1.0 - x1, 1.0 - y1)
            }
            Easing::Curve(curve, mode) => Easing::Curve(
                curve,
                match mode {
                    EaseMode::In => EaseMode::Out,
                    EaseMode::Out => EaseMode::In,
                    EaseMode::InOut => EaseMode::InOut,
                },
            ),
            Easing::Steps(count, position) => Easing::Steps(
                count,
                match position {
                    StepPosition::JumpStart => StepPosition::JumpEnd,
                    StepPosition::JumpEnd => StepPosition::JumpStart,
                    other => other,
                },
            ),
        }
    }
}

/// Where a [`Easing::Steps`] curve jumps (CSS's `<step-position>`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StepPosition {
    /// The first jump happens at the start (`jump-start`, `start`).
    JumpStart,
    /// The last jump happens at the end (`jump-end`, `end`; the default).
    #[default]
    JumpEnd,
    /// No jump at either end: holds the first and the last level
    /// (`jump-none`).
    JumpNone,
    /// Jumps at both ends (`jump-both`).
    JumpBoth,
}

impl StepPosition {
    /// Parses a CSS `<step-position>`.
    pub fn parse(name: &str) -> Option<StepPosition> {
        Some(match name {
            "jump-start" | "start" => StepPosition::JumpStart,
            "jump-end" | "end" => StepPosition::JumpEnd,
            "jump-none" => StepPosition::JumpNone,
            "jump-both" => StepPosition::JumpBoth,
            _ => return None,
        })
    }

    /// Its CSS name.
    pub fn name(self) -> &'static str {
        match self {
            StepPosition::JumpStart => "jump-start",
            StepPosition::JumpEnd => "jump-end",
            StepPosition::JumpNone => "jump-none",
            StepPosition::JumpBoth => "jump-both",
        }
    }
}

/// A shape of React Native's `Easing` module (its "in" form).
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Curve {
    /// `t²`.
    Quad,
    /// `t³`.
    Cubic,
    /// `tⁿ`.
    Poly(f32),
    /// Sinusoidal.
    Sin,
    /// Circular.
    Circle,
    /// Exponential.
    Exp,
    /// Springy overshoot; the bounciness (1 by default).
    Elastic(f32),
    /// Pulls back before going; the overshoot (1.70158 by default).
    Back(f32),
    /// Bounces at the end.
    Bounce,
}

/// How a [`Curve`] is applied.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EaseMode {
    /// As is.
    In,
    /// Mirrored: fast start, slow end.
    Out,
    /// `In` for the first half, `Out` for the second.
    InOut,
}

/// How an animation moves a value.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Driver {
    /// Fixed duration along an easing curve.
    Timing {
        /// Target value.
        to: f32,
        /// Duration in milliseconds.
        duration_ms: f32,
        /// Curve.
        easing: Easing,
        /// Delay before starting, in milliseconds.
        delay_ms: f32,
        /// The highest frame rate it needs, in Hz ([`Driver::frame_rate`]).
        frame_rate: Option<f32>,
    },
    /// Damped spring.
    Spring {
        /// Target value.
        to: f32,
        /// Spring stiffness `k`.
        stiffness: f32,
        /// Damping `c`.
        damping: f32,
        /// Mass `m`.
        mass: f32,
        /// Initial velocity (units per second); `None` keeps the current one.
        velocity: Option<f32>,
        /// The highest frame rate it needs, in Hz ([`Driver::frame_rate`]).
        frame_rate: Option<f32>,
    },
    /// Exponential slow-down from an initial velocity (flings).
    Decay {
        /// Initial velocity (units per second); `None` keeps the current one.
        velocity: Option<f32>,
        /// Fraction of velocity kept per millisecond, e.g. `0.998`.
        deceleration: f32,
        /// Bounds `(min, max)` the value stays within: it stops at a bound,
        /// or bounces back from it with `bounce`.
        clamp: Option<(f32, f32)>,
        /// The fraction of its speed the value keeps when it bounces off a
        /// bound (0: it stops there).
        bounce: f32,
        /// The highest frame rate it needs, in Hz ([`Driver::frame_rate`]).
        frame_rate: Option<f32>,
    },
}

impl Driver {
    /// The highest frame rate the animation needs, in Hz (TS's
    /// `frameRate`): a slow or small motion asks for less than the
    /// screen's highest, which platforms that can lower their frame rate
    /// honor while nothing else moves. `None` (or a rate that is not
    /// positive): the screen's highest.
    pub fn frame_rate(&self) -> Option<f32> {
        let (Driver::Timing { frame_rate, .. }
        | Driver::Spring { frame_rate, .. }
        | Driver::Decay { frame_rate, .. }) = self;
        crate::style::frame_rate(*frame_rate)
    }
}

/// The highest of two frame rates animations ask for ([`Driver::frame_rate`]):
/// `None`, the screen's highest, wins over any rate.
pub fn highest_frame_rate(a: Option<f32>, b: Option<f32>) -> Option<f32> {
    Some(a?.max(b?))
}

/// Output range of an interpolation.
#[derive(Debug, Clone, PartialEq)]
pub enum Output {
    /// Numbers.
    Numbers(Vec<f32>),
    /// Colors packed as `0xRRGGBBAA` (same as `nsh_core::Color`).
    Colors(Vec<u32>),
}

/// What an interpolation does outside its input range.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Extrapolate {
    /// Continue the first / last segment.
    Extend,
    /// Stick to the first / last output.
    Clamp,
    /// Return the input unchanged.
    Identity,
}

/// Binary operation of a math node.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MathOp {
    /// `a` modulo `b`, always non-negative for positive `b` (0 when `b`
    /// is 0).
    Mod,
    /// `a + b`.
    Add,
    /// `a - b`.
    Sub,
    /// `a * b`.
    Mul,
    /// `a / b` (0 when `b` is 0).
    Div,
}

/// An operand of a math node.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Operand {
    /// Another node.
    Node(NodeId),
    /// A constant.
    Const(f32),
}

/// A change to the animation graph, sent by JS.
#[derive(Debug, Clone, PartialEq)]
pub enum AnimationOp {
    /// A settable, animatable value.
    CreateValue {
        /// New node.
        node: NodeId,
        /// Initial value.
        value: f32,
    },
    /// Maps `input` through piecewise-linear ranges.
    CreateInterpolate {
        /// New node.
        node: NodeId,
        /// Source node.
        input: NodeId,
        /// Increasing input breakpoints.
        input_range: Vec<f32>,
        /// Outputs, same length as `input_range`.
        output: Output,
        /// Behaviour below the input range.
        extrapolate_left: Extrapolate,
        /// Behaviour above the input range.
        extrapolate_right: Extrapolate,
        /// Curve within each segment.
        easing: Easing,
    },
    /// Follows the changes of `input` but stays within `min..=max`
    /// (React Native's `diffClamp`: a header that hides as content scrolls
    /// up and shows as it scrolls down).
    CreateDiffClamp {
        /// New node.
        node: NodeId,
        /// Source node.
        input: NodeId,
        /// Lower bound: a constant or a node read at each evaluation.
        min: Operand,
        /// Upper bound: a constant or a node read at each evaluation.
        max: Operand,
        /// Settles at the nearer bound when the scroll view its input
        /// follows comes to rest ([`super::Graph::settle`]): a floating bar
        /// shows or hides fully.
        snap: bool,
    },
    /// Combines two operands.
    CreateMath {
        /// New node.
        node: NodeId,
        /// Operation.
        op: MathOp,
        /// Left operand.
        a: Operand,
        /// Right operand.
        b: Operand,
    },
    /// Gives an interpolation other outputs (the same number): colors that
    /// follow the appearance.
    SetOutput {
        /// Interpolation node.
        node: NodeId,
        /// Its outputs from now on.
        output: Output,
    },
    /// Sets a value immediately, stopping its animation.
    Set {
        /// Value node.
        node: NodeId,
        /// New value.
        value: f32,
    },
    /// Starts an animation on a value, replacing any running one.
    Start {
        /// Value node.
        node: NodeId,
        /// Reported back on completion.
        animation: AnimationId,
        /// How to move.
        driver: Driver,
    },
    /// Starts a composite animation (sequences, parallel groups, delays,
    /// loops) run natively, step after step.
    StartComposite {
        /// Reported back when the whole animation ends.
        animation: AnimationId,
        /// The composite (one JS object, run any number of times): the
        /// values it animates are recorded as they are now, for `Reset`.
        key: u32,
        /// What to run.
        spec: Composite,
    },
    /// Stops the composite `key` and puts its values back where they were
    /// when it last started.
    ResetComposite {
        /// The composite.
        key: u32,
    },
    /// Forgets the composite `key` (collected in JS).
    ReleaseComposite {
        /// The composite.
        key: u32,
    },
    /// Stops the animation of a value where it is.
    Stop {
        /// Value node.
        node: NodeId,
    },
    /// Stops a composite animation (its running steps stay where they are).
    StopAnimation {
        /// The animation.
        animation: AnimationId,
    },
    /// Frees a node.
    Release {
        /// Node.
        node: NodeId,
    },
    /// Sets the offset added to a value (React Native's `setOffset`): the
    /// value evaluates to its own value plus the offset; animations, `Set`
    /// and gestures move its own value.
    SetOffset {
        /// Value node.
        node: NodeId,
        /// The offset.
        offset: f32,
    },
    /// Merges a value's offset into it and clears the offset (React
    /// Native's `flattenOffset`).
    FlattenOffset {
        /// Value node.
        node: NodeId,
    },
    /// Moves a value into its offset, leaving it 0 (React Native's
    /// `extractOffset`: a drag then starts from 0).
    ExtractOffset {
        /// Value node.
        node: NodeId,
    },
    /// Reports the current value of any node as a [`super::Finished`] of
    /// `request` (with its `value`), so JS can read it back.
    Read {
        /// The node.
        node: NodeId,
        /// Reported back with the value.
        request: AnimationId,
    },
}

/// A composite animation.
#[derive(Debug, Clone, PartialEq)]
pub enum Composite {
    /// Moves a value.
    Run {
        /// Value node.
        node: NodeId,
        /// How.
        driver: Driver,
    },
    /// One after the other; stops at the first that does not finish.
    Sequence(Vec<Composite>),
    /// All at once.
    Parallel {
        /// The animations.
        children: Vec<Composite>,
        /// One stopping stops the others (React Native's default).
        stop_together: bool,
    },
    /// Waits, in milliseconds.
    Delay(f32),
    /// Repeats.
    Loop {
        /// The animation.
        child: Box<Composite>,
        /// Number of runs; `None` loops forever.
        iterations: Option<u32>,
        /// Values go back to where they were before each run.
        reset: bool,
    },
}

impl Composite {
    /// The highest frame rate its steps need ([`Driver::frame_rate`]):
    /// `None` when one asks for the screen's highest, or when it runs no
    /// value at all (delays alone).
    pub fn frame_rate(&self) -> Option<f32> {
        /// `None`: no step runs a value; `Some(None)`: one asks for the
        /// screen's highest.
        fn fold(c: &Composite) -> Option<Option<f32>> {
            match c {
                Composite::Run { driver, .. } => Some(driver.frame_rate()),
                Composite::Delay(_) => None,
                Composite::Loop { child, .. } => fold(child),
                Composite::Sequence(children) | Composite::Parallel { children, .. } => {
                    children.iter().filter_map(fold).reduce(highest_frame_rate)
                }
            }
        }
        fold(self).flatten()
    }

    /// The value nodes it animates.
    pub fn nodes(&self, out: &mut Vec<NodeId>) {
        match self {
            Composite::Run { node, .. } => {
                if !out.contains(node) {
                    out.push(*node);
                }
            }
            Composite::Sequence(children) | Composite::Parallel { children, .. } => {
                for c in children {
                    c.nodes(out);
                }
            }
            Composite::Delay(_) => {}
            Composite::Loop { child, .. } => child.nodes(out),
        }
    }
}