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,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,
@@ -19,11 +21,14 @@ import {
19
21
  import { referenceLineForm } from "../math/referenceLines";
20
22
  import type { ReferenceLine } from "../types";
21
23
  import {
22
- computeScrubDotY,
23
24
  computeValueAtY,
24
- snapPrice,
25
+ pinnedPlotY,
26
+ snapPriceOutward,
27
+ snapPriceWithin,
25
28
  } from "./crosshairShared";
26
29
 
30
+ import { useLatestCallback } from "./useLatestCallback";
31
+
27
32
  /** Vertical reach (px) around a line within which a touch grabs it. */
28
33
  const GRAB_SLOP = 14;
29
34
  /** Travel (px) past which a grabbed line starts dragging (in either axis). */
@@ -33,24 +38,74 @@ const DRAG_ACTIVATE_PX = 4;
33
38
  * (and the onDragIn/Out reaction never fires) when the feature is unused. */
34
39
  const EMPTY: never[] = [];
35
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
+
36
80
  /**
37
81
  * Builds the per-line **drag** gesture for draggable Form-A reference lines: grab a
38
82
  * line near its value-Y and drag vertically to set a new value, with optional
39
83
  * `snap` + `bounds` clamp. Mirrors the order-ticket reticle in {@link useCrosshair}
40
- * (value↔Y via `computeValueAtY` / `computeScrubDotY`, frozen value re-projected
41
- * each frame) but per line, writing into the shared `dragValues` array the layout
42
- * and overlays read.
84
+ * (value↔Y via `computeValueAtY` / `pinnedPlotY`, frozen value re-projected
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`.
43
87
  *
44
88
  * The pan uses `manualActivation`: it grabs only when a touch starts within
45
- * {@link GRAB_SLOP} of a draggable line, then **owns** that touch — any drag past
89
+ * {@link GRAB_SLOP} of a draggable line (and inside its `grabRange`, when it has
90
+ * one), then **owns** that touch — any drag past
46
91
  * {@link DRAG_ACTIVATE_PX} (in either axis) drags the line. A touch off every line
47
92
  * fails fast so the chart's other gestures (scrub / scroll) run everywhere else
48
93
  * (compose this ahead of them via `Gesture.Exclusive`). Crucially it no longer
49
94
  * falls through to scrub on a horizontal start, so a drag begun on a line always
50
95
  * wins the race rather than dropping a scrub crosshair (#163).
51
96
  *
52
- * Also fires the per-line drag callbacks: `onChange` (de-duped during drag),
53
- * `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
54
109
  * range or a `bounds`, from a drag or the axis rescaling — edge-detected each frame).
55
110
  */
56
111
  export function useReferenceDrag(
@@ -60,11 +115,16 @@ export function useReferenceDrag(
60
115
  dragValues: SharedValue<number[]>,
61
116
  dragActive: SharedValue<boolean[]>,
62
117
  enabled: boolean,
118
+ tagStack?: SharedValue<ReferenceTagStack>,
63
119
  ): {
64
120
  gesture: ReturnType<typeof Gesture.Pan>;
65
121
  /** True when a touch at (x,y) would grab a draggable line — lets the scrub
66
122
  * gesture decline that press so it never drops a crosshair on a line (#163). */
67
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[]>;
68
128
  } {
69
129
  const anyDraggable =
70
130
  enabled &&
@@ -75,6 +135,10 @@ export function useReferenceDrag(
75
135
  (l.onDragIn != null || l.onDragOut != null),
76
136
  );
77
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
+
78
142
  // Per-line handle Y (canvas px), index-aligned with `lines`; -1 when the line
79
143
  // isn't draggable or the canvas isn't laid out. Off-screen lines pin to the
80
144
  // nearest plot edge so they stay grabbable. Recomputed each frame (UI thread).
@@ -86,9 +150,10 @@ export function useReferenceDrag(
86
150
  const dMax = engine.displayMax.get();
87
151
  const top = padding.top;
88
152
  const bottom = ch - padding.bottom;
153
+ if (bottom <= top) return EMPTY;
89
154
  const out: number[] = [];
90
- for (let i = 0; i < lines.length; i++) {
91
- const l = lines[i];
155
+ for (let i = 0; i < linesSV.get().length; i++) {
156
+ const l = linesSV.get()[i];
92
157
  if (
93
158
  !l.draggable ||
94
159
  referenceLineForm(l) !== "line" ||
@@ -98,35 +163,131 @@ export function useReferenceDrag(
98
163
  continue;
99
164
  }
100
165
  const v = dragValues.get()[i] ?? l.value;
101
- const y = computeScrubDotY(v, dMin, dMax, ch, top, padding.bottom);
102
- out.push(y < 0 ? -1 : Math.min(bottom, Math.max(top, y)));
166
+ out.push(pinnedPlotY(v, dMin, dMax, ch, top, padding.bottom));
103
167
  }
104
168
  return out;
105
169
  });
106
170
 
171
+ // Per-line grab x-ranges (`ReferenceLine.grabRange`), index-aligned with
172
+ // `lines`; null = anywhere along the line. Read by the gesture worklets below,
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
+ });
179
+
107
180
  const dragIndex = useSharedValue(-1);
108
181
  const startX = useSharedValue(0);
109
182
  const startY = useSharedValue(0);
183
+ const pointerOffsetY = useSharedValue(0);
110
184
  const activated = useSharedValue(false);
111
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
+ });
112
221
 
