react-native-livechart 4.29.0 → 4.30.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/LiveChart.d.ts.map +1 -1
- package/dist/components/LiveChartSeries.d.ts.map +1 -1
- package/dist/components/ResetButtonOverlay.d.ts +25 -0
- package/dist/components/ResetButtonOverlay.d.ts.map +1 -0
- package/dist/core/gestureRangeOwnership.d.ts +13 -0
- package/dist/core/gestureRangeOwnership.d.ts.map +1 -0
- package/dist/core/liveChartEngineTick.d.ts +30 -0
- package/dist/core/liveChartEngineTick.d.ts.map +1 -1
- package/dist/core/resolveConfig.d.ts +35 -1
- package/dist/core/resolveConfig.d.ts.map +1 -1
- package/dist/core/useLiveChartEngine.d.ts +34 -3
- package/dist/core/useLiveChartEngine.d.ts.map +1 -1
- package/dist/hooks/crosshairShared.d.ts +2 -2
- package/dist/hooks/crosshairShared.d.ts.map +1 -1
- package/dist/hooks/resetButtonLayout.d.ts +24 -0
- package/dist/hooks/resetButtonLayout.d.ts.map +1 -0
- package/dist/hooks/resetView.d.ts +31 -0
- package/dist/hooks/resetView.d.ts.map +1 -0
- package/dist/hooks/useCrosshair.d.ts +3 -1
- package/dist/hooks/useCrosshair.d.ts.map +1 -1
- package/dist/hooks/useLiveDot.d.ts +6 -2
- package/dist/hooks/useLiveDot.d.ts.map +1 -1
- package/dist/hooks/usePanScroll.d.ts +39 -3
- package/dist/hooks/usePanScroll.d.ts.map +1 -1
- package/dist/hooks/useResetButton.d.ts +41 -0
- package/dist/hooks/useResetButton.d.ts.map +1 -0
- package/dist/hooks/useYAxisScaleGesture.d.ts +14 -1
- package/dist/hooks/useYAxisScaleGesture.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/types.d.ts +138 -5
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/components/LiveChart.tsx +67 -6
- package/src/components/LiveChartSeries.tsx +6 -1
- package/src/components/ResetButtonOverlay.tsx +129 -0
- package/src/core/gestureRangeOwnership.ts +46 -0
- package/src/core/liveChartEngineTick.ts +84 -4
- package/src/core/resolveConfig.ts +76 -0
- package/src/core/useLiveChartEngine.ts +191 -22
- package/src/hooks/crosshairShared.ts +2 -2
- package/src/hooks/resetButtonLayout.ts +80 -0
- package/src/hooks/resetView.ts +92 -0
- package/src/hooks/useCrosshair.ts +50 -19
- package/src/hooks/useLiveDot.ts +41 -4
- package/src/hooks/usePanScroll.ts +177 -5
- package/src/hooks/useResetButton.ts +181 -0
- package/src/hooks/useYAxisScaleGesture.ts +70 -13
- package/src/index.ts +3 -0
- package/src/types.ts +141 -5
|
@@ -3,6 +3,7 @@ import { useMemo, useRef } from "react";
|
|
|
3
3
|
import { Gesture } from "react-native-gesture-handler";
|
|
4
4
|
import {
|
|
5
5
|
useAnimatedReaction,
|
|
6
|
+
useDerivedValue,
|
|
6
7
|
useSharedValue,
|
|
7
8
|
type SharedValue,
|
|
8
9
|
} from "react-native-reanimated";
|
|
@@ -218,6 +219,8 @@ export function useCrosshair(
|
|
|
218
219
|
markerOptions?: ScrubMarkerOptions,
|
|
219
220
|
/** Price-axis ownership also guards against stale native long-press events. */
|
|
220
221
|
axisScaleActive?: SharedValue<boolean>,
|
|
222
|
+
/** Buffered line charts must not expose synthetic prices in future space. */
|
|
223
|
+
shouldClampToLatestPoint = false,
|
|
221
224
|
): CrosshairState {
|
|
222
225
|
// Config objects may be recreated without changing the plot geometry. Keep
|
|
223
226
|
// worklet closures and gestures stable until an actual inset changes.
|
|
@@ -226,8 +229,8 @@ export function useCrosshair(
|
|
|
226
229
|
() => ({ top, right, bottom, left }),
|
|
227
230
|
[top, right, bottom, left],
|
|
228
231
|
);
|
|
229
|
-
const
|
|
230
|
-
const
|
|
232
|
+
const scrubPointerX = useSharedValue(-1);
|
|
233
|
+
const scrubGestureActive = useSharedValue(false);
|
|
231
234
|
// Tracks whether the active scrub phase actually began, so a tap that never
|
|
232
235
|
// activates doesn't emit a spurious onGestureEnd.
|
|
233
236
|
const gestureStarted = useSharedValue(false);
|
|
@@ -285,6 +288,32 @@ export function useCrosshair(
|
|
|
285
288
|
clampToPlot,
|
|
286
289
|
);
|
|
287
290
|
|
|
291
|
+
// Keep the pointer separate so a held scrub follows new data and viewport changes.
|
|
292
|
+
const scrubX = useDerivedValue(() => {
|
|
293
|
+
const x = scrubPointerX.get();
|
|
294
|
+
if (!shouldClampToLatestPoint || isCandleMode || hasScrubAction || x < 0) {
|
|
295
|
+
return x;
|
|
296
|
+
}
|
|
297
|
+
const points = engine.data.get();
|
|
298
|
+
const last = points[points.length - 1];
|
|
299
|
+
const windowSecs = engine.displayWindow.get();
|
|
300
|
+
const chartWidth = engine.canvasWidth.get() - padding.left - padding.right;
|
|
301
|
+
if (!last || windowSecs <= 0 || chartWidth <= 0) {
|
|
302
|
+
return -1;
|
|
303
|
+
}
|
|
304
|
+
const startTime = engine.timestamp.get() - windowSecs;
|
|
305
|
+
// There is no recorded price to select in an entirely future window.
|
|
306
|
+
if (last.time < startTime) return -1;
|
|
307
|
+
const latestX = padding.left + ((last.time - startTime) / windowSecs) * chartWidth;
|
|
308
|
+
return Math.min(x, latestX);
|
|
309
|
+
});
|
|
310
|
+
// Preserve the gesture while hiding an unavailable selection. It can become
|
|
311
|
+
// visible again without another touch when data or the viewport moves back.
|
|
312
|
+
const scrubActive = useDerivedValue(() =>
|
|
313
|
+
scrubGestureActive.get() &&
|
|
314
|
+
(!shouldClampToLatestPoint || isCandleMode || hasScrubAction || scrubX.get() >= 0),
|
|
315
|
+
);
|
|
316
|
+
|
|
288
317
|
// Monospace advance width, measured once per render (not per scrub frame) so
|
|
289
318
|
// the tooltip layout worklet can size text by character count instead of a
|
|
290
319
|
// per-frame Skia measureText.
|
|
@@ -616,7 +645,8 @@ export function useCrosshair(
|
|
|
616
645
|
() => {
|
|
617
646
|
"worklet";
|
|
618
647
|
if (!hasOnScrub) return "__idle__";
|
|
619
|
-
if (!
|
|
648
|
+
if (!scrubGestureActive.get()) return "__inactive__";
|
|
649
|
+
if (!scrubActive.get()) return "__unavailable__";
|
|
620
650
|
const time = scrubTime.get();
|
|
621
651
|
const val = scrubValue.get();
|
|
622
652
|
const x = scrubX.get();
|
|
@@ -647,9 +677,11 @@ export function useCrosshair(
|
|
|
647
677
|
"worklet";
|
|
648
678
|
if (!hasOnScrub) return;
|
|
649
679
|
if (typeof curr === "string") {
|
|
650
|
-
//
|
|
651
|
-
|
|
652
|
-
|
|
680
|
+
// Clear unavailable buffered selections and opt-in gap readouts once.
|
|
681
|
+
if (
|
|
682
|
+
(curr === "__unavailable__" || (markerOptions && curr === "__pending__")) &&
|
|
683
|
+
prev && typeof prev === "object"
|
|
684
|
+
) {
|
|
653
685
|
scheduleOnRN(handleScrubEnd);
|
|
654
686
|
}
|
|
655
687
|
return;
|
|
@@ -837,7 +869,7 @@ export function useCrosshair(
|
|
|
837
869
|
}
|
|
838
870
|
// Defer to a marker / pressable badge under the finger: don't start a
|
|
839
871
|
// live scrub (or drop a crosshair) where the press is routed to an
|
|
840
|
-
// overlay tap.
|
|
872
|
+
// overlay tap. The gesture latch is only set here, so bailing in `onStart`
|
|
841
873
|
// also keeps a follow-on drag from showing a crosshair — no `onUpdate`
|
|
842
874
|
// guard needed. (Plain-scrub counterpart of the scrub-action tap defer.)
|
|
843
875
|
if (
|
|
@@ -850,8 +882,8 @@ export function useCrosshair(
|
|
|
850
882
|
padding,
|
|
851
883
|
canvasWidthSV.get(),
|
|
852
884
|
clampPlainScrubToPlot,
|
|
853
|
-
|
|
854
|
-
|
|
885
|
+
scrubPointerX,
|
|
886
|
+
scrubGestureActive,
|
|
855
887
|
gestureStarted,
|
|
856
888
|
);
|
|
857
889
|
if (hasOnGestureStart) scheduleOnRN(handleGestureStart);
|
|
@@ -891,8 +923,8 @@ export function useCrosshair(
|
|
|
891
923
|
padding,
|
|
892
924
|
canvasWidthSV.get(),
|
|
893
925
|
clampPlainScrubToPlot,
|
|
894
|
-
|
|
895
|
-
|
|
926
|
+
scrubPointerX,
|
|
927
|
+
scrubGestureActive,
|
|
896
928
|
);
|
|
897
929
|
},
|
|
898
930
|
)
|
|
@@ -900,11 +932,10 @@ export function useCrosshair(
|
|
|
900
932
|
/* istanbul ignore next */ () => {
|
|
901
933
|
"worklet";
|
|
902
934
|
resetDelayedPanGuard(fingerDown, panActivated, holdBroken);
|
|
903
|
-
// A lock-adjust drag leaves the reticle in place
|
|
904
|
-
//
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
scrubActive.set(false);
|
|
935
|
+
// A lock-adjust drag leaves the reticle in place. End the gesture even
|
|
936
|
+
// when its visible selection is hidden outside the recorded window.
|
|
937
|
+
if (scrubGestureActive.get()) {
|
|
938
|
+
scrubGestureActive.set(false);
|
|
908
939
|
if (hasOnScrub) scheduleOnRN(handleScrubEnd);
|
|
909
940
|
}
|
|
910
941
|
if (gestureStarted.get()) {
|
|
@@ -990,7 +1021,7 @@ export function useCrosshair(
|
|
|
990
1021
|
if (deferTapHit !== undefined && deferTapHit(e.x, e.y)) return;
|
|
991
1022
|
// 3. Place / move the reticle. Clear any in-progress live-scrub so its
|
|
992
1023
|
// crosshair never shows behind the placed reticle.
|
|
993
|
-
|
|
1024
|
+
scrubGestureActive.set(false);
|
|
994
1025
|
lockX.set(
|
|
995
1026
|
clampPlotX(e.x, padding.left, canvasWidthSV.get(), padding.right),
|
|
996
1027
|
);
|
|
@@ -1064,9 +1095,9 @@ export function useCrosshair(
|
|
|
1064
1095
|
padding,
|
|
1065
1096
|
panActivated,
|
|
1066
1097
|
scrollActiveSV,
|
|
1067
|
-
|
|
1098
|
+
scrubGestureActive,
|
|
1068
1099
|
scrubBottomExclude,
|
|
1069
|
-
|
|
1100
|
+
scrubPointerX,
|
|
1070
1101
|
snapToCandles,
|
|
1071
1102
|
timestampSV,
|
|
1072
1103
|
]);
|
package/src/hooks/useLiveDot.ts
CHANGED
|
@@ -7,12 +7,15 @@ import type { ChartPadding } from "../draw/line";
|
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Derive the live dot position (right edge of the chart, mapped to current value).
|
|
10
|
-
* Returns `{ dotX, dotY, indicatorValue }` as shared values.
|
|
11
|
-
* (off-screen sentinel) when canvas dimensions are
|
|
10
|
+
* Returns `{ dotX, dotY, indicatorValue, isFollowingViewEdge }` as shared values.
|
|
11
|
+
* Coordinates are set to `-100` (off-screen sentinel) when canvas dimensions are
|
|
12
|
+
* unavailable.
|
|
12
13
|
*
|
|
13
14
|
* With `followViewEdge` + `edgeValue`, the dot (and the value line that shares
|
|
14
15
|
* `dotY`) tracks the visible window's right-edge price while scrolled back, so it
|
|
15
16
|
* stays aligned with a `followViewEdge` badge instead of marking the live value.
|
|
17
|
+
* Buffered/controlled lines keep the dot on their latest recorded point while
|
|
18
|
+
* it is inside the visible time window; edge-following resumes outside it.
|
|
16
19
|
*
|
|
17
20
|
* With `trackWhileParked` (`dot.trackWhileParked`), the dot instead tracks the
|
|
18
21
|
* **true live point's x** while scrolled back / overscrolled (`viewEnd`
|
|
@@ -29,11 +32,41 @@ export function useLiveDot(
|
|
|
29
32
|
/** Keep the dot on recorded geometry when the viewport includes future space. */
|
|
30
33
|
shouldOmitTipBeyondData = false,
|
|
31
34
|
) {
|
|
35
|
+
const visibleRecordedTip = useDerivedValue(() => {
|
|
36
|
+
if (!shouldOmitTipBeyondData) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const data = engine.data.value;
|
|
40
|
+
const last = data[data.length - 1];
|
|
41
|
+
const win = engine.displayWindow.value;
|
|
42
|
+
const end = engine.timestamp.value;
|
|
43
|
+
const chartW = engine.canvasWidth.value - padding.left - padding.right;
|
|
44
|
+
if (!last || win <= 0 || chartW <= 0) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
if (last.time < end - win || last.time > end) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
x: padding.left + ((last.time - (end - win)) / win) * chartW,
|
|
52
|
+
value: last.value,
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
const isFollowingViewEdge = useDerivedValue(
|
|
56
|
+
() => followViewEdge && visibleRecordedTip.value === null,
|
|
57
|
+
);
|
|
58
|
+
|
|
32
59
|
// A line with no synthetic tip ends at its recorded price. Share that value
|
|
33
60
|
// with the badge so its text, the value line, and the dot stay aligned even
|
|
34
61
|
// while the engine's live value is still easing toward a new tick.
|
|
35
62
|
const indicatorValue = useDerivedValue(() => {
|
|
36
|
-
|
|
63
|
+
const tip = visibleRecordedTip.value;
|
|
64
|
+
if (tip) {
|
|
65
|
+
return tip.value;
|
|
66
|
+
}
|
|
67
|
+
if (isFollowingViewEdge.value && edgeValue) {
|
|
68
|
+
return edgeValue.value;
|
|
69
|
+
}
|
|
37
70
|
if (shouldOmitTipBeyondData) {
|
|
38
71
|
const data = engine.data.value;
|
|
39
72
|
const last = data[data.length - 1];
|
|
@@ -46,6 +79,10 @@ export function useLiveDot(
|
|
|
46
79
|
const w = engine.canvasWidth.value;
|
|
47
80
|
if (w === 0) return -100;
|
|
48
81
|
const right = w - padding.right;
|
|
82
|
+
const tip = visibleRecordedTip.value;
|
|
83
|
+
if (tip) {
|
|
84
|
+
return tip.x;
|
|
85
|
+
}
|
|
49
86
|
// Buffered or parked windows can put the last point before the plot edge.
|
|
50
87
|
// Track its real x so a pan doesn't lose the dot, and hide it only once
|
|
51
88
|
// the point leaves the window.
|
|
@@ -78,5 +115,5 @@ export function useLiveDot(
|
|
|
78
115
|
return padding.top + ((dMax - indicatorValue.value) / valRange) * chartH;
|
|
79
116
|
});
|
|
80
117
|
|
|
81
|
-
return { dotX, dotY, indicatorValue } as const;
|
|
118
|
+
return { dotX, dotY, indicatorValue, isFollowingViewEdge } as const;
|
|
82
119
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
1
|
+
import { useMemo, useState } from "react";
|
|
2
2
|
import { Gesture } from "react-native-gesture-handler";
|
|
3
3
|
import {
|
|
4
4
|
cancelAnimation,
|
|
@@ -8,8 +8,12 @@ import {
|
|
|
8
8
|
withDecay,
|
|
9
9
|
type SharedValue,
|
|
10
10
|
} from "react-native-reanimated";
|
|
11
|
+
import { scheduleOnRN } from "react-native-worklets";
|
|
11
12
|
|
|
13
|
+
import type { ResolvedPriceScrollConfig } from "../core/resolveConfig";
|
|
14
|
+
import { writeGestureRange } from "../core/gestureRangeOwnership";
|
|
12
15
|
import type { ChartPadding } from "../draw/line";
|
|
16
|
+
import type { ChartScale } from "../types";
|
|
13
17
|
|
|
14
18
|
/** Minimum height (px) of the bottom "grab the time ruler" band in axis-drag mode. */
|
|
15
19
|
export const AXIS_GRAB_MIN_PX = 44;
|
|
@@ -35,6 +39,17 @@ export interface PanScrollEngineRefs {
|
|
|
35
39
|
canvasWidth: SharedValue<number>;
|
|
36
40
|
/** Canvas height in px (for the axis-drag band hit-test). */
|
|
37
41
|
canvasHeight: SharedValue<number>;
|
|
42
|
+
/** Price-pan state (see `priceScroll`). All or none; omitted on engines without it. */
|
|
43
|
+
viewRange?: SharedValue<Pick<ChartScale, "min" | "max"> | null>;
|
|
44
|
+
viewRangeGestureOwned?: SharedValue<boolean>;
|
|
45
|
+
/** Eased display range, snapshotted into `viewRange` when a price pan begins. */
|
|
46
|
+
displayMin?: SharedValue<number>;
|
|
47
|
+
displayMax?: SharedValue<number>;
|
|
48
|
+
/** Fitted range the pan may travel `maxOvershoot` spans past. */
|
|
49
|
+
fitMin?: SharedValue<number>;
|
|
50
|
+
fitMax?: SharedValue<number>;
|
|
51
|
+
/** Pinch-zoom override; a non-null width arms an `"afterScale"` price pan. */
|
|
52
|
+
viewWindow?: SharedValue<number | null>;
|
|
38
53
|
}
|
|
39
54
|
|
|
40
55
|
export interface UsePanScrollOptions {
|
|
@@ -93,6 +108,14 @@ export interface UsePanScrollOptions {
|
|
|
93
108
|
fling?: boolean;
|
|
94
109
|
/** Re-clamp parked state on config changes. False for an app-owned viewport. Default true. */
|
|
95
110
|
clampOnOverscrollChange?: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* Vertical price panning (`holdToScrub` mode only). Resolved from
|
|
113
|
+
* `priceScroll`; `null` keeps the classic horizontal-only pan that fails on
|
|
114
|
+
* vertical travel so a parent vertical scroll wins.
|
|
115
|
+
*/
|
|
116
|
+
priceScroll?: ResolvedPriceScrollConfig | null;
|
|
117
|
+
/** Manual Y-range scale; `!== 1` arms an `"afterScale"` price pan. */
|
|
118
|
+
yRangeScale?: SharedValue<number>;
|
|
96
119
|
}
|
|
97
120
|
|
|
98
121
|
/**
|
|
@@ -206,12 +229,58 @@ export function axisBandTop(canvasHeight: number, padBottom: number): number {
|
|
|
206
229
|
return canvasHeight - Math.max(padBottom, AXIS_GRAB_MIN_PX);
|
|
207
230
|
}
|
|
208
231
|
|
|
232
|
+
/**
|
|
233
|
+
* Whether a vertical drag pans price. Mirrored into React state between
|
|
234
|
+
* gestures so the pan's native activation offsets claim vertical travel only
|
|
235
|
+
* while armed; an untouched chart still hands vertical drags to a parent
|
|
236
|
+
* ScrollView.
|
|
237
|
+
*/
|
|
238
|
+
export function isPriceScrollArmed(
|
|
239
|
+
activation: "always" | "afterScale",
|
|
240
|
+
yRangeScale: number,
|
|
241
|
+
viewWindow: number | null,
|
|
242
|
+
hasGestureRange: boolean,
|
|
243
|
+
): boolean {
|
|
244
|
+
"worklet";
|
|
245
|
+
if (activation === "always") return true;
|
|
246
|
+
return yRangeScale !== 1 || viewWindow !== null || hasGestureRange;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Next price bounds after dragging `changeY` px (drag down ⇒ the plot content
|
|
251
|
+
* moves down ⇒ higher prices come into view ⇒ both bounds rise), clamped so
|
|
252
|
+
* neither bound travels more than `maxOvershoot` spans past the fitted range.
|
|
253
|
+
* Returns `cur` unchanged when the span or plot height is degenerate.
|
|
254
|
+
*/
|
|
255
|
+
export function nextPriceRange(
|
|
256
|
+
cur: Pick<ChartScale, "min" | "max">,
|
|
257
|
+
changeY: number,
|
|
258
|
+
plotHeight: number,
|
|
259
|
+
fitMin: number,
|
|
260
|
+
fitMax: number,
|
|
261
|
+
maxOvershoot: number,
|
|
262
|
+
): Pick<ChartScale, "min" | "max"> {
|
|
263
|
+
"worklet";
|
|
264
|
+
const span = cur.max - cur.min;
|
|
265
|
+
if (!(span > 0) || !(plotHeight > 0) || !Number.isFinite(changeY)) return cur;
|
|
266
|
+
let shift = (changeY / plotHeight) * span;
|
|
267
|
+
if (Number.isFinite(fitMin) && Number.isFinite(fitMax) && fitMin < fitMax) {
|
|
268
|
+
const allowance = span * maxOvershoot;
|
|
269
|
+
const lo = fitMin - allowance;
|
|
270
|
+
const hi = fitMax + allowance;
|
|
271
|
+
if (cur.min + shift < lo) shift = lo - cur.min;
|
|
272
|
+
if (cur.max + shift > hi) shift = hi - cur.max;
|
|
273
|
+
}
|
|
274
|
+
return { min: cur.min + shift, max: cur.max + shift };
|
|
275
|
+
}
|
|
276
|
+
|
|
209
277
|
/**
|
|
210
278
|
* Horizontal pan that scrolls the chart back through time. Pick the activation
|
|
211
279
|
* with `mode`:
|
|
212
280
|
* - `"holdToScrub"` — a one-finger drag anywhere (activates on horizontal
|
|
213
|
-
* travel; vertical falls through
|
|
214
|
-
* drag races past it
|
|
281
|
+
* travel; vertical falls through unless `priceScroll` is armed, then it
|
|
282
|
+
* pans price). Scrub must be press-and-hold so a quick drag races past it
|
|
283
|
+
* (compose with `Gesture.Race`).
|
|
215
284
|
* - `"axisDrag"` — a one-finger drag starting in the bottom X-axis band, gated
|
|
216
285
|
* via `manualActivation` (compose with `Gesture.Exclusive`, this gesture
|
|
217
286
|
* first: outside the band it fails instantly so scrub runs).
|
|
@@ -232,17 +301,59 @@ export function usePanScroll({
|
|
|
232
301
|
overscroll = 0,
|
|
233
302
|
fling = true,
|
|
234
303
|
clampOnOverscrollChange = true,
|
|
304
|
+
priceScroll = null,
|
|
305
|
+
yRangeScale,
|
|
235
306
|
}: UsePanScrollOptions): ReturnType<typeof Gesture.Pan> {
|
|
236
|
-
const {
|
|
307
|
+
const {
|
|
308
|
+
viewEnd,
|
|
309
|
+
liveEdge,
|
|
310
|
+
displayWindow,
|
|
311
|
+
canvasWidth,
|
|
312
|
+
canvasHeight,
|
|
313
|
+
viewRange,
|
|
314
|
+
viewRangeGestureOwned,
|
|
315
|
+
displayMin,
|
|
316
|
+
displayMax,
|
|
317
|
+
fitMin,
|
|
318
|
+
fitMax,
|
|
319
|
+
viewWindow,
|
|
320
|
+
} = engine;
|
|
237
321
|
const padLeft = padding.left;
|
|
238
322
|
const padRight = padding.right;
|
|
323
|
+
const padTop = padding.top;
|
|
239
324
|
const padBottom = padding.bottom;
|
|
325
|
+
const priceActivation = priceScroll?.activation ?? null;
|
|
326
|
+
const maxOvershoot = priceScroll?.maxOvershoot ?? 1;
|
|
240
327
|
|
|
241
328
|
// Axis-drag activation tracking (manualActivation). Created unconditionally so
|
|
242
329
|
// the hook order is stable; unused in hold-to-scrub mode.
|
|
243
330
|
const startX = useSharedValue(0);
|
|
244
331
|
const startY = useSharedValue(0);
|
|
245
332
|
const armed = useSharedValue(false);
|
|
333
|
+
// Set once an armed drag has shown vertical intent; only then does the pan
|
|
334
|
+
// take the price range, so a horizontal scroll keeps the Y auto-fit.
|
|
335
|
+
const priceEngaged = useSharedValue(false);
|
|
336
|
+
// Price-pan arming (holdToScrub + priceScroll). RNGH evaluates activation
|
|
337
|
+
// offsets natively per recognizer, so the arming is mirrored into React
|
|
338
|
+
// state and the pan is rebuilt with a vertical active offset while armed.
|
|
339
|
+
// It flips between gestures (after a scale, pinch, pan or reset), never
|
|
340
|
+
// mid-drag, and native activation beats a parent ScrollView's own slop the
|
|
341
|
+
// same way the horizontal offset does.
|
|
342
|
+
const [priceArmed, setPriceArmed] = useState(priceActivation === "always");
|
|
343
|
+
useAnimatedReaction(
|
|
344
|
+
() =>
|
|
345
|
+
priceActivation !== null &&
|
|
346
|
+
isPriceScrollArmed(
|
|
347
|
+
priceActivation,
|
|
348
|
+
yRangeScale?.get() ?? 1,
|
|
349
|
+
viewWindow?.get() ?? null,
|
|
350
|
+
(viewRangeGestureOwned?.get() ?? false) && (viewRange?.get() ?? null) !== null,
|
|
351
|
+
),
|
|
352
|
+
/* istanbul ignore next -- UI-thread reaction; the pure predicate is unit-tested */
|
|
353
|
+
(next, prev) => {
|
|
354
|
+
if (next !== prev) scheduleOnRN(setPriceArmed, next);
|
|
355
|
+
},
|
|
356
|
+
);
|
|
246
357
|
const overscrollSV = useDerivedValue(() => overscroll);
|
|
247
358
|
|
|
248
359
|
// A runtime config change (the demo exposes Off / 50% / 90%) applies to an
|
|
@@ -298,9 +409,19 @@ export function usePanScroll({
|
|
|
298
409
|
|
|
299
410
|
const onChange =
|
|
300
411
|
/* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
|
|
301
|
-
(e: { changeX: number }) => {
|
|
412
|
+
(e: { changeX: number; changeY: number; translationY: number }) => {
|
|
302
413
|
"worklet";
|
|
303
414
|
if (scrubActive?.get()) return;
|
|
415
|
+
// Vertical travel engages the price pan at any point of the drag, so a
|
|
416
|
+
// drag that starts horizontal and turns diagonal moves both axes while
|
|
417
|
+
// a purely horizontal scroll keeps the Y auto-fit.
|
|
418
|
+
if (
|
|
419
|
+
priceArmed &&
|
|
420
|
+
!priceEngaged.get() &&
|
|
421
|
+
Math.abs(e.translationY) > AXIS_ACTIVATE_PX
|
|
422
|
+
) {
|
|
423
|
+
priceEngaged.set(true);
|
|
424
|
+
}
|
|
304
425
|
const win = displayWindow.get();
|
|
305
426
|
const chartW = canvasWidth.get() - padLeft - padRight;
|
|
306
427
|
if (chartW <= 0) return;
|
|
@@ -308,6 +429,27 @@ export function usePanScroll({
|
|
|
308
429
|
const cur = viewEnd.get() ?? edge;
|
|
309
430
|
const lo = panLowerBound(minTime.get(), win, edge, overscroll);
|
|
310
431
|
viewEnd.set(nextViewEnd(cur, e.changeX, chartW, win, edge, lo, overscroll));
|
|
432
|
+
if (priceEngaged.get() && viewRange && viewRangeGestureOwned && displayMin && displayMax) {
|
|
433
|
+
let range = viewRange.get();
|
|
434
|
+
if (!(viewRangeGestureOwned.get() && range !== null)) {
|
|
435
|
+
// Freeze the auto-fit into an explicit range the moment the drag turns
|
|
436
|
+
// vertical, so the drag moves a fixed window; a range an earlier pan
|
|
437
|
+
// left behind carries on from where it is.
|
|
438
|
+
range = { min: displayMin.get(), max: displayMax.get() };
|
|
439
|
+
}
|
|
440
|
+
writeGestureRange(
|
|
441
|
+
viewRange,
|
|
442
|
+
viewRangeGestureOwned,
|
|
443
|
+
nextPriceRange(
|
|
444
|
+
range,
|
|
445
|
+
e.changeY,
|
|
446
|
+
canvasHeight.get() - padTop - padBottom,
|
|
447
|
+
fitMin?.get() ?? NaN,
|
|
448
|
+
fitMax?.get() ?? NaN,
|
|
449
|
+
maxOvershoot,
|
|
450
|
+
),
|
|
451
|
+
);
|
|
452
|
+
}
|
|
311
453
|
};
|
|
312
454
|
|
|
313
455
|
const onEnd =
|
|
@@ -351,6 +493,7 @@ export function usePanScroll({
|
|
|
351
493
|
() => {
|
|
352
494
|
"worklet";
|
|
353
495
|
scrollActive?.set(false);
|
|
496
|
+
priceEngaged.set(false);
|
|
354
497
|
};
|
|
355
498
|
|
|
356
499
|
if (mode === "axisDrag") {
|
|
@@ -398,6 +541,22 @@ export function usePanScroll({
|
|
|
398
541
|
.onFinalize(onFinalize);
|
|
399
542
|
}
|
|
400
543
|
|
|
544
|
+
if (priceActivation !== null && priceArmed) {
|
|
545
|
+
// holdToScrub + armed priceScroll: vertical travel activates the pan too,
|
|
546
|
+
// so a vertical drag pans price (a parent vertical scroll never sees it)
|
|
547
|
+
// and a diagonal drag moves both axes. Unarmed charts use the classic pan
|
|
548
|
+
// below, which fails on vertical travel.
|
|
549
|
+
return Gesture.Pan()
|
|
550
|
+
.enabled(enabled)
|
|
551
|
+
.maxPointers(1)
|
|
552
|
+
.activeOffsetX([-AXIS_ACTIVATE_PX, AXIS_ACTIVATE_PX])
|
|
553
|
+
.activeOffsetY([-AXIS_ACTIVATE_PX, AXIS_ACTIVATE_PX])
|
|
554
|
+
.onStart(onStart)
|
|
555
|
+
.onChange(onChange)
|
|
556
|
+
.onEnd(onEnd)
|
|
557
|
+
.onFinalize(onFinalize);
|
|
558
|
+
}
|
|
559
|
+
|
|
401
560
|
// holdToScrub (default): a one-finger drag anywhere scrolls. Activate on
|
|
402
561
|
// horizontal travel so a quick one-finger drag scrolls; a still press-hold
|
|
403
562
|
// crosses no offset and falls through to the scrub gesture (which owns the
|
|
@@ -415,10 +574,15 @@ export function usePanScroll({
|
|
|
415
574
|
armed,
|
|
416
575
|
canvasHeight,
|
|
417
576
|
canvasWidth,
|
|
577
|
+
displayMax,
|
|
578
|
+
displayMin,
|
|
418
579
|
displayWindow,
|
|
419
580
|
enabled,
|
|
581
|
+
fitMax,
|
|
582
|
+
fitMin,
|
|
420
583
|
fling,
|
|
421
584
|
liveEdge,
|
|
585
|
+
maxOvershoot,
|
|
422
586
|
minTime,
|
|
423
587
|
mode,
|
|
424
588
|
onScrollStart,
|
|
@@ -426,11 +590,19 @@ export function usePanScroll({
|
|
|
426
590
|
padBottom,
|
|
427
591
|
padLeft,
|
|
428
592
|
padRight,
|
|
593
|
+
padTop,
|
|
594
|
+
priceActivation,
|
|
595
|
+
priceArmed,
|
|
596
|
+
priceEngaged,
|
|
429
597
|
scrollActive,
|
|
430
598
|
scrubActive,
|
|
431
599
|
startX,
|
|
432
600
|
startY,
|
|
433
601
|
viewEnd,
|
|
602
|
+
viewRange,
|
|
603
|
+
viewRangeGestureOwned,
|
|
604
|
+
viewWindow,
|
|
434
605
|
wakeEngine,
|
|
606
|
+
yRangeScale,
|
|
435
607
|
]);
|
|
436
608
|
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { useCallback, useMemo } from "react";
|
|
2
|
+
import { Gesture, type GestureType } from "react-native-gesture-handler";
|
|
3
|
+
import {
|
|
4
|
+
useDerivedValue,
|
|
5
|
+
useSharedValue,
|
|
6
|
+
type SharedValue,
|
|
7
|
+
} from "react-native-reanimated";
|
|
8
|
+
import { scheduleOnRN, scheduleOnUI } from "react-native-worklets";
|
|
9
|
+
|
|
10
|
+
import type { ResolvedResetButtonConfig } from "../core/resolveConfig";
|
|
11
|
+
import type {
|
|
12
|
+
ChartEngineLayout,
|
|
13
|
+
ChartEngineRange,
|
|
14
|
+
ChartEngineReturnToLive,
|
|
15
|
+
ChartEngineScroll,
|
|
16
|
+
} from "../core/useLiveChartEngine";
|
|
17
|
+
import type { ChartPadding } from "../draw/line";
|
|
18
|
+
import {
|
|
19
|
+
isResetButtonHit,
|
|
20
|
+
isViewOffFitted,
|
|
21
|
+
resetButtonRect,
|
|
22
|
+
type ResetButtonRect,
|
|
23
|
+
} from "./resetButtonLayout";
|
|
24
|
+
import { resetChartView, type ResetViewRefs } from "./resetView";
|
|
25
|
+
|
|
26
|
+
/** Travel (px) and hold (ms) that still count as a tap on the button. */
|
|
27
|
+
const TAP_MAX_DISTANCE_PX = 12;
|
|
28
|
+
const TAP_MAX_DURATION_MS = 400;
|
|
29
|
+
|
|
30
|
+
interface Options {
|
|
31
|
+
/** `null` hides the button; `resetView()` on the handle still works. */
|
|
32
|
+
config: ResolvedResetButtonConfig | null;
|
|
33
|
+
engine: Pick<ChartEngineLayout, "canvasWidth" | "canvasHeight"> &
|
|
34
|
+
ChartEngineScroll &
|
|
35
|
+
ChartEngineRange &
|
|
36
|
+
ChartEngineReturnToLive & { wake?: () => void };
|
|
37
|
+
padding: ChartPadding;
|
|
38
|
+
yRangeScale: SharedValue<number>;
|
|
39
|
+
/** The button hides while the crosshair owns the touch. */
|
|
40
|
+
scrubActive: SharedValue<boolean>;
|
|
41
|
+
/** Resolved `timeScroll.returnToLive` duration (ms). */
|
|
42
|
+
returnToLiveMs: number;
|
|
43
|
+
onResetView?: () => void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface ResetButtonState {
|
|
47
|
+
/** True while the view differs from the fitted live view (and no scrub). */
|
|
48
|
+
visible: SharedValue<boolean>;
|
|
49
|
+
/** Canvas-px rect the overlay positions itself at and the tap hit-tests. */
|
|
50
|
+
rect: SharedValue<ResetButtonRect>;
|
|
51
|
+
/** Written by the overlay's `onLayout`, so a custom visual of any size works. */
|
|
52
|
+
measuredWidth: SharedValue<number>;
|
|
53
|
+
measuredHeight: SharedValue<number>;
|
|
54
|
+
/** Tap on the button, or `null` when the button is off. Compose ahead of the plot graph. */
|
|
55
|
+
tapGesture: GestureType | null;
|
|
56
|
+
/** JS-thread reset (the handle's `resetView()` and accessibility activation). */
|
|
57
|
+
reset: () => void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Visibility, placement and the tap of the floating reset-view button. The
|
|
62
|
+
* tap claims only touches that land on the visible button and fails everywhere
|
|
63
|
+
* else, so composed `Exclusive` ahead of the plot gestures it never delays them.
|
|
64
|
+
*/
|
|
65
|
+
export function useResetButton({
|
|
66
|
+
config,
|
|
67
|
+
engine,
|
|
68
|
+
padding,
|
|
69
|
+
yRangeScale,
|
|
70
|
+
scrubActive,
|
|
71
|
+
returnToLiveMs,
|
|
72
|
+
onResetView,
|
|
73
|
+
}: Options): ResetButtonState {
|
|
74
|
+
const {
|
|
75
|
+
viewEnd,
|
|
76
|
+
viewWindow,
|
|
77
|
+
viewRange,
|
|
78
|
+
viewRangeGestureOwned,
|
|
79
|
+
canvasWidth,
|
|
80
|
+
canvasHeight,
|
|
81
|
+
returnT,
|
|
82
|
+
returnFrom,
|
|
83
|
+
wake,
|
|
84
|
+
} = engine;
|
|
85
|
+
const { left, right, top, bottom } = padding;
|
|
86
|
+
const enabled = config !== null;
|
|
87
|
+
const placement = config?.placement ?? "bottom-center";
|
|
88
|
+
const margin = config?.margin ?? 0;
|
|
89
|
+
const showYScale = config?.showFor.yScale ?? true;
|
|
90
|
+
const showPriceScroll = config?.showFor.priceScroll ?? true;
|
|
91
|
+
const showTimeScroll = config?.showFor.timeScroll ?? true;
|
|
92
|
+
const showZoom = config?.showFor.zoom ?? true;
|
|
93
|
+
|
|
94
|
+
const measuredWidth = useSharedValue(0);
|
|
95
|
+
const measuredHeight = useSharedValue(0);
|
|
96
|
+
|
|
97
|
+
const visible = useDerivedValue(() => {
|
|
98
|
+
if (!enabled || scrubActive.get()) return false;
|
|
99
|
+
return isViewOffFitted(
|
|
100
|
+
viewEnd.get(),
|
|
101
|
+
viewWindow.get(),
|
|
102
|
+
yRangeScale.get(),
|
|
103
|
+
viewRangeGestureOwned.get() && viewRange.get() !== null,
|
|
104
|
+
showTimeScroll,
|
|
105
|
+
showZoom,
|
|
106
|
+
showYScale,
|
|
107
|
+
showPriceScroll,
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const rect = useDerivedValue(() =>
|
|
112
|
+
resetButtonRect(
|
|
113
|
+
canvasWidth.get(),
|
|
114
|
+
canvasHeight.get(),
|
|
115
|
+
{ left, right, top, bottom },
|
|
116
|
+
placement,
|
|
117
|
+
margin,
|
|
118
|
+
measuredWidth.get(),
|
|
119
|
+
measuredHeight.get(),
|
|
120
|
+
),
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const refs = useMemo<ResetViewRefs>(
|
|
124
|
+
() => ({
|
|
125
|
+
viewEnd,
|
|
126
|
+
viewWindow,
|
|
127
|
+
viewRange,
|
|
128
|
+
viewRangeGestureOwned,
|
|
129
|
+
yRangeScale,
|
|
130
|
+
returnT,
|
|
131
|
+
returnFrom,
|
|
132
|
+
returnToLiveMs,
|
|
133
|
+
}),
|
|
134
|
+
[
|
|
135
|
+
returnFrom,
|
|
136
|
+
returnT,
|
|
137
|
+
returnToLiveMs,
|
|
138
|
+
viewEnd,
|
|
139
|
+
viewRange,
|
|
140
|
+
viewRangeGestureOwned,
|
|
141
|
+
viewWindow,
|
|
142
|
+
yRangeScale,
|
|
143
|
+
],
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
const resetOnUI = useCallback(() => {
|
|
147
|
+
"worklet";
|
|
148
|
+
wake?.();
|
|
149
|
+
resetChartView(refs);
|
|
150
|
+
if (onResetView) scheduleOnRN(onResetView);
|
|
151
|
+
}, [onResetView, refs, wake]);
|
|
152
|
+
const reset = useCallback(() => {
|
|
153
|
+
scheduleOnUI(resetOnUI);
|
|
154
|
+
}, [resetOnUI]);
|
|
155
|
+
|
|
156
|
+
const tapGesture = useMemo(() => {
|
|
157
|
+
if (!enabled) return null;
|
|
158
|
+
return Gesture.Tap()
|
|
159
|
+
.maxDistance(TAP_MAX_DISTANCE_PX)
|
|
160
|
+
.maxDuration(TAP_MAX_DURATION_MS)
|
|
161
|
+
.onTouchesDown((event, manager) => {
|
|
162
|
+
"worklet";
|
|
163
|
+
const touch = event.allTouches[0];
|
|
164
|
+
if (
|
|
165
|
+
event.numberOfTouches !== 1 ||
|
|
166
|
+
!touch ||
|
|
167
|
+
!visible.get() ||
|
|
168
|
+
!isResetButtonHit(touch.x, touch.y, rect.get())
|
|
169
|
+
) {
|
|
170
|
+
manager.fail();
|
|
171
|
+
}
|
|
172
|
+
})
|
|
173
|
+
.onEnd((_event, success) => {
|
|
174
|
+
"worklet";
|
|
175
|
+
if (!success) return;
|
|
176
|
+
resetOnUI();
|
|
177
|
+
});
|
|
178
|
+
}, [enabled, rect, resetOnUI, visible]);
|
|
179
|
+
|
|
180
|
+
return { visible, rect, measuredWidth, measuredHeight, tapGesture, reset };
|
|
181
|
+
}
|