react-native-livechart 4.25.0 → 4.27.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.
Files changed (125) hide show
  1. package/dist/components/CustomReferenceLineOverlay.d.ts +8 -1
  2. package/dist/components/CustomReferenceLineOverlay.d.ts.map +1 -1
  3. package/dist/components/CustomTooltipOverlay.d.ts +3 -2
  4. package/dist/components/CustomTooltipOverlay.d.ts.map +1 -1
  5. package/dist/components/LiveChart.d.ts +1 -1
  6. package/dist/components/LiveChart.d.ts.map +1 -1
  7. package/dist/components/LiveChartSeries.d.ts.map +1 -1
  8. package/dist/components/ReferenceLineOverlay.d.ts +4 -0
  9. package/dist/components/ReferenceLineOverlay.d.ts.map +1 -1
  10. package/dist/components/ReferenceValueBadge.d.ts +15 -0
  11. package/dist/components/ReferenceValueBadge.d.ts.map +1 -0
  12. package/dist/components/XAxisOverlay.d.ts +17 -0
  13. package/dist/components/XAxisOverlay.d.ts.map +1 -1
  14. package/dist/core/liveChartEngineTick.d.ts +6 -1
  15. package/dist/core/liveChartEngineTick.d.ts.map +1 -1
  16. package/dist/core/liveChartSeriesEngineTick.d.ts +4 -1
  17. package/dist/core/liveChartSeriesEngineTick.d.ts.map +1 -1
  18. package/dist/core/resolveConfig.d.ts +28 -1
  19. package/dist/core/resolveConfig.d.ts.map +1 -1
  20. package/dist/core/useLiveChartEngine.d.ts +10 -1
  21. package/dist/core/useLiveChartEngine.d.ts.map +1 -1
  22. package/dist/core/useLiveChartSeriesEngine.d.ts +5 -1
  23. package/dist/core/useLiveChartSeriesEngine.d.ts.map +1 -1
  24. package/dist/draw/grid.d.ts +2 -2
  25. package/dist/draw/line.d.ts +15 -1
  26. package/dist/draw/line.d.ts.map +1 -1
  27. package/dist/hooks/crosshairShared.d.ts +24 -1
  28. package/dist/hooks/crosshairShared.d.ts.map +1 -1
  29. package/dist/hooks/referenceBadgeFont.d.ts +5 -0
  30. package/dist/hooks/referenceBadgeFont.d.ts.map +1 -0
  31. package/dist/hooks/resolveChartLayout.d.ts +8 -0
  32. package/dist/hooks/resolveChartLayout.d.ts.map +1 -1
  33. package/dist/hooks/useBadge.d.ts +16 -1
  34. package/dist/hooks/useBadge.d.ts.map +1 -1
  35. package/dist/hooks/useChartSkiaFont.d.ts +1 -0
  36. package/dist/hooks/useChartSkiaFont.d.ts.map +1 -1
  37. package/dist/hooks/useCrosshair.d.ts +7 -2
  38. package/dist/hooks/useCrosshair.d.ts.map +1 -1
  39. package/dist/hooks/useCrosshairSeries.d.ts +4 -1
  40. package/dist/hooks/useCrosshairSeries.d.ts.map +1 -1
  41. package/dist/hooks/useLatestCallback.d.ts +6 -0
  42. package/dist/hooks/useLatestCallback.d.ts.map +1 -0
  43. package/dist/hooks/useMarkers.d.ts.map +1 -1
  44. package/dist/hooks/usePanScroll.d.ts +3 -1
  45. package/dist/hooks/usePanScroll.d.ts.map +1 -1
  46. package/dist/hooks/usePinchZoom.d.ts.map +1 -1
  47. package/dist/hooks/useReferenceDrag.d.ts +23 -7
  48. package/dist/hooks/useReferenceDrag.d.ts.map +1 -1
  49. package/dist/hooks/useReferenceLine.d.ts +24 -12
  50. package/dist/hooks/useReferenceLine.d.ts.map +1 -1
  51. package/dist/hooks/useReferenceLineGrouping.d.ts +19 -0
  52. package/dist/hooks/useReferenceLineGrouping.d.ts.map +1 -0
  53. package/dist/hooks/useReferenceLinePress.d.ts +4 -2
  54. package/dist/hooks/useReferenceLinePress.d.ts.map +1 -1
  55. package/dist/hooks/useReferenceTagStack.d.ts +31 -0
  56. package/dist/hooks/useReferenceTagStack.d.ts.map +1 -0
  57. package/dist/hooks/useScrubMarkers.d.ts +12 -0
  58. package/dist/hooks/useScrubMarkers.d.ts.map +1 -0
  59. package/dist/hooks/useXAxis.d.ts +1 -1
  60. package/dist/hooks/useXAxis.d.ts.map +1 -1
  61. package/dist/hooks/useYAxisScaleGesture.d.ts +34 -0
  62. package/dist/hooks/useYAxisScaleGesture.d.ts.map +1 -0
  63. package/dist/index.d.ts +1 -1
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/math/axisBadgeLayout.d.ts +16 -0
  66. package/dist/math/axisBadgeLayout.d.ts.map +1 -0
  67. package/dist/math/markers.d.ts +3 -0
  68. package/dist/math/markers.d.ts.map +1 -1
  69. package/dist/math/range.d.ts +1 -1
  70. package/dist/math/range.d.ts.map +1 -1
  71. package/dist/math/rangeAnimation.d.ts +4 -0
  72. package/dist/math/rangeAnimation.d.ts.map +1 -0
  73. package/dist/math/referenceDrag.d.ts +17 -5
  74. package/dist/math/referenceDrag.d.ts.map +1 -1
  75. package/dist/math/referenceGroup.d.ts +2 -2
  76. package/dist/math/referenceTagStack.d.ts +36 -0
  77. package/dist/math/referenceTagStack.d.ts.map +1 -0
  78. package/dist/math/scrubMarkers.d.ts +12 -0
  79. package/dist/math/scrubMarkers.d.ts.map +1 -0
  80. package/dist/types.d.ts +240 -30
  81. package/dist/types.d.ts.map +1 -1
  82. package/package.json +1 -1
  83. package/src/components/CustomReferenceLineOverlay.tsx +50 -8
  84. package/src/components/CustomTooltipOverlay.tsx +5 -1
  85. package/src/components/LiveChart.tsx +563 -200
  86. package/src/components/LiveChartSeries.tsx +117 -13
  87. package/src/components/ReferenceLineOverlay.tsx +62 -26
  88. package/src/components/ReferenceValueBadge.tsx +102 -0
  89. package/src/components/XAxisOverlay.tsx +104 -1
  90. package/src/core/liveChartEngineTick.ts +66 -10
  91. package/src/core/liveChartSeriesEngineTick.ts +55 -5
  92. package/src/core/resolveConfig.ts +95 -2
  93. package/src/core/useLiveChartEngine.ts +53 -11
  94. package/src/core/useLiveChartSeriesEngine.ts +11 -1
  95. package/src/draw/grid.ts +2 -2
  96. package/src/draw/line.ts +23 -1
  97. package/src/hooks/crosshairShared.ts +85 -1
  98. package/src/hooks/referenceBadgeFont.ts +35 -0
  99. package/src/hooks/resolveChartLayout.ts +17 -0
  100. package/src/hooks/useBadge.ts +44 -5
  101. package/src/hooks/useChartSkiaFont.ts +1 -1
  102. package/src/hooks/useCrosshair.ts +446 -300
  103. package/src/hooks/useCrosshairSeries.ts +29 -10
  104. package/src/hooks/useLatestCallback.ts +15 -0
  105. package/src/hooks/useMarkers.ts +51 -39
  106. package/src/hooks/usePanScroll.ts +155 -127
  107. package/src/hooks/usePinchZoom.ts +84 -63
  108. package/src/hooks/useReferenceDrag.ts +395 -146
  109. package/src/hooks/useReferenceLine.ts +453 -277
  110. package/src/hooks/useReferenceLineGrouping.ts +72 -0
  111. package/src/hooks/useReferenceLinePress.ts +121 -53
  112. package/src/hooks/useReferenceTagStack.ts +81 -0
  113. package/src/hooks/useScrubMarkers.ts +79 -0
  114. package/src/hooks/useXAxis.ts +11 -2
  115. package/src/hooks/useYAxisScaleGesture.ts +263 -0
  116. package/src/index.ts +11 -0
  117. package/src/math/axisBadgeLayout.ts +37 -0
  118. package/src/math/markers.ts +28 -3
  119. package/src/math/range.ts +35 -0
  120. package/src/math/rangeAnimation.ts +28 -0
  121. package/src/math/referenceDrag.ts +21 -4
  122. package/src/math/referenceGroup.ts +2 -2
  123. package/src/math/referenceTagStack.ts +112 -0
  124. package/src/math/scrubMarkers.ts +55 -0
  125. package/src/types.ts +246 -30
