react-native-livechart 4.29.0 → 4.30.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.
- 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 +33 -2
- package/dist/core/useLiveChartEngine.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/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 +125 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/components/LiveChart.tsx +61 -2
- 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 +117 -5
- package/src/hooks/resetButtonLayout.ts +80 -0
- package/src/hooks/resetView.ts +92 -0
- 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 +128 -2
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { ChartPadding } from "../draw/line";
|
|
2
|
+
|
|
3
|
+
/** Where the reset button sits inside the plot rect. */
|
|
4
|
+
export type ResetButtonPlacement = "bottom-center" | "bottom-right";
|
|
5
|
+
|
|
6
|
+
/** Canvas-px rect of the rendered reset button. */
|
|
7
|
+
export interface ResetButtonRect {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Position of the reset button inside the plot, from the measured button size.
|
|
16
|
+
* Shared by the overlay's layout and the tap hit-test so both agree.
|
|
17
|
+
*/
|
|
18
|
+
export function resetButtonRect(
|
|
19
|
+
canvasWidth: number,
|
|
20
|
+
canvasHeight: number,
|
|
21
|
+
padding: ChartPadding,
|
|
22
|
+
placement: ResetButtonPlacement,
|
|
23
|
+
margin: number,
|
|
24
|
+
width: number,
|
|
25
|
+
height: number,
|
|
26
|
+
): ResetButtonRect {
|
|
27
|
+
"worklet";
|
|
28
|
+
const plotLeft = padding.left;
|
|
29
|
+
const plotRight = canvasWidth - padding.right;
|
|
30
|
+
const plotBottom = canvasHeight - padding.bottom;
|
|
31
|
+
const x =
|
|
32
|
+
placement === "bottom-right"
|
|
33
|
+
? plotRight - margin - width
|
|
34
|
+
: (plotLeft + plotRight) / 2 - width / 2;
|
|
35
|
+
return { x, y: plotBottom - margin - height, width, height };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Whether a canvas-px touch lands on the button. */
|
|
39
|
+
export function isResetButtonHit(
|
|
40
|
+
x: number,
|
|
41
|
+
y: number,
|
|
42
|
+
rect: ResetButtonRect,
|
|
43
|
+
): boolean {
|
|
44
|
+
"worklet";
|
|
45
|
+
return (
|
|
46
|
+
rect.width > 0 &&
|
|
47
|
+
rect.height > 0 &&
|
|
48
|
+
x >= rect.x &&
|
|
49
|
+
x <= rect.x + rect.width &&
|
|
50
|
+
y >= rect.y &&
|
|
51
|
+
y <= rect.y + rect.height
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** `yRangeScale` this close to 1 counts as fitted (it eases back to exactly 1). */
|
|
56
|
+
const Y_RANGE_SCALE_EPSILON = 1e-3;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* True while any of the enabled manual states moves the view off the fitted
|
|
60
|
+
* live view: a window scrolled back in time, a pinched width, a scaled price
|
|
61
|
+
* axis or a gesture-owned price range.
|
|
62
|
+
*/
|
|
63
|
+
export function isViewOffFitted(
|
|
64
|
+
viewEnd: number | null,
|
|
65
|
+
viewWindow: number | null,
|
|
66
|
+
yRangeScale: number,
|
|
67
|
+
hasGestureRange: boolean,
|
|
68
|
+
showTimeScroll: boolean,
|
|
69
|
+
showZoom: boolean,
|
|
70
|
+
showYScale: boolean,
|
|
71
|
+
showPriceScroll: boolean,
|
|
72
|
+
): boolean {
|
|
73
|
+
"worklet";
|
|
74
|
+
return (
|
|
75
|
+
(showTimeScroll && viewEnd !== null) ||
|
|
76
|
+
(showZoom && viewWindow !== null) ||
|
|
77
|
+
(showYScale && Math.abs(yRangeScale - 1) > Y_RANGE_SCALE_EPSILON) ||
|
|
78
|
+
(showPriceScroll && hasGestureRange)
|
|
79
|
+
);
|
|
80
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cancelAnimation,
|
|
3
|
+
Easing,
|
|
4
|
+
withTiming,
|
|
5
|
+
type SharedValue,
|
|
6
|
+
} from "react-native-reanimated";
|
|
7
|
+
|
|
8
|
+
import type { ChartScale } from "../types";
|
|
9
|
+
|
|
10
|
+
/** Duration (ms) of the eased `yRangeScale` → 1 when no price pan is active. */
|
|
11
|
+
export const Y_RANGE_SCALE_RESET_MS = 240;
|
|
12
|
+
|
|
13
|
+
/** The subset of {@link ResetViewRefs} a chart without price panning resets. */
|
|
14
|
+
export type ResetSeriesViewRefs = Pick<
|
|
15
|
+
ResetViewRefs,
|
|
16
|
+
"viewEnd" | "viewWindow" | "yRangeScale"
|
|
17
|
+
>;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* `LiveChartSeries` counterpart of {@link resetChartView}: clear the pinch
|
|
21
|
+
* window and frozen edge (as `resetZoom()`) and ease `yRangeScale` back to 1.
|
|
22
|
+
*/
|
|
23
|
+
export function resetSeriesView(refs: ResetSeriesViewRefs): void {
|
|
24
|
+
"worklet";
|
|
25
|
+
const { viewEnd, viewWindow, yRangeScale } = refs;
|
|
26
|
+
cancelAnimation(viewWindow);
|
|
27
|
+
cancelAnimation(viewEnd);
|
|
28
|
+
viewWindow.set(null);
|
|
29
|
+
viewEnd.set(null);
|
|
30
|
+
cancelAnimation(yRangeScale);
|
|
31
|
+
yRangeScale.set(withTiming(1, { duration: Y_RANGE_SCALE_RESET_MS }));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Engine SharedValues a reset writes. */
|
|
35
|
+
export interface ResetViewRefs {
|
|
36
|
+
viewEnd: SharedValue<number | null>;
|
|
37
|
+
viewWindow: SharedValue<number | null>;
|
|
38
|
+
viewRange: SharedValue<Pick<ChartScale, "min" | "max"> | null>;
|
|
39
|
+
viewRangeGestureOwned: SharedValue<boolean>;
|
|
40
|
+
yRangeScale: SharedValue<number>;
|
|
41
|
+
returnT: SharedValue<number>;
|
|
42
|
+
returnFrom: SharedValue<number>;
|
|
43
|
+
/** "Return to live" glide duration in ms; `0` snaps. */
|
|
44
|
+
returnToLiveMs: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Return the chart to its fitted live view on the UI thread: clear the pinch
|
|
49
|
+
* window, glide the right edge back to live (the `timeScroll.returnToLive`
|
|
50
|
+
* timing, see #164), drop a gesture-owned price range and land `yRangeScale`
|
|
51
|
+
* on 1. Safe to call when the view is already fitted.
|
|
52
|
+
*/
|
|
53
|
+
export function resetChartView(refs: ResetViewRefs): void {
|
|
54
|
+
"worklet";
|
|
55
|
+
const {
|
|
56
|
+
viewEnd,
|
|
57
|
+
viewWindow,
|
|
58
|
+
viewRange,
|
|
59
|
+
viewRangeGestureOwned,
|
|
60
|
+
yRangeScale,
|
|
61
|
+
returnT,
|
|
62
|
+
returnFrom,
|
|
63
|
+
returnToLiveMs,
|
|
64
|
+
} = refs;
|
|
65
|
+
cancelAnimation(viewWindow);
|
|
66
|
+
viewWindow.set(null);
|
|
67
|
+
const frozenEnd = viewEnd.get();
|
|
68
|
+
cancelAnimation(viewEnd);
|
|
69
|
+
viewEnd.set(null);
|
|
70
|
+
if (frozenEnd != null && returnToLiveMs > 0) {
|
|
71
|
+
returnFrom.set(frozenEnd);
|
|
72
|
+
returnT.set(0);
|
|
73
|
+
returnT.set(
|
|
74
|
+
withTiming(1, {
|
|
75
|
+
duration: returnToLiveMs,
|
|
76
|
+
easing: Easing.out(Easing.cubic),
|
|
77
|
+
}),
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
cancelAnimation(yRangeScale);
|
|
81
|
+
if (viewRangeGestureOwned.get()) {
|
|
82
|
+
// Land the scale in one step: the tick snaps the range while the scale is
|
|
83
|
+
// moving, which would jump the panned window onto the scaled fit before
|
|
84
|
+
// easing. With the scale parked, the display range eases from the panned
|
|
85
|
+
// window to the fit instead.
|
|
86
|
+
viewRange.set(null);
|
|
87
|
+
viewRangeGestureOwned.set(false);
|
|
88
|
+
yRangeScale.set(1);
|
|
89
|
+
} else {
|
|
90
|
+
yRangeScale.set(withTiming(1, { duration: Y_RANGE_SCALE_RESET_MS }));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -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
|
+
}
|