Repository navigation
Expand file tree
/
Copy pathinteract.ts
More file actions
75 lines (66 loc) · 2.05 KB
/
Copy pathinteract.ts
File metadata and controls
75 lines (66 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { GestureTypes, View } from '@nativescript/core';
import { nextRenderPass } from './wait.js';
/**
* NativeScript routes `on('tap')` through two mutually exclusive paths:
* classes declaring a static `<name>Event` property (Button and friends)
* store plain listeners reachable via `notify`, while every other view wraps
* the callback in a native gesture observer that `notify` never reaches.
* Dispatching through both paths covers either kind without double-firing.
*/
function dispatchGesture(view: View, name: string, type: GestureTypes): void {
const eventData = {
eventName: name,
object: view,
view,
type,
ios: undefined,
android: undefined,
};
const observers = view.getGestureObservers(type);
observers?.forEach((observer) => {
const callback = observer.callback as
| ((data: unknown) => void)
| undefined;
callback?.call(observer.context, eventData);
});
view.notify(eventData);
}
export async function tap(view: View): Promise<void> {
dispatchGesture(view, 'tap', GestureTypes.tap);
await nextRenderPass();
}
export async function doubleTap(view: View): Promise<void> {
dispatchGesture(view, 'doubleTap', GestureTypes.doubleTap);
await nextRenderPass();
}
export async function longPress(view: View): Promise<void> {
dispatchGesture(view, 'longPress', GestureTypes.longPress);
await nextRenderPass();
}
interface TextInputView extends View {
text: string;
}
/**
* Simulates user text entry: programmatic `text` assignment alone does not
* fire `textChange` in NativeScript (mirroring native behavior), so the
* event is dispatched explicitly.
*/
export async function enterText(
view: TextInputView,
text: string,
): Promise<void> {
const oldValue = view.text;
view.text = text;
view.notify({
eventName: 'textChange',
object: view,
propertyName: 'text',
value: text,
oldValue,
});
await nextRenderPass();
}
export async function returnPress(view: View): Promise<void> {
view.notify({ eventName: 'returnPress', object: view });
await nextRenderPass();
}