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,129 @@
|
|
|
1
|
+
import { useCallback, useState, type ReactElement } from "react";
|
|
2
|
+
import { StyleSheet, Text, View, type AccessibilityActionEvent, type LayoutChangeEvent } from "react-native";
|
|
3
|
+
import Animated, {
|
|
4
|
+
useAnimatedReaction,
|
|
5
|
+
useAnimatedStyle,
|
|
6
|
+
useDerivedValue,
|
|
7
|
+
withTiming,
|
|
8
|
+
type SharedValue,
|
|
9
|
+
} from "react-native-reanimated";
|
|
10
|
+
import { scheduleOnRN } from "react-native-worklets";
|
|
11
|
+
|
|
12
|
+
import type { ResolvedResetButtonConfig } from "../core/resolveConfig";
|
|
13
|
+
import type { ResetButtonRect } from "../hooks/resetButtonLayout";
|
|
14
|
+
import type { LiveChartPalette, ResetButtonRenderProps } from "../types";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React Native overlay (NOT Skia) for the floating reset-view button. The
|
|
18
|
+
* wrapper is positioned from the chart's rect on the UI thread and faded with
|
|
19
|
+
* `visible`; the tap itself is an RNGH gesture composed ahead of the plot graph
|
|
20
|
+
* (see `useResetButton`), so the content here is a plain view. Renders the
|
|
21
|
+
* consumer's `renderResetButton` element when given, otherwise a round glyph
|
|
22
|
+
* button in palette colors.
|
|
23
|
+
*/
|
|
24
|
+
export function ResetButtonOverlay({
|
|
25
|
+
config,
|
|
26
|
+
visible,
|
|
27
|
+
rect,
|
|
28
|
+
measuredWidth,
|
|
29
|
+
measuredHeight,
|
|
30
|
+
reset,
|
|
31
|
+
render,
|
|
32
|
+
palette,
|
|
33
|
+
fontFamily,
|
|
34
|
+
}: {
|
|
35
|
+
config: ResolvedResetButtonConfig;
|
|
36
|
+
visible: SharedValue<boolean>;
|
|
37
|
+
rect: SharedValue<ResetButtonRect>;
|
|
38
|
+
measuredWidth: SharedValue<number>;
|
|
39
|
+
measuredHeight: SharedValue<number>;
|
|
40
|
+
reset: () => void;
|
|
41
|
+
render: ((props: ResetButtonRenderProps) => ReactElement) | undefined;
|
|
42
|
+
palette: LiveChartPalette;
|
|
43
|
+
fontFamily: string;
|
|
44
|
+
}) {
|
|
45
|
+
const { fadeMs } = config;
|
|
46
|
+
const opacity = useDerivedValue(() =>
|
|
47
|
+
withTiming(visible.get() ? 1 : 0, { duration: fadeMs }),
|
|
48
|
+
);
|
|
49
|
+
const wrapperStyle = useAnimatedStyle(() => {
|
|
50
|
+
const r = rect.get();
|
|
51
|
+
return {
|
|
52
|
+
opacity: opacity.get(),
|
|
53
|
+
transform: [{ translateX: r.x }, { translateY: r.y }],
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Mirrored to React so a hidden button is neither focusable by screen
|
|
58
|
+
// readers nor a target for native touches (the chart's RNGH gestures are
|
|
59
|
+
// unaffected either way: they live on the ancestor view).
|
|
60
|
+
const [isShown, setIsShown] = useState(false);
|
|
61
|
+
useAnimatedReaction(
|
|
62
|
+
() => visible.get(),
|
|
63
|
+
/* istanbul ignore next -- Reanimated reaction; state mirrored on the JS thread, not exercised under Jest */
|
|
64
|
+
(shown, previous) => {
|
|
65
|
+
if (shown !== previous) scheduleOnRN(setIsShown, shown);
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const onLayout = useCallback(
|
|
70
|
+
(event: LayoutChangeEvent) => {
|
|
71
|
+
const { width, height } = event.nativeEvent.layout;
|
|
72
|
+
measuredWidth.set(width);
|
|
73
|
+
measuredHeight.set(height);
|
|
74
|
+
},
|
|
75
|
+
[measuredHeight, measuredWidth],
|
|
76
|
+
);
|
|
77
|
+
const onAccessibilityAction = useCallback(
|
|
78
|
+
(event: AccessibilityActionEvent) => {
|
|
79
|
+
if (event.nativeEvent.actionName === "activate") reset();
|
|
80
|
+
},
|
|
81
|
+
[reset],
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<Animated.View
|
|
86
|
+
style={[styles.wrapper, wrapperStyle]}
|
|
87
|
+
onLayout={onLayout}
|
|
88
|
+
pointerEvents={isShown ? "box-none" : "none"}
|
|
89
|
+
accessibilityElementsHidden={!isShown}
|
|
90
|
+
importantForAccessibility={isShown ? "auto" : "no-hide-descendants"}
|
|
91
|
+
>
|
|
92
|
+
{render ? (
|
|
93
|
+
render({ visible, reset })
|
|
94
|
+
) : (
|
|
95
|
+
<View
|
|
96
|
+
accessible
|
|
97
|
+
accessibilityRole="button"
|
|
98
|
+
accessibilityLabel={config.accessibilityLabel}
|
|
99
|
+
onAccessibilityTap={reset}
|
|
100
|
+
accessibilityActions={[{ name: "activate" }]}
|
|
101
|
+
onAccessibilityAction={onAccessibilityAction}
|
|
102
|
+
style={{
|
|
103
|
+
width: config.size,
|
|
104
|
+
height: config.size,
|
|
105
|
+
borderRadius: config.size / 2,
|
|
106
|
+
backgroundColor: config.color ?? palette.badgeBg,
|
|
107
|
+
alignItems: "center",
|
|
108
|
+
justifyContent: "center",
|
|
109
|
+
}}
|
|
110
|
+
>
|
|
111
|
+
<Text
|
|
112
|
+
allowFontScaling={false}
|
|
113
|
+
style={{
|
|
114
|
+
fontFamily,
|
|
115
|
+
fontSize: config.size / 2,
|
|
116
|
+
color: config.iconColor ?? palette.badgeText,
|
|
117
|
+
}}
|
|
118
|
+
>
|
|
119
|
+
{config.icon}
|
|
120
|
+
</Text>
|
|
121
|
+
</View>
|
|
122
|
+
)}
|
|
123
|
+
</Animated.View>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const styles = StyleSheet.create({
|
|
128
|
+
wrapper: { position: "absolute", left: 0, top: 0 },
|
|
129
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { useLayoutEffect, useState } from "react";
|
|
2
|
+
import { useSharedValue, type SharedValue } from "react-native-reanimated";
|
|
3
|
+
import { scheduleOnUI } from "react-native-worklets";
|
|
4
|
+
|
|
5
|
+
import type { ChartScale } from "../types";
|
|
6
|
+
|
|
7
|
+
type PriceRange = Pick<ChartScale, "min" | "max"> | null;
|
|
8
|
+
|
|
9
|
+
// Keep separate from Reanimated's positive mapper IDs and history listener IDs.
|
|
10
|
+
let nextRangeListenerId = -1_000_000;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* App writes relinquish ownership synchronously, including modify() calls and
|
|
14
|
+
* writes followed by a reset in the same worklet. Replacing the supplied range
|
|
15
|
+
* also relinquishes ownership before the new viewport is painted.
|
|
16
|
+
*/
|
|
17
|
+
export function useGestureRangeOwnership(range: SharedValue<PriceRange>) {
|
|
18
|
+
const owned = useSharedValue(false);
|
|
19
|
+
const [listenerId] = useState(() => nextRangeListenerId--);
|
|
20
|
+
useLayoutEffect(() => {
|
|
21
|
+
scheduleOnUI(() => {
|
|
22
|
+
"worklet";
|
|
23
|
+
owned.set(false);
|
|
24
|
+
range.addListener(listenerId, () => {
|
|
25
|
+
"worklet";
|
|
26
|
+
owned.set(false);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
return () => scheduleOnUI(() => {
|
|
30
|
+
"worklet";
|
|
31
|
+
range.removeListener(listenerId);
|
|
32
|
+
});
|
|
33
|
+
}, [range, owned, listenerId]);
|
|
34
|
+
return owned;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Mark only this write as gesture-owned, after synchronous listeners run. */
|
|
38
|
+
export function writeGestureRange(
|
|
39
|
+
range: SharedValue<PriceRange>,
|
|
40
|
+
owned: SharedValue<boolean>,
|
|
41
|
+
next: Exclude<PriceRange, null>,
|
|
42
|
+
): void {
|
|
43
|
+
"worklet";
|
|
44
|
+
range.value = next;
|
|
45
|
+
owned.value = true;
|
|
46
|
+
}
|
|
@@ -52,6 +52,22 @@ export interface EngineTickMutable {
|
|
|
52
52
|
extremaMaxValue: number;
|
|
53
53
|
extremaMinTime: number;
|
|
54
54
|
extremaMaxTime: number;
|
|
55
|
+
/**
|
|
56
|
+
* Fitted Y bounds this frame — the target the eased display range pursues,
|
|
57
|
+
* before an explicit {@link EngineTickInput.viewRange} overrides it. The
|
|
58
|
+
* price-pan gesture clamps its travel against these. Written every frame;
|
|
59
|
+
* optional so existing state literals stay valid.
|
|
60
|
+
*/
|
|
61
|
+
fitMin?: number;
|
|
62
|
+
fitMax?: number;
|
|
63
|
+
/**
|
|
64
|
+
* A gesture-owned {@link EngineTickInput.viewRange} after
|
|
65
|
+
* {@link clampGestureRange}, when the clamp changed it. The engine writes it
|
|
66
|
+
* back to the shared value so the next gesture frame starts from the clamped
|
|
67
|
+
* bounds. `NaN` when nothing was adjusted; written every frame.
|
|
68
|
+
*/
|
|
69
|
+
adjustedViewRangeMin?: number;
|
|
70
|
+
adjustedViewRangeMax?: number;
|
|
55
71
|
/** Previous frame's yRangeScale — detects an in-flight scale drag. */
|
|
56
72
|
lastYRangeScale?: number;
|
|
57
73
|
/** Canvas width observed by the previous measured tick. */
|
|
@@ -95,6 +111,13 @@ export interface EngineTickInput {
|
|
|
95
111
|
yRangeScale?: number;
|
|
96
112
|
/** Explicit finite price bounds for a freely positioned external viewport. */
|
|
97
113
|
viewRange?: Pick<ChartScale, "min" | "max"> | null;
|
|
114
|
+
/**
|
|
115
|
+
* Apply {@link viewRange} while following live too (`viewEnd` null). Set for
|
|
116
|
+
* a range written by the price-pan / axis-scale gestures, so the price window
|
|
117
|
+
* holds as new data streams in. An app-written range keeps the
|
|
118
|
+
* `viewEnd`-only rule. Default `false`.
|
|
119
|
+
*/
|
|
120
|
+
viewRangeFollowsLive?: boolean;
|
|
98
121
|
targetValue: number;
|
|
99
122
|
points: LiveChartPoint[];
|
|
100
123
|
/** Internal notification revision for the selected history; omit to disable reuse. */
|
|
@@ -202,6 +225,44 @@ export function advanceTimestampByPixel(
|
|
|
202
225
|
return elapsedSteps > 0 ? current + elapsedSteps * step : current;
|
|
203
226
|
}
|
|
204
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Keep a gesture-owned explicit range inside the chart's hard bounds, which
|
|
230
|
+
* the fit normally enforces: widen a span below `minRange` about its midpoint,
|
|
231
|
+
* then slide the window to respect `nonNegative` / `maxValue`. Returns the
|
|
232
|
+
* input object itself when nothing changes.
|
|
233
|
+
*/
|
|
234
|
+
export function clampGestureRange(
|
|
235
|
+
range: Pick<ChartScale, "min" | "max">,
|
|
236
|
+
nonNegative: boolean,
|
|
237
|
+
maxValue: number | undefined,
|
|
238
|
+
minRange: number | undefined,
|
|
239
|
+
): Pick<ChartScale, "min" | "max"> {
|
|
240
|
+
"worklet";
|
|
241
|
+
let min = range.min;
|
|
242
|
+
let max = range.max;
|
|
243
|
+
if (
|
|
244
|
+
minRange !== undefined &&
|
|
245
|
+
minRange > 0 &&
|
|
246
|
+
Number.isFinite(minRange) &&
|
|
247
|
+
max - min < minRange
|
|
248
|
+
) {
|
|
249
|
+
const mid = min / 2 + max / 2;
|
|
250
|
+
min = mid - minRange / 2;
|
|
251
|
+
max = mid + minRange / 2;
|
|
252
|
+
}
|
|
253
|
+
if (nonNegative && min < 0) {
|
|
254
|
+
max -= min;
|
|
255
|
+
min = 0;
|
|
256
|
+
}
|
|
257
|
+
if (maxValue !== undefined && max > maxValue) {
|
|
258
|
+
min -= max - maxValue;
|
|
259
|
+
max = maxValue;
|
|
260
|
+
if (nonNegative && min < 0) min = 0;
|
|
261
|
+
}
|
|
262
|
+
if (!(Number.isFinite(min) && Number.isFinite(max) && min < max)) return range;
|
|
263
|
+
return min === range.min && max === range.max ? range : { min, max };
|
|
264
|
+
}
|
|
265
|
+
|
|
205
266
|
/**
|
|
206
267
|
* One frame of the live chart engine (mirrors `useLiveChartEngine` worklet body).
|
|
207
268
|
* Mutates `state` in place for testability and reuse from the hook.
|
|
@@ -223,8 +284,8 @@ export function tickLiveChartEngineFrame(
|
|
|
223
284
|
const viewEnd = input.viewEnd;
|
|
224
285
|
const requestedRange = input.viewRange;
|
|
225
286
|
const viewRange =
|
|
226
|
-
viewEnd != null &&
|
|
227
|
-
|
|
287
|
+
((viewEnd != null && Number.isFinite(viewEnd)) ||
|
|
288
|
+
input.viewRangeFollowsLive === true) &&
|
|
228
289
|
requestedRange &&
|
|
229
290
|
Number.isFinite(requestedRange.min) &&
|
|
230
291
|
Number.isFinite(requestedRange.max) &&
|
|
@@ -544,6 +605,8 @@ export function tickLiveChartEngineFrame(
|
|
|
544
605
|
if (input.nonNegative && tMin < 0) tMin = 0;
|
|
545
606
|
const maxV = input.maxValue;
|
|
546
607
|
if (maxV !== undefined && tMax > maxV) tMax = maxV;
|
|
608
|
+
state.fitMin = tMin;
|
|
609
|
+
state.fitMax = tMax;
|
|
547
610
|
|
|
548
611
|
const disjoint = tMin >= state.displayMax || tMax <= state.displayMin;
|
|
549
612
|
// A subpixel settle must not jump across the other bound of a disjoint fit.
|
|
@@ -576,9 +639,26 @@ export function tickLiveChartEngineFrame(
|
|
|
576
639
|
}
|
|
577
640
|
|
|
578
641
|
// Explicit bounds keep a gesture's price coordinates stable, even in empty space.
|
|
642
|
+
state.adjustedViewRangeMin = NaN;
|
|
643
|
+
state.adjustedViewRangeMax = NaN;
|
|
579
644
|
if (viewRange) {
|
|
580
|
-
|
|
581
|
-
|
|
645
|
+
// An app-written range is applied verbatim (the app owns its bounds); a
|
|
646
|
+
// gesture-written one must still honor the chart's hard bounds.
|
|
647
|
+
const bounded =
|
|
648
|
+
input.viewRangeFollowsLive === true
|
|
649
|
+
? clampGestureRange(
|
|
650
|
+
viewRange,
|
|
651
|
+
input.nonNegative === true,
|
|
652
|
+
input.maxValue,
|
|
653
|
+
input.minRange,
|
|
654
|
+
)
|
|
655
|
+
: viewRange;
|
|
656
|
+
if (bounded !== viewRange) {
|
|
657
|
+
state.adjustedViewRangeMin = bounded.min;
|
|
658
|
+
state.adjustedViewRangeMax = bounded.max;
|
|
659
|
+
}
|
|
660
|
+
state.displayMin = bounded.min;
|
|
661
|
+
state.displayMax = bounded.max;
|
|
582
662
|
}
|
|
583
663
|
|
|
584
664
|
// Edge value: the price at the visible window's right edge. Following live →
|
|
@@ -33,6 +33,8 @@ import type {
|
|
|
33
33
|
ScrubActionConfig,
|
|
34
34
|
ScrubConfig,
|
|
35
35
|
PerSeriesTooltipConfig,
|
|
36
|
+
PriceScrollConfig,
|
|
37
|
+
ResetButtonConfig,
|
|
36
38
|
SelectionDotConfig,
|
|
37
39
|
SelectionDotProps,
|
|
38
40
|
SelectionDotRingConfig,
|
|
@@ -806,6 +808,80 @@ export function resolveZoom(
|
|
|
806
808
|
return resolveToggle(prop, ZOOM_DEFAULTS, false);
|
|
807
809
|
}
|
|
808
810
|
|
|
811
|
+
export interface ResolvedPriceScrollConfig {
|
|
812
|
+
activation: "always" | "afterScale";
|
|
813
|
+
/** Non-negative, finite multiple of the visible span. */
|
|
814
|
+
maxOvershoot: number;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
const PRICE_SCROLL_DEFAULTS: ResolvedPriceScrollConfig = {
|
|
818
|
+
activation: "afterScale",
|
|
819
|
+
maxOvershoot: 1,
|
|
820
|
+
};
|
|
821
|
+
|
|
822
|
+
/**
|
|
823
|
+
* Resolves the `priceScroll` prop to a config or null (disabled). `true` →
|
|
824
|
+
* defaults, object → merged, falsy → null. A malformed `maxOvershoot` falls
|
|
825
|
+
* back to the default rather than unbounding the pan.
|
|
826
|
+
*/
|
|
827
|
+
export function resolvePriceScroll(
|
|
828
|
+
prop: boolean | PriceScrollConfig | undefined,
|
|
829
|
+
): ResolvedPriceScrollConfig | null {
|
|
830
|
+
const resolved = resolveToggle(prop, PRICE_SCROLL_DEFAULTS, false);
|
|
831
|
+
if (resolved === null) return null;
|
|
832
|
+
const validOvershoot =
|
|
833
|
+
Number.isFinite(resolved.maxOvershoot) && resolved.maxOvershoot >= 0;
|
|
834
|
+
return validOvershoot
|
|
835
|
+
? resolved
|
|
836
|
+
: { ...resolved, maxOvershoot: PRICE_SCROLL_DEFAULTS.maxOvershoot };
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
export interface ResolvedResetButtonConfig {
|
|
840
|
+
placement: "bottom-center" | "bottom-right";
|
|
841
|
+
showFor: {
|
|
842
|
+
yScale: boolean;
|
|
843
|
+
priceScroll: boolean;
|
|
844
|
+
timeScroll: boolean;
|
|
845
|
+
zoom: boolean;
|
|
846
|
+
};
|
|
847
|
+
margin: number;
|
|
848
|
+
fadeMs: number;
|
|
849
|
+
icon: string;
|
|
850
|
+
size: number;
|
|
851
|
+
/** undefined → palette.badgeBg at render time */
|
|
852
|
+
color: string | undefined;
|
|
853
|
+
/** undefined → palette.badgeText at render time */
|
|
854
|
+
iconColor: string | undefined;
|
|
855
|
+
accessibilityLabel: string | undefined;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
const RESET_BUTTON_DEFAULTS: ResolvedResetButtonConfig = {
|
|
859
|
+
placement: "bottom-center",
|
|
860
|
+
showFor: { yScale: true, priceScroll: true, timeScroll: true, zoom: true },
|
|
861
|
+
margin: 12,
|
|
862
|
+
fadeMs: 160,
|
|
863
|
+
icon: "↺",
|
|
864
|
+
size: 36,
|
|
865
|
+
color: undefined,
|
|
866
|
+
iconColor: undefined,
|
|
867
|
+
accessibilityLabel: undefined,
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* Resolves the `resetButton` prop to a config or null (disabled). `true` →
|
|
872
|
+
* defaults, object → merged (`showFor` merges per key), falsy → null.
|
|
873
|
+
*/
|
|
874
|
+
export function resolveResetButton(
|
|
875
|
+
prop: boolean | ResetButtonConfig | undefined,
|
|
876
|
+
): ResolvedResetButtonConfig | null {
|
|
877
|
+
const resolved = resolveToggle(prop, RESET_BUTTON_DEFAULTS, false);
|
|
878
|
+
if (resolved === null || typeof prop !== "object") return resolved;
|
|
879
|
+
return {
|
|
880
|
+
...resolved,
|
|
881
|
+
showFor: { ...RESET_BUTTON_DEFAULTS.showFor, ...prop.showFor },
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
|
|
809
885
|
/**
|
|
810
886
|
* Resolves `timeScroll.returnToLive` to the "return to live" glide duration in ms,
|
|
811
887
|
* where `0` means an instant snap (no animation):
|
|
@@ -16,11 +16,13 @@ import {
|
|
|
16
16
|
withTiming,
|
|
17
17
|
type SharedValue,
|
|
18
18
|
} from "react-native-reanimated";
|
|
19
|
+
import { scheduleOnUI } from "react-native-worklets";
|
|
19
20
|
import { useDemandFrameLoop } from "../hooks/useDemandFrameLoop";
|
|
20
21
|
import { MS_PER_FRAME_60FPS, RETURN_TO_LIVE_MS } from "../constants";
|
|
21
22
|
import type {
|
|
22
23
|
CandleGap,
|
|
23
24
|
CandlePoint,
|
|
25
|
+
ChartScale,
|
|
24
26
|
LiveChartFrameStats,
|
|
25
27
|
LiveChartPoint,
|
|
26
28
|
RangeAnimationConfig,
|
|
@@ -40,6 +42,7 @@ import {
|
|
|
40
42
|
|
|
41
43
|
import { useHistoryRevision } from "./useHistoryRevision";
|
|
42
44
|
import type { ResolvedViewportConfig } from "./resolveConfig";
|
|
45
|
+
import { useGestureRangeOwnership, writeGestureRange } from "./gestureRangeOwnership";
|
|
43
46
|
|
|
44
47
|
export interface EngineConfig {
|
|
45
48
|
/** External scroll/zoom values adopted in place of private gesture state. */
|
|
@@ -209,6 +212,28 @@ export interface ChartEngineEdge {
|
|
|
209
212
|
edgeValue: SharedValue<number>;
|
|
210
213
|
}
|
|
211
214
|
|
|
215
|
+
/** Price-range state shared by the price-pan / axis-scale gestures and the reset button. */
|
|
216
|
+
export interface ChartEngineRange {
|
|
217
|
+
/** Explicit price bounds: the app's `viewport.range` when supplied, else private state. */
|
|
218
|
+
viewRange: SharedValue<Pick<ChartScale, "min" | "max"> | null>;
|
|
219
|
+
/**
|
|
220
|
+
* True while {@link viewRange} was written by a gesture, which keeps it
|
|
221
|
+
* applied while following live. An app write or replacement viewport gives
|
|
222
|
+
* ownership back to the app. Resets, axis double-taps and framing changes
|
|
223
|
+
* clear only gesture-owned bounds.
|
|
224
|
+
*/
|
|
225
|
+
viewRangeGestureOwned: SharedValue<boolean>;
|
|
226
|
+
/** Fitted Y bounds the eased display range pursues, before a range override. */
|
|
227
|
+
fitMin: SharedValue<number>;
|
|
228
|
+
fitMax: SharedValue<number>;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** "Return to live" glide state (see `EngineTickInput.returnT`). */
|
|
232
|
+
export interface ChartEngineReturnToLive {
|
|
233
|
+
returnT: SharedValue<number>;
|
|
234
|
+
returnFrom: SharedValue<number>;
|
|
235
|
+
}
|
|
236
|
+
|
|
212
237
|
export interface SingleEngineState
|
|
213
238
|
extends ChartEngineLayout,
|
|
214
239
|
ChartEngineExtrema {
|
|
@@ -269,6 +294,11 @@ export interface EngineFrameRefs {
|
|
|
269
294
|
yRangeScaleSV?: SharedValue<number>;
|
|
270
295
|
/** External price bounds read in the same frame as end/window. */
|
|
271
296
|
viewRangeSV?: ResolvedViewportConfig["range"];
|
|
297
|
+
/** Honor `viewRangeSV` while following live (a gesture-owned range). */
|
|
298
|
+
viewRangeFollowsLiveSV?: SharedValue<boolean>;
|
|
299
|
+
/** Fitted bounds written back each frame (see `EngineTickMutable.fitMin`). */
|
|
300
|
+
fitMinSV?: SharedValue<number>;
|
|
301
|
+
fitMaxSV?: SharedValue<number>;
|
|
272
302
|
nowOverrideSV?: SharedValue<number | undefined>;
|
|
273
303
|
windowBufferSV?: SharedValue<number>;
|
|
274
304
|
pausedSV: SharedValue<boolean>;
|
|
@@ -330,6 +360,10 @@ export function makeEngineFrameScratch(): EngineFrameScratch {
|
|
|
330
360
|
extremaMaxValue: NaN,
|
|
331
361
|
extremaMinTime: NaN,
|
|
332
362
|
extremaMaxTime: NaN,
|
|
363
|
+
fitMin: 0,
|
|
364
|
+
fitMax: 1,
|
|
365
|
+
adjustedViewRangeMin: NaN,
|
|
366
|
+
adjustedViewRangeMax: NaN,
|
|
333
367
|
lastCanvasWidth: undefined,
|
|
334
368
|
lastCanvasHeight: undefined,
|
|
335
369
|
},
|
|
@@ -347,6 +381,21 @@ export function makeEngineFrameScratch(): EngineFrameScratch {
|
|
|
347
381
|
};
|
|
348
382
|
}
|
|
349
383
|
|
|
384
|
+
/**
|
|
385
|
+
* Drop a gesture-written price range so the next frame lands on the fit (a
|
|
386
|
+
* `snapKey` or `mode` change re-frames the chart). An app-written range,
|
|
387
|
+
* which the app owns, is left alone.
|
|
388
|
+
*/
|
|
389
|
+
export function clearGestureOwnedRange(
|
|
390
|
+
viewRange: SharedValue<Pick<ChartScale, "min" | "max"> | null>,
|
|
391
|
+
viewRangeGestureOwned: SharedValue<boolean>,
|
|
392
|
+
): void {
|
|
393
|
+
"worklet";
|
|
394
|
+
if (!viewRangeGestureOwned.get()) return;
|
|
395
|
+
viewRange.set(null);
|
|
396
|
+
viewRangeGestureOwned.set(false);
|
|
397
|
+
}
|
|
398
|
+
|
|
350
399
|
/**
|
|
351
400
|
* Shared between the `useFrameCallback` worklet and unit tests.
|
|
352
401
|
* Mutates shared values from a snapshot tick (`tickLiveChartEngineFrame`).
|
|
@@ -373,6 +422,10 @@ export function applyLiveChartEngineFrame(
|
|
|
373
422
|
extremaMaxValue: NaN,
|
|
374
423
|
extremaMinTime: NaN,
|
|
375
424
|
extremaMaxTime: NaN,
|
|
425
|
+
fitMin: 0,
|
|
426
|
+
fitMax: 1,
|
|
427
|
+
adjustedViewRangeMin: NaN,
|
|
428
|
+
adjustedViewRangeMax: NaN,
|
|
376
429
|
lastCanvasWidth: undefined,
|
|
377
430
|
lastCanvasHeight: undefined,
|
|
378
431
|
};
|
|
@@ -387,6 +440,8 @@ export function applyLiveChartEngineFrame(
|
|
|
387
440
|
state.extremaMaxValue = sv.extremaMaxValue.value;
|
|
388
441
|
state.extremaMinTime = sv.extremaMinTime.value;
|
|
389
442
|
state.extremaMaxTime = sv.extremaMaxTime.value;
|
|
443
|
+
state.fitMin = sv.fitMinSV?.value ?? state.fitMin;
|
|
444
|
+
state.fitMax = sv.fitMaxSV?.value ?? state.fitMax;
|
|
390
445
|
|
|
391
446
|
const input: EngineTickInput = scratch?.input ?? {
|
|
392
447
|
dt,
|
|
@@ -418,6 +473,7 @@ export function applyLiveChartEngineFrame(
|
|
|
418
473
|
input.minRange = sv.minRangeSV?.value;
|
|
419
474
|
input.yRangeScale = sv.yRangeScaleSV?.value ?? 1;
|
|
420
475
|
input.viewRange = sv.viewRangeSV?.value;
|
|
476
|
+
input.viewRangeFollowsLive = sv.viewRangeFollowsLiveSV?.value ?? false;
|
|
421
477
|
input.nowOverride = sv.nowOverrideSV?.value;
|
|
422
478
|
input.windowBuffer = sv.windowBufferSV?.value ?? 0;
|
|
423
479
|
input.targetValue = sv.value.value;
|
|
@@ -455,6 +511,25 @@ export function applyLiveChartEngineFrame(
|
|
|
455
511
|
sv.extremaMaxValue.value = state.extremaMaxValue;
|
|
456
512
|
sv.extremaMinTime.value = state.extremaMinTime;
|
|
457
513
|
sv.extremaMaxTime.value = state.extremaMaxTime;
|
|
514
|
+
if (sv.fitMinSV && state.fitMin !== undefined) sv.fitMinSV.value = state.fitMin;
|
|
515
|
+
if (sv.fitMaxSV && state.fitMax !== undefined) sv.fitMaxSV.value = state.fitMax;
|
|
516
|
+
// A gesture-owned range the tick had to clamp is written back, so the next
|
|
517
|
+
// gesture frame continues from the clamped bounds instead of drifting past them.
|
|
518
|
+
const adjustedMin = state.adjustedViewRangeMin;
|
|
519
|
+
const adjustedMax = state.adjustedViewRangeMax;
|
|
520
|
+
if (
|
|
521
|
+
sv.viewRangeSV &&
|
|
522
|
+
sv.viewRangeFollowsLiveSV?.value === true &&
|
|
523
|
+
adjustedMin !== undefined &&
|
|
524
|
+
adjustedMax !== undefined &&
|
|
525
|
+
Number.isFinite(adjustedMin) &&
|
|
526
|
+
Number.isFinite(adjustedMax)
|
|
527
|
+
) {
|
|
528
|
+
writeGestureRange(sv.viewRangeSV, sv.viewRangeFollowsLiveSV, {
|
|
529
|
+
min: adjustedMin,
|
|
530
|
+
max: adjustedMax,
|
|
531
|
+
});
|
|
532
|
+
}
|
|
458
533
|
// Clear the one-shot snap so the next frame eases normally again — but only
|
|
459
534
|
// once a real (measured) frame consumed it. The tick early-returns on a
|
|
460
535
|
// zero-size canvas (before applying the snap), so keep the flag pending until
|
|
@@ -471,7 +546,11 @@ export function applyLiveChartEngineFrame(
|
|
|
471
546
|
|
|
472
547
|
export function useLiveChartEngine(
|
|
473
548
|
config: EngineConfig,
|
|
474
|
-
): SingleEngineState &
|
|
549
|
+
): SingleEngineState &
|
|
550
|
+
ChartEngineScroll &
|
|
551
|
+
ChartEngineEdge &
|
|
552
|
+
ChartEngineRange &
|
|
553
|
+
ChartEngineReturnToLive & { wake: () => void } {
|
|
475
554
|
const {
|
|
476
555
|
viewport,
|
|
477
556
|
data,
|
|
@@ -520,7 +599,26 @@ export function useLiveChartEngine(
|
|
|
520
599
|
const internalViewWindow = useSharedValue<number | null>(null);
|
|
521
600
|
const viewWindow = viewport?.window ?? internalViewWindow;
|
|
522
601
|
const hasViewport = viewport != null;
|
|
523
|
-
|
|
602
|
+
// Explicit price bounds. The price-pan / axis-scale gestures write these (the
|
|
603
|
+
// app's `viewport.range` when supplied, else private state) and mark them
|
|
604
|
+
// gesture-owned so the tick keeps applying them while following live.
|
|
605
|
+
const internalViewRange = useSharedValue<Pick<ChartScale, "min" | "max"> | null>(null);
|
|
606
|
+
const viewRange = viewport?.range ?? internalViewRange;
|
|
607
|
+
const viewRangeGestureOwned = useGestureRangeOwnership(viewRange);
|
|
608
|
+
const fitMin = useSharedValue(0);
|
|
609
|
+
const fitMax = useSharedValue(1);
|
|
610
|
+
const clearGestureRange = useCallback(
|
|
611
|
+
() => scheduleOnUI(clearGestureOwnedRange, viewRange, viewRangeGestureOwned),
|
|
612
|
+
[viewRange, viewRangeGestureOwned],
|
|
613
|
+
);
|
|
614
|
+
// A mode switch (line ↔ candle) re-frames the price range like a `snapKey`
|
|
615
|
+
// change does below, so a panned window from the other mode doesn't linger.
|
|
616
|
+
const lastMode = useRef(mode);
|
|
617
|
+
useEffect(() => {
|
|
618
|
+
if (mode === lastMode.current) return;
|
|
619
|
+
lastMode.current = mode;
|
|
620
|
+
clearGestureRange();
|
|
621
|
+
}, [mode, clearGestureRange]);
|
|
524
622
|
const windowSmoothing = viewport?.windowSmoothing ?? true;
|
|
525
623
|
const viewWindowSmoothingSV = useDerivedValue(() => windowSmoothing);
|
|
526
624
|
// A change to the `timeWindow` prop (a range / timeframe selector) is an explicit
|
|
@@ -657,7 +755,9 @@ export function useLiveChartEngine(
|
|
|
657
755
|
if (snapKey === lastSnapKey.current) return;
|
|
658
756
|
lastSnapKey.current = snapKey;
|
|
659
757
|
snapSV.set(true);
|
|
660
|
-
|
|
758
|
+
// The new framing lands on the fit, not on a window panned for the old data.
|
|
759
|
+
clearGestureRange();
|
|
760
|
+
}, [snapKey, snapSV, clearGestureRange]);
|
|
661
761
|
|
|
662
762
|
// Static charts run zero per-frame loops. `isStaticSV` gates both the
|
|
663
763
|
// frame-callback autostart and the one-shot settle reaction below.
|
|
@@ -700,6 +800,9 @@ export function useLiveChartEngine(
|
|
|
700
800
|
minRangeSV,
|
|
701
801
|
yRangeScaleSV: yRangeScale,
|
|
702
802
|
viewRangeSV: viewRange,
|
|
803
|
+
viewRangeFollowsLiveSV: viewRangeGestureOwned,
|
|
804
|
+
fitMinSV: fitMin,
|
|
805
|
+
fitMaxSV: fitMax,
|
|
703
806
|
nowOverrideSV,
|
|
704
807
|
windowBufferSV,
|
|
705
808
|
pausedSV,
|
|
@@ -772,6 +875,9 @@ export function useLiveChartEngine(
|
|
|
772
875
|
viewWindow,
|
|
773
876
|
viewWindowSmoothingSV,
|
|
774
877
|
viewRange,
|
|
878
|
+
viewRangeGestureOwned,
|
|
879
|
+
fitMin,
|
|
880
|
+
fitMax,
|
|
775
881
|
windowBufferSV,
|
|
776
882
|
yRangeScale,
|
|
777
883
|
],
|
|
@@ -825,14 +931,14 @@ export function useLiveChartEngine(
|
|
|
825
931
|
exaggerateSV.get(), referenceValue.get(), referenceValues.get(), thresholdRangePoints.get(),
|
|
826
932
|
thresholdRangeExtendToStart.get(), thresholdRangeExtendToNow.get(), nonNegativeSV.get(),
|
|
827
933
|
maxValueSV.get(), minRangeSV.get(), yRangeScale?.get(), nowOverrideSV.get(), windowBufferSV.get(), pausedSV.get(),
|
|
828
|
-
viewEnd.get(), viewWindow.get(), viewRange
|
|
934
|
+
viewEnd.get(), viewWindow.get(), viewRange.get(), viewRangeGestureOwned.get(), viewWindowSmoothingSV.get(), allowFutureViewEndSV.get(), returnT.get(), returnFrom.get(),
|
|
829
935
|
snapSV.get(), modeSV.get(), candleGapsSV.get(), candleGapBridgeNoTradesSV.get(),
|
|
830
936
|
candleGapBridgeUnavailableSV.get(), candleGapBridgeUnknownSV.get(), isFrameLoopActive?.get(),
|
|
831
937
|
keepAwake?.get(), wakeSignal?.get()];
|
|
832
938
|
}, [pointHistory, candleHistory, data, value, candles, liveCandle, canvasWidth, canvasHeight, timeWindow, smoothing, rangeAnimationSV,
|
|
833
939
|
adaptiveSpeedBoostSV, exaggerateSV, referenceValue, referenceValues, thresholdRangePoints,
|
|
834
940
|
thresholdRangeExtendToStart, thresholdRangeExtendToNow, nonNegativeSV, maxValueSV, minRangeSV, yRangeScale,
|
|
835
|
-
nowOverrideSV, windowBufferSV, pausedSV, viewEnd, viewWindow, viewRange, viewWindowSmoothingSV, allowFutureViewEndSV, returnT,
|
|
941
|
+
nowOverrideSV, windowBufferSV, pausedSV, viewEnd, viewWindow, viewRange, viewRangeGestureOwned, viewWindowSmoothingSV, allowFutureViewEndSV, returnT,
|
|
836
942
|
returnFrom, snapSV, modeSV, candleGapsSV, candleGapBridgeNoTradesSV, candleGapBridgeUnavailableSV,
|
|
837
943
|
candleGapBridgeUnknownSV, isFrameLoopActive, keepAwake, wakeSignal]);
|
|
838
944
|
|
|
@@ -956,6 +1062,12 @@ export function useLiveChartEngine(
|
|
|
956
1062
|
timestamp,
|
|
957
1063
|
viewEnd,
|
|
958
1064
|
viewWindow,
|
|
1065
|
+
viewRange,
|
|
1066
|
+
viewRangeGestureOwned,
|
|
1067
|
+
fitMin,
|
|
1068
|
+
fitMax,
|
|
1069
|
+
returnT,
|
|
1070
|
+
returnFrom,
|
|
959
1071
|
liveEdge,
|
|
960
1072
|
edgeValue,
|
|
961
1073
|
extremaMinValue,
|