113
- // 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.
114
271
  /* istanbul ignore next -- runs via scheduleOnRN from the UI-thread gesture */
115
- function emitChange(i: number, v: number) {
116
- lines[i]?.onChange?.(v);
117
- }
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
+ });
118
276
  /* istanbul ignore next -- runs via scheduleOnRN from the UI-thread gesture */
119
- function emitCommit(i: number, v: number) {
120
- lines[i]?.onCommit?.(v);
121
- }
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
+ });
122
281
  /* istanbul ignore next -- runs via scheduleOnRN from the UI-thread reaction */
123
- function emitDragOut(i: number, v: number) {
124
- lines[i]?.onDragOut?.(v);
125
- }
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
+ });
126
286
  /* istanbul ignore next -- runs via scheduleOnRN from the UI-thread reaction */
127
- function emitDragIn(i: number, v: number) {
128
- lines[i]?.onDragIn?.(v);
129
- }
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
+ });
130
291
 
131
292
  // onDragIn / onDragOut — edge-detect each line's "out of the watched interval"
132
293
  // state (from a drag or the axis rescaling under a fixed value).
@@ -137,8 +298,8 @@ export function useReferenceDrag(
137
298
  const dMin = engine.displayMin.get();
138
299
  const dMax = engine.displayMax.get();
139
300
  const out: boolean[] = [];
140
- for (let i = 0; i < lines.length; i++) {
141
- const l = lines[i];
301
+ for (let i = 0; i < linesSV.get().length; i++) {
302
+ const l = linesSV.get()[i];
142
303
  if (
143
304
  referenceLineForm(l) !== "line" ||
144
305
  l.value === undefined ||
@@ -147,7 +308,7 @@ export function useReferenceDrag(
147
308
  out.push(false);
148
309
  continue;
149
310
  }
150
- const v = dragValues.get()[i] ?? l.value;
311
+ const v = drawnValues.get()[i] ?? l.value;
151
312
  out.push(referenceValueOut(v, dMin, dMax, l.bounds));
152
313
  }
153
314
  return out;
@@ -157,133 +318,221 @@ export function useReferenceDrag(
157
318
  if (!prev || curr === prev) return;
158
319
  for (let i = 0; i < curr.length; i++) {
159
320
  if (prev[i] === undefined || curr[i] === prev[i]) continue;
160
- const l = lines[i];
161
- const v = dragValues.get()[i] ?? l.value ?? 0;
162
- if (curr[i]) scheduleOnRN(emitDragOut, i, v);
163
- 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);
164
325
  }
165
326
  },
166
- [lines, anyDragInOut],
167
327
  );
168
328
 
169
- // ── Gesture callbacks (UI-thread worklets — excluded from Jest coverage) ──────
170
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
171
- const onTouchesDown = (
172
- e: { changedTouches: { x: number; y: number }[] },
173
- manager: { fail: () => void },
174
- ) => {
175
- "worklet";
176
- const t = e.changedTouches[0];
177
- if (!t) return;
178
- const i = nearestDraggableIndex(handleYs.get(), t.y, GRAB_SLOP);
179
- if (i < 0) {
180
- manager.fail();
181
- return;
182
- }
183
- dragIndex.set(i);
184
- startX.set(t.x);
185
- startY.set(t.y);
186
- };
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
+ };
187
361
 
