Skip to content

Commit 3b77fff

Browse files
authored
fix(core): stop accepting GestureTypes enum as an eventName (#10537)
1 parent 415ff34 commit 3b77fff

File tree

7 files changed

+30
-138
lines changed

7 files changed

+30
-138
lines changed

apps/automated/src/ui/gestures/gestures-tests.ts

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
/* tslint:disable:no-unused-variable */
2-
import { GestureEventData, Label, GestureTypes, PanGestureEventData, PinchGestureEventData, SwipeGestureEventData, RotationGestureEventData } from '@nativescript/core';
3-
4-
export var test_DummyTestForSnippetOnly0 = function () {
5-
// >> gestures-double-tap
6-
var label = new Label();
7-
var observer = label.on(GestureTypes.doubleTap, function (args: GestureEventData) {
8-
console.log('Double Tap');
9-
});
10-
// << gestures-double-tap
11-
};
2+
import { GestureEventData, Label, PanGestureEventData, PinchGestureEventData, SwipeGestureEventData, RotationGestureEventData } from '@nativescript/core';
123

134
export var test_DummyTestForSnippetOnly01 = function () {
145
// >> gestures-double-tap-alt
@@ -19,15 +10,6 @@ export var test_DummyTestForSnippetOnly01 = function () {
1910
// << gestures-double-tap-alt
2011
};
2112

22-
export var test_DummyTestForSnippetOnly1 = function () {
23-
// >> gestures-long-press
24-
var label = new Label();
25-
var observer = label.on(GestureTypes.longPress, function (args: GestureEventData) {
26-
console.log('Long Press');
27-
});
28-
// << gestures-long-press
29-
};
30-
3113
export var test_DummyTestForSnippetOnly11 = function () {
3214
// >> gestures-long-press-alt
3315
var label = new Label();
@@ -37,15 +19,6 @@ export var test_DummyTestForSnippetOnly11 = function () {
3719
// << gestures-long-press-alt
3820
};
3921

40-
export var test_DummyTestForSnippetOnly2 = function () {
41-
// >> gestures-pan
42-
var label = new Label();
43-
var observer = label.on(GestureTypes.pan, function (args: PanGestureEventData) {
44-
console.log('Pan deltaX:' + args.deltaX + '; deltaY:' + args.deltaY + ';');
45-
});
46-
// << gestures-pan
47-
};
48-
4922
export var test_DummyTestForSnippetOnly22 = function () {
5023
// >> gestures-pan-alt
5124
var label = new Label();
@@ -55,15 +28,6 @@ export var test_DummyTestForSnippetOnly22 = function () {
5528
// << gestures-pan-alt
5629
};
5730

58-
export var test_DummyTestForSnippetOnly3 = function () {
59-
// >> gestures-pan-pinch
60-
var label = new Label();
61-
var observer = label.on(GestureTypes.pinch, function (args: PinchGestureEventData) {
62-
console.log('Pinch scale: ' + args.scale);
63-
});
64-
// << gestures-pan-pinch
65-
};
66-
6731
export var test_DummyTestForSnippetOnly33 = function () {
6832
// >> gestures-pan-pinch-alt
6933
var label = new Label();
@@ -73,15 +37,6 @@ export var test_DummyTestForSnippetOnly33 = function () {
7337
// << gestures-pan-pinch-alt
7438
};
7539

76-
export var test_DummyTestForSnippetOnly4 = function () {
77-
// >> gestures-rotation
78-
var label = new Label();
79-
var observer = label.on(GestureTypes.rotation, function (args: RotationGestureEventData) {
80-
console.log('Rotation: ' + args.rotation);
81-
});
82-
// << gestures-rotation
83-
};
84-
8540
export var test_DummyTestForSnippetOnly44 = function () {
8641
// >> gestures-rotation-alt
8742
var label = new Label();
@@ -91,15 +46,6 @@ export var test_DummyTestForSnippetOnly44 = function () {
9146
// << gestures-rotation-alt
9247
};
9348

94-
export var test_DummyTestForSnippetOnly5 = function () {
95-
// >> gestures-swipe
96-
var label = new Label();
97-
var observer = label.on(GestureTypes.swipe, function (args: SwipeGestureEventData) {
98-
console.log('Swipe direction: ' + args.direction);
99-
});
100-
// << gestures-swipe
101-
};
102-
10349
export var test_DummyTestForSnippetOnly55 = function () {
10450
// >> gestures-swipe-alt
10551
var label = new Label();
@@ -109,15 +55,6 @@ export var test_DummyTestForSnippetOnly55 = function () {
10955
// << gestures-swipe-alt
11056
};
11157

112-
export var test_DummyTestForSnippetOnly6 = function () {
113-
// >> gestures-tap
114-
var label = new Label();
115-
var observer = label.on(GestureTypes.tap, function (args: GestureEventData) {
116-
console.log('Tap');
117-
});
118-
// << gestures-tap
119-
};
120-
12158
export var test_DummyTestForSnippetOnly66 = function () {
12259
// >> gestures-tap-alt
12360
var label = new Label();
@@ -127,25 +64,6 @@ export var test_DummyTestForSnippetOnly66 = function () {
12764
// << gestures-tap-alt
12865
};
12966

130-
export var test_DummyTestForSnippetOnly7 = function () {
131-
// >> gestures-stop-observe
132-
var label = new Label();
133-
var observer = label.on(GestureTypes.tap, function (args: GestureEventData) {
134-
console.log('Tap');
135-
});
136-
observer.disconnect();
137-
// << gestures-stop-observe
138-
};
139-
140-
export var test_DummyTestForSnippetOnly8 = function () {
141-
// >> gestures-multiple
142-
var label = new Label();
143-
var observer = label.on(GestureTypes.tap | GestureTypes.doubleTap | GestureTypes.longPress, function (args: GestureEventData) {
144-
console.log('Event: ' + args.eventName);
145-
});
146-
// << gestures-multiple
147-
};
148-
14967
export var test_DummyTestForSnippetOnly88 = function () {
15068
// >> gestures-string
15169
var label = new Label();

packages/core/ui/button/index.android.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { paddingLeftProperty, paddingTopProperty, paddingRightProperty, paddingB
44
import { textAlignmentProperty } from '../text-base';
55
import { CoreTypes } from '../../core-types';
66
import { profile } from '../../profiling';
7-
import { TouchGestureEventData, GestureTypes, TouchAction } from '../gestures';
7+
import { TouchGestureEventData, TouchAction, GestureTypes } from '../gestures';
88
import { Device } from '../../platform';
99
import { SDK_VERSION } from '../../utils/constants';
1010
import lazy from '../../utils/lazy';
@@ -126,9 +126,9 @@ export class Button extends ButtonBase {
126126
break;
127127
}
128128
});
129-
this.on(GestureTypes.touch, this._highlightedHandler);
129+
this.on(GestureTypes[GestureTypes.touch], this._highlightedHandler);
130130
} else {
131-
this.off(GestureTypes.touch, this._highlightedHandler);
131+
this.off(GestureTypes[GestureTypes.touch], this._highlightedHandler);
132132
}
133133
}
134134

packages/core/ui/core/view/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,15 +595,15 @@ export abstract class View extends ViewCommon {
595595
* @param callback An optional parameter pointing to a specific listener. If not defined, all listeners for the event names will be removed.
596596
* @param thisArg An optional parameter which when set will be used to refine search of the correct callback which will be removed as event listener.
597597
*/
598-
off(eventNames: string | GestureTypes, callback?: (args: EventData) => void, thisArg?: any);
598+
off(eventNames: string, callback?: (args: EventData) => void, thisArg?: any);
599599

600600
/**
601601
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
602602
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change") or you can use gesture types.
603603
* @param callback - Callback function which will be executed when event is raised.
604604
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
605605
*/
606-
on(eventNames: string | GestureTypes, callback: (args: EventData) => void, thisArg?: any);
606+
on(eventNames: string, callback: (args: EventData) => void, thisArg?: any);
607607

608608
/**
609609
* Raised when a loaded event occurs.

packages/core/ui/core/view/view-common.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,9 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
290290
return this._gestureObservers[type];
291291
}
292292

293-
public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any) {
294-
if (typeof arg === 'number') {
295-
this._observe(arg, callback as unknown as (data: GestureEventData) => void, thisArg);
296-
return;
297-
}
298-
293+
public addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: any) {
299294
// Normalize "ontap" -> "tap"
300-
const normalizedName = getEventOrGestureName(arg);
295+
const normalizedName = getEventOrGestureName(eventNames);
301296

302297
// Coerce "tap" -> GestureTypes.tap
303298
// Coerce "loaded" -> undefined
@@ -312,14 +307,9 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
312307
super.addEventListener(normalizedName, callback, thisArg);
313308
}
314309

315-
public removeEventListener(arg: string | GestureTypes, callback?: (data: EventData) => void, thisArg?: any) {
316-
if (typeof arg === 'number') {
317-
this._disconnectGestureObservers(arg);
318-
return;
319-
}
320-
310+
public removeEventListener(eventNames: string, callback?: (data: EventData) => void, thisArg?: any) {
321311
// Normalize "ontap" -> "tap"
322-
const normalizedName = getEventOrGestureName(arg);
312+
const normalizedName = getEventOrGestureName(eventNames);
323313

324314
// Coerce "tap" -> GestureTypes.tap
325315
// Coerce "loaded" -> undefined

packages/core/ui/gestures/gestures-common.ts

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -284,38 +284,42 @@ export interface RotationGestureEventData extends GestureEventDataWithState {
284284
* @param separator(optional) - Text separator between gesture type strings.
285285
*/
286286
export function toString(type: GestureTypes, separator?: string): string {
287-
const types = new Array<string>();
287+
// We can get stronger typings with `keyof typeof GestureTypes`, but sadly
288+
// indexing into an enum simply returns `string`, so we'd have to type-assert
289+
// all of the below anyway. Even this `(typeof GestureTypes)[GestureTypes]` is
290+
// more for documentation than for type-safety (it resolves to `string`, too).
291+
const types = new Array<(typeof GestureTypes)[GestureTypes]>();
288292

289293
if (type & GestureTypes.tap) {
290-
types.push('tap');
294+
types.push(GestureTypes[GestureTypes.tap]);
291295
}
292296

293297
if (type & GestureTypes.doubleTap) {
294-
types.push('doubleTap');
298+
types.push(GestureTypes[GestureTypes.doubleTap]);
295299
}
296300

297301
if (type & GestureTypes.pinch) {
298-
types.push('pinch');
302+
types.push(GestureTypes[GestureTypes.pinch]);
299303
}
300304

301305
if (type & GestureTypes.pan) {
302-
types.push('pan');
306+
types.push(GestureTypes[GestureTypes.pan]);
303307
}
304308

305309
if (type & GestureTypes.swipe) {
306-
types.push('swipe');
310+
types.push(GestureTypes[GestureTypes.swipe]);
307311
}
308312

309313
if (type & GestureTypes.rotation) {
310-
types.push('rotation');
314+
types.push(GestureTypes[GestureTypes.rotation]);
311315
}
312316

313317
if (type & GestureTypes.longPress) {
314-
types.push('longPress');
318+
types.push(GestureTypes[GestureTypes.longPress]);
315319
}
316320

317321
if (type & GestureTypes.touch) {
318-
types.push('touch');
322+
types.push(GestureTypes[GestureTypes.touch]);
319323
}
320324

321325
return types.join(separator);
@@ -327,28 +331,8 @@ export function toString(type: GestureTypes, separator?: string): string {
327331
* Returns a gesture type enum value from a string (case insensitive).
328332
* @param type - A string representation of a gesture type (e.g. Tap).
329333
*/
330-
export function fromString(type: string): GestureTypes {
331-
const t = type.trim().toLowerCase();
332-
333-
if (t === 'tap') {
334-
return GestureTypes.tap;
335-
} else if (t === 'doubletap') {
336-
return GestureTypes.doubleTap;
337-
} else if (t === 'pinch') {
338-
return GestureTypes.pinch;
339-
} else if (t === 'pan') {
340-
return GestureTypes.pan;
341-
} else if (t === 'swipe') {
342-
return GestureTypes.swipe;
343-
} else if (t === 'rotation') {
344-
return GestureTypes.rotation;
345-
} else if (t === 'longpress') {
346-
return GestureTypes.longPress;
347-
} else if (t === 'touch') {
348-
return GestureTypes.touch;
349-
}
350-
351-
return undefined;
334+
export function fromString(type: string): GestureTypes | undefined {
335+
return GestureTypes[type.trim()];
352336
}
353337

354338
export abstract class GesturesObserverBase implements GesturesObserverDefinition {

packages/core/ui/gestures/touch-manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class TouchManager {
114114
(<UILongPressGestureRecognizer>args.ios).minimumPressDuration = (<View>args.object)?.touchDelay || 0;
115115
}
116116
});
117-
view.on(GestureTypes.longPress, (args: GestureEventDataWithState) => {
117+
view.on(GestureTypes[GestureTypes.longPress], (args: GestureEventDataWithState) => {
118118
switch (args.state) {
119119
case GestureStateTypes.began:
120120
if (handleDown) {
@@ -133,7 +133,7 @@ export class TouchManager {
133133
}
134134
} else {
135135
if (handleDown || handleUp) {
136-
view.on(GestureTypes.touch, (args: TouchGestureEventData) => {
136+
view.on(GestureTypes[GestureTypes.touch], (args: TouchGestureEventData) => {
137137
switch (args.action) {
138138
case 'down':
139139
if (handleDown) {
@@ -280,7 +280,7 @@ export class TouchManager {
280280
TouchManager.visionHoverStyleCache['default'] = createHoverStyleFromOptions(
281281
defaultOptions || {
282282
effect: 'automatic',
283-
}
283+
},
284284
);
285285
}
286286

tools/scripts/api-reports/NativeScript.api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2749,8 +2749,8 @@ export abstract class View extends ViewBase {
27492749
modal: View;
27502750
// (undocumented)
27512751
_modalParent?: View;
2752-
off(eventNames: string | GestureTypes, callback?: (args: EventData) => void, thisArg?: any);
2753-
on(eventNames: string | GestureTypes, callback: (args: EventData) => void, thisArg?: any);
2752+
off(eventNames: string, callback?: (args: EventData) => void, thisArg?: any);
2753+
on(eventNames: string, callback: (args: EventData) => void, thisArg?: any);
27542754
on(event: "loaded", callback: (args: EventData) => void, thisArg?: any);
27552755
on(event: "unloaded", callback: (args: EventData) => void, thisArg?: any);
27562756
on(event: "androidBackPressed", callback: (args: EventData) => void, thisArg?: any);

0 commit comments

Comments
 (0)