react-native-livechart 4.26.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 (115) 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 +4 -0
  15. package/dist/core/liveChartEngineTick.d.ts.map +1 -1
  16. package/dist/core/liveChartSeriesEngineTick.d.ts +2 -0
  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 +7 -0
  21. package/dist/core/useLiveChartEngine.d.ts.map +1 -1
  22. package/dist/core/useLiveChartSeriesEngine.d.ts +2 -0
  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 +16 -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 +20 -5
  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/useReferenceLinePress.d.ts +4 -2
  52. package/dist/hooks/useReferenceLinePress.d.ts.map +1 -1
  53. package/dist/hooks/useReferenceTagStack.d.ts +31 -0
  54. package/dist/hooks/useReferenceTagStack.d.ts.map +1 -0
  55. package/dist/hooks/useScrubMarkers.d.ts +12 -0
  56. package/dist/hooks/useScrubMarkers.d.ts.map +1 -0
  57. package/dist/hooks/useXAxis.d.ts +1 -1
  58. package/dist/hooks/useXAxis.d.ts.map +1 -1
  59. package/dist/hooks/useYAxisScaleGesture.d.ts +34 -0
  60. package/dist/hooks/useYAxisScaleGesture.d.ts.map +1 -0
  61. package/dist/index.d.ts +1 -1
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/math/axisBadgeLayout.d.ts +16 -0
  64. package/dist/math/axisBadgeLayout.d.ts.map +1 -0
  65. package/dist/math/markers.d.ts +3 -0
  66. package/dist/math/markers.d.ts.map +1 -1
  67. package/dist/math/range.d.ts +1 -1
  68. package/dist/math/range.d.ts.map +1 -1
  69. package/dist/math/referenceTagStack.d.ts +36 -0
  70. package/dist/math/referenceTagStack.d.ts.map +1 -0
  71. package/dist/math/scrubMarkers.d.ts +12 -0
  72. package/dist/math/scrubMarkers.d.ts.map +1 -0
  73. package/dist/types.d.ts +202 -32
  74. package/dist/types.d.ts.map +1 -1
  75. package/package.json +1 -1
  76. package/src/components/CustomReferenceLineOverlay.tsx +50 -8
  77. package/src/components/CustomTooltipOverlay.tsx +5 -1
  78. package/src/components/LiveChart.tsx +560 -138
  79. package/src/components/LiveChartSeries.tsx +113 -13
  80. package/src/components/ReferenceLineOverlay.tsx +62 -26
  81. package/src/components/ReferenceValueBadge.tsx +102 -0
  82. package/src/components/XAxisOverlay.tsx +104 -1
  83. package/src/core/liveChartEngineTick.ts +49 -4
  84. package/src/core/liveChartSeriesEngineTick.ts +37 -0
  85. package/src/core/resolveConfig.ts +95 -2
  86. package/src/core/useLiveChartEngine.ts +43 -9
  87. package/src/core/useLiveChartSeriesEngine.ts +5 -0
  88. package/src/draw/grid.ts +2 -2
  89. package/src/draw/line.ts +23 -1
  90. package/src/hooks/crosshairShared.ts +56 -1
  91. package/src/hooks/referenceBadgeFont.ts +35 -0
  92. package/src/hooks/resolveChartLayout.ts +17 -0
  93. package/src/hooks/useBadge.ts +44 -5
  94. package/src/hooks/useChartSkiaFont.ts +1 -1
  95. package/src/hooks/useCrosshair.ts +444 -310
  96. package/src/hooks/useCrosshairSeries.ts +29 -10
  97. package/src/hooks/useLatestCallback.ts +15 -0
  98. package/src/hooks/useMarkers.ts +51 -39
  99. package/src/hooks/usePanScroll.ts +155 -127
  100. package/src/hooks/usePinchZoom.ts +84 -63
  101. package/src/hooks/useReferenceDrag.ts +390 -155
  102. package/src/hooks/useReferenceLine.ts +453 -277
  103. package/src/hooks/useReferenceLineGrouping.ts +1 -1
  104. package/src/hooks/useReferenceLinePress.ts +121 -53
  105. package/src/hooks/useReferenceTagStack.ts +81 -0
  106. package/src/hooks/useScrubMarkers.ts +79 -0
  107. package/src/hooks/useXAxis.ts +11 -2
  108. package/src/hooks/useYAxisScaleGesture.ts +263 -0
  109. package/src/index.ts +10 -0
  110. package/src/math/axisBadgeLayout.ts +37 -0
  111. package/src/math/markers.ts +28 -3
  112. package/src/math/range.ts +35 -0
  113. package/src/math/referenceTagStack.ts +112 -0
  114. package/src/math/scrubMarkers.ts +55 -0
  115. package/src/types.ts +207 -32
