@ng-native/components 0.22.2 → 0.23.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/android/build.gradle +22 -0
- package/android/src/main/java/dev/ngnative/components/ContextMenuAccessibility.kt +71 -0
- package/android/src/main/java/dev/ngnative/components/ContextMenuGesture.kt +161 -0
- package/android/src/main/java/dev/ngnative/components/ContextMenuPopup.kt +197 -0
- package/android/src/main/java/dev/ngnative/components/MenuHost.kt +45 -0
- package/android/src/main/java/dev/ngnative/components/MenuWindowCallback.kt +76 -0
- package/android/src/main/java/dev/ngnative/components/MenusModule.kt +85 -0
- package/android/src/main/java/dev/ngnative/components/Shortcut.kt +144 -0
- package/android/src/main/java/dev/ngnative/components/WindowWrapper.kt +22 -0
- package/dist/activity-indicator.d.ts +4 -1
- package/dist/activity-indicator.js +6 -2
- package/dist/activity-indicator.js.map +1 -1
- package/dist/context-menu.d.ts +52 -0
- package/dist/context-menu.js +101 -0
- package/dist/context-menu.js.map +1 -0
- package/dist/drag-and-drop.d.ts +52 -0
- package/dist/drag-and-drop.js +112 -0
- package/dist/drag-and-drop.js.map +1 -0
- package/dist/gesture-backend.d.ts +32 -2
- package/dist/gesture-backend.js.map +1 -1
- package/dist/gesture-dispatch.d.ts +8 -1
- package/dist/gesture-dispatch.js +60 -16
- package/dist/gesture-dispatch.js.map +1 -1
- package/dist/gesture-interaction.d.ts +13 -0
- package/dist/gesture-interaction.js +97 -0
- package/dist/gesture-interaction.js.map +1 -0
- package/dist/gesture-tracking.d.ts +18 -0
- package/dist/gesture-tracking.js +28 -0
- package/dist/gesture-tracking.js.map +1 -0
- package/dist/gestures-web.d.ts +3 -1
- package/dist/gestures-web.js +5 -2
- package/dist/gestures-web.js.map +1 -1
- package/dist/gestures.d.ts +1 -0
- package/dist/gestures.js +45 -3
- package/dist/gestures.js.map +1 -1
- package/dist/image.d.ts +1 -1
- package/dist/index.d.ts +12 -1
- package/dist/index.js +12 -1
- package/dist/index.js.map +1 -1
- package/dist/interaction-observer.d.ts +11 -0
- package/dist/interaction-observer.js +12 -0
- package/dist/interaction-observer.js.map +1 -0
- package/dist/keyboard-avoiding-view.js +3 -3
- package/dist/keyboard-avoiding-view.js.map +1 -1
- package/dist/markdown-stream.d.ts +3 -1
- package/dist/markdown-stream.js +14 -2
- package/dist/markdown-stream.js.map +1 -1
- package/dist/markdown.d.ts +2 -0
- package/dist/markdown.js +5 -3
- package/dist/markdown.js.map +1 -1
- package/dist/menu.d.ts +71 -0
- package/dist/menu.js +193 -0
- package/dist/menu.js.map +1 -0
- package/dist/modal.d.ts +30 -1
- package/dist/modal.js +67 -6
- package/dist/modal.js.map +1 -1
- package/dist/native-button.d.ts +54 -0
- package/dist/native-button.js +100 -0
- package/dist/native-button.js.map +1 -0
- package/dist/native-gesture.d.ts +9 -1
- package/dist/native-gesture.js +32 -3
- package/dist/native-gesture.js.map +1 -1
- package/dist/native-id.d.ts +10 -0
- package/dist/native-id.js +70 -0
- package/dist/native-id.js.map +1 -0
- package/dist/pressable.d.ts +31 -0
- package/dist/pressable.js +102 -9
- package/dist/pressable.js.map +1 -1
- package/dist/safe-area-provider.js +15 -8
- package/dist/safe-area-provider.js.map +1 -1
- package/dist/scroll-view-props.d.ts +7 -1
- package/dist/scroll-view-props.js +54 -3
- package/dist/scroll-view-props.js.map +1 -1
- package/dist/section-list.js +2 -2
- package/dist/section-list.js.map +1 -1
- package/dist/shortcut.d.ts +21 -0
- package/dist/shortcut.js +34 -0
- package/dist/shortcut.js.map +1 -0
- package/dist/sidebar-item.d.ts +53 -0
- package/dist/sidebar-item.js +152 -0
- package/dist/sidebar-item.js.map +1 -0
- package/dist/sidebar-section.d.ts +21 -0
- package/dist/sidebar-section.js +74 -0
- package/dist/sidebar-section.js.map +1 -0
- package/dist/sidebar.d.ts +47 -0
- package/dist/sidebar.js +110 -0
- package/dist/sidebar.js.map +1 -0
- package/dist/split-view.d.ts +63 -0
- package/dist/split-view.js +121 -0
- package/dist/split-view.js.map +1 -0
- package/dist/switch.d.ts +9 -1
- package/dist/switch.js +23 -2
- package/dist/switch.js.map +1 -1
- package/dist/text-input.d.ts +8 -3
- package/dist/text-input.js +10 -4
- package/dist/text-input.js.map +1 -1
- package/dist/transforms.d.ts +5 -0
- package/dist/transforms.js +7 -0
- package/dist/transforms.js.map +1 -1
- package/dist/tray-icon.d.ts +18 -0
- package/dist/tray-icon.js +46 -0
- package/dist/tray-icon.js.map +1 -0
- package/dist/view-base.d.ts +42 -1
- package/dist/view-base.js +67 -39
- package/dist/view-base.js.map +1 -1
- package/dist/virtual-list.js +12 -1
- package/dist/virtual-list.js.map +1 -1
- package/dist/written-props.js +25 -0
- package/dist/written-props.js.map +1 -1
- package/expo-module.config.json +6 -0
- package/ios/NgNativeButtonComponentView.mm +132 -0
- package/ios/NgNativeButtonMetrics.h +28 -0
- package/ios/NgNativeButtonMetrics.mm +68 -0
- package/ios/NgNativeButtonShadowNode.h +65 -0
- package/ios/NgNativeButtonShadowNode.mm +40 -0
- package/ios/NgNativeComponents.podspec +28 -0
- package/ios/NgNativeDragAndDrop.mm +342 -0
- package/ios/NgNativeDropItems.h +29 -0
- package/ios/NgNativeDropItems.mm +55 -0
- package/ios/NgNativeMenuBuilding.h +40 -0
- package/ios/NgNativeMenuBuilding.mm +217 -0
- package/ios/NgNativeMenus.mm +320 -0
- package/ios/NgNativeResponderHooks.h +53 -0
- package/ios/NgNativeResponderHooks.mm +215 -0
- package/ios/NgNativeSceneKeys.h +36 -0
- package/ios/NgNativeSceneKeys.mm +56 -0
- package/ios/NgNativeSplitView.mm +433 -0
- package/ios/NgNativeSplitViewDelegate.h +29 -0
- package/ios/NgNativeSplitViewDelegate.mm +122 -0
- package/ios/NgNativeSplitViewShadowNodes.h +115 -0
- package/ios/NgNativeSplitViewShadowNodes.mm +31 -0
- package/ios/NgNativeUndo.mm +107 -0
- package/ios/NgNativeUndoSteps.h +17 -0
- package/ios/NgNativeUndoSteps.mm +22 -0
- package/ios/NgNativeViewBinder.h +54 -0
- package/ios/NgNativeViewBinder.mm +225 -0
- package/ios/NgNativeWindows.mm +1126 -0
- package/package.json +13 -5
|
@@ -19,8 +19,38 @@ export type GestureSpec = object;
|
|
|
19
19
|
export interface GestureTarget {
|
|
20
20
|
readonly tag: number;
|
|
21
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* A gesture that has just ended successfully, as the backend hands it over on the JavaScript
|
|
24
|
+
* thread: what the library calls it, and the few numbers of its end event a report is built from.
|
|
25
|
+
*/
|
|
26
|
+
export interface EndedGesture {
|
|
27
|
+
/** The library's `handlerName`: `PanGestureHandler`, `TapGestureHandler`, ... */
|
|
28
|
+
readonly handlerName: string;
|
|
29
|
+
/** A fling's `direction` setting, the library's `Directions` bits, or undefined for its default. */
|
|
30
|
+
readonly directions?: number;
|
|
31
|
+
readonly end: GestureEndValues;
|
|
32
|
+
}
|
|
33
|
+
/** The numbers copied off a gesture's end event, and where its touch began in the window. */
|
|
34
|
+
export interface GestureEndValues {
|
|
35
|
+
readonly handlerTag: number;
|
|
36
|
+
readonly x?: number;
|
|
37
|
+
readonly y?: number;
|
|
38
|
+
readonly absoluteX?: number;
|
|
39
|
+
readonly absoluteY?: number;
|
|
40
|
+
readonly translationX?: number;
|
|
41
|
+
readonly translationY?: number;
|
|
42
|
+
readonly velocityX?: number;
|
|
43
|
+
readonly velocityY?: number;
|
|
44
|
+
readonly scale?: number;
|
|
45
|
+
readonly startX?: number;
|
|
46
|
+
readonly startY?: number;
|
|
47
|
+
}
|
|
22
48
|
export interface GestureBackend {
|
|
23
|
-
/**
|
|
24
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Attach `gesture` to `target`, and return the function that detaches it. `report`, when given,
|
|
51
|
+
* hears each of its gestures that ends successfully, on the JavaScript thread; without it the
|
|
52
|
+
* backend does no reporting work at all.
|
|
53
|
+
*/
|
|
54
|
+
attach(target: GestureTarget, gesture: GestureSpec, report?: (ended: EndedGesture) => void): () => void;
|
|
25
55
|
}
|
|
26
56
|
export declare const GESTURES: InjectionToken<GestureBackend>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gesture-backend.js","sourceRoot":"","sources":["../src/gesture-backend.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"gesture-backend.js","sourceRoot":"","sources":["../src/gesture-backend.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAsD/C,MAAM,CAAC,MAAM,QAAQ,GAAG,IAAI,cAAc,CAAiB,yBAAyB,CAAC,CAAC","sourcesContent":["/**\n * Platform capability: native gesture recognition, from react-native-gesture-handler.\n *\n * A gesture is recognised by the platform - a `UIPanGestureRecognizer`, an Android\n * `GestureDetector` - rather than by our own responder negotiation. That is what makes a drag\n * inside a scroll view behave, and what a pinch or a rotation needs to exist at all.\n *\n * Injected rather than imported for the reason everything in this shape is: the library reaches\n * React Native's Flow source, which Node cannot parse. `NativeGesture` in `../gestures.ts` carries\n * the real backend as its own provider, so importing the directive is the whole setup.\n */\nimport { InjectionToken } from '@angular/core';\n\n/**\n * A gesture built by the library's own `Gesture` API: `Gesture.Pan()`, or a composition of\n * several. Opaque here on purpose - the shape is the library's, and this package does not know it.\n */\nexport type GestureSpec = object;\n\n/** What a gesture is attached to: a node's react tag. */\nexport interface GestureTarget {\n readonly tag: number;\n}\n\n/**\n * A gesture that has just ended successfully, as the backend hands it over on the JavaScript\n * thread: what the library calls it, and the few numbers of its end event a report is built from.\n */\nexport interface EndedGesture {\n /** The library's `handlerName`: `PanGestureHandler`, `TapGestureHandler`, ... */\n readonly handlerName: string;\n /** A fling's `direction` setting, the library's `Directions` bits, or undefined for its default. */\n readonly directions?: number;\n readonly end: GestureEndValues;\n}\n\n/** The numbers copied off a gesture's end event, and where its touch began in the window. */\nexport interface GestureEndValues {\n readonly handlerTag: number;\n readonly x?: number;\n readonly y?: number;\n readonly absoluteX?: number;\n readonly absoluteY?: number;\n readonly translationX?: number;\n readonly translationY?: number;\n readonly velocityX?: number;\n readonly velocityY?: number;\n readonly scale?: number;\n readonly startX?: number;\n readonly startY?: number;\n}\n\nexport interface GestureBackend {\n /**\n * Attach `gesture` to `target`, and return the function that detaches it. `report`, when given,\n * hears each of its gestures that ends successfully, on the JavaScript thread; without it the\n * backend does no reporting work at all.\n */\n attach(\n target: GestureTarget,\n gesture: GestureSpec,\n report?: (ended: EndedGesture) => void,\n ): () => void;\n}\n\nexport const GESTURES = new InjectionToken<GestureBackend>('angular-native.gestures');\n"]}
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* make this package unloadable in Node - and because they are a protocol between native and us,
|
|
12
12
|
* not an API surface: native puts these integers in the event.
|
|
13
13
|
*/
|
|
14
|
+
import type { GestureEndValues } from './gesture-backend.ts';
|
|
14
15
|
/** `State` in react-native-gesture-handler. */
|
|
15
16
|
export declare const GESTURE_STATE: {
|
|
16
17
|
readonly UNDETERMINED: 0;
|
|
@@ -57,11 +58,17 @@ export interface GestureEvent {
|
|
|
57
58
|
oldState?: number;
|
|
58
59
|
eventType?: number;
|
|
59
60
|
}
|
|
61
|
+
/** Hears a gesture that ended successfully, with the numbers its report needs. */
|
|
62
|
+
export type GestureEndReport = (end: GestureEndValues) => void;
|
|
60
63
|
/**
|
|
61
64
|
* Build the worklet that receives a view's gesture events.
|
|
62
65
|
*
|
|
63
66
|
* `createManager` is the library's `GestureStateManager.create`, passed in rather than imported
|
|
64
67
|
* for the reason the constants are restated. It has to be a worklet itself: everything this
|
|
65
68
|
* closure holds is serialised onto the UI runtime with it.
|
|
69
|
+
*
|
|
70
|
+
* `report`, when given, hears each gesture that ends successfully, once, after its own `onEnd`.
|
|
71
|
+
* On a device it is a worklet that schedules the report onto the JavaScript thread. `reported`
|
|
72
|
+
* narrows it to the gestures with those handler tags, and the rest do no reporting work.
|
|
66
73
|
*/
|
|
67
|
-
export declare function gestureDispatcher(callbacks: readonly GestureCallbacks[], createManager: (handlerTag: number) => GestureStateManager): (event: GestureEvent) => void;
|
|
74
|
+
export declare function gestureDispatcher(callbacks: readonly GestureCallbacks[], createManager: (handlerTag: number) => GestureStateManager, report?: GestureEndReport, reported?: readonly number[]): (event: GestureEvent) => void;
|
package/dist/gesture-dispatch.js
CHANGED
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The state machine between a gesture's raw events and the callbacks it declared.
|
|
3
|
-
*
|
|
4
|
-
* This is what `useAnimatedGesture` does with hooks in react-native-gesture-handler, and the only
|
|
5
|
-
* part of a gesture we write rather than borrow. It is here, apart from the library, so it can be
|
|
6
|
-
* driven by a test: on a device it is serialised onto the UI runtime and a mistake in it is a
|
|
7
|
-
* callback that silently never runs.
|
|
8
|
-
*
|
|
9
|
-
* The numbers below are the ones on the wire. They are the library's `State` and `TouchEventType`,
|
|
10
|
-
* restated rather than imported because importing them would reach React Native's Flow source and
|
|
11
|
-
* make this package unloadable in Node - and because they are a protocol between native and us,
|
|
12
|
-
* not an API surface: native puts these integers in the event.
|
|
13
|
-
*/
|
|
14
1
|
/** `State` in react-native-gesture-handler. */
|
|
15
2
|
export const GESTURE_STATE = {
|
|
16
3
|
UNDETERMINED: 0,
|
|
@@ -28,6 +15,35 @@ export const TOUCH_EVENT = {
|
|
|
28
15
|
UP: 3,
|
|
29
16
|
CANCELLED: 4,
|
|
30
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* The numbers of an end event a report is built from, copied into a plain object: the event is
|
|
20
|
+
* native's, and only plain values cross from the UI runtime to the JavaScript one.
|
|
21
|
+
*/
|
|
22
|
+
function endValues(handlerTag, event, start) {
|
|
23
|
+
'worklet';
|
|
24
|
+
const values = {};
|
|
25
|
+
const from = event;
|
|
26
|
+
for (const key of [
|
|
27
|
+
'x',
|
|
28
|
+
'y',
|
|
29
|
+
'absoluteX',
|
|
30
|
+
'absoluteY',
|
|
31
|
+
'translationX',
|
|
32
|
+
'translationY',
|
|
33
|
+
'velocityX',
|
|
34
|
+
'velocityY',
|
|
35
|
+
'scale',
|
|
36
|
+
]) {
|
|
37
|
+
const value = from[key];
|
|
38
|
+
if (typeof value === 'number')
|
|
39
|
+
values[key] = value;
|
|
40
|
+
}
|
|
41
|
+
if (start?.x !== undefined)
|
|
42
|
+
values['startX'] = start.x;
|
|
43
|
+
if (start?.y !== undefined)
|
|
44
|
+
values['startY'] = start.y;
|
|
45
|
+
return { ...values, handlerTag };
|
|
46
|
+
}
|
|
31
47
|
function stateChange(gesture, event, memory) {
|
|
32
48
|
'worklet';
|
|
33
49
|
const state = event.state;
|
|
@@ -51,6 +67,26 @@ function stateChange(gesture, event, memory) {
|
|
|
51
67
|
gesture.onFinalize?.(event, succeeded);
|
|
52
68
|
}
|
|
53
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Report a gesture that ended successfully: an end from active, as `onEnd` with success is. A tap
|
|
72
|
+
* that failed, or a pan a scroll view took over, did nothing. Run after the gesture's own
|
|
73
|
+
* callbacks, as an observer hears a press after its handler.
|
|
74
|
+
*/
|
|
75
|
+
function reportEnd(handlerTag, event, memory, report) {
|
|
76
|
+
'worklet';
|
|
77
|
+
const state = event.state;
|
|
78
|
+
if (state === GESTURE_STATE.BEGAN) {
|
|
79
|
+
const at = event;
|
|
80
|
+
memory.starts[handlerTag] = { x: at.absoluteX, y: at.absoluteY };
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (state === GESTURE_STATE.ACTIVE)
|
|
84
|
+
return;
|
|
85
|
+
if (state === GESTURE_STATE.END && event.oldState === GESTURE_STATE.ACTIVE) {
|
|
86
|
+
report(endValues(handlerTag, event, memory.starts[handlerTag]));
|
|
87
|
+
}
|
|
88
|
+
memory.starts[handlerTag] = undefined;
|
|
89
|
+
}
|
|
54
90
|
function update(gesture, event, memory) {
|
|
55
91
|
'worklet';
|
|
56
92
|
gesture.onUpdate?.(event);
|
|
@@ -84,18 +120,26 @@ function touches(gesture, event, memory, createManager) {
|
|
|
84
120
|
* `createManager` is the library's `GestureStateManager.create`, passed in rather than imported
|
|
85
121
|
* for the reason the constants are restated. It has to be a worklet itself: everything this
|
|
86
122
|
* closure holds is serialised onto the UI runtime with it.
|
|
123
|
+
*
|
|
124
|
+
* `report`, when given, hears each gesture that ends successfully, once, after its own `onEnd`.
|
|
125
|
+
* On a device it is a worklet that schedules the report onto the JavaScript thread. `reported`
|
|
126
|
+
* narrows it to the gestures with those handler tags, and the rest do no reporting work.
|
|
87
127
|
*/
|
|
88
|
-
export function gestureDispatcher(callbacks, createManager) {
|
|
128
|
+
export function gestureDispatcher(callbacks, createManager, report, reported) {
|
|
89
129
|
// Captured, not passed: a worklet's closure is serialised once and reused, so this is how what
|
|
90
130
|
// one event knew survives to the next. Reanimated's own dispatcher keeps its state the same way.
|
|
91
|
-
const memory = { previous: {}, managers: {} };
|
|
131
|
+
const memory = { previous: {}, managers: {}, starts: {} };
|
|
92
132
|
return (event) => {
|
|
93
133
|
'worklet';
|
|
94
134
|
for (const gesture of callbacks) {
|
|
95
135
|
if (event.handlerTag !== gesture.handlerTag)
|
|
96
136
|
continue;
|
|
97
|
-
if (event.oldState != null)
|
|
137
|
+
if (event.oldState != null) {
|
|
98
138
|
stateChange(gesture, event, memory);
|
|
139
|
+
if (report && (!reported || reported.includes(gesture.handlerTag))) {
|
|
140
|
+
reportEnd(gesture.handlerTag, event, memory, report);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
99
143
|
else if (event.eventType != null)
|
|
100
144
|
touches(gesture, event, memory, createManager);
|
|
101
145
|
else
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gesture-dispatch.js","sourceRoot":"","sources":["../src/gesture-dispatch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,+CAA+C;AAC/C,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,YAAY,EAAE,CAAC;IACf,MAAM,EAAE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AAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AACxF,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAC/B,SAAsC,EACtC,aAA0D;IAE1D,+FAA+F;IAC/F,iGAAiG;IACjG,MAAM,MAAM,GAAW,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC;IACtD,OAAO,CAAC,KAAmB,EAAE,EAAE;QAC7B,SAAS,CAAC;QACV,KAAK,MAAM,OAAO,IAAI,SAAS,EAAE,CAAC;YAChC,IAAI,KAAK,CAAC,UAAU,KAAK,OAAO,CAAC,UAAU;gBAAE,SAAS;YACtD,IAAI,KAAK,CAAC,QAAQ,IAAI,IAAI;gBAAE,WAAW,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;iBAC3D,IAAI,KAAK,CAAC,SAAS,IAAI,IAAI;gBAAE,OAAO,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC;;gBAC5E,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * The state machine between a gesture's raw events and the callbacks it declared.\n *\n * This is what `useAnimatedGesture` does with hooks in react-native-gesture-handler, and the only\n * part of a gesture we write rather than borrow. It is here, apart from the library, so it can be\n * driven by a test: on a device it is serialised onto the UI runtime and a mistake in it is a\n * callback that silently never runs.\n *\n * The numbers below are the ones on the wire. They are the library's `State` and `TouchEventType`,\n * restated rather than imported because importing them would reach React Native's Flow source and\n * make this package unloadable in Node - and because they are a protocol between native and us,\n * not an API surface: native puts these integers in the event.\n */\n\n/** `State` in react-native-gesture-handler. */\nexport const GESTURE_STATE = {\n UNDETERMINED: 0,\n FAILED: 1,\n BEGAN: 2,\n CANCELLED: 3,\n ACTIVE: 4,\n END: 5,\n} as const;\n\n/** `TouchEventType` in react-native-gesture-handler. */\nexport const TOUCH_EVENT = {\n UNDETERMINED: 0,\n DOWN: 1,\n MOVE: 2,\n UP: 3,\n CANCELLED: 4,\n} as const;\n\n/** Lets a touch callback accept or reject the gesture from the UI thread. */\nexport interface GestureStateManager {\n begin(): void;\n activate(): void;\n fail(): void;\n end(): void;\n}\n\n/** A gesture's declared callbacks, as the library leaves them once it has prepared the gesture. */\nexport interface GestureCallbacks {\n handlerTag: number;\n onBegin?: (event: object) => void;\n onStart?: (event: object) => void;\n onUpdate?: (event: object) => void;\n onChange?: (event: object) => void;\n onEnd?: (event: object, success: boolean) => void;\n onFinalize?: (event: object, success: boolean) => void;\n onTouchesDown?: (event: object, manager: GestureStateManager) => void;\n onTouchesMove?: (event: object, manager: GestureStateManager) => void;\n onTouchesUp?: (event: object, manager: GestureStateManager) => void;\n onTouchesCancelled?: (event: object, manager: GestureStateManager) => void;\n changeEventCalculator?: (current: object, previous?: object) => object;\n}\n\n/** What native sends. Which of the three kinds it is, is told by which fields are present. */\nexport interface GestureEvent {\n handlerTag: number;\n state?: number;\n oldState?: number;\n eventType?: number;\n}\n\n/** Per-gesture memory the dispatcher keeps across events. */\ninterface Memory {\n previous: Record<number, object | undefined>;\n managers: Record<number, GestureStateManager | undefined>;\n}\n\nfunction stateChange(gesture: GestureCallbacks, event: GestureEvent, memory: Memory): void {\n 'worklet';\n const state = event.state as number;\n // A state-change event is a transition, so `oldState !== state` always holds and the only part\n // of the old state that matters is whether the gesture had gone active: that is what decides\n // between an end and a finalize alone.\n const wasActive = event.oldState === GESTURE_STATE.ACTIVE;\n if (state === GESTURE_STATE.BEGAN) {\n gesture.onBegin?.(event);\n } else if (state === GESTURE_STATE.ACTIVE) {\n gesture.onStart?.(event);\n memory.previous[gesture.handlerTag] = undefined;\n } else if (\n state === GESTURE_STATE.END ||\n state === GESTURE_STATE.FAILED ||\n state === GESTURE_STATE.CANCELLED\n ) {\n const succeeded = state === GESTURE_STATE.END;\n if (wasActive) gesture.onEnd?.(event, succeeded);\n gesture.onFinalize?.(event, succeeded);\n }\n}\n\nfunction update(gesture: GestureCallbacks, event: GestureEvent, memory: Memory): void {\n 'worklet';\n gesture.onUpdate?.(event);\n // `onChange` is `onUpdate` measured against the event before it, which is what makes a drag\n // accumulate rather than jump. The calculator belongs to the gesture; only the memory is ours.\n if (gesture.onChange && gesture.changeEventCalculator) {\n gesture.onChange(gesture.changeEventCalculator(event, memory.previous[gesture.handlerTag]));\n memory.previous[gesture.handlerTag] = event;\n }\n}\n\nfunction touches(\n gesture: GestureCallbacks,\n event: GestureEvent,\n memory: Memory,\n createManager: (handlerTag: number) => GestureStateManager,\n): void {\n 'worklet';\n let manager = memory.managers[gesture.handlerTag];\n if (!manager) {\n manager = createManager(gesture.handlerTag);\n memory.managers[gesture.handlerTag] = manager;\n }\n const type = event.eventType;\n if (type === TOUCH_EVENT.DOWN) gesture.onTouchesDown?.(event, manager);\n else if (type === TOUCH_EVENT.MOVE) gesture.onTouchesMove?.(event, manager);\n else if (type === TOUCH_EVENT.UP) gesture.onTouchesUp?.(event, manager);\n else if (type === TOUCH_EVENT.CANCELLED) gesture.onTouchesCancelled?.(event, manager);\n}\n\n/**\n * Build the worklet that receives a view's gesture events.\n *\n * `createManager` is the library's `GestureStateManager.create`, passed in rather than imported\n * for the reason the constants are restated. It has to be a worklet itself: everything this\n * closure holds is serialised onto the UI runtime with it.\n */\nexport function gestureDispatcher(\n callbacks: readonly GestureCallbacks[],\n createManager: (handlerTag: number) => GestureStateManager,\n): (event: GestureEvent) => void {\n // Captured, not passed: a worklet's closure is serialised once and reused, so this is how what\n // one event knew survives to the next. Reanimated's own dispatcher keeps its state the same way.\n const memory: Memory = { previous: {}, managers: {} };\n return (event: GestureEvent) => {\n 'worklet';\n for (const gesture of callbacks) {\n if (event.handlerTag !== gesture.handlerTag) continue;\n if (event.oldState != null) stateChange(gesture, event, memory);\n else if (event.eventType != null) touches(gesture, event, memory, createManager);\n else update(gesture, event, memory);\n }\n };\n}\n"]}
|
|
1
|
+
{"version":3,"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AAK,CAAC,SAAS,IAAI,IAAI;gBAAE,OAAO,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC;;gBAC9E,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * The state machine between a gesture's raw events and the callbacks it declared.\n *\n * This is what `useAnimatedGesture` does with hooks in react-native-gesture-handler, and the only\n * part of a gesture we write rather than borrow. It is here, apart from the library, so it can be\n * driven by a test: on a device it is serialised onto the UI runtime and a mistake in it is a\n * callback that silently never runs.\n *\n * The numbers below are the ones on the wire. They are the library's `State` and `TouchEventType`,\n * restated rather than imported because importing them would reach React Native's Flow source and\n * make this package unloadable in Node - and because they are a protocol between native and us,\n * not an API surface: native puts these integers in the event.\n */\nimport type { GestureEndValues } from './gesture-backend.ts';\n\n/** `State` in react-native-gesture-handler. */\nexport const GESTURE_STATE = {\n UNDETERMINED: 0,\n FAILED: 1,\n BEGAN: 2,\n CANCELLED: 3,\n ACTIVE: 4,\n END: 5,\n} as const;\n\n/** `TouchEventType` in react-native-gesture-handler. */\nexport const TOUCH_EVENT = {\n UNDETERMINED: 0,\n DOWN: 1,\n MOVE: 2,\n UP: 3,\n CANCELLED: 4,\n} as const;\n\n/** Lets a touch callback accept or reject the gesture from the UI thread. */\nexport interface GestureStateManager {\n begin(): void;\n activate(): void;\n fail(): void;\n end(): void;\n}\n\n/** A gesture's declared callbacks, as the library leaves them once it has prepared the gesture. */\nexport interface GestureCallbacks {\n handlerTag: number;\n onBegin?: (event: object) => void;\n onStart?: (event: object) => void;\n onUpdate?: (event: object) => void;\n onChange?: (event: object) => void;\n onEnd?: (event: object, success: boolean) => void;\n onFinalize?: (event: object, success: boolean) => void;\n onTouchesDown?: (event: object, manager: GestureStateManager) => void;\n onTouchesMove?: (event: object, manager: GestureStateManager) => void;\n onTouchesUp?: (event: object, manager: GestureStateManager) => void;\n onTouchesCancelled?: (event: object, manager: GestureStateManager) => void;\n changeEventCalculator?: (current: object, previous?: object) => object;\n}\n\n/** What native sends. Which of the three kinds it is, is told by which fields are present. */\nexport interface GestureEvent {\n handlerTag: number;\n state?: number;\n oldState?: number;\n eventType?: number;\n}\n\n/** Where a gesture's touch began in the window, for a report: a fling's end has no translation. */\ntype Start = { x?: number; y?: number };\n\n/** Per-gesture memory the dispatcher keeps across events. */\ninterface Memory {\n previous: Record<number, object | undefined>;\n managers: Record<number, GestureStateManager | undefined>;\n starts: Record<number, Start | undefined>;\n}\n\n/** Hears a gesture that ended successfully, with the numbers its report needs. */\nexport type GestureEndReport = (end: GestureEndValues) => void;\n\n/**\n * The numbers of an end event a report is built from, copied into a plain object: the event is\n * native's, and only plain values cross from the UI runtime to the JavaScript one.\n */\nfunction endValues(handlerTag: number, event: object, start: Start | undefined): GestureEndValues {\n 'worklet';\n const values: Record<string, number> = {};\n const from = event as Record<string, unknown>;\n for (const key of [\n 'x',\n 'y',\n 'absoluteX',\n 'absoluteY',\n 'translationX',\n 'translationY',\n 'velocityX',\n 'velocityY',\n 'scale',\n ]) {\n const value = from[key];\n if (typeof value === 'number') values[key] = value;\n }\n if (start?.x !== undefined) values['startX'] = start.x;\n if (start?.y !== undefined) values['startY'] = start.y;\n return { ...values, handlerTag };\n}\n\nfunction stateChange(gesture: GestureCallbacks, event: GestureEvent, memory: Memory): void {\n 'worklet';\n const state = event.state as number;\n // A state-change event is a transition, so `oldState !== state` always holds and the only part\n // of the old state that matters is whether the gesture had gone active: that is what decides\n // between an end and a finalize alone.\n const wasActive = event.oldState === GESTURE_STATE.ACTIVE;\n if (state === GESTURE_STATE.BEGAN) {\n gesture.onBegin?.(event);\n } else if (state === GESTURE_STATE.ACTIVE) {\n gesture.onStart?.(event);\n memory.previous[gesture.handlerTag] = undefined;\n } else if (\n state === GESTURE_STATE.END ||\n state === GESTURE_STATE.FAILED ||\n state === GESTURE_STATE.CANCELLED\n ) {\n const succeeded = state === GESTURE_STATE.END;\n if (wasActive) gesture.onEnd?.(event, succeeded);\n gesture.onFinalize?.(event, succeeded);\n }\n}\n\n/**\n * Report a gesture that ended successfully: an end from active, as `onEnd` with success is. A tap\n * that failed, or a pan a scroll view took over, did nothing. Run after the gesture's own\n * callbacks, as an observer hears a press after its handler.\n */\nfunction reportEnd(\n handlerTag: number,\n event: GestureEvent,\n memory: Memory,\n report: GestureEndReport,\n): void {\n 'worklet';\n const state = event.state;\n if (state === GESTURE_STATE.BEGAN) {\n const at = event as { absoluteX?: number; absoluteY?: number };\n memory.starts[handlerTag] = { x: at.absoluteX, y: at.absoluteY };\n return;\n }\n if (state === GESTURE_STATE.ACTIVE) return;\n if (state === GESTURE_STATE.END && event.oldState === GESTURE_STATE.ACTIVE) {\n report(endValues(handlerTag, event, memory.starts[handlerTag]));\n }\n memory.starts[handlerTag] = undefined;\n}\n\nfunction update(gesture: GestureCallbacks, event: GestureEvent, memory: Memory): void {\n 'worklet';\n gesture.onUpdate?.(event);\n // `onChange` is `onUpdate` measured against the event before it, which is what makes a drag\n // accumulate rather than jump. The calculator belongs to the gesture; only the memory is ours.\n if (gesture.onChange && gesture.changeEventCalculator) {\n gesture.onChange(gesture.changeEventCalculator(event, memory.previous[gesture.handlerTag]));\n memory.previous[gesture.handlerTag] = event;\n }\n}\n\nfunction touches(\n gesture: GestureCallbacks,\n event: GestureEvent,\n memory: Memory,\n createManager: (handlerTag: number) => GestureStateManager,\n): void {\n 'worklet';\n let manager = memory.managers[gesture.handlerTag];\n if (!manager) {\n manager = createManager(gesture.handlerTag);\n memory.managers[gesture.handlerTag] = manager;\n }\n const type = event.eventType;\n if (type === TOUCH_EVENT.DOWN) gesture.onTouchesDown?.(event, manager);\n else if (type === TOUCH_EVENT.MOVE) gesture.onTouchesMove?.(event, manager);\n else if (type === TOUCH_EVENT.UP) gesture.onTouchesUp?.(event, manager);\n else if (type === TOUCH_EVENT.CANCELLED) gesture.onTouchesCancelled?.(event, manager);\n}\n\n/**\n * Build the worklet that receives a view's gesture events.\n *\n * `createManager` is the library's `GestureStateManager.create`, passed in rather than imported\n * for the reason the constants are restated. It has to be a worklet itself: everything this\n * closure holds is serialised onto the UI runtime with it.\n *\n * `report`, when given, hears each gesture that ends successfully, once, after its own `onEnd`.\n * On a device it is a worklet that schedules the report onto the JavaScript thread. `reported`\n * narrows it to the gestures with those handler tags, and the rest do no reporting work.\n */\nexport function gestureDispatcher(\n callbacks: readonly GestureCallbacks[],\n createManager: (handlerTag: number) => GestureStateManager,\n report?: GestureEndReport,\n reported?: readonly number[],\n): (event: GestureEvent) => void {\n // Captured, not passed: a worklet's closure is serialised once and reused, so this is how what\n // one event knew survives to the next. Reanimated's own dispatcher keeps its state the same way.\n const memory: Memory = { previous: {}, managers: {}, starts: {} };\n return (event: GestureEvent) => {\n 'worklet';\n for (const gesture of callbacks) {\n if (event.handlerTag !== gesture.handlerTag) continue;\n if (event.oldState != null) {\n stateChange(gesture, event, memory);\n if (report && (!reported || reported.includes(gesture.handlerTag))) {\n reportEnd(gesture.handlerTag, event, memory, report);\n }\n } else if (event.eventType != null) touches(gesture, event, memory, createManager);\n else update(gesture, event, memory);\n }\n };\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A native gesture that ended, as the parts of an `InteractionEvent` the gesture decides.
|
|
3
|
+
*
|
|
4
|
+
* A tap is a `click` and a long press a `long-press`, as on a `<pressable>`. A fling is a `swipe`,
|
|
5
|
+
* and so is a pan released at a flick's speed; a slower pan, a pinch, a rotation and anything else
|
|
6
|
+
* is a `gesture`. `gesture` always names the kind, so an observer can tell a tap gesture from a
|
|
7
|
+
* pressed control.
|
|
8
|
+
*/
|
|
9
|
+
import type { EndedGesture } from './gesture-backend.ts';
|
|
10
|
+
import { type InteractionEvent } from './interaction-observer.ts';
|
|
11
|
+
export type GestureInteraction = Pick<InteractionEvent, 'type' | 'gesture' | 'direction' | 'distance' | 'scale' | 'pageX' | 'pageY' | 'locationX' | 'locationY'>;
|
|
12
|
+
/** The event's parts for a gesture that ended, or null for one that is not reported. */
|
|
13
|
+
export declare function gestureInteraction(ended: EndedGesture): GestureInteraction | null;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { SWIPE_VELOCITY, } from "./interaction-observer.js";
|
|
2
|
+
/** The library's `Directions` bits. */
|
|
3
|
+
const DIRECTIONS = {
|
|
4
|
+
1: 'right',
|
|
5
|
+
2: 'left',
|
|
6
|
+
4: 'up',
|
|
7
|
+
8: 'down',
|
|
8
|
+
};
|
|
9
|
+
/** A fling's default, as the library documents it. */
|
|
10
|
+
const FLING_DEFAULT = 1;
|
|
11
|
+
/**
|
|
12
|
+
* Gestures that are not an interaction of their own: a pointer passing over a view, and a native
|
|
13
|
+
* view's own touch handling, which that view (a scroll view, a control) reports itself.
|
|
14
|
+
*/
|
|
15
|
+
const UNREPORTED = new Set(['hover', 'native-view']);
|
|
16
|
+
/** `LongPressGestureHandler` as `long-press`. */
|
|
17
|
+
function gestureKind(handlerName) {
|
|
18
|
+
return handlerName
|
|
19
|
+
.replace(/GestureHandler$/, '')
|
|
20
|
+
.replace(/([a-z])([A-Z])/g, '$1-$2')
|
|
21
|
+
.toLowerCase();
|
|
22
|
+
}
|
|
23
|
+
/** Which way the finger moved, by the axis it moved further along, or undefined if it did not. */
|
|
24
|
+
function fingerDirection(dx, dy) {
|
|
25
|
+
if (dx === 0 && dy === 0)
|
|
26
|
+
return undefined;
|
|
27
|
+
if (Math.abs(dx) > Math.abs(dy))
|
|
28
|
+
return dx > 0 ? 'right' : 'left';
|
|
29
|
+
return dy > 0 ? 'down' : 'up';
|
|
30
|
+
}
|
|
31
|
+
/** How far the finger moved from where it came down, or undefined without both points. */
|
|
32
|
+
function fingerMoved(end) {
|
|
33
|
+
const { absoluteX, absoluteY, startX, startY } = end;
|
|
34
|
+
if (absoluteX === undefined || absoluteY === undefined)
|
|
35
|
+
return undefined;
|
|
36
|
+
if (startX === undefined || startY === undefined)
|
|
37
|
+
return undefined;
|
|
38
|
+
return { dx: absoluteX - startX, dy: absoluteY - startY };
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A fling's direction: the one it was set to recognise, or with several, the way the finger
|
|
42
|
+
* moved from where it came down, since its end event carries no translation.
|
|
43
|
+
*/
|
|
44
|
+
function flingDirection(ended, moved) {
|
|
45
|
+
const only = DIRECTIONS[ended.directions ?? FLING_DEFAULT];
|
|
46
|
+
if (only)
|
|
47
|
+
return only;
|
|
48
|
+
return moved && fingerDirection(moved.dx, moved.dy);
|
|
49
|
+
}
|
|
50
|
+
/** Where a tap or long press landed, as a press reports it. */
|
|
51
|
+
function pressed(type, end) {
|
|
52
|
+
return {
|
|
53
|
+
type,
|
|
54
|
+
...(end.absoluteX !== undefined && { pageX: end.absoluteX }),
|
|
55
|
+
...(end.absoluteY !== undefined && { pageY: end.absoluteY }),
|
|
56
|
+
...(end.x !== undefined && { locationX: end.x }),
|
|
57
|
+
...(end.y !== undefined && { locationY: end.y }),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function panned(end) {
|
|
61
|
+
const dx = end.translationX ?? 0;
|
|
62
|
+
const dy = end.translationY ?? 0;
|
|
63
|
+
// The library's velocities are points per second, and `SWIPE_VELOCITY` is points per
|
|
64
|
+
// millisecond: the threshold a scroll view's drag is held to, so a flick through a carousel
|
|
65
|
+
// built on a pan and a flick through a list are told apart the same way.
|
|
66
|
+
const speed = Math.hypot(end.velocityX ?? 0, end.velocityY ?? 0) / 1000;
|
|
67
|
+
const direction = fingerDirection(dx, dy);
|
|
68
|
+
return {
|
|
69
|
+
type: direction && speed >= SWIPE_VELOCITY ? 'swipe' : 'gesture',
|
|
70
|
+
...(direction && { direction }),
|
|
71
|
+
distance: Math.round(Math.hypot(dx, dy)),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const DETAIL = {
|
|
75
|
+
tap: ({ end }) => pressed('click', end),
|
|
76
|
+
'long-press': ({ end }) => pressed('long-press', end),
|
|
77
|
+
pan: ({ end }) => panned(end),
|
|
78
|
+
fling: (ended) => {
|
|
79
|
+
const moved = fingerMoved(ended.end);
|
|
80
|
+
const direction = flingDirection(ended, moved);
|
|
81
|
+
return {
|
|
82
|
+
type: 'swipe',
|
|
83
|
+
...(direction && { direction }),
|
|
84
|
+
...(moved && { distance: Math.round(Math.hypot(moved.dx, moved.dy)) }),
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
pinch: ({ end }) => ({ type: 'gesture', ...(end.scale !== undefined && { scale: end.scale }) }),
|
|
88
|
+
};
|
|
89
|
+
/** The event's parts for a gesture that ended, or null for one that is not reported. */
|
|
90
|
+
export function gestureInteraction(ended) {
|
|
91
|
+
const gesture = gestureKind(ended.handlerName);
|
|
92
|
+
if (UNREPORTED.has(gesture))
|
|
93
|
+
return null;
|
|
94
|
+
const { type, ...detail } = DETAIL[gesture]?.(ended) ?? { type: 'gesture' };
|
|
95
|
+
return { type, gesture, ...detail };
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=gesture-interaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gesture-interaction.js","sourceRoot":"","sources":["../src/gesture-interaction.ts"],"names":[],"mappings":"AASA,OAAO,EACL,cAAc,GAGf,MAAM,2BAA2B,CAAC;AAEnC,uCAAuC;AACvC,MAAM,UAAU,GAAyC;IACvD,CAAC,EAAE,OAAO;IACV,CAAC,EAAE,MAAM;IACT,CAAC,EAAE,IAAI;IACP,CAAC,EAAE,MAAM;CACV,CAAC;AAEF,sDAAsD;AACtD,MAAM,aAAa,GAAG,CAAC,CAAC;AAExB;;;GAGG;AACH,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC;AAerD,iDAAiD;AACjD,SAAS,WAAW,CAAC,WAAmB;IACtC,OAAO,WAAW;SACf,OAAO,CAAC,iBAAiB,EAAE,EAAE,CAAC;SAC9B,OAAO,CAAC,iBAA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AAG,MAAM,EAAE,CAAC;AACtC,CAAC","sourcesContent":["/**\n * A native gesture that ended, as the parts of an `InteractionEvent` the gesture decides.\n *\n * A tap is a `click` and a long press a `long-press`, as on a `<pressable>`. A fling is a `swipe`,\n * and so is a pan released at a flick's speed; a slower pan, a pinch, a rotation and anything else\n * is a `gesture`. `gesture` always names the kind, so an observer can tell a tap gesture from a\n * pressed control.\n */\nimport type { EndedGesture, GestureEndValues } from './gesture-backend.ts';\nimport {\n SWIPE_VELOCITY,\n type InteractionDirection,\n type InteractionEvent,\n} from './interaction-observer.ts';\n\n/** The library's `Directions` bits. */\nconst DIRECTIONS: Record<number, InteractionDirection> = {\n 1: 'right',\n 2: 'left',\n 4: 'up',\n 8: 'down',\n};\n\n/** A fling's default, as the library documents it. */\nconst FLING_DEFAULT = 1;\n\n/**\n * Gestures that are not an interaction of their own: a pointer passing over a view, and a native\n * view's own touch handling, which that view (a scroll view, a control) reports itself.\n */\nconst UNREPORTED = new Set(['hover', 'native-view']);\n\nexport type GestureInteraction = Pick<\n InteractionEvent,\n | 'type'\n | 'gesture'\n | 'direction'\n | 'distance'\n | 'scale'\n | 'pageX'\n | 'pageY'\n | 'locationX'\n | 'locationY'\n>;\n\n/** `LongPressGestureHandler` as `long-press`. */\nfunction gestureKind(handlerName: string): string {\n return handlerName\n .replace(/GestureHandler$/, '')\n .replace(/([a-z])([A-Z])/g, '$1-$2')\n .toLowerCase();\n}\n\n/** Which way the finger moved, by the axis it moved further along, or undefined if it did not. */\nfunction fingerDirection(dx: number, dy: number): InteractionDirection | undefined {\n if (dx === 0 && dy === 0) return undefined;\n if (Math.abs(dx) > Math.abs(dy)) return dx > 0 ? 'right' : 'left';\n return dy > 0 ? 'down' : 'up';\n}\n\n/** How far the finger moved from where it came down, or undefined without both points. */\nfunction fingerMoved(end: GestureEndValues): { dx: number; dy: number } | undefined {\n const { absoluteX, absoluteY, startX, startY } = end;\n if (absoluteX === undefined || absoluteY === undefined) return undefined;\n if (startX === undefined || startY === undefined) return undefined;\n return { dx: absoluteX - startX, dy: absoluteY - startY };\n}\n\n/**\n * A fling's direction: the one it was set to recognise, or with several, the way the finger\n * moved from where it came down, since its end event carries no translation.\n */\nfunction flingDirection(\n ended: EndedGesture,\n moved: { dx: number; dy: number } | undefined,\n): InteractionDirection | undefined {\n const only = DIRECTIONS[ended.directions ?? FLING_DEFAULT];\n if (only) return only;\n return moved && fingerDirection(moved.dx, moved.dy);\n}\n\ntype Detail = Omit<GestureInteraction, 'gesture'>;\n\n/** Where a tap or long press landed, as a press reports it. */\nfunction pressed(type: 'click' | 'long-press', end: GestureEndValues): Detail {\n return {\n type,\n ...(end.absoluteX !== undefined && { pageX: end.absoluteX }),\n ...(end.absoluteY !== undefined && { pageY: end.absoluteY }),\n ...(end.x !== undefined && { locationX: end.x }),\n ...(end.y !== undefined && { locationY: end.y }),\n };\n}\n\nfunction panned(end: GestureEndValues): Detail {\n const dx = end.translationX ?? 0;\n const dy = end.translationY ?? 0;\n // The library's velocities are points per second, and `SWIPE_VELOCITY` is points per\n // millisecond: the threshold a scroll view's drag is held to, so a flick through a carousel\n // built on a pan and a flick through a list are told apart the same way.\n const speed = Math.hypot(end.velocityX ?? 0, end.velocityY ?? 0) / 1000;\n const direction = fingerDirection(dx, dy);\n return {\n type: direction && speed >= SWIPE_VELOCITY ? 'swipe' : 'gesture',\n ...(direction && { direction }),\n distance: Math.round(Math.hypot(dx, dy)),\n };\n}\n\nconst DETAIL: Record<string, (ended: EndedGesture) => Detail> = {\n tap: ({ end }) => pressed('click', end),\n 'long-press': ({ end }) => pressed('long-press', end),\n pan: ({ end }) => panned(end),\n fling: (ended) => {\n const moved = fingerMoved(ended.end);\n const direction = flingDirection(ended, moved);\n return {\n type: 'swipe',\n ...(direction && { direction }),\n ...(moved && { distance: Math.round(Math.hypot(moved.dx, moved.dy)) }),\n };\n },\n pinch: ({ end }) => ({ type: 'gesture', ...(end.scale !== undefined && { scale: end.scale }) }),\n};\n\n/** The event's parts for a gesture that ended, or null for one that is not reported. */\nexport function gestureInteraction(ended: EndedGesture): GestureInteraction | null {\n const gesture = gestureKind(ended.handlerName);\n if (UNREPORTED.has(gesture)) return null;\n const { type, ...detail } = DETAIL[gesture]?.(ended) ?? { type: 'gesture' };\n return { type, gesture, ...detail };\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `untrackedGesture(gesture)`: the per-gesture form of `[trackGesture]="false"`, for one gesture
|
|
3
|
+
* of a composed one.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* Gesture.Race(Gesture.Tap(), untrackedGesture(Gesture.Pan()));
|
|
7
|
+
* ```
|
|
8
|
+
*
|
|
9
|
+
* The gesture is recorded here rather than on the gesture object, so the library's classes stay
|
|
10
|
+
* as they are, and the backend reads the record when the gesture is attached.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Keep `gesture` out of interaction reports, and return it. A composed gesture keeps every
|
|
14
|
+
* gesture it is made of out.
|
|
15
|
+
*/
|
|
16
|
+
export declare function untrackedGesture<T extends object>(gesture: T): T;
|
|
17
|
+
/** The gestures of `gestures` that interaction observers hear: all but the untracked ones. */
|
|
18
|
+
export declare function trackedGestures<T extends object>(gestures: readonly T[]): T[];
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `untrackedGesture(gesture)`: the per-gesture form of `[trackGesture]="false"`, for one gesture
|
|
3
|
+
* of a composed one.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* Gesture.Race(Gesture.Tap(), untrackedGesture(Gesture.Pan()));
|
|
7
|
+
* ```
|
|
8
|
+
*
|
|
9
|
+
* The gesture is recorded here rather than on the gesture object, so the library's classes stay
|
|
10
|
+
* as they are, and the backend reads the record when the gesture is attached.
|
|
11
|
+
*/
|
|
12
|
+
/** The gestures `untrackedGesture` was given, and the gestures inside a composed one among them. */
|
|
13
|
+
const UNTRACKED = new WeakSet();
|
|
14
|
+
/**
|
|
15
|
+
* Keep `gesture` out of interaction reports, and return it. A composed gesture keeps every
|
|
16
|
+
* gesture it is made of out.
|
|
17
|
+
*/
|
|
18
|
+
export function untrackedGesture(gesture) {
|
|
19
|
+
UNTRACKED.add(gesture);
|
|
20
|
+
for (const part of gesture.toGestureArray?.() ?? [])
|
|
21
|
+
UNTRACKED.add(part);
|
|
22
|
+
return gesture;
|
|
23
|
+
}
|
|
24
|
+
/** The gestures of `gestures` that interaction observers hear: all but the untracked ones. */
|
|
25
|
+
export function trackedGestures(gestures) {
|
|
26
|
+
return gestures.filter((gesture) => !UNTRACKED.has(gesture));
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=gesture-tracking.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gesture-tracking.js","sourceRoot":"","sources":["../src/gesture-tracking.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,oGAAoG;AACpG,MAAM,SAAS,GAAG,IAAI,OAAO,EAAU,CAAC;AAOxC;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAmB,OAAU;IAC3D,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACvB,KAAK,MAAM,IAAI,IAAK,OAAsB,CAAC,cAAc,EAAE,EAAE,IAAI,EAAE;QAAE,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACzF,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,8FAA8F;AAC9F,MAAM,UAAU,eAAe,CAAmB,QAAsB;IACtE,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC;AAC/D,CAAC","sourcesContent":["/**\n * `untrackedGesture(gesture)`: the per-gesture form of `[trackGesture]=\"false\"`, for one gesture\n * of a composed one.\n *\n * ```ts\n * Gesture.Race(Gesture.Tap(), untrackedGesture(Gesture.Pan()));\n * ```\n *\n * The gesture is recorded here rather than on the gesture object, so the library's classes stay\n * as they are, and the backend reads the record when the gesture is attached.\n */\n\n/** The gestures `untrackedGesture` was given, and the gestures inside a composed one among them. */\nconst UNTRACKED = new WeakSet<object>();\n\n/** What the library's gestures, single and composed, have: the single gestures they attach. */\ninterface Composable {\n toGestureArray?(): readonly object[];\n}\n\n/**\n * Keep `gesture` out of interaction reports, and return it. A composed gesture keeps every\n * gesture it is made of out.\n */\nexport function untrackedGesture<T extends object>(gesture: T): T {\n UNTRACKED.add(gesture);\n for (const part of (gesture as Composable).toGestureArray?.() ?? []) UNTRACKED.add(part);\n return gesture;\n}\n\n/** The gestures of `gestures` that interaction observers hear: all but the untracked ones. */\nexport function trackedGestures<T extends object>(gestures: readonly T[]): T[] {\n return gestures.filter((gesture) => !UNTRACKED.has(gesture));\n}\n"]}
|
package/dist/gestures-web.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import * as i0 from "@angular/core";
|
|
2
2
|
export { type GestureSpec, type GestureTarget } from './gesture-backend.ts';
|
|
3
|
+
export { untrackedGesture } from './gesture-tracking.ts';
|
|
3
4
|
export declare class NativeGesture {
|
|
4
5
|
readonly gesture: import("@angular/core").InputSignal<object>;
|
|
6
|
+
readonly trackGesture: import("@angular/core").InputSignalWithTransform<boolean, unknown>;
|
|
5
7
|
static ɵfac: i0.ɵɵFactoryDeclaration<NativeGesture, never>;
|
|
6
|
-
static ɵdir: i0.ɵɵDirectiveDeclaration<NativeGesture, "[gesture]", never, { "gesture": { "alias": "gesture"; "required": true; "isSignal": true; }; }, {}, never, never, true, never>;
|
|
8
|
+
static ɵdir: i0.ɵɵDirectiveDeclaration<NativeGesture, "[gesture]", never, { "gesture": { "alias": "gesture"; "required": true; "isSignal": true; }; "trackGesture": { "alias": "trackGesture"; "required": false; "isSignal": true; }; }, {}, never, never, true, never>;
|
|
7
9
|
}
|
|
8
10
|
/** A box that fills its parent, as the native root does. `@ng-native/web`'s reset gives it `flex: 1`. */
|
|
9
11
|
export declare class GestureRoot {
|
package/dist/gestures-web.js
CHANGED
|
@@ -8,18 +8,21 @@
|
|
|
8
8
|
* attaches nothing.
|
|
9
9
|
*/
|
|
10
10
|
import { Component, Directive, input } from '@angular/core';
|
|
11
|
+
import { booleanOr } from "./transforms.js";
|
|
11
12
|
import * as i0 from "@angular/core";
|
|
12
13
|
export {} from "./gesture-backend.js";
|
|
14
|
+
export { untrackedGesture } from "./gesture-tracking.js";
|
|
13
15
|
export class NativeGesture {
|
|
14
16
|
gesture = input.required(/* @ts-ignore */
|
|
15
17
|
...(ngDevMode ? [{ debugName: "gesture" }] : /* istanbul ignore next */ []));
|
|
18
|
+
trackGesture = input(true, { ...(ngDevMode ? { debugName: "trackGesture" } : /* istanbul ignore next */ {}), transform: booleanOr(true) });
|
|
16
19
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.0", ngImport: i0, type: NativeGesture, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
17
|
-
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.2.0", type: NativeGesture, isStandalone: true, selector: "[gesture]", inputs: { gesture: { classPropertyName: "gesture", publicName: "gesture", isSignal: true, isRequired: true, transformFunction: null } }, ngImport: i0 });
|
|
20
|
+
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.2.0", type: NativeGesture, isStandalone: true, selector: "[gesture]", inputs: { gesture: { classPropertyName: "gesture", publicName: "gesture", isSignal: true, isRequired: true, transformFunction: null }, trackGesture: { classPropertyName: "trackGesture", publicName: "trackGesture", isSignal: true, isRequired: false, transformFunction: null } }, ngImport: i0 });
|
|
18
21
|
}
|
|
19
22
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.0", ngImport: i0, type: NativeGesture, decorators: [{
|
|
20
23
|
type: Directive,
|
|
21
24
|
args: [{ selector: '[gesture]' }]
|
|
22
|
-
}], propDecorators: { gesture: [{ type: i0.Input, args: [{ isSignal: true, alias: "gesture", required: true }] }] } });
|
|
25
|
+
}], propDecorators: { gesture: [{ type: i0.Input, args: [{ isSignal: true, alias: "gesture", required: true }] }], trackGesture: [{ type: i0.Input, args: [{ isSignal: true, alias: "trackGesture", required: false }] }] } });
|
|
23
26
|
/** A box that fills its parent, as the native root does. `@ng-native/web`'s reset gives it `flex: 1`. */
|
|
24
27
|
export class GestureRoot {
|
|
25
28
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.0", ngImport: i0, type: GestureRoot, deps: [], target: i0.ɵɵFactoryTarget.Component });
|
package/dist/gestures-web.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gestures-web.js","sourceRoot":"","sources":["../src/gestures-web.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;;
|
|
1
|
+
{"version":3,"file":"gestures-web.js","sourceRoot":"","sources":["../src/gestures-web.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAE5D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;;AAE5C,OAAO,EAAwC,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAGzD,MAAM,OAAO,aAAa;IACf,OAAO,GAAG,KAAK,CAAC,QAAQ;gFAAe,CAAC;IACxC,YAAY,GAAG,KAAK,CAAC,IAAI,oFAAI,SAAS,EAAE,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC;uGAFzD,aAAa;2FAAb,aAAa;;2FAAb,aAAa;kBADzB,SAAS;mBAAC,EAAE,QAAQ,EAAE,WAAW,EAAE;;AAMpC,yGAAyG;AAEzG,MAAM,OAAO,WAAW;uGAAX,WAAW;2FAAX,WAAW,wEADyB,gBAAgB;;2FACpD,WAAW;kBADvB,SAAS;mBAAC,EAAE,QAAQ,EAAE,cAAc,EAAE,QAAQ,EAAE,gBAAgB,EAAE","sourcesContent":["/**\n * `gestures.ts`, for a browser build.\n *\n * A web bundler resolves `@ng-native/components/gestures` here, through the `browser` condition in\n * this package's `exports`; Metro, on a device, never sets that condition and gets the native file.\n * A browser has no native gesture recognisers, so everything here is inert: the names are the\n * same, a template that uses `<gesture-root>` and `[gesture]` still renders, and `[gesture]`\n * attaches nothing.\n */\nimport { Component, Directive, input } from '@angular/core';\nimport type { GestureSpec } from './gesture-backend.ts';\nimport { booleanOr } from './transforms.ts';\n\nexport { type GestureSpec, type GestureTarget } from './gesture-backend.ts';\nexport { untrackedGesture } from './gesture-tracking.ts';\n\n@Directive({ selector: '[gesture]' })\nexport class NativeGesture {\n readonly gesture = input.required<GestureSpec>();\n readonly trackGesture = input(true, { transform: booleanOr(true) });\n}\n\n/** A box that fills its parent, as the native root does. `@ng-native/web`'s reset gives it `flex: 1`. */\n@Component({ selector: 'gesture-root', template: '<ng-content />' })\nexport class GestureRoot {}\n"]}
|
package/dist/gestures.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { NativeGestureBase } from './native-gesture.ts';
|
|
2
2
|
import * as i0 from "@angular/core";
|
|
3
3
|
export { type GestureSpec, type GestureTarget } from './gesture-backend.ts';
|
|
4
|
+
export { untrackedGesture } from './gesture-tracking.ts';
|
|
4
5
|
export declare class NativeGesture extends NativeGestureBase {
|
|
5
6
|
static ɵfac: i0.ɵɵFactoryDeclaration<NativeGesture, never>;
|
|
6
7
|
static ɵdir: i0.ɵɵDirectiveDeclaration<NativeGesture, "[gesture]", never, {}, {}, never, never, true, never>;
|
package/dist/gestures.js
CHANGED
|
@@ -48,8 +48,9 @@
|
|
|
48
48
|
import { Component, Directive, ElementRef, inject } from '@angular/core';
|
|
49
49
|
import { claimHost, nativePlatform, registerViewName } from '@ng-native/fabric';
|
|
50
50
|
import { gestureDispatcher, } from "./gesture-dispatch.js";
|
|
51
|
-
import { GESTURES } from "./gesture-backend.js";
|
|
51
|
+
import { GESTURES, } from "./gesture-backend.js";
|
|
52
52
|
import { NativeGestureBase } from "./native-gesture.js";
|
|
53
|
+
import { trackedGestures } from "./gesture-tracking.js";
|
|
53
54
|
import * as i0 from "@angular/core";
|
|
54
55
|
// For the side effect: evaluating the package's index is what starts its event receiver, which
|
|
55
56
|
// is what delivers a gesture whose callbacks are ordinary functions. An app importing `Gesture`
|
|
@@ -57,6 +58,7 @@ import * as i0 from "@angular/core";
|
|
|
57
58
|
// name a library a web app may not have installed.
|
|
58
59
|
require('react-native-gesture-handler');
|
|
59
60
|
export {} from "./gesture-backend.js";
|
|
61
|
+
export { untrackedGesture } from "./gesture-tracking.js";
|
|
60
62
|
/**
|
|
61
63
|
* The internals, required rather than imported, and each with a literal path because Metro builds
|
|
62
64
|
* its graph from those.
|
|
@@ -79,6 +81,16 @@ const { GestureStateManager } = require('react-native-gesture-handler/src/handle
|
|
|
79
81
|
* view tag, which is all a gesture needs to reach the UI runtime. Not public either.
|
|
80
82
|
*/
|
|
81
83
|
const { registerEventHandler, unregisterEventHandler } = require('react-native-reanimated/src/core.ts');
|
|
84
|
+
/**
|
|
85
|
+
* How a report leaves the UI runtime: the call a worklet makes to run a function on the
|
|
86
|
+
* JavaScript thread. Reanimated depends on the package, so an app with worklet gestures has it.
|
|
87
|
+
*/
|
|
88
|
+
const { scheduleOnRN } = require('react-native-worklets');
|
|
89
|
+
/**
|
|
90
|
+
* Where native sends the events of a gesture whose callbacks run on the JavaScript thread. The
|
|
91
|
+
* library's own receiver listens here too; a second listener only observes.
|
|
92
|
+
*/
|
|
93
|
+
const { DeviceEventEmitter } = require('react-native');
|
|
82
94
|
const EVENT_NAMES = ['onGestureHandlerStateChange', 'onGestureHandlerEvent'];
|
|
83
95
|
/**
|
|
84
96
|
* A gesture's own state controller, for the touch callbacks: a worklet that accepts or rejects
|
|
@@ -89,8 +101,35 @@ const createManager = (handlerTag) => {
|
|
|
89
101
|
'worklet';
|
|
90
102
|
return GestureStateManager.create(handlerTag);
|
|
91
103
|
};
|
|
104
|
+
/**
|
|
105
|
+
* Hand each of `gestures` that ends successfully to `report`: worklet gestures from the UI runtime
|
|
106
|
+
* through `fromUI`, the rest through a listener of ours beside the library's. Both are fed to the
|
|
107
|
+
* same state machine the callbacks are, so a report means what `onEnd` with success means.
|
|
108
|
+
*/
|
|
109
|
+
function reporting(gestures, report) {
|
|
110
|
+
const byTag = new Map(gestures.map((g) => [g.handlers.handlerTag, g]));
|
|
111
|
+
const onJS = (end) => {
|
|
112
|
+
const g = byTag.get(end.handlerTag);
|
|
113
|
+
if (g)
|
|
114
|
+
report({ handlerName: g.handlerName, directions: g.config.direction, end });
|
|
115
|
+
};
|
|
116
|
+
const fromUI = (end) => {
|
|
117
|
+
'worklet';
|
|
118
|
+
scheduleOnRN(onJS, end);
|
|
119
|
+
};
|
|
120
|
+
const onThread = gestures.filter((g) => !g.shouldUseReanimated);
|
|
121
|
+
if (!onThread.length)
|
|
122
|
+
return { fromUI, stop: () => { } };
|
|
123
|
+
const observe = gestureDispatcher(onThread.map((g) => ({ handlerTag: g.handlers.handlerTag })), createManager, onJS);
|
|
124
|
+
const listeners = EVENT_NAMES.map((name) => DeviceEventEmitter.addListener(name, (event) => {
|
|
125
|
+
// State changes only: the touch callbacks are the library's to run here, not ours.
|
|
126
|
+
if (event.oldState != null)
|
|
127
|
+
observe(event);
|
|
128
|
+
}));
|
|
129
|
+
return { fromUI, stop: () => listeners.forEach((listener) => listener.remove()) };
|
|
130
|
+
}
|
|
92
131
|
const backend = {
|
|
93
|
-
attach: (target, gesture) => {
|
|
132
|
+
attach: (target, gesture, report) => {
|
|
94
133
|
maybeInitializeFabric();
|
|
95
134
|
const config = gesture;
|
|
96
135
|
const gesturesToAttach = config.toGestureArray();
|
|
@@ -115,7 +154,9 @@ const backend = {
|
|
|
115
154
|
// One dispatcher for both event names, as the library's own event handler is: a gesture's
|
|
116
155
|
// state changes and its updates arrive on different names and share the same state.
|
|
117
156
|
const animated = gesturesToAttach.filter((g) => g.shouldUseReanimated);
|
|
118
|
-
const
|
|
157
|
+
const tracked = report ? trackedGestures(gesturesToAttach) : [];
|
|
158
|
+
const reports = report && tracked.length ? reporting(tracked, report) : null;
|
|
159
|
+
const dispatch = gestureDispatcher(animated.map((g) => g.handlers), createManager, reports?.fromUI, tracked.map((g) => g.handlers.handlerTag));
|
|
119
160
|
const registrations = animated.length
|
|
120
161
|
? EVENT_NAMES.map((name) => registerEventHandler(dispatch, name, target.tag))
|
|
121
162
|
: [];
|
|
@@ -123,6 +164,7 @@ const backend = {
|
|
|
123
164
|
prepared.isMounted = false;
|
|
124
165
|
for (const id of registrations)
|
|
125
166
|
unregisterEventHandler(id);
|
|
167
|
+
reports?.stop();
|
|
126
168
|
dropHandlers(prepared);
|
|
127
169
|
};
|
|
128
170
|
},
|