@rafaelaugusto/jolt 0.1.0-beta.1 → 0.1.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,225 @@
1
+ import { Image, ImageSource, isIOS } from '@nativescript/core'
2
+ import { toView } from './animate.js'
3
+ import type { Animation } from './engine.js'
4
+ import type { Target } from './types.js'
5
+
6
+ export type SymbolEffectName = 'bounce' | 'pulse' | 'scale' | 'variableColor'
7
+
8
+ export type SymbolEffect = SymbolEffectName | { replace: `sys://${string}` }
9
+
10
+ export interface SymbolEffectOptions {
11
+ /** Native playback speed multiplier. Defaults to 1. */
12
+ speed?: number
13
+ /** Repeat bounce, pulse or variableColor until stopped. Defaults to false. */
14
+ repeat?: boolean
15
+ }
16
+
17
+ const running = new WeakMap<Image, SymbolAnimation>()
18
+ const names: readonly string[] = ['bounce', 'pulse', 'scale', 'variableColor']
19
+
20
+ /**
21
+ * Plays an iOS 17+ SF Symbol effect on a NativeScript Image.
22
+ * Scale holds until stopped; replacements also update the Image's src.
23
+ *
24
+ * ```ts
25
+ * symbolEffect(icon, 'bounce')
26
+ * symbolEffect(icon, { replace: 'sys://heart.fill' })
27
+ * ```
28
+ */
29
+ export function symbolEffect(
30
+ target: Target,
31
+ effect: SymbolEffect,
32
+ options: SymbolEffectOptions = {},
33
+ ): Animation {
34
+ const view = toView(target)
35
+ if (!(view instanceof Image)) throw new TypeError('symbolEffect expects an Image.')
36
+ if (
37
+ typeof effect === 'string'
38
+ ? !names.includes(effect)
39
+ : !effect ||
40
+ typeof effect.replace !== 'string' ||
41
+ !/^sys:\/\/[^\s/]+$/.test(effect.replace)
42
+ ) {
43
+ throw new TypeError('Expected a symbol effect name or { replace: "sys://name" }.')
44
+ }
45
+ const speed = options.speed ?? 1
46
+ if (!Number.isFinite(speed) || speed <= 0)
47
+ throw new RangeError('Symbol effect speed must be a positive finite number.')
48
+ if (options.repeat !== undefined && typeof options.repeat !== 'boolean')
49
+ throw new TypeError('Symbol effect repeat must be a boolean.')
50
+ if (options.repeat && (typeof effect !== 'string' || effect === 'scale'))
51
+ throw new TypeError('Only bounce, pulse and variableColor can repeat.')
52
+
53
+ return new SymbolAnimation(view, effect, speed, options.repeat ?? false)
54
+ }
55
+
56
+ class SymbolAnimation implements Animation {
57
+ readonly finished: Promise<boolean>
58
+ private resolve!: (finished: boolean) => void
59
+ private settled = false
60
+ private started = false
61
+ private changingSource = false
62
+ private waitingForLoad: boolean
63
+ private native: UIImageView | undefined
64
+ private nativeEffect: NSSymbolEffect | undefined
65
+ private replacement: UIImage | undefined
66
+
67
+ constructor(
68
+ private readonly view: Image,
69
+ private readonly effect: SymbolEffect,
70
+ private readonly speed: number,
71
+ private readonly repeat: boolean,
72
+ ) {
73
+ this.finished = new Promise((resolve) => (this.resolve = resolve))
74
+ this.waitingForLoad = !view.isLoaded
75
+ running.get(view)?.stop()
76
+ running.set(view, this)
77
+ view.on('unloaded', this.stop)
78
+ view.on('srcChange', this.sourceChanged)
79
+ view.on('imageSourceChange', this.imageChanged)
80
+ view.on('loaded', this.loaded)
81
+ view.on('isLoadingChange', this.start)
82
+ this.start()
83
+ }
84
+
85
+ then<A = boolean, B = never>(
86
+ onfulfilled?: ((finished: boolean) => A | PromiseLike<A>) | null,
87
+ onrejected?: ((reason: unknown) => B | PromiseLike<B>) | null,
88
+ ): Promise<A | B> {
89
+ return this.finished.then(onfulfilled, onrejected)
90
+ }
91
+
92
+ stop = () => this.end(false, true)
93
+
94
+ private sourceChanged = () => {
95
+ if (!this.changingSource) this.stop()
96
+ }
97
+
98
+ private imageChanged = () => {
99
+ if (this.changingSource) return
100
+ if (this.started) this.stop()
101
+ else this.start()
102
+ }
103
+
104
+ private loaded = () => {
105
+ this.waitingForLoad = false
106
+ this.start()
107
+ }
108
+
109
+ private start = () => {
110
+ if (this.settled || this.started) return
111
+ if (!isIOS) return this.end(false)
112
+ const { view, effect } = this
113
+ // isLoaded becomes true before NativeScript resumes native property updates.
114
+ // Wait for the loaded event so intermediate image resets don't start playback.
115
+ if (
116
+ this.waitingForLoad ||
117
+ !view.isLoaded ||
118
+ !view.nativeViewProtected ||
119
+ view.isLoading
120
+ )
121
+ return
122
+ this.started = true
123
+ const native = (this.native = view.nativeViewProtected as UIImageView)
124
+ const reducedMotion = UIAccessibilityIsReduceMotionEnabled()
125
+
126
+ if (typeof effect !== 'string') {
127
+ // Resolve first so a symbol absent on this OS doesn't clear the current image.
128
+ if (!ImageSource.fromSystemImageSync(effect.replace.slice(6))?.ios)
129
+ return this.end(false)
130
+ const previous = native.image
131
+ const loadMode = view.loadMode
132
+ this.changingSource = true
133
+ try {
134
+ // Let NativeScript update its source, tint, configuration and pending loads.
135
+ // Both assignments happen in this turn, before UIKit draws the next frame.
136
+ view.loadMode = 'sync'
137
+ view.src = effect.replace
138
+ } finally {
139
+ view.loadMode = loadMode
140
+ this.changingSource = false
141
+ }
142
+ // A source-change listener may have started another effect or changed the src.
143
+ if (this.settled) return
144
+ if (view.src !== effect.replace) return this.end(false)
145
+ const image = native.image
146
+ if (!image) return this.end(false)
147
+ if (
148
+ reducedMotion ||
149
+ !previous?.symbolImage ||
150
+ !native.respondsToSelector(
151
+ 'setSymbolImage:withContentTransition:options:completion:',
152
+ )
153
+ )
154
+ return this.end(true)
155
+
156
+ this.replacement = image
157
+ native.image = previous
158
+ native.setSymbolImageWithContentTransitionOptionsCompletion(
159
+ image,
160
+ NSSymbolReplaceContentTransition.transition(),
161
+ this.options(),
162
+ this.completed,
163
+ )
164
+ return
165
+ }
166
+
167
+ if (
168
+ reducedMotion ||
169
+ !native.image?.symbolImage ||
170
+ !native.respondsToSelector('addSymbolEffect:options:animated:completion:')
171
+ )
172
+ return this.end(false)
173
+
174
+ const factories = {
175
+ bounce: NSSymbolBounceEffect,
176
+ pulse: NSSymbolPulseEffect,
177
+ scale: NSSymbolScaleEffect,
178
+ variableColor: NSSymbolVariableColorEffect,
179
+ }
180
+ this.nativeEffect = factories[effect].effect()
181
+ native.addSymbolEffectOptionsAnimatedCompletion(
182
+ this.nativeEffect,
183
+ this.options(),
184
+ true,
185
+ this.completed,
186
+ )
187
+ }
188
+
189
+ private options(): NSSymbolEffectOptions {
190
+ const options = this.repeat
191
+ ? NSSymbolEffectOptions.optionsWithRepeating()
192
+ : NSSymbolEffectOptions.optionsWithNonRepeating()
193
+ return options.optionsWithSpeed(this.speed)
194
+ }
195
+
196
+ private completed = (context: UISymbolEffectCompletionContext) => {
197
+ // Scale's entrance can complete while the effect is still holding its state.
198
+ if (context.finished && (this.effect === 'scale' || this.repeat)) return
199
+ this.end(context.finished)
200
+ }
201
+
202
+ private end(finished: boolean, remove = false) {
203
+ if (this.settled) return
204
+ this.settled = true
205
+ const { view, native, nativeEffect, replacement } = this
206
+ view.off('loaded', this.loaded)
207
+ view.off('isLoadingChange', this.start)
208
+ view.off('unloaded', this.stop)
209
+ view.off('srcChange', this.sourceChanged)
210
+ view.off('imageSourceChange', this.imageChanged)
211
+ if (running.get(view) === this) running.delete(view)
212
+ if (remove && native) {
213
+ if (nativeEffect) native.removeSymbolEffectOfType(nativeEffect)
214
+ // Content transitions have no removal API. Keep the destination icon.
215
+ if (replacement && native.image === replacement) {
216
+ native.image = null
217
+ native.image = replacement
218
+ }
219
+ }
220
+ this.native = undefined
221
+ this.nativeEffect = undefined
222
+ this.replacement = undefined
223
+ this.resolve(finished)
224
+ }
225
+ }
package/src/types.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { View } from '@nativescript/core'
2
+ import type { Haptic } from './haptics.js'
2
3
  import type { SpringName, Transition } from './transition.js'
3
4
 
4
5
  /** A value to animate to, or keyframes. `null` stands for the value the view has when the animation starts. */
@@ -38,4 +39,6 @@ export type AnimateOptions = Transition & {
38
39
  relative?: boolean
39
40
  /** With several targets, delays each one by this much more than the previous. */
40
41
  stagger?: number | Stagger
42
+ /** iOS impact feedback at arrival, or at explicit points in a finite animation. */
43
+ haptic?: Haptic
41
44
  }