react-native-marquee-animation 0.1.0
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/LICENSE +21 -0
- package/README.md +174 -0
- package/RNMarquee.podspec +27 -0
- package/THIRD_PARTY_NOTICES.md +8 -0
- package/android/build.gradle +58 -0
- package/android/settings.gradle +34 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/com/marquee/MarqueeEvent.kt +44 -0
- package/android/src/main/java/com/marquee/MarqueeModels.kt +45 -0
- package/android/src/main/java/com/marquee/MarqueeMotion.kt +163 -0
- package/android/src/main/java/com/marquee/MarqueePackage.kt +16 -0
- package/android/src/main/java/com/marquee/MarqueeView.kt +376 -0
- package/android/src/main/java/com/marquee/MarqueeViewManager.kt +135 -0
- package/ios/MarqueeRenderer.swift +652 -0
- package/ios/RNMarqueeView.h +14 -0
- package/ios/RNMarqueeView.mm +118 -0
- package/lib/module/RNMarqueeViewNativeComponent.ts +32 -0
- package/lib/module/index.js +4 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/internal/events.js +10 -0
- package/lib/module/internal/events.js.map +1 -0
- package/lib/module/internal/geometry.js +10 -0
- package/lib/module/internal/geometry.js.map +1 -0
- package/lib/module/internal/options.js +29 -0
- package/lib/module/internal/options.js.map +1 -0
- package/lib/module/marquee.js +32 -0
- package/lib/module/marquee.js.map +1 -0
- package/lib/module/marquee.native.js +95 -0
- package/lib/module/marquee.native.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/types.js +4 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/use-reduced-motion.js +61 -0
- package/lib/module/use-reduced-motion.js.map +1 -0
- package/lib/module/view-props.js +5 -0
- package/lib/module/view-props.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/RNMarqueeViewNativeComponent.d.ts +28 -0
- package/lib/typescript/src/RNMarqueeViewNativeComponent.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +3 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/internal/events.d.ts +3 -0
- package/lib/typescript/src/internal/events.d.ts.map +1 -0
- package/lib/typescript/src/internal/geometry.d.ts +3 -0
- package/lib/typescript/src/internal/geometry.d.ts.map +1 -0
- package/lib/typescript/src/internal/options.d.ts +16 -0
- package/lib/typescript/src/internal/options.d.ts.map +1 -0
- package/lib/typescript/src/marquee.d.ts +469 -0
- package/lib/typescript/src/marquee.d.ts.map +1 -0
- package/lib/typescript/src/marquee.native.d.ts +470 -0
- package/lib/typescript/src/marquee.native.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +46 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/use-reduced-motion.d.ts +3 -0
- package/lib/typescript/src/use-reduced-motion.d.ts.map +1 -0
- package/lib/typescript/src/view-props.d.ts +4 -0
- package/lib/typescript/src/view-props.d.ts.map +1 -0
- package/package.json +166 -0
- package/src/RNMarqueeViewNativeComponent.ts +32 -0
- package/src/index.ts +12 -0
- package/src/internal/events.ts +12 -0
- package/src/internal/geometry.ts +14 -0
- package/src/internal/options.ts +48 -0
- package/src/marquee.native.tsx +144 -0
- package/src/marquee.tsx +35 -0
- package/src/types.ts +59 -0
- package/src/use-reduced-motion.ts +88 -0
- package/src/view-props.ts +24 -0
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
package com.marquee
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.graphics.Canvas
|
|
5
|
+
import android.graphics.Rect
|
|
6
|
+
import android.util.AttributeSet
|
|
7
|
+
import android.view.Choreographer
|
|
8
|
+
import android.view.MotionEvent
|
|
9
|
+
import android.view.VelocityTracker
|
|
10
|
+
import android.view.View
|
|
11
|
+
import android.view.ViewConfiguration
|
|
12
|
+
import android.view.ViewGroup
|
|
13
|
+
import com.facebook.react.uimanager.PixelUtil
|
|
14
|
+
import kotlin.math.abs
|
|
15
|
+
|
|
16
|
+
class MarqueeView : ViewGroup, Choreographer.FrameCallback {
|
|
17
|
+
constructor(context: Context) : super(context)
|
|
18
|
+
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
|
|
19
|
+
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
|
|
20
|
+
|
|
21
|
+
internal val pendingProps = MarqueeProps()
|
|
22
|
+
private var committedProps = MarqueeProps()
|
|
23
|
+
private val motion = MarqueeMotion()
|
|
24
|
+
private val visibleRect = Rect()
|
|
25
|
+
private var framePosted = false
|
|
26
|
+
private var attachedAndVisible = false
|
|
27
|
+
private var eventListener: MarqueeEventListener? = null
|
|
28
|
+
private var lastReportedState: MotionState? = null
|
|
29
|
+
private var lastLayoutReport: LayoutReport? = null
|
|
30
|
+
private var velocityTracker: VelocityTracker? = null
|
|
31
|
+
private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop.toFloat()
|
|
32
|
+
private var pointerDownX = 0f
|
|
33
|
+
private var pointerDownY = 0f
|
|
34
|
+
private var previousPointerX = 0f
|
|
35
|
+
private var didPan = false
|
|
36
|
+
private val resumeRunnable = Runnable { reconcileMotionState() }
|
|
37
|
+
|
|
38
|
+
init {
|
|
39
|
+
setWillNotDraw(false)
|
|
40
|
+
clipChildren = false
|
|
41
|
+
isClickable = true
|
|
42
|
+
isFocusable = false
|
|
43
|
+
importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_YES
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
internal fun setEventListener(listener: MarqueeEventListener?) {
|
|
47
|
+
eventListener = listener
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
internal fun commitProps() {
|
|
51
|
+
val previousPeriod = periodPx()
|
|
52
|
+
committedProps = pendingProps.copy()
|
|
53
|
+
val nextPeriod = periodPx()
|
|
54
|
+
configureMotion(preservePhase = previousPeriod > 0.0 && nextPeriod > 0.0)
|
|
55
|
+
reportContentLayout()
|
|
56
|
+
reconcileMotionState()
|
|
57
|
+
invalidate()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
internal fun resetForRecycle() {
|
|
61
|
+
removeCallbacks(resumeRunnable)
|
|
62
|
+
stopFrames()
|
|
63
|
+
velocityTracker?.recycle()
|
|
64
|
+
velocityTracker = null
|
|
65
|
+
pendingProps.copyFrom(MarqueeProps())
|
|
66
|
+
committedProps = MarqueeProps()
|
|
67
|
+
motion.configure(0.0, 0.0, preservePhase = false)
|
|
68
|
+
motion.pause()
|
|
69
|
+
lastReportedState = null
|
|
70
|
+
lastLayoutReport = null
|
|
71
|
+
eventListener = null
|
|
72
|
+
contentDescription = null
|
|
73
|
+
invalidate()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
|
77
|
+
setMeasuredDimension(
|
|
78
|
+
resolveSize(suggestedMinimumWidth, widthMeasureSpec),
|
|
79
|
+
resolveSize(suggestedMinimumHeight, heightMeasureSpec),
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
|
|
84
|
+
synchronizeContentWidthFromChild()
|
|
85
|
+
if (changed) {
|
|
86
|
+
configureMotion(preservePhase = true)
|
|
87
|
+
reportContentLayout()
|
|
88
|
+
reconcileMotionState()
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
override fun onViewAdded(child: View) {
|
|
93
|
+
super.onViewAdded(child)
|
|
94
|
+
require(childCount <= 1) { "Marquee accepts exactly one package-owned content container." }
|
|
95
|
+
invalidate()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
override fun dispatchDraw(canvas: Canvas) {
|
|
99
|
+
val child = getChildAt(0) ?: return
|
|
100
|
+
synchronizeContentWidthFromChild()
|
|
101
|
+
val contentWidth = contentWidthPx()
|
|
102
|
+
if (width <= 0 || height <= 0 || contentWidth <= 0f) return
|
|
103
|
+
|
|
104
|
+
canvas.save()
|
|
105
|
+
canvas.clipRect(paddingLeft, paddingTop, width - paddingRight, height - paddingBottom)
|
|
106
|
+
if (!shouldRepeat()) {
|
|
107
|
+
drawChildAt(canvas, child, staticContentX(contentWidth))
|
|
108
|
+
canvas.restore()
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
val period = periodPx().toFloat()
|
|
113
|
+
if (period <= 0f) {
|
|
114
|
+
canvas.restore()
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
val viewportLeft = paddingLeft.toFloat()
|
|
118
|
+
val viewportRight = (width - paddingRight).toFloat()
|
|
119
|
+
var x = viewportLeft - motion.phasePx.toFloat()
|
|
120
|
+
while (x + contentWidth < viewportLeft) x += period
|
|
121
|
+
while (x > viewportLeft) x -= period
|
|
122
|
+
while (x < viewportRight) {
|
|
123
|
+
drawChildAt(canvas, child, x)
|
|
124
|
+
x += period
|
|
125
|
+
}
|
|
126
|
+
canvas.restore()
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private fun drawChildAt(canvas: Canvas, child: View, x: Float) {
|
|
130
|
+
canvas.save()
|
|
131
|
+
canvas.translate(x - child.left, paddingTop.toFloat() - child.top)
|
|
132
|
+
drawChild(canvas, child, drawingTime)
|
|
133
|
+
canvas.restore()
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
override fun doFrame(frameTimeNanos: Long) {
|
|
137
|
+
framePosted = false
|
|
138
|
+
if (!attachedAndVisible || !getGlobalVisibleRect(visibleRect) || visibleRect.isEmpty) return
|
|
139
|
+
if (motion.advance(frameTimeNanos)) invalidate()
|
|
140
|
+
reportMotionState()
|
|
141
|
+
if (motion.isFrameDriven() && shouldScheduleMotion()) postFrame()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
override fun onTouchEvent(event: MotionEvent): Boolean {
|
|
145
|
+
if (!committedProps.active || committedProps.reduceMotion || !shouldRepeat()) return false
|
|
146
|
+
when (event.actionMasked) {
|
|
147
|
+
MotionEvent.ACTION_DOWN -> {
|
|
148
|
+
val edgeInset = PixelUtil.toPixelFromDIP(EDGE_GESTURE_INSET_DP)
|
|
149
|
+
if (event.x <= edgeInset || event.x >= width - edgeInset) return false
|
|
150
|
+
removeCallbacks(resumeRunnable)
|
|
151
|
+
velocityTracker?.recycle()
|
|
152
|
+
velocityTracker = VelocityTracker.obtain().also { it.addMovement(event) }
|
|
153
|
+
pointerDownX = event.x
|
|
154
|
+
pointerDownY = event.y
|
|
155
|
+
previousPointerX = event.x
|
|
156
|
+
didPan = false
|
|
157
|
+
if (committedProps.pauseOnPress) motion.beginHold()
|
|
158
|
+
reportMotionState()
|
|
159
|
+
return true
|
|
160
|
+
}
|
|
161
|
+
MotionEvent.ACTION_MOVE -> {
|
|
162
|
+
velocityTracker?.addMovement(event)
|
|
163
|
+
val totalDeltaX = event.x - pointerDownX
|
|
164
|
+
val totalDeltaY = event.y - pointerDownY
|
|
165
|
+
if (!didPan && abs(totalDeltaY) >= touchSlop &&
|
|
166
|
+
abs(totalDeltaY) * PAN_DOMINANCE_RATIO > abs(totalDeltaX)) {
|
|
167
|
+
finishGesture(0.0, canceled = true)
|
|
168
|
+
return false
|
|
169
|
+
}
|
|
170
|
+
if (!didPan && abs(totalDeltaX) >= touchSlop &&
|
|
171
|
+
abs(totalDeltaX) > abs(totalDeltaY) * PAN_DOMINANCE_RATIO) {
|
|
172
|
+
didPan = true
|
|
173
|
+
parent?.requestDisallowInterceptTouchEvent(true)
|
|
174
|
+
}
|
|
175
|
+
if (didPan) {
|
|
176
|
+
motion.panBy((event.x - previousPointerX).toDouble())
|
|
177
|
+
invalidate()
|
|
178
|
+
reportMotionState()
|
|
179
|
+
}
|
|
180
|
+
previousPointerX = event.x
|
|
181
|
+
return true
|
|
182
|
+
}
|
|
183
|
+
MotionEvent.ACTION_UP -> {
|
|
184
|
+
velocityTracker?.addMovement(event)
|
|
185
|
+
velocityTracker?.computeCurrentVelocity(1000)
|
|
186
|
+
val velocity = if (didPan) velocityTracker?.xVelocity?.toDouble() ?: 0.0 else 0.0
|
|
187
|
+
finishGesture(velocity, canceled = false)
|
|
188
|
+
performClick()
|
|
189
|
+
return true
|
|
190
|
+
}
|
|
191
|
+
MotionEvent.ACTION_CANCEL -> {
|
|
192
|
+
finishGesture(0.0, canceled = true)
|
|
193
|
+
return true
|
|
194
|
+
}
|
|
195
|
+
MotionEvent.ACTION_POINTER_DOWN -> {
|
|
196
|
+
finishGesture(0.0, canceled = true)
|
|
197
|
+
return false
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return super.onTouchEvent(event)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
override fun performClick(): Boolean = super.performClick()
|
|
204
|
+
|
|
205
|
+
override fun onAttachedToWindow() {
|
|
206
|
+
super.onAttachedToWindow()
|
|
207
|
+
attachedAndVisible = isShown
|
|
208
|
+
reconcileMotionState()
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
override fun onDetachedFromWindow() {
|
|
212
|
+
attachedAndVisible = false
|
|
213
|
+
removeCallbacks(resumeRunnable)
|
|
214
|
+
stopFrames()
|
|
215
|
+
motion.pause()
|
|
216
|
+
reportMotionState()
|
|
217
|
+
super.onDetachedFromWindow()
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
override fun onVisibilityAggregated(isVisible: Boolean) {
|
|
221
|
+
super.onVisibilityAggregated(isVisible)
|
|
222
|
+
attachedAndVisible = isVisible && isAttachedToWindow
|
|
223
|
+
if (!attachedAndVisible) {
|
|
224
|
+
stopFrames()
|
|
225
|
+
motion.pause()
|
|
226
|
+
}
|
|
227
|
+
reconcileMotionState()
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private fun configureMotion(preservePhase: Boolean) {
|
|
231
|
+
val direction = if (committedProps.direction == "right") -1.0 else 1.0
|
|
232
|
+
motion.configure(
|
|
233
|
+
periodPx = periodPx(),
|
|
234
|
+
speedPxPerSecond = PixelUtil.toPixelFromDIP(committedProps.speed).toDouble() * direction,
|
|
235
|
+
preservePhase = preservePhase,
|
|
236
|
+
)
|
|
237
|
+
motion.configureInteraction(
|
|
238
|
+
PixelUtil.toPixelFromDIP(committedProps.maxFlingVelocity).toDouble(),
|
|
239
|
+
committedProps.deceleration.toDouble(),
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private fun reconcileMotionState() {
|
|
244
|
+
removeCallbacks(resumeRunnable)
|
|
245
|
+
when {
|
|
246
|
+
committedProps.reduceMotion -> motion.reduceMotion()
|
|
247
|
+
!committedProps.active || !attachedAndVisible || !shouldRepeat() -> motion.pause()
|
|
248
|
+
motion.mode == MarqueeMotion.Mode.HOLD ||
|
|
249
|
+
motion.mode == MarqueeMotion.Mode.PAN ||
|
|
250
|
+
motion.mode == MarqueeMotion.Mode.FLING ||
|
|
251
|
+
motion.mode == MarqueeMotion.Mode.BLEND ||
|
|
252
|
+
motion.mode == MarqueeMotion.Mode.AUTO -> Unit
|
|
253
|
+
else -> motion.startAuto()
|
|
254
|
+
}
|
|
255
|
+
if (motion.isFrameDriven() && shouldScheduleMotion()) postFrame() else stopFrames()
|
|
256
|
+
reportMotionState()
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private fun finishGesture(velocity: Double, canceled: Boolean) {
|
|
260
|
+
velocityTracker?.recycle()
|
|
261
|
+
velocityTracker = null
|
|
262
|
+
parent?.requestDisallowInterceptTouchEvent(false)
|
|
263
|
+
if (canceled) {
|
|
264
|
+
motion.release(0.0)
|
|
265
|
+
reconcileMotionState()
|
|
266
|
+
} else if (didPan && abs(velocity) > 0.0) {
|
|
267
|
+
motion.release(velocity)
|
|
268
|
+
postFrame()
|
|
269
|
+
} else if (committedProps.resumeDelayMs > 0.0) {
|
|
270
|
+
motion.pause()
|
|
271
|
+
postDelayed(resumeRunnable, committedProps.resumeDelayMs.toLong())
|
|
272
|
+
} else {
|
|
273
|
+
motion.startAuto()
|
|
274
|
+
postFrame()
|
|
275
|
+
}
|
|
276
|
+
reportMotionState()
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
private fun contentWidthPx(): Float = PixelUtil.toPixelFromDIP(committedProps.contentWidth)
|
|
280
|
+
|
|
281
|
+
private fun synchronizeContentWidthFromChild() {
|
|
282
|
+
val childWidth = getChildAt(0)?.width?.toFloat() ?: return
|
|
283
|
+
if (childWidth <= 0f || abs(childWidth - contentWidthPx()) <= WIDTH_EPSILON) return
|
|
284
|
+
val widthInDip = PixelUtil.toDIPFromPixel(childWidth)
|
|
285
|
+
committedProps.contentWidth = widthInDip
|
|
286
|
+
pendingProps.contentWidth = widthInDip
|
|
287
|
+
configureMotion(preservePhase = true)
|
|
288
|
+
reportContentLayout()
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
private fun periodPx(): Double = contentWidthPx().toDouble() +
|
|
292
|
+
PixelUtil.toPixelFromDIP(committedProps.spacing).toDouble()
|
|
293
|
+
|
|
294
|
+
private fun viewportWidthPx(): Float = (width - paddingLeft - paddingRight).coerceAtLeast(0).toFloat()
|
|
295
|
+
|
|
296
|
+
private fun shouldRepeat(): Boolean {
|
|
297
|
+
val contentWidth = contentWidthPx()
|
|
298
|
+
return contentWidth > 0f &&
|
|
299
|
+
(contentWidth > viewportWidthPx() || committedProps.shortContentMode == ShortContentMode.REPEAT)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private fun staticContentX(contentWidth: Float): Float {
|
|
303
|
+
val available = (viewportWidthPx() - contentWidth).coerceAtLeast(0f)
|
|
304
|
+
return paddingLeft + when (committedProps.contentAlignment) {
|
|
305
|
+
ContentAlignment.CENTER -> available / 2f
|
|
306
|
+
ContentAlignment.END -> available
|
|
307
|
+
ContentAlignment.START -> 0f
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private fun shouldScheduleMotion(): Boolean = attachedAndVisible && committedProps.active &&
|
|
312
|
+
!committedProps.reduceMotion && shouldRepeat() &&
|
|
313
|
+
(committedProps.speed != 0.0 || motion.mode == MarqueeMotion.Mode.FLING ||
|
|
314
|
+
motion.mode == MarqueeMotion.Mode.BLEND)
|
|
315
|
+
|
|
316
|
+
private fun postFrame() {
|
|
317
|
+
if (framePosted || !isAttachedToWindow) return
|
|
318
|
+
framePosted = true
|
|
319
|
+
Choreographer.getInstance().postFrameCallback(this)
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private fun stopFrames() {
|
|
323
|
+
if (!framePosted) return
|
|
324
|
+
Choreographer.getInstance().removeFrameCallback(this)
|
|
325
|
+
framePosted = false
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private fun reportMotionState() {
|
|
329
|
+
val state = when {
|
|
330
|
+
committedProps.reduceMotion -> MotionState.REDUCED_MOTION
|
|
331
|
+
!committedProps.active -> MotionState.PAUSED
|
|
332
|
+
!shouldRepeat() -> MotionState.IDLE
|
|
333
|
+
committedProps.speed == 0.0 && motion.mode != MarqueeMotion.Mode.FLING -> MotionState.IDLE
|
|
334
|
+
motion.mode == MarqueeMotion.Mode.HOLD ||
|
|
335
|
+
motion.mode == MarqueeMotion.Mode.PAN ||
|
|
336
|
+
motion.mode == MarqueeMotion.Mode.PAUSED -> MotionState.PAUSED
|
|
337
|
+
else -> MotionState.RUNNING
|
|
338
|
+
}
|
|
339
|
+
if (lastReportedState == state) return
|
|
340
|
+
lastReportedState = state
|
|
341
|
+
eventListener?.onAnimationStateChange(state)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
private fun reportContentLayout() {
|
|
345
|
+
val report = LayoutReport(contentWidthPx(), viewportWidthPx())
|
|
346
|
+
val previous = lastLayoutReport
|
|
347
|
+
if (previous != null &&
|
|
348
|
+
abs(report.contentWidth - previous.contentWidth) <= WIDTH_EPSILON &&
|
|
349
|
+
abs(report.containerWidth - previous.containerWidth) <= WIDTH_EPSILON) return
|
|
350
|
+
lastLayoutReport = report
|
|
351
|
+
eventListener?.onContentLayout(report.contentWidth, report.containerWidth)
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
private data class LayoutReport(val contentWidth: Float, val containerWidth: Float)
|
|
355
|
+
|
|
356
|
+
private companion object {
|
|
357
|
+
const val WIDTH_EPSILON = 0.5f
|
|
358
|
+
const val EDGE_GESTURE_INSET_DP = 24f
|
|
359
|
+
const val PAN_DOMINANCE_RATIO = 1.15f
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
private fun MarqueeProps.copyFrom(other: MarqueeProps) {
|
|
364
|
+
contentWidth = other.contentWidth
|
|
365
|
+
spacing = other.spacing
|
|
366
|
+
speed = other.speed
|
|
367
|
+
direction = other.direction
|
|
368
|
+
active = other.active
|
|
369
|
+
reduceMotion = other.reduceMotion
|
|
370
|
+
shortContentMode = other.shortContentMode
|
|
371
|
+
contentAlignment = other.contentAlignment
|
|
372
|
+
maxFlingVelocity = other.maxFlingVelocity
|
|
373
|
+
deceleration = other.deceleration
|
|
374
|
+
pauseOnPress = other.pauseOnPress
|
|
375
|
+
resumeDelayMs = other.resumeDelayMs
|
|
376
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
package com.marquee
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
4
|
+
import com.facebook.react.uimanager.PixelUtil
|
|
5
|
+
import com.facebook.react.uimanager.ThemedReactContext
|
|
6
|
+
import com.facebook.react.uimanager.UIManagerHelper
|
|
7
|
+
import com.facebook.react.uimanager.ViewGroupManager
|
|
8
|
+
import com.facebook.react.uimanager.ViewManagerDelegate
|
|
9
|
+
import com.facebook.react.uimanager.annotations.ReactProp
|
|
10
|
+
import com.facebook.react.viewmanagers.RNMarqueeViewManagerDelegate
|
|
11
|
+
import com.facebook.react.viewmanagers.RNMarqueeViewManagerInterface
|
|
12
|
+
|
|
13
|
+
@ReactModule(name = MarqueeViewManager.NAME)
|
|
14
|
+
class MarqueeViewManager : ViewGroupManager<MarqueeView>(),
|
|
15
|
+
RNMarqueeViewManagerInterface<MarqueeView> {
|
|
16
|
+
private val delegate: ViewManagerDelegate<MarqueeView> = RNMarqueeViewManagerDelegate(this)
|
|
17
|
+
|
|
18
|
+
override fun getDelegate(): ViewManagerDelegate<MarqueeView> = delegate
|
|
19
|
+
override fun getName(): String = NAME
|
|
20
|
+
public override fun createViewInstance(context: ThemedReactContext) = MarqueeView(context)
|
|
21
|
+
|
|
22
|
+
override fun addEventEmitters(context: ThemedReactContext, view: MarqueeView) {
|
|
23
|
+
super.addEventEmitters(context, view)
|
|
24
|
+
val dispatcher = UIManagerHelper.getEventDispatcher(context)
|
|
25
|
+
val surfaceId = UIManagerHelper.getSurfaceId(context)
|
|
26
|
+
view.setEventListener(object : MarqueeEventListener {
|
|
27
|
+
override fun onAnimationStateChange(state: MotionState) {
|
|
28
|
+
dispatcher?.dispatchEvent(
|
|
29
|
+
AnimationStateChangeEvent(surfaceId, view.id, state.toEventValue()),
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
override fun onContentLayout(contentWidth: Float, containerWidth: Float) {
|
|
34
|
+
dispatcher?.dispatchEvent(
|
|
35
|
+
ContentLayoutEvent(
|
|
36
|
+
surfaceId,
|
|
37
|
+
view.id,
|
|
38
|
+
PixelUtil.toDIPFromPixel(contentWidth),
|
|
39
|
+
PixelUtil.toDIPFromPixel(containerWidth),
|
|
40
|
+
),
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
override fun getExportedCustomDirectEventTypeConstants(): Map<String, Any> =
|
|
47
|
+
(super.getExportedCustomDirectEventTypeConstants() ?: mutableMapOf()).apply {
|
|
48
|
+
put(AnimationStateChangeEvent.EVENT_NAME, mapOf("registrationName" to "onAnimationStateChange"))
|
|
49
|
+
put(ContentLayoutEvent.EVENT_NAME, mapOf("registrationName" to "onContentLayout"))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
override fun onAfterUpdateTransaction(view: MarqueeView) {
|
|
53
|
+
super.onAfterUpdateTransaction(view)
|
|
54
|
+
view.commitProps()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
override fun onDropViewInstance(view: MarqueeView) {
|
|
58
|
+
view.resetForRecycle()
|
|
59
|
+
super.onDropViewInstance(view)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@ReactProp(name = "contentWidth")
|
|
63
|
+
override fun setContentWidth(view: MarqueeView, value: Float) {
|
|
64
|
+
view.pendingProps.contentWidth = value.finiteOr(0f).coerceAtLeast(0f)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@ReactProp(name = "spacing")
|
|
68
|
+
override fun setSpacing(view: MarqueeView, value: Float) {
|
|
69
|
+
view.pendingProps.spacing = value.finiteOr(0f).coerceAtLeast(0f)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@ReactProp(name = "active")
|
|
73
|
+
override fun setActive(view: MarqueeView, value: Boolean) {
|
|
74
|
+
view.pendingProps.active = value
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
@ReactProp(name = "reduceMotion")
|
|
78
|
+
override fun setReduceMotion(view: MarqueeView, value: Boolean) {
|
|
79
|
+
view.pendingProps.reduceMotion = value
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
@ReactProp(name = "speed")
|
|
83
|
+
override fun setSpeed(view: MarqueeView, value: Double) {
|
|
84
|
+
view.pendingProps.speed = value.finiteOr(25.0).coerceAtLeast(0.0)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
@ReactProp(name = "direction")
|
|
88
|
+
override fun setDirection(view: MarqueeView, value: String?) {
|
|
89
|
+
view.pendingProps.direction = if (value == "right") "right" else "left"
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
@ReactProp(name = "shortContentMode")
|
|
93
|
+
override fun setShortContentMode(view: MarqueeView, value: String?) {
|
|
94
|
+
view.pendingProps.shortContentMode = shortContentModeFrom(value)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@ReactProp(name = "contentAlignment")
|
|
98
|
+
override fun setContentAlignment(view: MarqueeView, value: String?) {
|
|
99
|
+
view.pendingProps.contentAlignment = contentAlignmentFrom(value)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@ReactProp(name = "maxFlingVelocity")
|
|
103
|
+
override fun setMaxFlingVelocity(view: MarqueeView, value: Float) {
|
|
104
|
+
view.pendingProps.maxFlingVelocity = value.finiteOr(3200f).coerceAtLeast(0f)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
@ReactProp(name = "deceleration")
|
|
108
|
+
override fun setDeceleration(view: MarqueeView, value: Float) {
|
|
109
|
+
view.pendingProps.deceleration = value.finiteOr(0.9985f)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
@ReactProp(name = "pauseOnPress")
|
|
113
|
+
override fun setPauseOnPress(view: MarqueeView, value: Boolean) {
|
|
114
|
+
view.pendingProps.pauseOnPress = value
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
@ReactProp(name = "resumeDelayMs")
|
|
118
|
+
override fun setResumeDelayMs(view: MarqueeView, value: Double) {
|
|
119
|
+
view.pendingProps.resumeDelayMs = value.finiteOr(0.0).coerceAtLeast(0.0)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
companion object {
|
|
123
|
+
const val NAME = "RNMarqueeView"
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private fun MotionState.toEventValue(): String = when (this) {
|
|
128
|
+
MotionState.IDLE -> "idle"
|
|
129
|
+
MotionState.RUNNING -> "running"
|
|
130
|
+
MotionState.PAUSED -> "paused"
|
|
131
|
+
MotionState.REDUCED_MOTION -> "reducedMotion"
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private fun Double.finiteOr(fallback: Double): Double = if (isFinite()) this else fallback
|
|
135
|
+
private fun Float.finiteOr(fallback: Float): Float = if (isFinite()) this else fallback
|