@@ -1,3 +1,5 @@
1
+ import { stackedTagAt, type ReferenceTagStack } from "../math/referenceTagStack";
2
+ import { useCallback, useMemo } from "react";
1
3
  import { Gesture } from "react-native-gesture-handler";
2
4
  import {
3
5
  useAnimatedReaction,
@@ -18,7 +20,14 @@ import {
18
20
  } from "../math/referenceDrag";
19
21
  import { referenceLineForm } from "../math/referenceLines";
20
22
  import type { ReferenceLine } from "../types";
21
- import { computeValueAtY, pinnedPlotY, snapPrice } from "./crosshairShared";
23
+ import {
24
+ computeValueAtY,
25
+ pinnedPlotY,
26
+ snapPriceOutward,
27
+ snapPriceWithin,
28
+ } from "./crosshairShared";
29
+
30
+ import { useLatestCallback } from "./useLatestCallback";
22
31
 
23
32
  /** Vertical reach (px) around a line within which a touch grabs it. */
24
33
  const GRAB_SLOP = 14;
@@ -29,13 +38,52 @@ const DRAG_ACTIVATE_PX = 4;
29
38
  * (and the onDragIn/Out reaction never fires) when the feature is unused. */
30
39
  const EMPTY: never[] = [];
31
40
 
41
+ /**
42
+ * The value a drag at canvas `y` gives `line` against the current range: the
43
+ * price at that Y, snapped to `line.snap` and clamped to `line.bounds`. Null
44
+ * before the canvas is laid out. `atFinger`: the value the finger sets (what the
45
+ * range fits and `onChange` reports) — the nearest increment, rounded outward
46
+ * once the finger is past the plot's edge so even a coarse `snap` can widen the
47
+ * range. Otherwise the value the line is drawn and committed at — rounded inward
48
+ * at the edges, so the line stays on the plot.
49
+ */
50
+ function dragValueAtY(
51
+ line: ReferenceLine,
52
+ y: number,
53
+ displayMin: number,
54
+ displayMax: number,
55
+ canvasHeight: number,
56
+ padTop: number,
57
+ padBottom: number,
58
+ atFinger: boolean,
59
+ ): number | null {
60
+ "worklet";
61
+ const raw = computeValueAtY(
62
+ y,
63
+ displayMin,
64
+ displayMax,
65
+ canvasHeight,
66
+ padTop,
67
+ padBottom,
68
+ );
69
+ if (raw === null) return null;
70
+ const snapped = atFinger
71
+ ? snapPriceOutward(
72
+ raw,
73
+ line.snap,
74
+ y < padTop ? 1 : y > canvasHeight - padBottom ? -1 : 0,
75
+ )
76
+ : snapPriceWithin(raw, line.snap, displayMin, displayMax);
77
+ return clampToBounds(snapped, line.bounds);
78
+ }
79
+
32
80
  /**
33
81
  * Builds the per-line **drag** gesture for draggable Form-A reference lines: grab a
34
82
  * line near its value-Y and drag vertically to set a new value, with optional
35
83
  * `snap` + `bounds` clamp. Mirrors the order-ticket reticle in {@link useCrosshair}
36
84
  * (value↔Y via `computeValueAtY` / `pinnedPlotY`, frozen value re-projected
37
- * each frame) but per line, writing into the shared `dragValues` array the layout
38
- * and overlays read.
85
+ * each frame) but per line, writing the value the finger sets into the shared
86
+ * `dragValues` array (what the range fit reads). The overlays read `drawnValues`.
39
87
  *
40
88
  * The pan uses `manualActivation`: it grabs only when a touch starts within
41
89
  * {@link GRAB_SLOP} of a draggable line (and inside its `grabRange`, when it has
@@ -46,8 +94,18 @@ const EMPTY: never[] = [];
46
94
  * falls through to scrub on a horizontal start, so a drag begun on a line always
47
95
  * wins the race rather than dropping a scrub crosshair (#163).
48
96
  *
49
- * Also fires the per-line drag callbacks: `onChange` (de-duped during drag),
50
- * `onCommit` (on release), and `onDragIn` / `onDragOut` (value crossing the visible
97
+ * The dragged line stays under the finger: `drawnValues` re-maps it from the
98
+ * finger's last Y whenever the range changes, or the plot is resized, while the
99
+ * finger rests. `dragValues` keeps the value the finger last set. If another line
100
+ * takes the dragged line's index mid-drag (lines added or removed before it, told
101
+ * apart by `id`), the drag lets go: it moves and commits nothing, and
102
+ * gives that line back its own value immediately. This touch stays cancelled if
103
+ * the original line later returns to the grabbed index.
104
+ *
105
+ * Also fires the per-line drag callbacks: `onChange` (as the finger moves the
106
+ * line, de-duped to value changes, and once more on release if the line was
107
+ * drawn elsewhere), `onCommit` (on release, with the value under the finger),
108
+ * and `onDragIn` / `onDragOut` (drawn value crossing the visible
51
109
  * range or a `bounds`, from a drag or the axis rescaling — edge-detected each frame).
52
110
  */
53
111
  export function useReferenceDrag(
@@ -57,11 +115,16 @@ export function useReferenceDrag(
57
115
  dragValues: SharedValue<number[]>,
58
116
  dragActive: SharedValue<boolean[]>,
59
117
  enabled: boolean,
118
+ tagStack?: SharedValue<ReferenceTagStack>,
60
119
  ): {
61
120
  gesture: ReturnType<typeof Gesture.Pan>;
62
121
  /** True when a touch at (x,y) would grab a draggable line — lets the scrub
63
122
  * gesture decline that press so it never drops a crosshair on a line (#163). */
64
123
  hitTest: (x: number, y: number) => boolean;
124
+ /** `dragValues` with the line being dragged kept under the finger, re-mapped
125
+ * against the current range. What the lines' overlays, grouping and press
126
+ * hit-test read; the range fit reads `dragValues`. */
127
+ drawnValues: SharedValue<number[]>;
65
128
  } {
66
129
  const anyDraggable =
67
130
  enabled &&
@@ -72,6 +135,10 @@ export function useReferenceDrag(
72
135
  (l.onDragIn != null || l.onDragOut != null),
73
136
  );
74
137
 
138
+ // The gesture retains this container while committed line props change.
139
+ // Reanimated updates it on the UI thread without rebuilding the recognizer.
140
+ const linesSV = useDerivedValue(() => lines);
141
+
75
142
  // Per-line handle Y (canvas px), index-aligned with `lines`; -1 when the line
76
143
  // isn't draggable or the canvas isn't laid out. Off-screen lines pin to the
77
144
  // nearest plot edge so they stay grabbable. Recomputed each frame (UI thread).
@@ -85,8 +152,8 @@ export function useReferenceDrag(
85
152
  const bottom = ch - padding.bottom;
86
153
  if (bottom <= top) return EMPTY;
87
154
  const out: number[] = [];
88
- for (let i = 0; i < lines.length; i++) {
89
- const l = lines[i];
155
+ for (let i = 0; i < linesSV.get().length; i++) {
156
+ const l = linesSV.get()[i];
90
157
  if (
91
158
  !l.draggable ||
92
159
  referenceLineForm(l) !== "line" ||
@@ -103,32 +170,124 @@ export function useReferenceDrag(
103
170
 
104
171
  // Per-line grab x-ranges (`ReferenceLine.grabRange`), index-aligned with
105
172
  // `lines`; null = anywhere along the line. Read by the gesture worklets below,
106
- // which close over this render's `lines` as the callbacks do.
107
- const grabRanges = lines.map((l) => l.grabRange ?? null);
173
+ // through shared values so line updates do not rebuild the recognizer.
174
+ const grabRanges = useDerivedValue(() => {
175
+ const ranges: (ReferenceLine["grabRange"] | null)[] = [];
176
+ for (const line of linesSV.get()) ranges.push(line.grabRange ?? null);
177
+ return ranges;
178
+ });
108
179
 
109
180
  const dragIndex = useSharedValue(-1);
110
181
  const startX = useSharedValue(0);
111
182
  const startY = useSharedValue(0);
183
+ const pointerOffsetY = useSharedValue(0);
112
184
  const activated = useSharedValue(false);
113
185
  const lastChange = useSharedValue(0);
186
+ // The finger's latest Y during a drag, and the grabbed line's `id`: lines are
187
+ // index-aligned, so a line added or removed before it mid-drag hands its index
188
+ // to another line, and the drag must not move that one.
189
+ const lastY = useSharedValue(0);
190
+ const grabbedId = useSharedValue<string | undefined>(undefined);
191
+ const { displayMin, displayMax, canvasHeight } = engine;
192
+
193
+ // The dragged line under the finger. The range (live data, a range animation)
194
+ // can move, or the plot be resized, while the finger rests, and the gesture maps
195
+ // the finger only when it moves, so re-map from its last Y here. Being a
196
+ // derived value, everything that reads it is ordered after it and draws this
197
+ // frame's value. `dragValues` keeps the value the finger last set: the range
198
+ // fits that one (fitting this one would let a line held near the plot edge push
199
+ // the range out every frame) and `onChange` reports it.
200
+ const drawnValues = useDerivedValue<number[]>(() => {
201
+ const values = dragValues.get();
202
+ const i = dragIndex.get();
203
+ if (i < 0 || !activated.get()) return values;
204
+ const l = linesSV.get()[i];
205
+ if (l === undefined || l.id !== grabbedId.get()) return values;
206
+ const v = dragValueAtY(
207
+ l,
208
+ lastY.get(),
209
+ displayMin.get(),
210
+ displayMax.get(),
211
+ canvasHeight.get(),
212
+ padding.top,
213
+ padding.bottom,
214
+ false,
215
+ );
216
+ if (v === null || v === values[i]) return values;
217
+ const out = values.slice();
218
+ out[i] = v;
219
+ return out;
220
+ });
114
221
 
115
- // JS-thread callback dispatch — closes over the latest `lines` each render.
222
+ // Once the grabbed identity is lost, this touch cannot resume a drag if that
223
+ // line later returns. Restore a replacement's own value and clear ownership.
224
+ const clearDrag = useCallback(() => {
225
+ "worklet";
226
+ const i = dragIndex.get();
227
+ const l = linesSV.get()[i];
228
+ if (i >= 0 && activated.get()) {
229
+ if (l !== undefined && l.id !== grabbedId.get()) {
230
+ const own = l.value ?? 0;
231
+ const values = dragValues.get();
232
+ if (i < values.length && values[i] !== own) {
233
+ const restored = values.slice();
234
+ restored[i] = own;
235
+ dragValues.set(restored);
236
+ }
237
+ }
238
+ const active = dragActive.get();
239
+ if (i < active.length && active[i]) {
240
+ const next = active.slice();
241
+ next[i] = false;
242
+ dragActive.set(next);
243
+ }
244
+ }
245
+ dragIndex.set(-1);
246
+ activated.set(false);
247
+ }, [
248
+ activated,
249
+ dragActive,
250
+ dragIndex,
251
+ dragValues,
252
+ grabbedId,
253
+ linesSV,
254
+ ]);
255
+
256
+ // Reordering/removal must also cancel while the finger is resting. Derived
257
+ // drawing alone cannot reset the gesture state or the range-fit override.
258
+ useAnimatedReaction(
259
+ () => {
260
+ const i = dragIndex.get();
261
+ const l = linesSV.get()[i];
262
+ return i >= 0 && (l === undefined || l.id !== grabbedId.get());
263
+ },
264
+ (invalid) => {
265
+ if (invalid) clearDrag();
266
+ },
267
+ );
268
+
269
+ // JS dispatchers always use the latest committed callback, and reject an
270
+ // event queued for a line that has since been replaced.
116
271
  /* istanbul ignore next -- runs via scheduleOnRN from the UI-thread gesture */
117
- function emitChange(i: number, v: number) {
118
- lines[i]?.onChange?.(v);
119
- }
272
+ const emitChange = useLatestCallback((i: number, id: string | undefined, v: number) => {
273
+ const line = lines[i];
274
+ if (line && line.id === id) line.onChange?.(v);
275
+ });
120
276
  /* istanbul ignore next -- runs via scheduleOnRN from the UI-thread gesture */
121
- function emitCommit(i: number, v: number) {
122
- lines[i]?.onCommit?.(v);
123
- }
277
+ const emitCommit = useLatestCallback((i: number, id: string | undefined, v: number) => {
278
+ const line = lines[i];
279
+ if (line && line.id === id) line.onCommit?.(v);
280
+ });
124
281
  /* istanbul ignore next -- runs via scheduleOnRN from the UI-thread reaction */
125
- function emitDragOut(i: number, v: number) {
126
- lines[i]?.onDragOut?.(v);
127
- }
282
+ const emitDragOut = useLatestCallback((i: number, id: string | undefined, v: number) => {
283
+ const line = lines[i];
284
+ if (line && line.id === id) line.onDragOut?.(v);
285
+ });
128
286
  /* istanbul ignore next -- runs via scheduleOnRN from the UI-thread reaction */
129
- function emitDragIn(i: number, v: number) {
130
- lines[i]?.onDragIn?.(v);
131
- }
287
+ const emitDragIn = useLatestCallback((i: number, id: string | undefined, v: number) => {
288
+ const line = lines[i];
289
+ if (line && line.id === id) line.onDragIn?.(v);
290
+ });
132
291
 
133
292
  // onDragIn / onDragOut — edge-detect each line's "out of the watched interval"
134
293
  // state (from a drag or the axis rescaling under a fixed value).
@@ -139,8 +298,8 @@ export function useReferenceDrag(
139
298
  const dMin = engine.displayMin.get();
140
299
  const dMax = engine.displayMax.get();
141
300
  const out: boolean[] = [];
142
- for (let i = 0; i < lines.length; i++) {
143
- const l = lines[i];
301
+ for (let i = 0; i < linesSV.get().length; i++) {
302
+ const l = linesSV.get()[i];
144
303
  if (
145
304
  referenceLineForm(l) !== "line" ||
146
305
  l.value === undefined ||
@@ -149,7 +308,7 @@ export function useReferenceDrag(
149
308
  out.push(false);
150
309
  continue;
151
310
  }
152
- const v = dragValues.get()[i] ?? l.value;
311
+ const v = drawnValues.get()[i] ?? l.value;
153
312
  out.push(referenceValueOut(v, dMin, dMax, l.bounds));
154
313
  }
155
314
  return out;
@@ -159,145 +318,221 @@ export function useReferenceDrag(
159
318
  if (!prev || curr === prev) return;
160
319
  for (let i = 0; i < curr.length; i++) {
161
320
  if (prev[i] === undefined || curr[i] === prev[i]) continue;
162
- const l = lines[i];
163
- const v = dragValues.get()[i] ?? l.value ?? 0;
164
- if (curr[i]) scheduleOnRN(emitDragOut, i, v);
165
- else scheduleOnRN(emitDragIn, i, v);
321
+ const l = linesSV.get()[i];
322
+ const v = drawnValues.get()[i] ?? l.value ?? 0;
323
+ if (curr[i]) scheduleOnRN(emitDragOut, i, l.id, v);
324
+ else scheduleOnRN(emitDragIn, i, l.id, v);
166
325
  }
167
326
  },
168
- [lines, anyDragInOut],
169
327
  );
170
328
 
171
- // ── Gesture callbacks (UI-thread worklets — excluded from Jest coverage) ──────
172
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
173
- const onTouchesDown = (
174
- e: { changedTouches: { x: number; y: number }[] },
175
- manager: { fail: () => void },
176
- ) => {
177
- "worklet";
178
- const t = e.changedTouches[0];
179
- if (!t) return;
180
- const i = nearestDraggableIndex(handleYs.get(), t.y, GRAB_SLOP, {
181
- x: t.x,
182
- ranges: grabRanges,
183
- });
184
- if (i < 0) {
185
- manager.fail();
186
- return;
187
- }
188
- dragIndex.set(i);
189
- startX.set(t.x);
190
- startY.set(t.y);
191
- };
192
-
193
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
194
- const onTouchesMove = (
195
- e: { allTouches: { x: number; y: number }[] },
196
- manager: { activate: () => void },
197
- ) => {
198
- "worklet";
199
- if (dragIndex.get() < 0) return;
200
- const t = e.allTouches[0];
201
- if (!t) return;
202
- // A line is grabbed → it owns the touch: any drag past the threshold drags it.
203
- // We don't fail on horizontal intent (which used to hand the touch to scrub) —
204
- // that let the scrub crosshair win the race even on a vertical drag started on
205
- // the line (#163). Scrub / scroll still run off a line's grab band.
206
- const intent = resolveDragIntent(
207
- t.x - startX.get(),
208
- t.y - startY.get(),
209
- DRAG_ACTIVATE_PX,
210
- );
211
- if (intent === "activate") manager.activate();
212
- };
213
-
214
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
215
- const onStart = () => {
216
- "worklet";
217
- const i = dragIndex.get();
218
- if (i < 0) return;
219
- activated.set(true);
220
- const arr = dragActive.get().slice();
221
- arr[i] = true;
222
- dragActive.set(arr);
223
- lastChange.set(dragValues.get()[i] ?? lines[i].value ?? 0);
224
- };
329
+ const { gesture, hitTest } = useMemo(() => {
330
+ const canDragTag = (index: number, x: number) => {
331
+ "worklet";
332
+ if (!linesSV.get()[index]?.draggable) return false;
333
+ const range = grabRanges.get()[index];
334
+ return !range || (x >= range[0] && x <= range[1]);
335
+ };
336
+ // ── Gesture callbacks (UI-thread worklets — excluded from Jest coverage) ──────
337
+ /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
338
+ const onTouchesDown = (
339
+ e: { changedTouches: { x: number; y: number }[] },
340
+ manager: { fail: () => void },
341
+ ) => {
342
+ "worklet";
343
+ const t = e.changedTouches[0];
344
+ if (!t) return;
345
+ const tag = stackedTagAt(tagStack?.get().tags ?? [], t.x, t.y);
346
+ const i = tag ? (linesSV.get()[tag.index]?.id === tag.lineId && canDragTag(tag.index, t.x) ? tag.index : -1) : nearestDraggableIndex(handleYs.get(), t.y, GRAB_SLOP, {
347
+ x: t.x,
348
+ ranges: grabRanges.get(),
349
+ });
350
+ const l = linesSV.get()[i];
351
+ if (l === undefined) {
352
+ manager.fail();
353
+ return;
354
+ }
355
+ pointerOffsetY.set(tag ? t.y - tag.lineY : 0);
356
+ dragIndex.set(i);
357
+ grabbedId.set(l.id);
358
+ startX.set(t.x);
359
+ startY.set(t.y);
360
+ };
225
361
 
226
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
227
- const onUpdate = (e: { y: number }) => {
228
- "worklet";
229
- const i = dragIndex.get();
230
- if (i < 0) return;
231
- const l = lines[i];
232
- const raw = computeValueAtY(
233
- e.y,
234
- engine.displayMin.get(),
235
- engine.displayMax.get(),
236
- engine.canvasHeight.get(),
237
- padding.top,
238
- padding.bottom,
239
- );
240
- if (raw === null) return;
241
- const v = clampToBounds(snapPrice(raw, l.snap), l.bounds);
242
- const arr = dragValues.get().slice();
243
- arr[i] = v;
244
- dragValues.set(arr);
245
- if (v !== lastChange.get()) {
246
- lastChange.set(v);
247
- if (l.onChange) scheduleOnRN(emitChange, i, v);
248
- }
249
- };
362
+ /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
363
+ const onTouchesMove = (
364
+ e: { allTouches: { x: number; y: number }[] },
365
+ manager: { activate: () => void },
366
+ ) => {
367
+ "worklet";
368
+ if (dragIndex.get() < 0) return;
369
+ const t = e.allTouches[0];
370
+ if (!t) return;
371
+ // A line is grabbed → it owns the touch: any drag past the threshold drags it.
372
+ // We don't fail on horizontal intent (which used to hand the touch to scrub) —
373
+ // that let the scrub crosshair win the race even on a vertical drag started on
374
+ // the line (#163). Scrub / scroll still run off a line's grab band.
375
+ const intent = resolveDragIntent(
376
+ t.x - startX.get(),
377
+ t.y - startY.get(),
378
+ DRAG_ACTIVATE_PX,
379
+ );
380
+ if (intent === "activate") manager.activate();
381
+ };
250
382
 
251
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
252
- const onFinalize = () => {
253
- "worklet";
254
- const i = dragIndex.get();
255
- if (i >= 0 && activated.get()) {
256
- const v = dragValues.get()[i] ?? lines[i].value ?? 0;
383
+ /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
384
+ const onStart = (e: { y: number }) => {
385
+ "worklet";
386
+ const i = dragIndex.get();
387
+ const l = linesSV.get()[i];
388
+ // The line may be gone, or another have taken its index, since the grab.
389
+ if (l === undefined || l.id !== grabbedId.get()) {
390
+ clearDrag();
391
+ return;
392
+ }
393
+ lastY.set(e.y - pointerOffsetY.get());
394
+ activated.set(true);
257
395
  const arr = dragActive.get().slice();
258
- arr[i] = false;
396
+ arr[i] = true;
259
397
  dragActive.set(arr);
260
- if (lines[i].onCommit) scheduleOnRN(emitCommit, i, v);
261
- }
262
- dragIndex.set(-1);
263
- activated.set(false);
264
- };
398
+ lastChange.set(dragValues.get()[i] ?? l.value ?? 0);
399
+ };
265
400
 
266
- // Hit-test shared with the scrub gesture: does this drag own the touch? The
267
- // scrub's `onStart` consults it to bail, so it never drops a crosshair on a
268
- // line even though it activates independently of this manual-activation pan
269
- // (the `Exclusive` priority alone doesn't hold scrub back — not while this
270
- // gesture is merely pressed, and not while it is dragging either). Once a line
271
- // is grabbed the answer is yes regardless of position: the scrub asks with its
272
- // touch-DOWN point, which a longer drag has carried the line away from, so the
273
- // geometric test alone said "no line here" and a crosshair opened mid-drag.
274
- // Otherwise it is the y-reach around the handles, along the whole line — or,
275
- // for a line with a `grabRange`, only inside it.
276
- /* istanbul ignore next -- worklet, runs on the UI thread */
277
- const hitTest = (x: number, y: number): boolean => {
278
- "worklet";
279
- if (!anyDraggable) return false;
280
- return referenceDragOwnsTouch(
281
- dragIndex.get(),
282
- handleYs.get(),
283
- y,
284
- GRAB_SLOP,
285
- {
286
- x,
287
- ranges: grabRanges,
288
- },
289
- );
290
- };
401
+ /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
402
+ const onUpdate = (e: { y: number }) => {
403
+ "worklet";
404
+ const i = dragIndex.get();
405
+ // Not after an `onStart` that let go (the line gone or replaced by then).
406
+ if (i < 0 || !activated.get()) return;
407
+ lastY.set(e.y - pointerOffsetY.get());
408
+ const l = linesSV.get()[i];
409
+ if (l === undefined || l.id !== grabbedId.get()) {
410
+ clearDrag();
411
+ return;
412
+ }
413
+ const v = dragValueAtY(
414
+ l,
415
+ e.y - pointerOffsetY.get(),
416
+ displayMin.get(),
417
+ displayMax.get(),
418
+ canvasHeight.get(),
419
+ padding.top,
420
+ padding.bottom,
421
+ true,
422
+ );
423
+ if (v === null) return;
424
+ const arr = dragValues.get().slice();
425
+ arr[i] = v;
426
+ dragValues.set(arr);
427
+ if (v !== lastChange.get()) {
428
+ lastChange.set(v);
429
+ if (l.onChange) scheduleOnRN(emitChange, i, l.id, v);
430
+ }
431
+ };
432
+
433
+ /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
434
+ const onFinalize = () => {
435
+ "worklet";
436
+ const i = dragIndex.get();
437
+ const l = linesSV.get()[i];
438
+ if (i >= 0 && activated.get() && l !== undefined) {
439
+ if (l.id === grabbedId.get()) {
440
+ // Commit what is drawn: the range may have moved under a still finger
441
+ // since its last move. It stays there once released.
442
+ const v =
443
+ dragValueAtY(
444
+ l,
445
+ lastY.get(),
446
+ displayMin.get(),
447
+ displayMax.get(),
448
+ canvasHeight.get(),
449
+ padding.top,
450
+ padding.bottom,
451
+ false,
452
+ ) ??
453
+ dragValues.get()[i] ??
454
+ l.value ??
455
+ 0;
456
+ if (dragValues.get()[i] !== v) {
457
+ const values = dragValues.get().slice();
458
+ values[i] = v;
459
+ dragValues.set(values);
460
+ }
461
+ // An `onChange`-only consumer ends where the line is drawn.
462
+ if (v !== lastChange.get() && l.onChange) {
463
+ scheduleOnRN(emitChange, i, l.id, v);
464
+ }
465
+ if (l.onCommit) scheduleOnRN(emitCommit, i, l.id, v);
466
+ }
467
+ }
468
+ clearDrag();
469
+ };
470
+
471
+ // Hit-test shared with the scrub gesture: does this drag own the touch? The
472
+ // scrub's `onStart` consults it to bail, so it never drops a crosshair on a
473
+ // line even though it activates independently of this manual-activation pan
474
+ // (the `Exclusive` priority alone doesn't hold scrub back — not while this
475
+ // gesture is merely pressed, and not while it is dragging either). Once a line
476
+ // is grabbed the answer is yes regardless of position: the scrub asks with its
477
+ // touch-DOWN point, which a longer drag has carried the line away from, so the
478
+ // geometric test alone said "no line here" and a crosshair opened mid-drag.
479
+ // Otherwise it is the y-reach around the handles, along the whole line — or,
480
+ // for a line with a `grabRange`, only inside it.
481
+ /* istanbul ignore next -- worklet, runs on the UI thread */
482
+ const hitTest = (x: number, y: number): boolean => {
483
+ "worklet";
484
+ if (!anyDraggable) return false;
485
+ if (dragIndex.get() >= 0) return true;
486
+ const tag = stackedTagAt(tagStack?.get().tags ?? [], x, y);
487
+ if (tag) return linesSV.get()[tag.index]?.id === tag.lineId && canDragTag(tag.index, x);
488
+ return referenceDragOwnsTouch(
489
+ dragIndex.get(),
490
+ handleYs.get(),
491
+ y,
492
+ GRAB_SLOP,
493
+ {
494
+ x,
495
+ ranges: grabRanges.get(),
496
+ },
497
+ );
498
+ };
499
+
500
+ const gesture = Gesture.Pan()
501
+ .enabled(anyDraggable)
502
+ .maxPointers(1)
503
+ .manualActivation(true)
504
+ .onTouchesDown(onTouchesDown)
505
+ .onTouchesMove(onTouchesMove)
506
+ .onStart(onStart)
507
+ .onUpdate(onUpdate)
508
+ .onFinalize(onFinalize);
291
509
 
292
- const gesture = Gesture.Pan()
293
- .enabled(anyDraggable)
294
- .maxPointers(1)
295
- .manualActivation(true)
296
- .onTouchesDown(onTouchesDown)
297
- .onTouchesMove(onTouchesMove)
298
- .onStart(onStart)
299
- .onUpdate(onUpdate)
300
- .onFinalize(onFinalize);
510
+ return { gesture, hitTest };
511
+ }, [
512
+ activated,
513
+ anyDraggable,
514
+ canvasHeight,
515
+ clearDrag,
516
+ displayMax,
517
+ displayMin,
518
+ dragActive,
519
+ dragIndex,
520
+ dragValues,
521
+ emitChange,
522
+ emitCommit,
523
+ grabRanges,
524
+ grabbedId,
525
+ handleYs,
526
+ lastChange,
527
+ lastY,
528
+ linesSV,
529
+ padding.bottom,
530
+ padding.top,
531
+ startX,
532
+ startY,
533
+ pointerOffsetY,
534
+ tagStack,
535
+ ]);
301
536
 
302
- return { gesture, hitTest };
537
+ return { gesture, hitTest, drawnValues };
303
538
  }