@rafaelaugusto/jolt 0.1.0-beta.2 → 0.1.0-beta.3
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 +60 -6
- package/dist/animate.d.ts +3 -1
- package/dist/animate.d.ts.map +1 -1
- package/dist/animate.js +65 -1
- package/dist/animate.js.map +1 -1
- package/dist/decay.d.ts +11 -0
- package/dist/decay.d.ts.map +1 -0
- package/dist/decay.js +22 -0
- package/dist/decay.js.map +1 -0
- package/dist/driver/android.d.ts +1 -1
- package/dist/driver/android.d.ts.map +1 -1
- package/dist/driver/android.js +95 -5
- package/dist/driver/android.js.map +1 -1
- package/dist/driver/index.d.ts +3 -1
- package/dist/driver/index.d.ts.map +1 -1
- package/dist/driver/index.js.map +1 -1
- package/dist/engine.d.ts +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +6 -3
- package/dist/engine.js.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/loader.d.ts +19 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +172 -0
- package/dist/loader.js.map +1 -0
- package/dist/properties.d.ts +1 -1
- package/dist/properties.d.ts.map +1 -1
- package/dist/properties.js +1 -1
- package/dist/properties.js.map +1 -1
- package/dist/reveal.d.ts +25 -0
- package/dist/reveal.d.ts.map +1 -0
- package/dist/reveal.js +293 -0
- package/dist/reveal.js.map +1 -0
- package/dist/text-layout.d.ts +22 -0
- package/dist/text-layout.d.ts.map +1 -0
- package/dist/text-layout.js +179 -0
- package/dist/text-layout.js.map +1 -0
- package/dist/text.d.ts +17 -0
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +264 -0
- package/dist/text.js.map +1 -0
- package/dist/track.d.ts.map +1 -1
- package/dist/track.js +25 -3
- package/dist/track.js.map +1 -1
- package/dist/transition.d.ts +5 -0
- package/dist/transition.d.ts.map +1 -1
- package/dist/transition.js.map +1 -1
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/animate.ts +75 -1
- package/src/decay.ts +45 -0
- package/src/driver/android.ts +112 -6
- package/src/driver/index.ts +3 -1
- package/src/engine.ts +8 -4
- package/src/index.ts +7 -1
- package/src/loader.ts +201 -0
- package/src/properties.ts +1 -1
- package/src/reveal.ts +374 -0
- package/src/text-layout.ts +216 -0
- package/src/text.ts +296 -0
- package/src/track.ts +42 -3
- package/src/transition.ts +6 -0
- package/src/types.ts +13 -0
package/src/loader.ts
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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 type LoaderStyle = 'dots' | 'bars' | 'ring'
|
|
6
|
+
|
|
7
|
+
export interface LoaderOptions {
|
|
8
|
+
/** Defaults to 'dots'. */
|
|
9
|
+
style?: LoaderStyle
|
|
10
|
+
/** A NativeScript color string. Defaults to '#71717a'. */
|
|
11
|
+
color?: string
|
|
12
|
+
/** Maximum width and height in DIPs, fitted inside the target. Defaults to 48. */
|
|
13
|
+
size?: number
|
|
14
|
+
/** Milliseconds per cycle. Defaults to 1200. */
|
|
15
|
+
duration?: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const running = new WeakMap<View, () => void>()
|
|
19
|
+
const key = 'jolt:loader'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Adds a centered native iOS loading indicator. Returns an idempotent cleanup.
|
|
23
|
+
* Unloading releases native resources; loading resumes until explicitly stopped.
|
|
24
|
+
* Reduce Motion keeps the indicator static. Android is a no-op.
|
|
25
|
+
*/
|
|
26
|
+
export function loader(target: Target, options: LoaderOptions = {}): () => void {
|
|
27
|
+
const settings: Required<LoaderOptions> = {
|
|
28
|
+
style: options.style ?? 'dots',
|
|
29
|
+
color: options.color ?? '#71717a',
|
|
30
|
+
size: options.size ?? 48,
|
|
31
|
+
duration: options.duration ?? 1200,
|
|
32
|
+
}
|
|
33
|
+
if (!['dots', 'bars', 'ring'].includes(settings.style))
|
|
34
|
+
throw new TypeError('Unknown loader style.')
|
|
35
|
+
if (typeof settings.color !== 'string' || !Color.isValid(settings.color))
|
|
36
|
+
throw new TypeError('Loader color must be a valid color string.')
|
|
37
|
+
for (const name of ['size', 'duration'] as const)
|
|
38
|
+
if (!Number.isFinite(settings[name]) || settings[name] <= 0)
|
|
39
|
+
throw new RangeError(`Loader ${name} must be a positive finite number.`)
|
|
40
|
+
const view = toView(target)
|
|
41
|
+
running.get(view)?.()
|
|
42
|
+
if (!isIOS) return () => {}
|
|
43
|
+
return new Loader(view, settings).stop
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
class Loader {
|
|
47
|
+
private stopped = false
|
|
48
|
+
private queued = false
|
|
49
|
+
private waitingForLoad: boolean
|
|
50
|
+
private native: UIView | undefined
|
|
51
|
+
private layer: CAReplicatorLayer | undefined
|
|
52
|
+
private item: CALayer | undefined
|
|
53
|
+
private animating = false
|
|
54
|
+
private observer: ReturnType<typeof Application.ios.addNotificationObserver> | undefined
|
|
55
|
+
|
|
56
|
+
constructor(
|
|
57
|
+
private readonly view: View,
|
|
58
|
+
private readonly settings: Required<LoaderOptions>,
|
|
59
|
+
) {
|
|
60
|
+
this.waitingForLoad = !view.isLoaded
|
|
61
|
+
running.set(view, this.stop)
|
|
62
|
+
view.on('loaded', this.loaded)
|
|
63
|
+
view.on('layoutChanged', this.schedule)
|
|
64
|
+
view.on('unloaded', this.unloaded)
|
|
65
|
+
this.schedule()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
stop = () => {
|
|
69
|
+
if (this.stopped) return
|
|
70
|
+
this.stopped = true
|
|
71
|
+
this.view.off('loaded', this.loaded)
|
|
72
|
+
this.view.off('layoutChanged', this.schedule)
|
|
73
|
+
this.view.off('unloaded', this.unloaded)
|
|
74
|
+
if (running.get(this.view) === this.stop) running.delete(this.view)
|
|
75
|
+
this.unloaded()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private loaded = () => {
|
|
79
|
+
this.waitingForLoad = false
|
|
80
|
+
this.schedule()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private unloaded = () => {
|
|
84
|
+
this.waitingForLoad = true
|
|
85
|
+
if (this.observer) {
|
|
86
|
+
Application.ios.removeNotificationObserver(
|
|
87
|
+
this.observer,
|
|
88
|
+
UIAccessibilityReduceMotionStatusDidChangeNotification,
|
|
89
|
+
)
|
|
90
|
+
this.observer = undefined
|
|
91
|
+
}
|
|
92
|
+
this.remove()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private schedule = () => {
|
|
96
|
+
if (this.stopped || this.queued) return
|
|
97
|
+
this.queued = true
|
|
98
|
+
void Promise.resolve().then(() => {
|
|
99
|
+
this.queued = false
|
|
100
|
+
if (this.stopped) return
|
|
101
|
+
try {
|
|
102
|
+
this.update()
|
|
103
|
+
} catch (error) {
|
|
104
|
+
this.stop()
|
|
105
|
+
throw error
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private update() {
|
|
111
|
+
if (this.waitingForLoad || !this.view.isLoaded) return
|
|
112
|
+
const native = this.view.nativeViewProtected as UIView | undefined
|
|
113
|
+
if (!native?.window) return this.remove()
|
|
114
|
+
const bounds = native.layer.bounds
|
|
115
|
+
const size = Math.min(this.settings.size, bounds.size.width, bounds.size.height)
|
|
116
|
+
if (size <= 0) return this.remove()
|
|
117
|
+
if (this.native && this.native !== native) this.remove()
|
|
118
|
+
this.observer ??= Application.ios.addNotificationObserver(
|
|
119
|
+
UIAccessibilityReduceMotionStatusDidChangeNotification,
|
|
120
|
+
this.schedule,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
CATransaction.begin()
|
|
124
|
+
CATransaction.setDisableActions(true)
|
|
125
|
+
try {
|
|
126
|
+
if (!this.layer) {
|
|
127
|
+
this.native = native
|
|
128
|
+
this.layer = CAReplicatorLayer.new()
|
|
129
|
+
this.layer.name = key
|
|
130
|
+
this.item = CALayer.new()
|
|
131
|
+
this.item.backgroundColor = new Color(this.settings.color).ios.CGColor
|
|
132
|
+
this.layer.addSublayer(this.item)
|
|
133
|
+
native.layer.addSublayer(this.layer)
|
|
134
|
+
}
|
|
135
|
+
const layer = this.layer
|
|
136
|
+
const item = this.item!
|
|
137
|
+
const { style, duration } = this.settings
|
|
138
|
+
layer.bounds = CGRectMake(0, 0, size, size)
|
|
139
|
+
layer.position = CGPointMake(
|
|
140
|
+
bounds.origin.x + bounds.size.width / 2,
|
|
141
|
+
bounds.origin.y + bounds.size.height / 2,
|
|
142
|
+
)
|
|
143
|
+
layer.instanceCount = style === 'dots' ? 3 : style === 'bars' ? 5 : 12
|
|
144
|
+
layer.instanceDelay = duration / 1000 / (style === 'ring' ? 12 : 5)
|
|
145
|
+
if (style === 'ring') {
|
|
146
|
+
const diameter = size * 0.16
|
|
147
|
+
item.bounds = CGRectMake(0, 0, diameter, diameter)
|
|
148
|
+
item.position = CGPointMake(size / 2, diameter / 2)
|
|
149
|
+
item.cornerRadius = diameter / 2
|
|
150
|
+
layer.instanceTransform = CATransform3DMakeRotation((2 * Math.PI) / 12, 0, 0, 1)
|
|
151
|
+
} else {
|
|
152
|
+
const width = size * (style === 'dots' ? 0.2 : 0.12)
|
|
153
|
+
const height = style === 'dots' ? width : size * 0.75
|
|
154
|
+
item.bounds = CGRectMake(0, 0, width, height)
|
|
155
|
+
item.position = CGPointMake(width / 2, size / 2)
|
|
156
|
+
item.cornerRadius = width / 2
|
|
157
|
+
layer.instanceTransform = CATransform3DMakeTranslation(
|
|
158
|
+
(size - width) / (layer.instanceCount - 1),
|
|
159
|
+
0,
|
|
160
|
+
0,
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
const reduced = UIAccessibilityIsReduceMotionEnabled()
|
|
164
|
+
item.opacity = style === 'ring' ? 0.35 : 1
|
|
165
|
+
if (reduced && this.animating) {
|
|
166
|
+
item.removeAnimationForKey(key)
|
|
167
|
+
this.animating = false
|
|
168
|
+
} else if (!reduced && !this.animating) {
|
|
169
|
+
const animation = CAKeyframeAnimation.animationWithKeyPath(
|
|
170
|
+
style === 'ring'
|
|
171
|
+
? 'opacity'
|
|
172
|
+
: style === 'bars'
|
|
173
|
+
? 'transform.scale.y'
|
|
174
|
+
: 'transform.scale',
|
|
175
|
+
)
|
|
176
|
+
animation.values = NSArray.arrayWithArray(
|
|
177
|
+
style === 'ring' ? [1, 0.2] : [0.35, 1, 0.35, 0.35],
|
|
178
|
+
)
|
|
179
|
+
animation.keyTimes = NSArray.arrayWithArray(
|
|
180
|
+
style === 'ring' ? [0, 1] : [0, 0.3, 0.6, 1],
|
|
181
|
+
)
|
|
182
|
+
animation.duration = duration / 1000
|
|
183
|
+
animation.repeatCount = Infinity
|
|
184
|
+
// Start one cycle in so every delayed copy is already participating.
|
|
185
|
+
animation.beginTime =
|
|
186
|
+
item.convertTimeFromLayer(CACurrentMediaTime(), null) - animation.duration
|
|
187
|
+
item.addAnimationForKey(animation, key)
|
|
188
|
+
this.animating = true
|
|
189
|
+
}
|
|
190
|
+
} finally {
|
|
191
|
+
CATransaction.commit()
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private remove() {
|
|
196
|
+
this.item?.removeAnimationForKey(key)
|
|
197
|
+
this.layer?.removeFromSuperlayer()
|
|
198
|
+
this.item = this.layer = this.native = undefined
|
|
199
|
+
this.animating = false
|
|
200
|
+
}
|
|
201
|
+
}
|
package/src/properties.ts
CHANGED
|
@@ -25,7 +25,7 @@ const defaults: Record<Property, number> = {
|
|
|
25
25
|
rotateY: 0,
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
/** How close to its target a spring has to be to count as settled. */
|
|
28
|
+
/** How close to its target a spring or decay has to be to count as settled. */
|
|
29
29
|
export const precision: Record<Property, number> = {
|
|
30
30
|
opacity: 0.002,
|
|
31
31
|
x: 0.25,
|
package/src/reveal.ts
ADDED
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { Application, isIOS, type View } from '@nativescript/core'
|
|
2
|
+
import { toView } from './animate.js'
|
|
3
|
+
import { resolveEasing } from './easing.js'
|
|
4
|
+
import type { Animation } from './engine.js'
|
|
5
|
+
import type { Target } from './types.js'
|
|
6
|
+
|
|
7
|
+
export interface RevealOptions {
|
|
8
|
+
/** Circle origin in DIPs from the target's top-left corner. Defaults to its center. */
|
|
9
|
+
from?: { x: number; y: number }
|
|
10
|
+
/** Milliseconds. Defaults to 400; zero changes visibility immediately. */
|
|
11
|
+
duration?: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ConcealOptions {
|
|
15
|
+
/** Circle destination in DIPs from the target's top-left corner. Defaults to its center. */
|
|
16
|
+
to?: { x: number; y: number }
|
|
17
|
+
/** Milliseconds. Defaults to 400; zero changes visibility immediately. */
|
|
18
|
+
duration?: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type Circle = { x: number; y: number; radius: number }
|
|
22
|
+
type MaskedView = UIView & { originalMask?: CALayer | null; maskType?: unknown }
|
|
23
|
+
type Snapshot = { circle: Circle; native: UIView; bounds: CGRect }
|
|
24
|
+
const running = new WeakMap<View, CircularAnimation>()
|
|
25
|
+
const callbacks = new WeakMap<NSObject, (finished: boolean) => void>()
|
|
26
|
+
const ease = resolveEasing('easeInOut')
|
|
27
|
+
const key = 'jolt:reveal'
|
|
28
|
+
let Delegate: typeof NSObject | undefined
|
|
29
|
+
|
|
30
|
+
/** Reveals an iOS view through an expanding circle, then removes the temporary mask. */
|
|
31
|
+
export function reveal(target: Target, options: RevealOptions = {}): Animation {
|
|
32
|
+
return start(target, true, options.from, options.duration ?? 400)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Contracts an iOS view to a point, then sets visibility to 'hidden' without collapsing layout. */
|
|
36
|
+
export function conceal(target: Target, options: ConcealOptions = {}): Animation {
|
|
37
|
+
return start(target, false, options.to, options.duration ?? 400)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function start(
|
|
41
|
+
target: Target,
|
|
42
|
+
opening: boolean,
|
|
43
|
+
point: { x: number; y: number } | undefined,
|
|
44
|
+
duration: number,
|
|
45
|
+
): Animation {
|
|
46
|
+
if (!Number.isFinite(duration) || duration < 0)
|
|
47
|
+
throw new RangeError('Reveal duration must be a nonnegative finite number.')
|
|
48
|
+
if (
|
|
49
|
+
point !== undefined &&
|
|
50
|
+
(!point || !Number.isFinite(point.x) || !Number.isFinite(point.y))
|
|
51
|
+
)
|
|
52
|
+
throw new TypeError('Reveal origin must have finite x and y coordinates.')
|
|
53
|
+
const view = toView(target)
|
|
54
|
+
const previous = running.get(view)
|
|
55
|
+
const snapshot = previous?.snapshot()
|
|
56
|
+
const original = previous?.originalVisibility ?? view.visibility
|
|
57
|
+
previous?.stop()
|
|
58
|
+
return new CircularAnimation(
|
|
59
|
+
view,
|
|
60
|
+
opening,
|
|
61
|
+
point && { ...point },
|
|
62
|
+
duration,
|
|
63
|
+
original,
|
|
64
|
+
snapshot,
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function sameBounds(a: CGRect, b: CGRect) {
|
|
69
|
+
return (
|
|
70
|
+
a.origin.x === b.origin.x &&
|
|
71
|
+
a.origin.y === b.origin.y &&
|
|
72
|
+
a.size.width === b.size.width &&
|
|
73
|
+
a.size.height === b.size.height
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function path(circle: Circle): CAShapeLayer['path'] {
|
|
78
|
+
// Keep the same oval topology even at the closed endpoint.
|
|
79
|
+
const radius = Math.max(0.01, circle.radius)
|
|
80
|
+
return UIBezierPath.bezierPathWithOvalInRect(
|
|
81
|
+
CGRectMake(circle.x - radius, circle.y - radius, radius * 2, radius * 2),
|
|
82
|
+
).CGPath
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function completion(
|
|
86
|
+
callback: (finished: boolean) => void,
|
|
87
|
+
): NSObject & CAAnimationDelegate {
|
|
88
|
+
if (!Delegate) {
|
|
89
|
+
// Use the runtime subclass API so published JS needs no NativeClass transform.
|
|
90
|
+
const base = NSObject as typeof NSObject & {
|
|
91
|
+
extend(
|
|
92
|
+
methods: object,
|
|
93
|
+
options: { protocols: unknown[]; name: string },
|
|
94
|
+
): typeof NSObject
|
|
95
|
+
}
|
|
96
|
+
Delegate = base.extend(
|
|
97
|
+
{
|
|
98
|
+
animationDidStopFinished(
|
|
99
|
+
this: NSObject,
|
|
100
|
+
_animation: CAAnimation,
|
|
101
|
+
finished: boolean,
|
|
102
|
+
) {
|
|
103
|
+
callbacks.get(this)?.(finished)
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
{ protocols: [CAAnimationDelegate], name: 'JoltCircularRevealDelegate' },
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
const delegate = Delegate.new()
|
|
110
|
+
callbacks.set(delegate, callback)
|
|
111
|
+
return delegate
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
class CircularAnimation implements Animation {
|
|
115
|
+
readonly finished: Promise<boolean>
|
|
116
|
+
private resolve!: (finished: boolean) => void
|
|
117
|
+
private settled = false
|
|
118
|
+
private queued = false
|
|
119
|
+
private changingVisibility = false
|
|
120
|
+
private waitingForLoad: boolean
|
|
121
|
+
private native: MaskedView | undefined
|
|
122
|
+
private bounds: CGRect | undefined
|
|
123
|
+
private wrapper: CALayer | undefined
|
|
124
|
+
private shape: CAShapeLayer | undefined
|
|
125
|
+
private originalMask: CALayer | null = null
|
|
126
|
+
private savedMask: CALayer | null = null
|
|
127
|
+
private delegate: NSObject | undefined
|
|
128
|
+
private observer: ReturnType<typeof Application.ios.addNotificationObserver> | undefined
|
|
129
|
+
private from: Circle | undefined
|
|
130
|
+
private to: Circle | undefined
|
|
131
|
+
private startedAt = 0
|
|
132
|
+
|
|
133
|
+
constructor(
|
|
134
|
+
private readonly view: View,
|
|
135
|
+
private readonly opening: boolean,
|
|
136
|
+
private readonly point: { x: number; y: number } | undefined,
|
|
137
|
+
private readonly duration: number,
|
|
138
|
+
readonly originalVisibility: View['visibility'],
|
|
139
|
+
private readonly inherited?: Snapshot,
|
|
140
|
+
) {
|
|
141
|
+
this.finished = new Promise((resolve) => {
|
|
142
|
+
this.resolve = resolve
|
|
143
|
+
})
|
|
144
|
+
this.waitingForLoad = !view.isLoaded
|
|
145
|
+
running.set(view, this)
|
|
146
|
+
if (!isIOS) {
|
|
147
|
+
this.end(false)
|
|
148
|
+
return
|
|
149
|
+
}
|
|
150
|
+
view.on('loaded', this.loaded)
|
|
151
|
+
view.on('layoutChanged', this.layoutChanged)
|
|
152
|
+
view.on('unloaded', this.stop)
|
|
153
|
+
view.style.on('backgroundInternalChange', this.stop)
|
|
154
|
+
view.style.on('visibilityChange', this.visibilityChanged)
|
|
155
|
+
if (opening && view.visibility === 'collapse') this.visibility('hidden')
|
|
156
|
+
this.schedule()
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
then<A = boolean, B = never>(
|
|
160
|
+
onfulfilled?: ((finished: boolean) => A | PromiseLike<A>) | null,
|
|
161
|
+
onrejected?: ((reason: unknown) => B | PromiseLike<B>) | null,
|
|
162
|
+
): Promise<A | B> {
|
|
163
|
+
return this.finished.then(onfulfilled, onrejected)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Cancels and restores visibility from before the interrupted reveal/conceal chain. */
|
|
167
|
+
stop = () => this.end(false)
|
|
168
|
+
|
|
169
|
+
snapshot(): Snapshot | undefined {
|
|
170
|
+
if (!this.shape || !this.native || !this.bounds || !this.from || !this.to) return
|
|
171
|
+
const presented = this.shape.presentationLayer() as CAShapeLayer | null
|
|
172
|
+
let circle: Circle
|
|
173
|
+
if (presented?.path) {
|
|
174
|
+
const box = CGPathGetBoundingBox(presented.path)
|
|
175
|
+
circle = {
|
|
176
|
+
x: box.origin.x + box.size.width / 2,
|
|
177
|
+
y: box.origin.y + box.size.height / 2,
|
|
178
|
+
radius: box.size.width / 2,
|
|
179
|
+
}
|
|
180
|
+
} else {
|
|
181
|
+
const progress = ease(
|
|
182
|
+
Math.max(
|
|
183
|
+
0,
|
|
184
|
+
Math.min(1, ((CACurrentMediaTime() - this.startedAt) * 1000) / this.duration),
|
|
185
|
+
),
|
|
186
|
+
)
|
|
187
|
+
const lerp = (a: number, b: number) => a + (b - a) * progress
|
|
188
|
+
circle = {
|
|
189
|
+
x: lerp(this.from.x, this.to.x),
|
|
190
|
+
y: lerp(this.from.y, this.to.y),
|
|
191
|
+
radius: lerp(this.from.radius, this.to.radius),
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return { circle, native: this.native, bounds: this.bounds }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private visibility(value: View['visibility']) {
|
|
198
|
+
this.changingVisibility = true
|
|
199
|
+
try {
|
|
200
|
+
this.view.visibility = value
|
|
201
|
+
} finally {
|
|
202
|
+
this.changingVisibility = false
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private visibilityChanged = () => {
|
|
207
|
+
if (!this.changingVisibility) this.end(false, true)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private loaded = () => {
|
|
211
|
+
this.waitingForLoad = false
|
|
212
|
+
this.schedule()
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private layoutChanged = () => {
|
|
216
|
+
if (this.shape) {
|
|
217
|
+
const native = this.view.nativeViewProtected as UIView | undefined
|
|
218
|
+
if (
|
|
219
|
+
native !== this.native ||
|
|
220
|
+
!native?.window ||
|
|
221
|
+
!sameBounds(native.layer.bounds, this.bounds!)
|
|
222
|
+
) {
|
|
223
|
+
this.stop()
|
|
224
|
+
return
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
this.schedule()
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private schedule = () => {
|
|
231
|
+
if (this.settled || this.queued) return
|
|
232
|
+
this.queued = true
|
|
233
|
+
void Promise.resolve().then(() => {
|
|
234
|
+
this.queued = false
|
|
235
|
+
if (this.settled) return
|
|
236
|
+
try {
|
|
237
|
+
this.update()
|
|
238
|
+
} catch (error) {
|
|
239
|
+
this.stop()
|
|
240
|
+
throw error
|
|
241
|
+
}
|
|
242
|
+
})
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private motionChanged = () => {
|
|
246
|
+
if (UIAccessibilityIsReduceMotionEnabled()) this.end(true)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private update() {
|
|
250
|
+
if (!this.opening && !this.inherited && this.view.visibility !== 'visible')
|
|
251
|
+
return this.end(true, true)
|
|
252
|
+
if (this.duration === 0 || UIAccessibilityIsReduceMotionEnabled())
|
|
253
|
+
return this.end(true)
|
|
254
|
+
if (this.waitingForLoad || !this.view.isLoaded) return
|
|
255
|
+
const native = this.view.nativeViewProtected as MaskedView | undefined
|
|
256
|
+
if (this.shape) {
|
|
257
|
+
if (native !== this.native || native?.layer.mask !== this.wrapper) this.stop()
|
|
258
|
+
return
|
|
259
|
+
}
|
|
260
|
+
if (!native?.window) return
|
|
261
|
+
const bounds = native.layer.bounds
|
|
262
|
+
if (bounds.size.width <= 0 || bounds.size.height <= 0) return
|
|
263
|
+
const inherited =
|
|
264
|
+
this.inherited?.native === native && sameBounds(this.inherited.bounds, bounds)
|
|
265
|
+
? this.inherited.circle
|
|
266
|
+
: undefined
|
|
267
|
+
const x = this.point
|
|
268
|
+
? bounds.origin.x + this.point.x
|
|
269
|
+
: (inherited?.x ?? bounds.origin.x + bounds.size.width / 2)
|
|
270
|
+
const y = this.point
|
|
271
|
+
? bounds.origin.y + this.point.y
|
|
272
|
+
: (inherited?.y ?? bounds.origin.y + bounds.size.height / 2)
|
|
273
|
+
const radius =
|
|
274
|
+
Math.hypot(
|
|
275
|
+
Math.max(
|
|
276
|
+
Math.abs(x - bounds.origin.x),
|
|
277
|
+
Math.abs(x - bounds.origin.x - bounds.size.width),
|
|
278
|
+
),
|
|
279
|
+
Math.max(
|
|
280
|
+
Math.abs(y - bounds.origin.y),
|
|
281
|
+
Math.abs(y - bounds.origin.y - bounds.size.height),
|
|
282
|
+
),
|
|
283
|
+
) + 1
|
|
284
|
+
this.from = inherited ?? { x, y, radius: this.opening ? 0 : radius }
|
|
285
|
+
this.to = { x, y, radius: this.opening ? radius : 0 }
|
|
286
|
+
this.native = native
|
|
287
|
+
this.bounds = bounds
|
|
288
|
+
this.originalMask = native.layer.mask
|
|
289
|
+
this.savedMask =
|
|
290
|
+
native.maskType != null ? (native.originalMask ?? null) : this.originalMask
|
|
291
|
+
|
|
292
|
+
CATransaction.begin()
|
|
293
|
+
CATransaction.setDisableActions(true)
|
|
294
|
+
try {
|
|
295
|
+
// A plain container prevents Core from mistaking our circle for its own clip path.
|
|
296
|
+
const wrapper = (this.wrapper = CALayer.new())
|
|
297
|
+
const shape = (this.shape = CAShapeLayer.new())
|
|
298
|
+
wrapper.name = key
|
|
299
|
+
for (const layer of [wrapper, shape]) {
|
|
300
|
+
layer.bounds = bounds
|
|
301
|
+
layer.position = CGPointMake(
|
|
302
|
+
bounds.origin.x + bounds.size.width / 2,
|
|
303
|
+
bounds.origin.y + bounds.size.height / 2,
|
|
304
|
+
)
|
|
305
|
+
}
|
|
306
|
+
shape.fillColor = UIColor.blackColor.CGColor
|
|
307
|
+
shape.path = path(this.to)
|
|
308
|
+
wrapper.addSublayer(shape)
|
|
309
|
+
// Same coordinate space as the original host, including bounds origins.
|
|
310
|
+
native.layer.mask = null
|
|
311
|
+
wrapper.mask = this.originalMask
|
|
312
|
+
native.layer.mask = wrapper
|
|
313
|
+
const animation = CABasicAnimation.animationWithKeyPath('path')
|
|
314
|
+
animation.fromValue = path(this.from)
|
|
315
|
+
animation.toValue = shape.path
|
|
316
|
+
animation.duration = this.duration / 1000
|
|
317
|
+
animation.timingFunction = CAMediaTimingFunction.functionWithName(
|
|
318
|
+
kCAMediaTimingFunctionEaseInEaseOut,
|
|
319
|
+
)
|
|
320
|
+
this.delegate = animation.delegate = completion((finished) => this.end(finished))
|
|
321
|
+
this.startedAt = CACurrentMediaTime()
|
|
322
|
+
shape.addAnimationForKey(animation, key)
|
|
323
|
+
this.observer = Application.ios.addNotificationObserver(
|
|
324
|
+
UIAccessibilityReduceMotionStatusDidChangeNotification,
|
|
325
|
+
this.motionChanged,
|
|
326
|
+
)
|
|
327
|
+
this.visibility('visible')
|
|
328
|
+
} finally {
|
|
329
|
+
CATransaction.commit()
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private end(finished: boolean, keepVisibility = false) {
|
|
334
|
+
if (this.settled) return
|
|
335
|
+
this.settled = true
|
|
336
|
+
const { view, native, wrapper, shape } = this
|
|
337
|
+
view.off('loaded', this.loaded)
|
|
338
|
+
view.off('layoutChanged', this.layoutChanged)
|
|
339
|
+
view.off('unloaded', this.stop)
|
|
340
|
+
view.style.off('backgroundInternalChange', this.stop)
|
|
341
|
+
view.style.off('visibilityChange', this.visibilityChanged)
|
|
342
|
+
if (running.get(view) === this) running.delete(view)
|
|
343
|
+
if (this.observer)
|
|
344
|
+
Application.ios.removeNotificationObserver(
|
|
345
|
+
this.observer,
|
|
346
|
+
UIAccessibilityReduceMotionStatusDidChangeNotification,
|
|
347
|
+
)
|
|
348
|
+
if (this.delegate) callbacks.delete(this.delegate)
|
|
349
|
+
if (native && wrapper) {
|
|
350
|
+
CATransaction.begin()
|
|
351
|
+
CATransaction.setDisableActions(true)
|
|
352
|
+
try {
|
|
353
|
+
// Hide before removing a closed mask so conceal never flashes its content.
|
|
354
|
+
if (!keepVisibility)
|
|
355
|
+
this.visibility(
|
|
356
|
+
finished ? (this.opening ? 'visible' : 'hidden') : this.originalVisibility,
|
|
357
|
+
)
|
|
358
|
+
shape?.removeAnimationForKey(key)
|
|
359
|
+
wrapper.mask = null
|
|
360
|
+
if (native.layer.mask === wrapper) native.layer.mask = this.originalMask
|
|
361
|
+
// Core can save the temporary mask while installing a new clip/border mask.
|
|
362
|
+
if (native.originalMask === wrapper) native.originalMask = this.savedMask
|
|
363
|
+
} finally {
|
|
364
|
+
CATransaction.commit()
|
|
365
|
+
}
|
|
366
|
+
} else if (isIOS && !keepVisibility) {
|
|
367
|
+
this.visibility(
|
|
368
|
+
finished ? (this.opening ? 'visible' : 'hidden') : this.originalVisibility,
|
|
369
|
+
)
|
|
370
|
+
}
|
|
371
|
+
this.native = this.wrapper = this.shape = this.delegate = this.observer = undefined
|
|
372
|
+
this.resolve(finished)
|
|
373
|
+
}
|
|
374
|
+
}
|