188
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
189
- const onTouchesMove = (
190
- e: { allTouches: { x: number; y: number }[] },
191
- manager: { activate: () => void },
192
- ) => {
193
- "worklet";
194
- if (dragIndex.get() < 0) return;
195
- const t = e.allTouches[0];
196
- if (!t) return;
197
- // A line is grabbed → it owns the touch: any drag past the threshold drags it.
198
- // We don't fail on horizontal intent (which used to hand the touch to scrub) —
199
- // that let the scrub crosshair win the race even on a vertical drag started on
200
- // the line (#163). Scrub / scroll still run off a line's grab band.
201
- const intent = resolveDragIntent(
202
- t.x - startX.get(),
203
- t.y - startY.get(),
204
- DRAG_ACTIVATE_PX,
205
- );
206
- if (intent === "activate") manager.activate();
207
- };
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
+ };
208
382
 
209
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
210
- const onStart = () => {
211
- "worklet";
212
- const i = dragIndex.get();
213
- if (i < 0) return;
214
- activated.set(true);
215
- const arr = dragActive.get().slice();
216
- arr[i] = true;
217
- dragActive.set(arr);
218
- lastChange.set(dragValues.get()[i] ?? lines[i].value ?? 0);
219
- };
220
-
221
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
222
- const onUpdate = (e: { y: number }) => {
223
- "worklet";
224
- const i = dragIndex.get();
225
- if (i < 0) return;
226
- const l = lines[i];
227
- const raw = computeValueAtY(
228
- e.y,
229
- engine.displayMin.get(),
230
- engine.displayMax.get(),
231
- engine.canvasHeight.get(),
232
- padding.top,
233
- padding.bottom,
234
- );
235
- if (raw === null) return;
236
- const v = clampToBounds(snapPrice(raw, l.snap), l.bounds);
237
- const arr = dragValues.get().slice();
238
- arr[i] = v;
239
- dragValues.set(arr);
240
- if (v !== lastChange.get()) {
241
- lastChange.set(v);
242
- if (l.onChange) scheduleOnRN(emitChange, i, v);
243
- }
244
- };
245
-
246
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
247
- const onFinalize = () => {
248
- "worklet";
249
- const i = dragIndex.get();
250
- if (i >= 0 && activated.get()) {
251
- 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);
252
395
  const arr = dragActive.get().slice();
253
- arr[i] = false;
396
+ arr[i] = true;
254
397
  dragActive.set(arr);
255
- if (lines[i].onCommit) scheduleOnRN(emitCommit, i, v);
256
- }
257
- dragIndex.set(-1);
258
- activated.set(false);
259
- };
398
+ lastChange.set(dragValues.get()[i] ?? l.value ?? 0);
399
+ };
260
400
 
261
- // Hit-test shared with the scrub gesture: does this drag own the touch? The
262
- // scrub's `onStart` consults it to bail, so it never drops a crosshair on a
263
- // line even though it activates independently of this manual-activation pan
264
- // (the `Exclusive` priority alone doesn't hold scrub back — not while this
265
- // gesture is merely pressed, and not while it is dragging either). Once a line
266
- // is grabbed the answer is yes regardless of position: the scrub asks with its
267
- // touch-DOWN point, which a longer drag has carried the line away from, so the
268
- // geometric test alone said "no line here" and a crosshair opened mid-drag.
269
- // Otherwise it is the y-reach around the handles (x is unused — a Form-A line
270
- // spans the full width).
271
- /* istanbul ignore next -- worklet, runs on the UI thread */
272
- const hitTest = (_x: number, y: number): boolean => {
273
- "worklet";
274
- if (!anyDraggable) return false;
275
- return referenceDragOwnsTouch(dragIndex.get(), handleYs.get(), y, GRAB_SLOP);
276
- };
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);
277
509
 
278
- const gesture = Gesture.Pan()
279
- .enabled(anyDraggable)
280
- .maxPointers(1)
281
- .manualActivation(true)
282
- .onTouchesDown(onTouchesDown)
283
- .onTouchesMove(onTouchesMove)
284
- .onStart(onStart)
285
- .onUpdate(onUpdate)
286
- .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
+ ]);
287
536
 
288
- return { gesture, hitTest };
537
+ return { gesture, hitTest, drawnValues };
289
538
  }