react-native-marquee-animation 0.1.0 → 0.1.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 CHANGED
@@ -95,6 +95,9 @@ are native-owned:
95
95
 
96
96
  After a fling decelerates, both platforms blend velocity into the configured
97
97
  automatic direction over 350 ms instead of stopping or snapping.
98
+ `pauseOnPress: true` pauses immediately on touch-down. `resumeDelay` controls
99
+ when automatic motion resumes after release. On iOS, drag, fling, and the blend
100
+ back to automatic motion are synchronized to the active screen's refresh rate.
98
101
 
99
102
  ## Content contract
100
103
 
@@ -133,11 +136,14 @@ items must be clickable.
133
136
  display callback during automatic motion.
134
137
  - Android mounts one Fabric child container in a custom `ViewGroup` and replays
135
138
  the existing hardware display lists at repeated canvas offsets. A monotonic
136
- `Choreographer` clock changes only the translation phase.
139
+ `Choreographer` clock changes only the translation phase. Missed callbacks
140
+ advance by at most roughly one current display interval instead of catching
141
+ up in one visible jump; API 35+ requests a high refresh rate only while active.
137
142
  - Same-width visual updates do not restart motion. Width changes preserve the
138
143
  physical offset and velocity while native code recomputes the loop period.
139
- - Content that fits, inactive/offscreen/detached views, backgrounded apps, and
140
- Reduced Motion schedule no continuous frame work.
144
+ - Content that fits, `active={false}`, detached views, backgrounded apps, and
145
+ Reduced Motion schedule no continuous frame work. Window-attached views use
146
+ `active` as the authoritative application-level visibility signal.
141
147
  - Only the host is accessible; visually repeated descendants are hidden from
142
148
  VoiceOver and TalkBack.
143
149
 
@@ -84,7 +84,10 @@ internal class MarqueeMotion {
84
84
  lastFrameNanos = 0L
85
85
  }
86
86
 
