@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.
- package/README.md +58 -5
- package/dist/animate.d.ts.map +1 -1
- package/dist/animate.js +18 -5
- package/dist/animate.js.map +1 -1
- package/dist/driver/ios.d.ts.map +1 -1
- package/dist/driver/ios.js +7 -0
- package/dist/driver/ios.js.map +1 -1
- package/dist/engine.d.ts +6 -3
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +15 -2
- package/dist/engine.js.map +1 -1
- package/dist/haptics.d.ts +18 -0
- package/dist/haptics.d.ts.map +1 -0
- package/dist/haptics.js +96 -0
- package/dist/haptics.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/particles.d.ts +39 -0
- package/dist/particles.d.ts.map +1 -0
- package/dist/particles.js +264 -0
- package/dist/particles.js.map +1 -0
- package/dist/shimmer.d.ts +12 -0
- package/dist/shimmer.d.ts.map +1 -0
- package/dist/shimmer.js +160 -0
- package/dist/shimmer.js.map +1 -0
- package/dist/symbol-effect.d.ts +23 -0
- package/dist/symbol-effect.d.ts.map +1 -0
- package/dist/symbol-effect.js +186 -0
- package/dist/symbol-effect.js.map +1 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -1
- package/src/animate.ts +21 -3
- package/src/driver/ios.ts +11 -0
- package/src/engine.ts +15 -3
- package/src/haptics.ts +125 -0
- package/src/index.ts +11 -0
- package/src/particles.ts +323 -0
- package/src/shimmer.ts +179 -0
- package/src/symbol-effect.ts +225 -0
- package/src/types.ts +3 -0
package/src/particles.ts
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { Application, Color, isIOS, type View } from '@nativescript/core'
|
|
2
|
+
import { toView } from './animate.js'
|
|
3
|
+
import type { Target } from './types.js'
|
|
4
|
+
|
|
5
|
+
export interface ConfettiOptions {
|
|
6
|
+
/** Approximate number of particles in the burst. Defaults to 80. */
|
|
7
|
+
count?: number
|
|
8
|
+
colors?: readonly string[]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ParticleOptions {
|
|
12
|
+
colors?: readonly string[]
|
|
13
|
+
shape?: 'circle' | 'rectangle' | 'ring'
|
|
14
|
+
/** Center point, or a line across the top/bottom of the target. */
|
|
15
|
+
source?: 'center' | 'top' | 'bottom'
|
|
16
|
+
/** Total particles per second, shared across all colors. Defaults to 20. */
|
|
17
|
+
rate?: number
|
|
18
|
+
/** Particle lifetime in milliseconds. Defaults to 3000. */
|
|
19
|
+
lifetime?: number
|
|
20
|
+
/** Longest side in DIPs. Defaults to 6. */
|
|
21
|
+
size?: number
|
|
22
|
+
/** DIPs per second. Defaults to 60. */
|
|
23
|
+
velocity?: number
|
|
24
|
+
velocityRange?: number
|
|
25
|
+
/** Degrees clockwise from right: -90 is up, 90 is down. Defaults to -90. */
|
|
26
|
+
angle?: number
|
|
27
|
+
/** Emission cone in degrees, from 0 to 360. Defaults to 30. */
|
|
28
|
+
spread?: number
|
|
29
|
+
/** Downward acceleration in DIPs per second squared. Defaults to 0. */
|
|
30
|
+
gravity?: number
|
|
31
|
+
/** Degrees per second. Defaults to 0. */
|
|
32
|
+
spin?: number
|
|
33
|
+
spinRange?: number
|
|
34
|
+
/** Fade each particle over its lifetime. Defaults to true. */
|
|
35
|
+
fadeOut?: boolean
|
|
36
|
+
/** Emit for this many milliseconds, then let particles expire. Omit to keep emitting. */
|
|
37
|
+
duration?: number
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type Settings = Required<Omit<ParticleOptions, 'duration'>> & { duration?: number }
|
|
41
|
+
const bursts = new WeakMap<View, () => void>()
|
|
42
|
+
const emitters = new WeakMap<View, () => void>()
|
|
43
|
+
const palette = ['#facc15', '#ef4444', '#3b82f6', '#22c55e', '#a855f7']
|
|
44
|
+
const textures = new Map<Settings['shape'], UIImage>()
|
|
45
|
+
const radians = (degrees: number) => (degrees * Math.PI) / 180
|
|
46
|
+
const BURST_DURATION = 200
|
|
47
|
+
|
|
48
|
+
/** Bursts from the target's center into its window. iOS only; returns immediate cleanup. */
|
|
49
|
+
export function confetti(target: Target, options: ConfettiOptions = {}): () => void {
|
|
50
|
+
const count = number('count', options.count ?? 80, 0, 10000)
|
|
51
|
+
if (!Number.isInteger(count)) throw new RangeError('Particle count must be an integer.')
|
|
52
|
+
const settings = resolve(
|
|
53
|
+
{
|
|
54
|
+
colors: options.colors ?? palette,
|
|
55
|
+
shape: 'rectangle',
|
|
56
|
+
rate: count / (BURST_DURATION / 1000),
|
|
57
|
+
duration: BURST_DURATION,
|
|
58
|
+
lifetime: 2200,
|
|
59
|
+
size: 8,
|
|
60
|
+
velocity: 260,
|
|
61
|
+
velocityRange: 100,
|
|
62
|
+
angle: -90,
|
|
63
|
+
spread: 100,
|
|
64
|
+
gravity: 420,
|
|
65
|
+
spinRange: 360,
|
|
66
|
+
},
|
|
67
|
+
true,
|
|
68
|
+
)
|
|
69
|
+
return start(target, settings, true)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Adds a native particle emitter to the target. iOS only; returns immediate cleanup. */
|
|
73
|
+
export function particles(target: Target, options: ParticleOptions = {}): () => void {
|
|
74
|
+
return start(target, resolve(options), false)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function start(target: Target, settings: Settings, overlay: boolean): () => void {
|
|
78
|
+
const view = toView(target)
|
|
79
|
+
const running = overlay ? bursts : emitters
|
|
80
|
+
running.get(view)?.()
|
|
81
|
+
if (!isIOS || settings.rate === 0 || UIAccessibilityIsReduceMotionEnabled())
|
|
82
|
+
return () => {}
|
|
83
|
+
return new Emitter(view, settings, overlay, running).stop
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function resolve(options: ParticleOptions, burst = false): Settings {
|
|
87
|
+
const colors = options.colors ?? ['#ffffff']
|
|
88
|
+
if (
|
|
89
|
+
!Array.isArray(colors) ||
|
|
90
|
+
colors.length === 0 ||
|
|
91
|
+
colors.length > 32 ||
|
|
92
|
+
Array.from(colors).some((color) => typeof color !== 'string' || !Color.isValid(color))
|
|
93
|
+
)
|
|
94
|
+
throw new TypeError('Particles need 1 to 32 valid color strings.')
|
|
95
|
+
const shape = options.shape ?? 'circle'
|
|
96
|
+
const source = options.source ?? 'center'
|
|
97
|
+
if (!['circle', 'rectangle', 'ring'].includes(shape))
|
|
98
|
+
throw new TypeError('Unknown particle shape.')
|
|
99
|
+
if (!['center', 'top', 'bottom'].includes(source))
|
|
100
|
+
throw new TypeError('Unknown particle source.')
|
|
101
|
+
const lifetime = number('lifetime', options.lifetime ?? 3000, 1, 60000)
|
|
102
|
+
const rate = number('rate', options.rate ?? 20, 0, burst ? 50000 : 10000)
|
|
103
|
+
if (!burst && (rate * lifetime) / 1000 > 10000)
|
|
104
|
+
throw new RangeError('Particle rate × lifetime must not exceed 10000 live particles.')
|
|
105
|
+
const duration =
|
|
106
|
+
options.duration === undefined
|
|
107
|
+
? undefined
|
|
108
|
+
: number('duration', options.duration, 1, 2147483647 - lifetime - 50)
|
|
109
|
+
const fadeOut = options.fadeOut ?? true
|
|
110
|
+
if (typeof fadeOut !== 'boolean')
|
|
111
|
+
throw new TypeError('Particle fadeOut must be a boolean.')
|
|
112
|
+
return {
|
|
113
|
+
colors: [...colors],
|
|
114
|
+
shape,
|
|
115
|
+
source,
|
|
116
|
+
lifetime,
|
|
117
|
+
rate,
|
|
118
|
+
duration,
|
|
119
|
+
fadeOut,
|
|
120
|
+
size: number('size', options.size ?? 6, 1, 128),
|
|
121
|
+
velocity: number('velocity', options.velocity ?? 60, 0, 10000),
|
|
122
|
+
velocityRange: number('velocityRange', options.velocityRange ?? 0, 0, 10000),
|
|
123
|
+
angle: number('angle', options.angle ?? -90, -360, 360),
|
|
124
|
+
spread: number('spread', options.spread ?? 30, 0, 360),
|
|
125
|
+
gravity: number('gravity', options.gravity ?? 0, -10000, 10000),
|
|
126
|
+
spin: number('spin', options.spin ?? 0, -3600, 3600),
|
|
127
|
+
spinRange: number('spinRange', options.spinRange ?? 0, 0, 3600),
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function number(name: string, value: number, min: number, max: number): number {
|
|
132
|
+
if (!Number.isFinite(value) || value < min || value > max)
|
|
133
|
+
throw new RangeError(`Particle ${name} must be between ${min} and ${max}.`)
|
|
134
|
+
return value
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
class Emitter {
|
|
138
|
+
private stopped = false
|
|
139
|
+
private queued = false
|
|
140
|
+
private layer: CAEmitterLayer | undefined
|
|
141
|
+
private native: UIView | undefined
|
|
142
|
+
private window: UIWindow | undefined
|
|
143
|
+
private timer: ReturnType<typeof setTimeout> | undefined
|
|
144
|
+
private observer: ReturnType<typeof Application.ios.addNotificationObserver> | undefined
|
|
145
|
+
|
|
146
|
+
constructor(
|
|
147
|
+
private readonly view: View,
|
|
148
|
+
private readonly settings: Settings,
|
|
149
|
+
private readonly overlay: boolean,
|
|
150
|
+
private readonly running: WeakMap<View, () => void>,
|
|
151
|
+
) {
|
|
152
|
+
running.set(view, this.stop)
|
|
153
|
+
view.on('loaded', this.schedule)
|
|
154
|
+
view.on('layoutChanged', this.schedule)
|
|
155
|
+
view.on('unloaded', this.stop)
|
|
156
|
+
this.schedule()
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
stop = () => {
|
|
160
|
+
if (this.stopped) return
|
|
161
|
+
this.stopped = true
|
|
162
|
+
this.view.off('loaded', this.schedule)
|
|
163
|
+
this.view.off('layoutChanged', this.schedule)
|
|
164
|
+
this.view.off('unloaded', this.stop)
|
|
165
|
+
if (this.running.get(this.view) === this.stop) this.running.delete(this.view)
|
|
166
|
+
if (this.timer !== undefined) clearTimeout(this.timer)
|
|
167
|
+
if (this.observer)
|
|
168
|
+
Application.ios.removeNotificationObserver(
|
|
169
|
+
this.observer,
|
|
170
|
+
UIAccessibilityReduceMotionStatusDidChangeNotification,
|
|
171
|
+
)
|
|
172
|
+
this.layer?.removeAllAnimations()
|
|
173
|
+
this.layer?.removeFromSuperlayer()
|
|
174
|
+
this.layer = this.native = this.window = undefined
|
|
175
|
+
this.timer = this.observer = undefined
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private schedule = () => {
|
|
179
|
+
if (this.stopped || this.queued) return
|
|
180
|
+
this.queued = true
|
|
181
|
+
void Promise.resolve().then(() => {
|
|
182
|
+
this.queued = false
|
|
183
|
+
if (this.stopped) return
|
|
184
|
+
try {
|
|
185
|
+
this.update()
|
|
186
|
+
} catch (error) {
|
|
187
|
+
this.stop()
|
|
188
|
+
throw error
|
|
189
|
+
}
|
|
190
|
+
})
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private motionChanged = () => {
|
|
194
|
+
if (UIAccessibilityIsReduceMotionEnabled()) this.stop()
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private update() {
|
|
198
|
+
if (!this.view.isLoaded) return
|
|
199
|
+
if (UIAccessibilityIsReduceMotionEnabled()) return this.stop()
|
|
200
|
+
const native = this.view.nativeViewProtected as UIView | undefined
|
|
201
|
+
const window = native?.window
|
|
202
|
+
if (this.layer && (!native || !window)) return this.stop()
|
|
203
|
+
if (!native || !window) return
|
|
204
|
+
if (this.native && (this.native !== native || this.window !== window))
|
|
205
|
+
return this.stop()
|
|
206
|
+
const bounds = native.layer.bounds
|
|
207
|
+
if (bounds.size.width <= 0 || bounds.size.height <= 0) {
|
|
208
|
+
if (this.layer) this.stop()
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
CATransaction.begin()
|
|
212
|
+
CATransaction.setDisableActions(true)
|
|
213
|
+
try {
|
|
214
|
+
if (!this.layer) {
|
|
215
|
+
this.native = native
|
|
216
|
+
this.window = window
|
|
217
|
+
const host = this.overlay ? window.layer : native.layer
|
|
218
|
+
const layer = (this.layer = CAEmitterLayer.new())
|
|
219
|
+
layer.name = this.overlay ? 'jolt:confetti' : 'jolt:particles'
|
|
220
|
+
layer.frame = host.bounds
|
|
221
|
+
layer.emitterShape =
|
|
222
|
+
this.settings.source === 'center' ? kCAEmitterLayerPoint : kCAEmitterLayerLine
|
|
223
|
+
layer.emitterCells = NSArray.arrayWithArray(this.cells())
|
|
224
|
+
host.addSublayer(layer)
|
|
225
|
+
// Start from an empty emitter, even if its parent has been alive for hours.
|
|
226
|
+
layer.beginTime = host.convertTimeFromLayer(CACurrentMediaTime(), null)
|
|
227
|
+
if (this.overlay)
|
|
228
|
+
layer.emitterPosition = native.convertPointToView(
|
|
229
|
+
CGPointMake(
|
|
230
|
+
bounds.origin.x + bounds.size.width / 2,
|
|
231
|
+
bounds.origin.y + bounds.size.height / 2,
|
|
232
|
+
),
|
|
233
|
+
window,
|
|
234
|
+
)
|
|
235
|
+
this.observer = Application.ios.addNotificationObserver(
|
|
236
|
+
UIAccessibilityReduceMotionStatusDidChangeNotification,
|
|
237
|
+
this.motionChanged,
|
|
238
|
+
)
|
|
239
|
+
if (this.settings.duration !== undefined) {
|
|
240
|
+
// The render server stops emission even if JS is blocked. The timer
|
|
241
|
+
// only removes the empty layer after the last particles have expired.
|
|
242
|
+
layer.birthRate = 0
|
|
243
|
+
const emission = CABasicAnimation.animationWithKeyPath('birthRate')
|
|
244
|
+
emission.fromValue = 1
|
|
245
|
+
emission.toValue = 1
|
|
246
|
+
emission.duration = this.settings.duration / 1000
|
|
247
|
+
layer.addAnimationForKey(emission, 'jolt:emission')
|
|
248
|
+
this.timer = setTimeout(
|
|
249
|
+
this.stop,
|
|
250
|
+
this.settings.duration + this.settings.lifetime + 50,
|
|
251
|
+
)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (!this.overlay) {
|
|
255
|
+
const layer = this.layer
|
|
256
|
+
layer.frame = bounds
|
|
257
|
+
layer.emitterSize = CGSizeMake(bounds.size.width, 0)
|
|
258
|
+
layer.emitterPosition = CGPointMake(
|
|
259
|
+
bounds.size.width / 2,
|
|
260
|
+
this.settings.source === 'top'
|
|
261
|
+
? 0
|
|
262
|
+
: this.settings.source === 'bottom'
|
|
263
|
+
? bounds.size.height
|
|
264
|
+
: bounds.size.height / 2,
|
|
265
|
+
)
|
|
266
|
+
}
|
|
267
|
+
} finally {
|
|
268
|
+
CATransaction.commit()
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private cells(): CAEmitterCell[] {
|
|
273
|
+
const options = this.settings
|
|
274
|
+
const image = texture(options.shape)
|
|
275
|
+
return options.colors.map((color) => {
|
|
276
|
+
const cell = CAEmitterCell.emitterCell()
|
|
277
|
+
cell.contents = image.CGImage
|
|
278
|
+
cell.color = new Color(color).ios.CGColor
|
|
279
|
+
cell.birthRate = options.rate / options.colors.length
|
|
280
|
+
cell.lifetime = options.lifetime / 1000
|
|
281
|
+
cell.scale = options.size / 32
|
|
282
|
+
cell.velocity = options.velocity
|
|
283
|
+
cell.velocityRange = options.velocityRange
|
|
284
|
+
// Horizontal line emitters point upward at longitude zero; point
|
|
285
|
+
// emitters point right. Keep the public angle in view coordinates.
|
|
286
|
+
cell.emissionLongitude = radians(
|
|
287
|
+
options.angle + (options.source === 'center' ? 0 : 90),
|
|
288
|
+
)
|
|
289
|
+
cell.emissionRange = radians(options.spread)
|
|
290
|
+
cell.yAcceleration = options.gravity
|
|
291
|
+
cell.spin = radians(options.spin)
|
|
292
|
+
cell.spinRange = radians(options.spinRange)
|
|
293
|
+
cell.alphaSpeed = options.fadeOut ? -1 / cell.lifetime : 0
|
|
294
|
+
return cell
|
|
295
|
+
})
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function texture(shape: Settings['shape']): UIImage {
|
|
300
|
+
const cached = textures.get(shape)
|
|
301
|
+
if (cached) return cached
|
|
302
|
+
const format = UIGraphicsImageRendererFormat.defaultFormat()
|
|
303
|
+
format.scale = 1
|
|
304
|
+
format.opaque = false
|
|
305
|
+
const height = shape === 'rectangle' ? 18 : 32
|
|
306
|
+
const renderer = UIGraphicsImageRenderer.alloc().initWithSizeFormat(
|
|
307
|
+
CGSizeMake(32, height),
|
|
308
|
+
format,
|
|
309
|
+
)
|
|
310
|
+
const image = renderer.imageWithActions((context) => {
|
|
311
|
+
const cg = context.CGContext
|
|
312
|
+
CGContextSetFillColorWithColor(cg, UIColor.whiteColor.CGColor)
|
|
313
|
+
if (shape === 'rectangle') CGContextFillRect(cg, CGRectMake(0, 0, 32, height))
|
|
314
|
+
else if (shape === 'circle') CGContextFillEllipseInRect(cg, CGRectMake(0, 0, 32, 32))
|
|
315
|
+
else {
|
|
316
|
+
CGContextSetStrokeColorWithColor(cg, UIColor.whiteColor.CGColor)
|
|
317
|
+
CGContextSetLineWidth(cg, 3)
|
|
318
|
+
CGContextStrokeEllipseInRect(cg, CGRectMake(2, 2, 28, 28))
|
|
319
|
+
}
|
|
320
|
+
})
|
|
321
|
+
textures.set(shape, image)
|
|
322
|
+
return image
|
|
323
|
+
}
|
package/src/shimmer.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { Application, isIOS, type View } from '@nativescript/core'
|
|
2
|
+
import { toView } from './animate.js'
|
|
3
|
+
import type { Target } from './types.js'
|
|
4
|
+
|
|
5
|
+
export interface ShimmerOptions {
|
|
6
|
+
/** Milliseconds per sweep. Defaults to 1500. */
|
|
7
|
+
duration?: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const running = new WeakMap<View, () => void>()
|
|
11
|
+
const key = 'jolt:shimmer'
|
|
12
|
+
|
|
13
|
+
// NativeScript saves a mask here while applying clip-path or non-uniform borders.
|
|
14
|
+
type MaskedView = UIView & { originalMask?: CALayer | null }
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Sweeps a native gradient mask across an iOS placeholder until stopped.
|
|
18
|
+
* Waits for layout, respects Reduce Motion and pauses while the view is unloaded.
|
|
19
|
+
* Android is a no-op. Returns an idempotent cleanup function.
|
|
20
|
+
*/
|
|
21
|
+
export function shimmer(target: Target, options: ShimmerOptions = {}): () => void {
|
|
22
|
+
const view = toView(target)
|
|
23
|
+
const duration = options.duration ?? 1500
|
|
24
|
+
if (!Number.isFinite(duration) || duration <= 0)
|
|
25
|
+
throw new RangeError('Shimmer duration must be a positive finite number.')
|
|
26
|
+
running.get(view)?.()
|
|
27
|
+
if (!isIOS) return () => {}
|
|
28
|
+
return new Shimmer(view, duration).stop
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
class Shimmer {
|
|
32
|
+
private stopped = false
|
|
33
|
+
private queued = false
|
|
34
|
+
private waitingForLoad: boolean
|
|
35
|
+
private native: MaskedView | undefined
|
|
36
|
+
private owner: CALayer | undefined
|
|
37
|
+
private gradient: CAGradientLayer | undefined
|
|
38
|
+
private observer: ReturnType<typeof Application.ios.addNotificationObserver> | undefined
|
|
39
|
+
|
|
40
|
+
constructor(
|
|
41
|
+
private readonly view: View,
|
|
42
|
+
private readonly duration: number,
|
|
43
|
+
) {
|
|
44
|
+
this.waitingForLoad = !view.isLoaded
|
|
45
|
+
running.set(view, this.stop)
|
|
46
|
+
view.on('loaded', this.loaded)
|
|
47
|
+
view.on('layoutChanged', this.schedule)
|
|
48
|
+
view.on('unloaded', this.unloaded)
|
|
49
|
+
view.style.on('backgroundInternalChange', this.schedule)
|
|
50
|
+
this.update()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
stop = () => {
|
|
54
|
+
if (this.stopped) return
|
|
55
|
+
this.stopped = true
|
|
56
|
+
const { view } = this
|
|
57
|
+
view.off('loaded', this.loaded)
|
|
58
|
+
view.off('layoutChanged', this.schedule)
|
|
59
|
+
view.off('unloaded', this.unloaded)
|
|
60
|
+
view.style.off('backgroundInternalChange', this.schedule)
|
|
61
|
+
if (running.get(view) === this.stop) running.delete(view)
|
|
62
|
+
this.unloaded()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private loaded = () => {
|
|
66
|
+
this.waitingForLoad = false
|
|
67
|
+
this.schedule()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private unloaded = () => {
|
|
71
|
+
this.waitingForLoad = true
|
|
72
|
+
if (this.observer) {
|
|
73
|
+
Application.ios.removeNotificationObserver(
|
|
74
|
+
this.observer,
|
|
75
|
+
UIAccessibilityReduceMotionStatusDidChangeNotification,
|
|
76
|
+
)
|
|
77
|
+
this.observer = undefined
|
|
78
|
+
}
|
|
79
|
+
this.remove()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private schedule = () => {
|
|
83
|
+
if (this.stopped || this.queued) return
|
|
84
|
+
this.queued = true
|
|
85
|
+
// Core raises layoutChanged before redrawing the background and its mask.
|
|
86
|
+
// Attach after that pass, coalescing events from the same layout.
|
|
87
|
+
void Promise.resolve().then(() => {
|
|
88
|
+
this.queued = false
|
|
89
|
+
this.update()
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private update = () => {
|
|
94
|
+
if (this.stopped || this.waitingForLoad || !this.view.isLoaded) return
|
|
95
|
+
const native = this.view.nativeViewProtected as MaskedView | undefined
|
|
96
|
+
if (!native) return
|
|
97
|
+
this.observer ??= Application.ios.addNotificationObserver(
|
|
98
|
+
UIAccessibilityReduceMotionStatusDidChangeNotification,
|
|
99
|
+
this.update,
|
|
100
|
+
)
|
|
101
|
+
if (UIAccessibilityIsReduceMotionEnabled()) return this.remove()
|
|
102
|
+
const bounds = native.layer.bounds
|
|
103
|
+
if (bounds.size.width <= 0 || bounds.size.height <= 0) return this.remove()
|
|
104
|
+
if (this.native && this.native !== native) this.remove()
|
|
105
|
+
|
|
106
|
+
CATransaction.begin()
|
|
107
|
+
CATransaction.setDisableActions(true)
|
|
108
|
+
try {
|
|
109
|
+
this.native = native
|
|
110
|
+
const gradient = (this.gradient ??= this.createGradient())
|
|
111
|
+
// Leave Core's mask at the root so it can keep updating its path on resize.
|
|
112
|
+
let owner = native.layer
|
|
113
|
+
while (owner.mask && owner.mask !== gradient) owner = owner.mask
|
|
114
|
+
if (this.owner && this.owner !== owner && this.owner.mask === gradient)
|
|
115
|
+
this.owner.mask = null
|
|
116
|
+
this.owner = owner
|
|
117
|
+
owner.mask = gradient
|
|
118
|
+
// Don't let a later background redraw resurrect our mask after cleanup.
|
|
119
|
+
if (native.originalMask === gradient) native.originalMask = null
|
|
120
|
+
|
|
121
|
+
// Core's CAShapeLayer masks draw a path with zero bounds. In that case the
|
|
122
|
+
// path uses the view's coordinates; other masks have their own bounds.
|
|
123
|
+
const area =
|
|
124
|
+
owner.bounds.size.width > 0 && owner.bounds.size.height > 0
|
|
125
|
+
? owner.bounds
|
|
126
|
+
: bounds
|
|
127
|
+
gradient.frame = CGRectMake(
|
|
128
|
+
area.origin.x - area.size.width,
|
|
129
|
+
area.origin.y,
|
|
130
|
+
area.size.width * 3,
|
|
131
|
+
area.size.height,
|
|
132
|
+
)
|
|
133
|
+
} finally {
|
|
134
|
+
CATransaction.commit()
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private createGradient(): CAGradientLayer {
|
|
139
|
+
const gradient = CAGradientLayer.new()
|
|
140
|
+
const dim = UIColor.colorWithWhiteAlpha(1, 0.35).CGColor
|
|
141
|
+
gradient.colors = NSArray.arrayWithArray([dim, UIColor.whiteColor.CGColor, dim])
|
|
142
|
+
gradient.startPoint = CGPointMake(0, 0.5)
|
|
143
|
+
gradient.endPoint = CGPointMake(1, 0.5)
|
|
144
|
+
|
|
145
|
+
// A three-width mask lets the band enter and leave while every stop stays
|
|
146
|
+
// within the documented 0...1 range for CAGradientLayer.locations.
|
|
147
|
+
const from = NSArray.arrayWithArray([0, 0.125, 0.25])
|
|
148
|
+
const to = NSArray.arrayWithArray([0.75, 0.875, 1])
|
|
149
|
+
gradient.locations = from
|
|
150
|
+
const animation = CABasicAnimation.animationWithKeyPath('locations')
|
|
151
|
+
animation.fromValue = from
|
|
152
|
+
animation.toValue = to
|
|
153
|
+
animation.duration = this.duration / 1000
|
|
154
|
+
animation.repeatCount = Infinity
|
|
155
|
+
animation.removedOnCompletion = false
|
|
156
|
+
animation.timingFunction = CAMediaTimingFunction.functionWithName(
|
|
157
|
+
kCAMediaTimingFunctionLinear,
|
|
158
|
+
)
|
|
159
|
+
gradient.addAnimationForKey(animation, key)
|
|
160
|
+
return gradient
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private remove() {
|
|
164
|
+
const { gradient, owner, native } = this
|
|
165
|
+
if (!gradient) return
|
|
166
|
+
CATransaction.begin()
|
|
167
|
+
CATransaction.setDisableActions(true)
|
|
168
|
+
try {
|
|
169
|
+
gradient.removeAnimationForKey(key)
|
|
170
|
+
if (owner?.mask === gradient) owner.mask = null
|
|
171
|
+
if (native?.originalMask === gradient) native.originalMask = null
|
|
172
|
+
} finally {
|
|
173
|
+
CATransaction.commit()
|
|
174
|
+
}
|
|
175
|
+
this.owner = undefined
|
|
176
|
+
this.gradient = undefined
|
|
177
|
+
this.native = undefined
|
|
178
|
+
}
|
|
179
|
+
}
|