@@ -1,4 +1,5 @@
1
1
  import { type SkFont } from "@shopify/react-native-skia";
2
+ import { useMemo, useRef } from "react";
2
3
  import { Gesture } from "react-native-gesture-handler";
3
4
  import {
4
5
  useAnimatedReaction,
@@ -22,6 +23,7 @@ import type {
22
23
  CandleGap,
23
24
  CandlePoint,
24
25
  LiveChartPalette,
26
+ Marker,
25
27
  ScrubActionPoint,
26
28
  ScrubPoint,
27
29
  } from "../types";
@@ -57,6 +59,9 @@ import {
57
59
  resetDelayedPanGuard,
58
60
  shouldStartDelayedPan,
59
61
  } from "./delayedPanGuard";
62
+ import { useScrubMarkers, type ScrubMarkerOptions } from "./useScrubMarkers";
63
+
64
+ import { useLatestCallback } from "./useLatestCallback";
60
65
 
61
66
  const ACTION_HIT_SLOP = 6;
62
67
  const RETICLE_HIT = 14;
@@ -114,6 +119,10 @@ export interface CrosshairChartOpts {
114
119
  bridgeUnknown?: boolean;
115
120
  }
116
121
 
122
+ /** Stable empty gap list so the crosshair worklets that capture it aren't
123
+ * re-registered on every render when the chart has no gaps. */
124
+ const EMPTY_GAPS: CandleGap[] = [];
125
+
117
126
  function resolveCrosshairSettings(
118
127
  chartOpts: CrosshairChartOpts | undefined,
119
128
  scrubAction: ResolvedScrubActionConfig | null | undefined,
@@ -135,7 +144,7 @@ function resolveCrosshairSettings(
135
144
  candlesSV: chartOpts?.candles,
136
145
  liveCandleSV: chartOpts?.liveCandle,
137
146
  candleWidthSecs: chartOpts?.candleWidthSecs ?? 60,
138
- chartGaps: chartOpts?.gaps ?? [],
147
+ chartGaps: chartOpts?.gaps ?? EMPTY_GAPS,
139
148
  bridgeNoTrades: chartOpts?.bridgeNoTrades ?? false,
140
149
  bridgeUnavailable: chartOpts?.bridgeUnavailable ?? false,
141
150
  bridgeUnknown: chartOpts?.bridgeUnknown ?? false,
@@ -147,7 +156,7 @@ function resolveCrosshairSettings(
147
156
  */
148
157
  export function useCrosshair(
149
158
  engine: SingleEngineState,
150
- padding: ChartPadding,
159
+ paddingInput: ChartPadding,
151
160
  _palette: LiveChartPalette,
152
161
  formatValue: (v: number) => string,
153
162
  formatTime: (t: number) => string,
@@ -204,7 +213,19 @@ export function useCrosshair(
204
213
  * Default `false`.
205
214
  */
206
215
  snapToCandles = false,
216
+ /** Candle mode: fires once per candle the crosshair enters, `null` on exit. */
217
+ onScrubCandleChange?: (candle: CandlePoint | null) => void,
218
+ markerOptions?: ScrubMarkerOptions,
219
+ /** Price-axis ownership also guards against stale native long-press events. */
220
+ axisScaleActive?: SharedValue<boolean>,
207
221
  ): CrosshairState {
222
+ // Config objects may be recreated without changing the plot geometry. Keep
223
+ // worklet closures and gestures stable until an actual inset changes.
224
+ const { top, right, bottom, left } = paddingInput;
225
+ const padding = useMemo(
226
+ () => ({ top, right, bottom, left }),
227
+ [top, right, bottom, left],
228
+ );
208
229
  const scrubX = useSharedValue(-1);
209
230
  const scrubActive = useSharedValue(false);
210
231
  // Tracks whether the active scrub phase actually began, so a tap that never
@@ -214,8 +235,8 @@ export function useCrosshair(
214
235
  // lifecycle helpers are also used by LiveChartSeries and unit-tested directly.
215
236
  const fingerDown = useSharedValue(false);
216
237
  const panActivated = useSharedValue(false);
217
- // Where and when the current touch went down, for the stationary-hold and
218
- // stale-timer guards (see delayedPanGuard).
238
+ // Where and when the current touch went down, for overlay arbitration and
239
+ // the stationary-hold / stale-timer guards (see delayedPanGuard).
219
240
  const downX = useSharedValue(0);
220
241
  const downY = useSharedValue(0);
221
242
  const downAtMs = useSharedValue(0);
@@ -420,6 +441,15 @@ export function useCrosshair(
420
441
  },
421
442
  );
422
443
 
444
+ const scrubMarkers = useScrubMarkers(
445
+ markerOptions, scrubActive, scrubTime, engine.timestamp, engine.displayWindow,
446
+ engine.canvasWidth, padding.left + padding.right,
447
+ isCandleMode, scrubCandle, candleWidthSecs, scrubGap,
448
+ );
449
+ // Cache marker details on JS so ordinary scrub frames only bridge the scalar
450
+ // point. A new selection / marker input carries a fresh payload once.
451
+ const jsScrubMarkers = useRef<Marker[]>([]);
452
+
423
453
  // ── Scrub-action lock derivations ──────────────────────────────────────────
424
454
  const lockPrice = useSharedValue<number | null>(null);
425
455
  const lockY = useSharedValue(-1);
@@ -521,51 +551,62 @@ export function useCrosshair(
521
551
  );
522
552
 
523
553
  /* istanbul ignore next -- invoked only via scheduleOnRN from UI-thread gesture */
524
- function handleScrubAction(
554
+ const handleScrubAction = useLatestCallback((
525
555
  price: number,
526
556
  time: number,
527
557
  x: number,
528
558
  y: number,
529
559
  candleJson: string | null,
530
- ) {
560
+ ) => {
531
561
  const candle: CandlePoint | undefined = candleJson
532
562
  ? (JSON.parse(candleJson) as CandlePoint)
533
563
  : undefined;
534
564
  onScrubAction?.({ price, time, x, y, candle });
535
- }
565
+ });
536
566
 
537
567
  /* istanbul ignore next -- invoked only via scheduleOnRN from UI-thread gesture */
538
- function handleScrub(
568
+ const handleScrub = useLatestCallback((
539
569
  x: number,
540
570
  y: number,
541
571
  time: number,
542
572
  value: number,
543
573
  candleJson: string | null,
544
574
  gapJson: string | null,
545
- ) {
575
+ markerUpdate: Marker[] | undefined,
576
+ ) => {
546
577
  const candle: CandlePoint | undefined = candleJson
547
578
  ? (JSON.parse(candleJson) as CandlePoint)
548
579
  : undefined;
549
580
  const gap: CandleGap | undefined = gapJson
550
581
  ? (JSON.parse(gapJson) as CandleGap)
551
582
  : undefined;
552
- onScrub?.({ time, value, x, y, candle, gap });
553
- }
583
+ if (markerUpdate !== undefined) jsScrubMarkers.current = markerUpdate;
584
+ onScrub?.({
585
+ time, value, x, y, candle, gap,
586
+ ...(markerOptions ? { markers: jsScrubMarkers.current } : {}),
587
+ });
588
+ });
589
+
590
+ const handleScrubCandleChange = useLatestCallback((candleJson: string | null) => {
591
+ onScrubCandleChange?.(
592
+ candleJson ? (JSON.parse(candleJson) as CandlePoint) : null,
593
+ );
594
+ });
554
595
 
555
596
  /* istanbul ignore next */
556
- function handleScrubEnd() {
597
+ const handleScrubEnd = useLatestCallback(() => {
557
598
  onScrub?.(null);
558
- }
599
+ });
559
600
 
560
601
  /* istanbul ignore next */
561
- function handleGestureStart() {
602
+ const handleGestureStart = useLatestCallback(() => {
562
603
  onGestureStart?.();
563
- }
604
+ });
564
605
 
565
606
  /* istanbul ignore next */
566
- function handleGestureEnd() {
607
+ const handleGestureEnd = useLatestCallback(() => {
567
608
  onGestureEnd?.();
568
- }
609
+ });
569
610
 
570
611
  const hasOnScrub = onScrub != null;
571
612
  const hasOnGestureStart = onGestureStart != null;
@@ -597,20 +638,26 @@ export function useCrosshair(
597
638
  }
598
639
  const gap = scrubGap.get();
599
640
  const gapJson = gap ? JSON.stringify(gap) : null;
600
- return JSON.stringify([time, val, x, dotY, candleJson, gapJson]);
641
+ return {
642
+ point: JSON.stringify([time, val, x, dotY, candleJson, gapJson]),
643
+ markers: markerOptions ? scrubMarkers.get() : undefined,
644
+ };
601
645
  },
602
646
  (curr, prev) => {
603
647
  "worklet";
604
648
  if (!hasOnScrub) return;
605
- if (
606
- curr === "__idle__" ||
607
- curr === "__inactive__" ||
608
- curr === "__pending__"
609
- ) {
649
+ if (typeof curr === "string") {
650
+ // No ScrubPoint can represent an unpriced gap. Clear opt-in trade
651
+ // readouts once when leaving a valid point rather than leave stale data.
652
+ if (markerOptions && curr === "__pending__" && prev && typeof prev === "object") {
653
+ scheduleOnRN(handleScrubEnd);
654
+ }
610
655
  return;
611
656
  }
612
- if (curr === prev) return;
613
- const row = JSON.parse(curr) as [
657
+ const previous = typeof prev === "object" ? prev : null;
658
+ const markersChanged = curr.markers !== previous?.markers;
659
+ if (curr.point === previous?.point && !markersChanged) return;
660
+ const row = JSON.parse(curr.point) as [
614
661
  number,
615
662
  number,
616
663
  number,
@@ -618,7 +665,31 @@ export function useCrosshair(
618
665
  string | null,
619
666
  string | null,
620
667
  ];
621
- scheduleOnRN(handleScrub, row[2], row[3], row[0], row[1], row[4], row[5]);
668
+ scheduleOnRN(handleScrub, row[2], row[3], row[0], row[1], row[4], row[5],
669
+ markersChanged ? curr.markers : undefined);
670
+ },
671
+ );
672
+
673
+ const hasOnScrubCandleChange = onScrubCandleChange != null;
674
+
675
+ // Keyed on the candle's bucket time rather than the candle itself, so the
676
+ // forming candle's ticks don't re-fire it: one bridge crossing per candle.
677
+ useAnimatedReaction(
678
+ () => {
679
+ "worklet";
680
+ if (!hasOnScrubCandleChange || !isCandleMode || !scrubActive.get()) {
681
+ return null;
682
+ }
683
+ return scrubCandle.get()?.time ?? null;
684
+ },
685
+ (curr, prev) => {
686
+ "worklet";
687
+ if (!hasOnScrubCandleChange || curr === prev) return;
688
+ const candle = curr === null ? null : scrubCandle.get();
689
+ scheduleOnRN(
690
+ handleScrubCandleChange,
691
+ candle ? JSON.stringify(candle) : null,
692
+ );
622
693
  },
623
694
  );
624
695
 
@@ -634,299 +705,374 @@ export function useCrosshair(
634
705
  ? SCRUB_ACTION_PRESS_HOLD_MS
635
706
  : panGestureDelay;
636
707
 
637
- // `scrub.snapToCandles`: quantize the scrub X to the hovered candle's center
638
- // before it enters `scrubX`, so the crosshair — and everything derived from
639
- // it (time, tooltip, dim edge) — jumps candle-to-candle instead of gliding.
640
- // Line mode and gaps between candles pass the raw X through.
641
- /* istanbul ignore next -- worklet, called only from UI-thread gesture handlers */
642
- const snapCandleX = (x: number): number => {
643
- "worklet";
644
- if (!snapToCandles || !isCandleMode || !candlesSV) return x;
645
- return snapScrubXToCandleCenter(
646
- x,
647
- candlesSV.get(),
648
- liveCandleSV?.get() ?? null,
649
- candleWidthSecs,
650
- padding,
651
- engine.canvasWidth.get(),
652
- engine.timestamp.get(),
653
- engine.displayWindow.get(),
654
- chartGaps,
655
- );
656
- };
708
+ const {
709
+ canvasWidth: canvasWidthSV,
710
+ canvasHeight: canvasHeightSV,
711
+ displayMin: displayMinSV,
712
+ displayMax: displayMaxSV,
713
+ timestamp: timestampSV,
714
+ displayWindow: displayWindowSV,
715
+ } = engine;
716
+ const { gesture, tapGesture } = useMemo(() => {
717
+ // `scrub.snapToCandles`: quantize the scrub X to the hovered candle's center
718
+ // before it enters `scrubX`, so the crosshair — and everything derived from
719
+ // it (time, tooltip, dim edge) — jumps candle-to-candle instead of gliding.
720
+ // Line mode and gaps between candles pass the raw X through.
721
+ /* istanbul ignore next -- worklet, called only from UI-thread gesture handlers */
722
+ const snapCandleX = (x: number): number => {
723
+ "worklet";
724
+ if (!snapToCandles || !isCandleMode || !candlesSV) return x;
725
+ return snapScrubXToCandleCenter(
726
+ x,
727
+ candlesSV.get(),
728
+ liveCandleSV?.get() ?? null,
729
+ candleWidthSecs,
730
+ padding,
731
+ canvasWidthSV.get(),
732
+ timestampSV.get(),
733
+ displayWindowSV.get(),
734
+ chartGaps,
735
+ );
736
+ };
657
737
 
658
- let gesture = Gesture.Pan()
659
- .maxPointers(1)
660
- .shouldCancelWhenOutside(false)
661
- .onTouchesDown(
662
- /* istanbul ignore next */ (e) => {
663
- "worklet";
664
- delayedPanTouchDown(
665
- longPressMs,
666
- e,
667
- fingerDown,
668
- downX,
669
- downY,
670
- downAtMs,
671
- holdBroken,
672
- );
673
- },
674
- )
675
- // Stationary hold: drift beyond the slop while the long-press is still
676
- // pending fails the pan, so a drag can never mature into a scrub.
677
- .onTouchesMove(
678
- /* istanbul ignore next */ (e, manager) => {
679
- "worklet";
680
- delayedPanTouchMove(
681
- longPressMs,
682
- e,
683
- manager,
684
- panActivated,
685
- downX,
686
- downY,
687
- holdBroken,
688
- );
689
- },
690
- )
691
- .onTouchesUp(
692
- /* istanbul ignore next */ (e, manager) => {
693
- "worklet";
694
- delayedPanTouchUp(longPressMs, e, manager, fingerDown, panActivated);
695
- },
696
- )
697
- .onTouchesCancelled(
698
- /* istanbul ignore next */ () => {
699
- "worklet";
700
- delayedPanTouchCancelled(longPressMs, fingerDown);
701
- },
702
- )
703
- // Start scrubbing on ACTIVE (onStart), not on touch-down (onBegin):
704
- // `activateAfterLongPress` only delays activation, so onBegin still fires
705
- // immediately — using it would scrub instantly and ignore panGestureDelay,
706
- // and leave scrubActive stuck for taps that never reach the long-press.
707
- .onStart(
708
- /* istanbul ignore next */ (e) => {
709
- "worklet";
710
- if (
711
- !shouldStartDelayedPan(
738
+ let gesture = Gesture.Pan()
739
+ .maxPointers(1)
740
+ .shouldCancelWhenOutside(false)
741
+ .onTouchesDown(
742
+ /* istanbul ignore next */ (e) => {
743
+ "worklet";
744
+ // Overlay drags decide ownership at touch-down. Preserve that point
745
+ // even without a hold: the finger can enter a grabRange before the
746
+ // scrub reaches its horizontal activation threshold.
747
+ const touch = e.changedTouches[0];
748
+ if (touch) {
749
+ downX.set(touch.x);
750
+ downY.set(touch.y);
751
+ }
752
+ delayedPanTouchDown(
712
753
  longPressMs,
754
+ e,
713
755
  fingerDown,
714
- panActivated,
756
+ downX,
757
+ downY,
715
758
  downAtMs,
716
759
  holdBroken,
717
- scrollActiveSV,
718
- )
719
- )
720
- return;
721
- if (!enabled) return;
722
- // Scrub-action: once a reticle is placed, drag adjusts it (2D — the Y is
723
- // the price). The ephemeral live-scrub never engages, so scrubActive
724
- // stays false and the live crosshair hides itself.
725
- if (hasScrubAction && lockActive.get()) {
726
- lockX.set(
727
- clampPlotX(
728
- e.x,
729
- padding.left,
730
- engine.canvasWidth.get(),
731
- padding.right,
732
- ),
733
760
  );
734
- // Freeze the chosen price (value at the pointer Y); the line's Y is
735
- // re-derived from it so the level stays put in PRICE as the axis rescales.
736
- {
737
- const p = computeValueAtY(
738
- e.y,
739
- engine.displayMin.get(),
740
- engine.displayMax.get(),
741
- engine.canvasHeight.get(),
742
- padding.top,
743
- padding.bottom,
761
+ },
762
+ )
763
+ // Stationary hold: drift beyond the slop while the long-press is still
764
+ // pending fails the pan, so a drag can never mature into a scrub.
765
+ .onTouchesMove(
766
+ /* istanbul ignore next */ (e, manager) => {
767
+ "worklet";
768
+ delayedPanTouchMove(
769
+ longPressMs,
770
+ e,
771
+ manager,
772
+ panActivated,
773
+ downX,
774
+ downY,
775
+ holdBroken,
776
+ );
777
+ },
778
+ )
779
+ .onTouchesUp(
780
+ /* istanbul ignore next */ (e, manager) => {
781
+ "worklet";
782
+ delayedPanTouchUp(longPressMs, e, manager, fingerDown, panActivated);
783
+ },
784
+ )
785
+ .onTouchesCancelled(
786
+ /* istanbul ignore next */ () => {
787
+ "worklet";
788
+ delayedPanTouchCancelled(longPressMs, fingerDown);
789
+ },
790
+ )
791
+ // Start scrubbing on ACTIVE (onStart), not on touch-down (onBegin):
792
+ // `activateAfterLongPress` only delays activation, so onBegin still fires
793
+ // immediately — using it would scrub instantly and ignore panGestureDelay,
794
+ // and leave scrubActive stuck for taps that never reach the long-press.
795
+ .onStart(
796
+ /* istanbul ignore next */ (e) => {
797
+ "worklet";
798
+ if (axisScaleActive?.get()) return;
799
+ if (
800
+ !shouldStartDelayedPan(
801
+ longPressMs,
802
+ fingerDown,
803
+ panActivated,
804
+ downAtMs,
805
+ holdBroken,
806
+ scrollActiveSV,
807
+ )
808
+ )
809
+ return;
810
+ if (!enabled) return;
811
+ // Scrub-action: once a reticle is placed, drag adjusts it (2D — the Y is
812
+ // the price). The ephemeral live-scrub never engages, so scrubActive
813
+ // stays false and the live crosshair hides itself.
814
+ if (hasScrubAction && lockActive.get()) {
815
+ lockX.set(
816
+ clampPlotX(
817
+ e.x,
818
+ padding.left,
819
+ canvasWidthSV.get(),
820
+ padding.right,
821
+ ),
744
822
  );
745
- if (p !== null) lockPriceValue.set(p);
823
+ // Freeze the chosen price (value at the pointer Y); the line's Y is
824
+ // re-derived from it so the level stays put in PRICE as the axis rescales.
825
+ {
826
+ const p = computeValueAtY(
827
+ e.y,
828
+ displayMinSV.get(),
829
+ displayMaxSV.get(),
830
+ canvasHeightSV.get(),
831
+ padding.top,
832
+ padding.bottom,
833
+ );
834
+ if (p !== null) lockPriceValue.set(p);
835
+ }
836
+ return;
746
837
  }
747
- return;
748
- }
749
- // Defer to a marker / pressable badge under the finger: don't start a
750
- // live scrub (or drop a crosshair) where the press is routed to an
751
- // overlay tap. `scrubActive` is only set here, so bailing in `onStart`
752
- // also keeps a follow-on drag from showing a crosshair — no `onUpdate`
753
- // guard needed. (Plain-scrub counterpart of the scrub-action tap defer.)
754
- if (deferTapHit !== undefined && deferTapHit(e.x, e.y)) return;
755
- startPlainScrub(
756
- snapCandleX(e.x),
757
- padding,
758
- engine.canvasWidth.get(),
759
- clampPlainScrubToPlot,
760
- scrubX,
761
- scrubActive,
762
- gestureStarted,
763
- );
764
- if (hasOnGestureStart) scheduleOnRN(handleGestureStart);
765
- },
766
- )
767
- .onUpdate(
768
- /* istanbul ignore next */ (e) => {
769
- "worklet";
770
- if (!enabled) return;
771
- if (hasScrubAction && lockActive.get()) {
772
- lockX.set(
773
- clampPlotX(
774
- e.x,
775
- padding.left,
776
- engine.canvasWidth.get(),
777
- padding.right,
778
- ),
838
+ // Defer to a marker / pressable badge under the finger: don't start a
839
+ // live scrub (or drop a crosshair) where the press is routed to an
840
+ // overlay tap. `scrubActive` is only set here, so bailing in `onStart`
841
+ // also keeps a follow-on drag from showing a crosshair — no `onUpdate`
842
+ // guard needed. (Plain-scrub counterpart of the scrub-action tap defer.)
843
+ if (
844
+ deferTapHit !== undefined &&
845
+ deferTapHit(downX.get(), downY.get())
846
+ )
847
+ return;
848
+ startPlainScrub(
849
+ snapCandleX(e.x),
850
+ padding,
851
+ canvasWidthSV.get(),
852
+ clampPlainScrubToPlot,
853
+ scrubX,
854
+ scrubActive,
855
+ gestureStarted,
779
856
  );
780
- // Freeze the chosen price (value at the pointer Y); the line's Y is
781
- // re-derived from it so the level stays put in PRICE as the axis rescales.
782
- {
783
- const p = computeValueAtY(
784
- e.y,
785
- engine.displayMin.get(),
786
- engine.displayMax.get(),
787
- engine.canvasHeight.get(),
788
- padding.top,
789
- padding.bottom,
857
+ if (hasOnGestureStart) scheduleOnRN(handleGestureStart);
858
+ },
859
+ )
860
+ .onUpdate(
861
+ /* istanbul ignore next */ (e) => {
862
+ "worklet";
863
+ if (axisScaleActive?.get()) return;
864
+ if (!enabled) return;
865
+ if (hasScrubAction && lockActive.get()) {
866
+ lockX.set(
867
+ clampPlotX(
868
+ e.x,
869
+ padding.left,
870
+ canvasWidthSV.get(),
871
+ padding.right,
872
+ ),
790
873
  );
791
- if (p !== null) lockPriceValue.set(p);
874
+ // Freeze the chosen price (value at the pointer Y); the line's Y is
875
+ // re-derived from it so the level stays put in PRICE as the axis rescales.
876
+ {
877
+ const p = computeValueAtY(
878
+ e.y,
879
+ displayMinSV.get(),
880
+ displayMaxSV.get(),
881
+ canvasHeightSV.get(),
882
+ padding.top,
883
+ padding.bottom,
884
+ );
885
+ if (p !== null) lockPriceValue.set(p);
886
+ }
887
+ return;
792
888
  }
793
- return;
794
- }
795
- updatePlainScrub(
796
- snapCandleX(e.x),
797
- padding,
798
- engine.canvasWidth.get(),
799
- clampPlainScrubToPlot,
800
- scrubX,
801
- scrubActive,
802
- );
803
- },
804
- )
805
- .onFinalize(
806
- /* istanbul ignore next */ () => {
807
- "worklet";
808
- resetDelayedPanGuard(fingerDown, panActivated, holdBroken);
809
- // A lock-adjust drag leaves the reticle in place (scrubActive was never
810
- // set); a live-scrub clears its crosshair. Always clear scrubActive so a
811
- // stray scrub can never linger behind a placed reticle.
812
- if (scrubActive.get()) {
813
- scrubActive.set(false);
814
- if (hasOnScrub) scheduleOnRN(handleScrubEnd);
815
- }
816
- if (gestureStarted.get()) {
817
- gestureStarted.set(false);
818
- if (hasOnGestureEnd) scheduleOnRN(handleGestureEnd);
819
- }
820
- },
889
+ updatePlainScrub(
890
+ snapCandleX(e.x),
891
+ padding,
892
+ canvasWidthSV.get(),
893
+ clampPlainScrubToPlot,
894
+ scrubX,
895
+ scrubActive,
896
+ );
897
+ },
898
+ )
899
+ .onFinalize(
900
+ /* istanbul ignore next */ () => {
901
+ "worklet";
902
+ resetDelayedPanGuard(fingerDown, panActivated, holdBroken);
903
+ // A lock-adjust drag leaves the reticle in place (scrubActive was never
904
+ // set); a live-scrub clears its crosshair. Always clear scrubActive so a
905
+ // stray scrub can never linger behind a placed reticle.
906
+ if (scrubActive.get()) {
907
+ scrubActive.set(false);
908
+ if (hasOnScrub) scheduleOnRN(handleScrubEnd);
909
+ }
910
+ if (gestureStarted.get()) {
911
+ gestureStarted.set(false);
912
+ if (hasOnGestureEnd) scheduleOnRN(handleGestureEnd);
913
+ }
914
+ },
915
+ );
916
+
917
+ // Passing zero-valued activation modifiers lets RNGH's iOS pan recognizer
918
+ // activate before the axis constraints classify the drag. Only configure a
919
+ // hold when one is requested, and reserve minDistance(0) for scrub-action's
920
+ // deliberate press-hold interaction.
921
+ if (longPressMs > 0) {
922
+ gesture = gesture.activateAfterLongPress(longPressMs);
923
+ }
924
+
925
+ if (hasScrubAction) {
926
+ gesture = gesture.minDistance(0);
927
+ } else {
928
+ // Lock mode needs free vertical drag (Y = price), so the failOffsetY clamp —
929
+ // which would kill a vertical adjust — is applied only outside scrub-action.
930
+ gesture = gesture
931
+ .activeOffsetX([-SCRUB_ACTIVATE_X_PX, SCRUB_ACTIVATE_X_PX])
932
+ .failOffsetY([-SCRUB_FAIL_Y_PX, SCRUB_FAIL_Y_PX]);
933
+ }
934
+
935
+ // Restrict recognition by touch-down position. This rejects before ACTIVE,
936
+ // leaving outside starts to competing/parent gestures. Once accepted,
937
+ // `shouldCancelWhenOutside(false)` keeps tracking beyond these bounds.
938
+ const scrubHitSlop = resolveScrubHitSlop(
939
+ padding,
940
+ clampPlainScrubToPlot,
941
+ scrubBottomExclude,
821
942
  );
943
+ if (scrubHitSlop) gesture = gesture.hitSlop(scrubHitSlop);
822
944
 
823
- // Passing zero-valued activation modifiers lets RNGH's iOS pan recognizer
824
- // activate before the axis constraints classify the drag. Only configure a
825
- // hold when one is requested, and reserve minDistance(0) for scrub-action's
826
- // deliberate press-hold interaction.
827
- if (longPressMs > 0) {
828
- gesture = gesture.activateAfterLongPress(longPressMs);
829
- }
830
-
831
- if (hasScrubAction) {
832
- gesture = gesture.minDistance(0);
833
- } else {
834
- // Lock mode needs free vertical drag (Y = price), so the failOffsetY clamp —
835
- // which would kill a vertical adjust — is applied only outside scrub-action.
836
- gesture = gesture
837
- .activeOffsetX([-SCRUB_ACTIVATE_X_PX, SCRUB_ACTIVATE_X_PX])
838
- .failOffsetY([-SCRUB_FAIL_Y_PX, SCRUB_FAIL_Y_PX]);
839
- }
840
-
841
- // Restrict recognition by touch-down position. This rejects before ACTIVE,
842
- // leaving outside starts to competing/parent gestures. Once accepted,
843
- // `shouldCancelWhenOutside(false)` keeps tracking beyond these bounds.
844
- const scrubHitSlop = resolveScrubHitSlop(
845
- padding,
846
- clampPlainScrubToPlot,
847
- scrubBottomExclude,
848
- );
849
- if (scrubHitSlop) gesture = gesture.hitSlop(scrubHitSlop);
850
-
851
- // Tap: place/move the reticle, press the action badge, or dismiss the lock.
852
- // Composed ahead of the pan by the controller, so a tap is never swallowed.
853
- // Built only in scrub-action mode (the plain-scrub path never constructs a Tap).
854
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
855
- const handleActionTap = (e: { x: number; y: number }, success: boolean) => {
856
- "worklet";
857
- if (!enabled || !hasScrubAction || !success) return;
858
- if (lockActive.get()) {
859
- // 1. Action-badge press → fire onScrubAction with the chosen price.
860
- const rect = actionBadge.get();
861
- if (rect.visible && pointInRect(e.x, e.y, rect, ACTION_HIT_SLOP)) {
862
- const price = lockPrice.get();
863
- if (price !== null) {
864
- let candleJson: string | null = null;
865
- if (isCandleMode) {
866
- const c = lockCandle.get();
867
- if (c) candleJson = JSON.stringify(c);
945
+ // Tap: place/move the reticle, press the action badge, or dismiss the lock.
946
+ // Composed ahead of the pan by the controller, so a tap is never swallowed.
947
+ // Built only in scrub-action mode (the plain-scrub path never constructs a Tap).
948
+ /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
949
+ const handleActionTap = (e: { x: number; y: number }, success: boolean) => {
950
+ "worklet";
951
+ if (!enabled || !hasScrubAction || !success) return;
952
+ if (lockActive.get()) {
953
+ // 1. Action-badge press → fire onScrubAction with the chosen price.
954
+ const rect = actionBadge.get();
955
+ if (rect.visible && pointInRect(e.x, e.y, rect, ACTION_HIT_SLOP)) {
956
+ const price = lockPrice.get();
957
+ if (price !== null) {
958
+ let candleJson: string | null = null;
959
+ if (isCandleMode) {
960
+ const c = lockCandle.get();
961
+ if (c) candleJson = JSON.stringify(c);
962
+ }
963
+ if (hasOnScrubAction)
964
+ scheduleOnRN(
965
+ handleScrubAction,
966
+ price,
967
+ lockTime.get(),
968
+ lockX.get(),
969
+ lockY.get(),
970
+ candleJson,
971
+ );
972
+ // Clear the reticle once the action fires, so no crosshair lingers after
973
+ // the order is placed via the badge.
974
+ if (dismissOnAction) lockActive.set(false);
868
975
  }
869
- if (hasOnScrubAction)
870
- scheduleOnRN(
871
- handleScrubAction,
872
- price,
873
- lockTime.get(),
874
- lockX.get(),
875
- lockY.get(),
876
- candleJson,
877
- );
878
- // Clear the reticle once the action fires, so no crosshair lingers after
879
- // the order is placed via the badge.
880
- if (dismissOnAction) lockActive.set(false);
976
+ return;
977
+ }
978
+ // 2. Dismiss: tap the reticle itself, or any empty tap when configured.
979
+ const onReticle =
980
+ Math.abs(e.x - lockX.get()) <= RETICLE_HIT &&
981
+ Math.abs(e.y - lockY.get()) <= RETICLE_HIT;
982
+ if (onReticle || dismissOnTapOutside) {
983
+ lockActive.set(false);
984
+ return;
881
985
  }
882
- return;
883
- }
884
- // 2. Dismiss: tap the reticle itself, or any empty tap when configured.
885
- const onReticle =
886
- Math.abs(e.x - lockX.get()) <= RETICLE_HIT &&
887
- Math.abs(e.y - lockY.get()) <= RETICLE_HIT;
888
- if (onReticle || dismissOnTapOutside) {
889
- lockActive.set(false);
890
- return;
891
986
  }
892
- }
893
- // 2.5. Defer to a marker / reference-line badge under the tap: when one
894
- // coexists, the tap fires both this handler and that overlay's tap
895
- // (Simultaneous) — yield so it acts instead of dropping a reticle on top.
896
- if (deferTapHit !== undefined && deferTapHit(e.x, e.y)) return;
897
- // 3. Place / move the reticle. Clear any in-progress live-scrub so its
898
- // crosshair never shows behind the placed reticle.
899
- scrubActive.set(false);
900
- lockX.set(
901
- clampPlotX(e.x, padding.left, engine.canvasWidth.get(), padding.right),
902
- );
903
- // Freeze the chosen price at the tap Y; the line's Y derives from it (see
904
- // `lockY`). No-op until the canvas is laid out (price === null).
905
- const placedPrice = computeValueAtY(
906
- e.y,
907
- engine.displayMin.get(),
908
- engine.displayMax.get(),
909
- engine.canvasHeight.get(),
910
- padding.top,
911
- padding.bottom,
912
- );
913
- if (placedPrice === null) return;
914
- lockPriceValue.set(placedPrice);
915
- lockActive.set(true);
916
- };
987
+ // 2.5. Defer to a marker / reference-line badge under the tap: when one
988
+ // coexists, the tap fires both this handler and that overlay's tap
989
+ // (Simultaneous) — yield so it acts instead of dropping a reticle on top.
990
+ if (deferTapHit !== undefined && deferTapHit(e.x, e.y)) return;
991
+ // 3. Place / move the reticle. Clear any in-progress live-scrub so its
992
+ // crosshair never shows behind the placed reticle.
993
+ scrubActive.set(false);
994
+ lockX.set(
995
+ clampPlotX(e.x, padding.left, canvasWidthSV.get(), padding.right),
996
+ );
997
+ // Freeze the chosen price at the tap Y; the line's Y derives from it (see
998
+ // `lockY`). No-op until the canvas is laid out (price === null).
999
+ const placedPrice = computeValueAtY(
1000
+ e.y,
1001
+ displayMinSV.get(),
1002
+ displayMaxSV.get(),
1003
+ canvasHeightSV.get(),
1004
+ padding.top,
1005
+ padding.bottom,
1006
+ );
1007
+ if (placedPrice === null) return;
1008
+ lockPriceValue.set(placedPrice);
1009
+ lockActive.set(true);
1010
+ };
917
1011
 
918
- let tapGesture = hasScrubAction
919
- ? Gesture.Tap()
920
- .maxDuration(250)
921
- .maxDistance(SCRUB_ACTION_TAP_SLOP)
922
- .onEnd(handleActionTap)
923
- : undefined;
924
- // Keep the bottom axis scroll-only. Do not apply horizontal plot bounds here:
925
- // the action badge is intentionally tappable in the right gutter.
926
- const tapHitSlop = resolveScrubHitSlop(padding, false, scrubBottomExclude);
927
- if (tapGesture && tapHitSlop) tapGesture = tapGesture.hitSlop(tapHitSlop);
1012
+ let tapGesture = hasScrubAction
1013
+ ? Gesture.Tap()
1014
+ .maxDuration(250)
1015
+ .maxDistance(SCRUB_ACTION_TAP_SLOP)
1016
+ .onEnd(handleActionTap)
1017
+ : undefined;
1018
+ // Keep the bottom axis scroll-only. Do not apply horizontal plot bounds here:
1019
+ // the action badge is intentionally tappable in the right gutter.
1020
+ const tapHitSlop = resolveScrubHitSlop(padding, false, scrubBottomExclude);
1021
+ if (tapGesture && tapHitSlop) tapGesture = tapGesture.hitSlop(tapHitSlop);
1022
+ return { gesture, tapGesture };
1023
+ }, [
1024
+ actionBadge,
1025
+ axisScaleActive,
1026
+ candleWidthSecs,
1027
+ candlesSV,
1028
+ canvasHeightSV,
1029
+ canvasWidthSV,
1030
+ chartGaps,
1031
+ clampPlainScrubToPlot,
1032
+ deferTapHit,
1033
+ dismissOnAction,
1034
+ dismissOnTapOutside,
1035
+ displayMaxSV,
1036
+ displayMinSV,
1037
+ displayWindowSV,
1038
+ downAtMs,
1039
+ downX,
1040
+ downY,
1041
+ enabled,
1042
+ fingerDown,
1043
+ gestureStarted,
1044
+ handleGestureEnd,
1045
+ handleGestureStart,
1046
+ handleScrubAction,
1047
+ handleScrubEnd,
1048
+ hasOnGestureEnd,
1049
+ hasOnGestureStart,
1050
+ hasOnScrub,
1051
+ hasOnScrubAction,
1052
+ hasScrubAction,
1053
+ holdBroken,
1054
+ isCandleMode,
1055
+ liveCandleSV,
1056
+ lockActive,
1057
+ lockCandle,
1058
+ lockPrice,
1059
+ lockPriceValue,
1060
+ lockTime,
1061
+ lockX,
1062
+ lockY,
1063
+ longPressMs,
1064
+ padding,
1065
+ panActivated,
1066
+ scrollActiveSV,
1067
+ scrubActive,
1068
+ scrubBottomExclude,
1069
+ scrubX,
1070
+ snapToCandles,
1071
+ timestampSV,
1072
+ ]);
928
1073
 
929
1074
  return {
1075
+ scrubMarkers,
930
1076
  scrubX,
931
1077
  scrubActive,
932
1078
  scrubTime,