87
- fun advance(frameTimeNanos: Long): Boolean {
87
+ fun advance(
88
+ frameTimeNanos: Long,
89
+ maximumDeltaSeconds: Double = DEFAULT_MAX_FRAME_DELTA_SECONDS,
90
+ ): Boolean {
88
91
  crossedLoopBoundary = false
89
92
  if (mode != Mode.AUTO && mode != Mode.FLING && mode != Mode.BLEND) {
90
93
  lastFrameNanos = frameTimeNanos
@@ -95,8 +98,10 @@ internal class MarqueeMotion {
95
98
  return true
96
99
  }
97
100
 
101
+ val safeMaximumDelta = maximumDeltaSeconds.takeIf { it.isFinite() && it > 0.0 }
102
+ ?: DEFAULT_MAX_FRAME_DELTA_SECONDS
98
103
  val deltaSeconds = ((frameTimeNanos - lastFrameNanos).coerceAtLeast(0L) / NANOS_PER_SECOND)
99
- .coerceAtMost(MAX_FRAME_DELTA_SECONDS)
104
+ .coerceAtMost(safeMaximumDelta)
100
105
  lastFrameNanos = frameTimeNanos
101
106
  val velocity = when (mode) {
102
107
  Mode.FLING -> flingVelocityPxPerSecond
@@ -154,7 +159,7 @@ internal class MarqueeMotion {
154
159
 
155
160
  private companion object {
156
161
  const val NANOS_PER_SECOND = 1_000_000_000.0
157
- const val MAX_FRAME_DELTA_SECONDS = 0.05
162
+ const val DEFAULT_MAX_FRAME_DELTA_SECONDS = 0.05
158
163
  const val MILLIS_PER_SECOND = 1_000.0
159
164
  const val DEFAULT_DECELERATION = 0.9985
160
165
  const val AUTO_BLEND_DURATION_SECONDS = 0.35
@@ -3,6 +3,7 @@ package com.marquee
3
3
  import android.content.Context
4
4
  import android.graphics.Canvas
5
5
  import android.graphics.Rect
6
+ import android.os.Build
6
7
  import android.util.AttributeSet
7
8
  import android.view.Choreographer
8
9
  import android.view.MotionEvent
@@ -97,7 +98,6 @@ class MarqueeView : ViewGroup, Choreographer.FrameCallback {
97
98
 
98
99
  override fun dispatchDraw(canvas: Canvas) {
99
100
  val child = getChildAt(0) ?: return
100
- synchronizeContentWidthFromChild()
101
101
  val contentWidth = contentWidthPx()
102
102
  if (width <= 0 || height <= 0 || contentWidth <= 0f) return
103
103
 
@@ -135,8 +135,11 @@ class MarqueeView : ViewGroup, Choreographer.FrameCallback {
135
135
 
136
136
  override fun doFrame(frameTimeNanos: Long) {
137
137
  framePosted = false
138
- if (!attachedAndVisible || !getGlobalVisibleRect(visibleRect) || visibleRect.isEmpty) return
139
- if (motion.advance(frameTimeNanos)) invalidate()
138
+ if (!attachedAndVisible || !getGlobalVisibleRect(visibleRect) || visibleRect.isEmpty) {
139
+ stopFrames()
140
+ return
141
+ }
142
+ if (motion.advance(frameTimeNanos, maximumFrameDeltaSeconds())) invalidate()
140
143
  reportMotionState()
141
144
  if (motion.isFrameDriven() && shouldScheduleMotion()) postFrame()
142
145
  }
@@ -313,16 +316,37 @@ class MarqueeView : ViewGroup, Choreographer.FrameCallback {
313
316
  (committedProps.speed != 0.0 || motion.mode == MarqueeMotion.Mode.FLING ||
314
317
  motion.mode == MarqueeMotion.Mode.BLEND)
315
318
 
319
+ private fun maximumFrameDeltaSeconds(): Double {
320
+ val refreshRate = display?.refreshRate?.toDouble()
321
+ ?.takeIf { it.isFinite() && it > 0.0 }
322
+ ?: DEFAULT_REFRESH_RATE_HZ
323
+ return FRAME_INTERVAL_TOLERANCE / refreshRate
324
+ }
325
+
316
326
  private fun postFrame() {
317
- if (framePosted || !isAttachedToWindow) return
327
+ if (!isAttachedToWindow) return
328
+ requestHighFrameRate()
329
+ if (framePosted) return
318
330
  framePosted = true
319
331
  Choreographer.getInstance().postFrameCallback(this)
320
332
  }
321
333
 
322
334
  private fun stopFrames() {
323
- if (!framePosted) return
324
- Choreographer.getInstance().removeFrameCallback(this)
335
+ if (framePosted) Choreographer.getInstance().removeFrameCallback(this)
325
336
  framePosted = false
337
+ clearFrameRateRequest()
338
+ }
339
+
340
+ private fun requestHighFrameRate() {
341
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
342
+ requestedFrameRate = REQUESTED_FRAME_RATE_CATEGORY_HIGH
343
+ }
344
+ }
345
+
346
+ private fun clearFrameRateRequest() {
347
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
348
+ requestedFrameRate = REQUESTED_FRAME_RATE_CATEGORY_DEFAULT
349
+ }
326
350
  }
327
351
 
328
352
  private fun reportMotionState() {
@@ -357,6 +381,8 @@ class MarqueeView : ViewGroup, Choreographer.FrameCallback {
357
381
  const val WIDTH_EPSILON = 0.5f
358
382
  const val EDGE_GESTURE_INSET_DP = 24f
359
383
  const val PAN_DOMINANCE_RATIO = 1.15f
384
+ const val DEFAULT_REFRESH_RATE_HZ = 60.0
385
+ const val FRAME_INTERVAL_TOLERANCE = 1.25
360
386
  }
361
387
  }
362
388
 
@@ -38,6 +38,16 @@ enum MarqueeMath {
38
38
  return from + (to - from) * eased
39
39
  }
40
40
 
41
+ static func clampedFrameElapsed(
42
+ elapsed: CFTimeInterval,
43
+ frameInterval: CFTimeInterval
44
+ ) -> CFTimeInterval {
45
+ let safeInterval = frameInterval.isFinite && frameInterval > 0
46
+ ? frameInterval
47
+ : 1 / 60
48
+ return min(max(0, elapsed), safeInterval * 1.25)
49
+ }
50
+
41
51
  static func shouldBeginPan(
42
52
  velocity: CGPoint,
43
53
  startX: CGFloat,
@@ -60,6 +70,11 @@ private enum MarqueeState: String {
60
70
  case reducedMotion = "reducedMotion"
61
71
  }
62
72
 
73
+ private enum InteractiveDisplayMode {
74
+ case dragging
75
+ case inertia
76
+ }
77
+
63
78
  @objcMembers public final class MarqueeRenderer: UIView, UIGestureRecognizerDelegate {
64
79
  public weak var delegate: MarqueeRendererDelegate?
65
80
 
@@ -74,7 +89,9 @@ private enum MarqueeState: String {
74
89
  private var isHolding = false
75
90
  private var isDragging = false
76
91
  private var panStartOffset: CGFloat = 0
92
+ private var pendingDragOffset: CGFloat?
77
93
  private var displayLink: CADisplayLink?
94
+ private var interactiveDisplayMode: InteractiveDisplayMode?
78
95
  private var lastDisplayTimestamp: CFTimeInterval = 0
79
96
  private var inertiaVelocity: CGFloat = 0
80
97
  private var isBlendingToAuto = false
@@ -272,6 +289,8 @@ private enum MarqueeState: String {
272
289
  accessibilityElementsHidden = true
273
290
  panGesture.delegate = self
274
291
  holdGesture.delegate = self
292
+ holdGesture.minimumPressDuration = 0
293
+ holdGesture.cancelsTouchesInView = false
275
294
  addGestureRecognizer(panGesture)
276
295
  addGestureRecognizer(holdGesture)
277
296
  registerNotifications()
@@ -458,26 +477,58 @@ private enum MarqueeState: String {
458
477
  guard canScrollGeometry, !effectiveReduceMotion else { return }
459
478
  switch gesture.state {
460
479
  case .began:
461
- cancelResume()
462
- isDragging = true
463
- freezeMotion()
464
- panStartOffset = currentPhysicalOffset()
465
- emitState(.paused)
480
+ beginDrag()
466
481
  case .changed:
467
- setContentOffset(panStartOffset + gesture.translation(in: self).x)
482
+ queueDrag(translation: gesture.translation(in: self).x)
468
483
  case .ended:
469
- isDragging = false
470
- isHolding = false
471
- beginInertia(velocity: gesture.velocity(in: self).x)
484
+ endDrag(
485
+ translation: gesture.translation(in: self).x,
486
+ velocity: gesture.velocity(in: self).x
487
+ )
472
488
  case .cancelled, .failed:
473
- isDragging = false
474
- isHolding = false
475
- scheduleResume()
489
+ cancelDrag()
476
490
  default:
477
491
  break
478
492
  }
479
493
  }
480
494
 
495
+ private func beginDrag() {
496
+ cancelResume()
497
+ isDragging = true
498
+ freezeMotion()
499
+ panStartOffset = currentPhysicalOffset()
500
+ pendingDragOffset = panStartOffset
501
+ startDisplayLink(mode: .dragging)
502
+ emitState(.paused)
503
+ }
504
+
505
+ private func queueDrag(translation: CGFloat) {
506
+ pendingDragOffset = panStartOffset + translation
507
+ }
508
+
509
+ private func applyPendingDragOffset() {
510
+ guard let pendingDragOffset else { return }
511
+ setContentOffset(pendingDragOffset)
512
+ self.pendingDragOffset = nil
513
+ }
514
+
515
+ private func endDrag(translation: CGFloat, velocity: CGFloat) {
516
+ queueDrag(translation: translation)
517
+ applyPendingDragOffset()
518
+ stopDisplayLink()
519
+ isDragging = false
520
+ isHolding = false
521
+ beginInertia(velocity: velocity)
522
+ }
523
+
524
+ private func cancelDrag() {
525
+ applyPendingDragOffset()
526
+ stopDisplayLink()
527
+ isDragging = false
528
+ isHolding = false
529
+ scheduleResume()
530
+ }
531
+
481
532
  private func beginInertia(velocity: CGFloat) {
482
533
  let maximum = max(0, configuration.maxFlingVelocity)
483
534
  let clampedVelocity = min(maximum, max(-maximum, velocity))
@@ -494,18 +545,36 @@ private enum MarqueeState: String {
494
545
  blendStartVelocity = inertiaVelocity
495
546
  isBlendingToAuto = true
496
547
  }
497
- let link = CADisplayLink(target: self, selector: #selector(stepInertia(_:)))
548
+ startDisplayLink(mode: .inertia)
549
+ emitState(.running)
550
+ }
551
+
552
+ private func startDisplayLink(mode: InteractiveDisplayMode) {
553
+ let link = CADisplayLink(target: self, selector: #selector(stepDisplayLink(_:)))
554
+ let maximum = Float(max(1, window?.screen.maximumFramesPerSecond ?? 60))
555
+ link.preferredFrameRateRange = CAFrameRateRange(
556
+ minimum: min(60, maximum),
557
+ maximum: maximum,
558
+ preferred: maximum
559
+ )
560
+ interactiveDisplayMode = mode
498
561
  displayLink = link
499
562
  link.add(to: .main, forMode: .common)
500
- emitState(.running)
501
563
  }
502
564
 
503
- @objc private func stepInertia(_ link: CADisplayLink) {
565
+ @objc private func stepDisplayLink(_ link: CADisplayLink) {
566
+ guard interactiveDisplayMode == .inertia else {
567
+ applyPendingDragOffset()
568
+ return
569
+ }
504
570
  if lastDisplayTimestamp == 0 {
505
571
  lastDisplayTimestamp = link.timestamp
506
572
  return
507
573
  }
508
- let elapsed = min(0.05, max(0, link.timestamp - lastDisplayTimestamp))
574
+ let elapsed = MarqueeMath.clampedFrameElapsed(
575
+ elapsed: link.timestamp - lastDisplayTimestamp,
576
+ frameInterval: link.targetTimestamp - link.timestamp
577
+ )
509
578
  lastDisplayTimestamp = link.timestamp
510
579
  if isBlendingToAuto {
511
580
  blendElapsed = min(autoBlendDuration, blendElapsed + elapsed)
@@ -533,6 +602,8 @@ private enum MarqueeState: String {
533
602
  private func stopDisplayLink() {
534
603
  displayLink?.invalidate()
535
604
  displayLink = nil
605
+ interactiveDisplayMode = nil
606
+ pendingDragOffset = nil
536
607
  lastDisplayTimestamp = 0
537
608
  inertiaVelocity = 0
538
609
  isBlendingToAuto = false
@@ -627,6 +698,13 @@ private enum MarqueeState: String {
627
698
  }
628
699
 
629
700
  var testingContentWidth: CGFloat { configuration.contentWidth }
701
+ var testingHoldEnabled: Bool { holdGesture.isEnabled }
702
+ var testingHoldMinimumPressDuration: TimeInterval { holdGesture.minimumPressDuration }
703
+ var testingHoldCancelsTouchesInView: Bool { holdGesture.cancelsTouchesInView }
704
+ var testingDisplayLinkPreferredFrameRate: Float? {
705
+ displayLink?.preferredFrameRateRange.preferred
706
+ }
707
+ var testingHasDisplayLink: Bool { displayLink != nil }
630
708
  var testingState: String? { lastState?.rawValue }
631
709
  var testingHasAutoAnimation: Bool {
632
710
  contentView?.layer.animation(forKey: animationKey) != nil
@@ -642,6 +720,12 @@ private enum MarqueeState: String {
642
720
  }
643
721
  }
644
722
  func testingBeginInertia(_ velocity: CGFloat) { beginInertia(velocity: velocity) }
723
+ func testingBeginDrag() { beginDrag() }
724
+ func testingQueueDrag(translation: CGFloat) { queueDrag(translation: translation) }
725
+ func testingStepDrag() { applyPendingDragOffset() }
726
+ func testingEndDrag(translation: CGFloat, velocity: CGFloat) {
727
+ endDrag(translation: translation, velocity: velocity)
728
+ }
645
729
  func testingSetOffset(_ offset: CGFloat) {
646
730
  contentView?.layer.removeAnimation(forKey: animationKey)
647
731
  setContentOffset(offset)
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
 
3
- export { Marquee } from './marquee';
3
+ export { Marquee } from './marquee.js';
4
4
  //# sourceMappingURL=index.js.map
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { forwardRef, useCallback, useMemo, useState } from 'react';
4
4
  import { StyleSheet, View } from 'react-native';
5
- import NativeMarqueeView from './RNMarqueeViewNativeComponent';
5
+ import NativeMarqueeView from './RNMarqueeViewNativeComponent.js';
6
6
  import { resolveAnimationState } from "./internal/events.js";
7
7
  import { resolveMeasuredContentWidth } from "./internal/geometry.js";
8
8
  import { resolveInteraction, resolveSpacing, resolveSpeed } from "./internal/options.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-marquee-animation",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Production-grade generic native marquee for React Native",
5
5
  "main": "./lib/module/index.js",
6
6
  "react-native": "./src/index.ts",
@@ -39,7 +39,7 @@
39
39
  ],
40
40
  "scripts": {
41
41
  "example": "bun --filter react-native-marquee-example",
42
- "build": "bob build",
42
+ "build": "bob build && node scripts/fix-module-imports.mjs",
43
43
  "clean": "del-cli lib",
44
44
  "format": "prettier --write .",
45
45
  "format:check": "prettier --check .",
@@ -47,8 +47,9 @@
47
47
  "test": "bun test",
48
48
  "test:android": "cd example/android && ./gradlew :react-native-marquee-animation:testDebugUnitTest",
49
49
  "typecheck": "tsc --noEmit",
50
+ "verify:module-imports": "node scripts/verify-module-imports.mjs",
50
51
  "lint": "eslint \"**/*.{js,mjs,ts,tsx}\"",
51
- "check": "bun run format:check && bun run lint && bun run typecheck && bun run test && bun run build",
52
+ "check": "bun run format:check && bun run lint && bun run typecheck && bun run test && bun run build && bun run verify:module-imports",
52
53
  "release": "release-it",
53
54
  "release:next": "release-it --preRelease=next"
54
55
  },