fix(core): stop accepting GestureTypes enum as an eventName (#10537)

This commit is contained in:
Jamie Birch
2024-05-07 03:04:53 +09:00
committed by GitHub
parent 415ff34660
commit 3b77fffad5
7 changed files with 30 additions and 138 deletions

View File

@ -1,14 +1,5 @@
/* tslint:disable:no-unused-variable */ /* tslint:disable:no-unused-variable */
import { GestureEventData, Label, GestureTypes, PanGestureEventData, PinchGestureEventData, SwipeGestureEventData, RotationGestureEventData } from '@nativescript/core'; import { GestureEventData, Label, PanGestureEventData, PinchGestureEventData, SwipeGestureEventData, RotationGestureEventData } from '@nativescript/core';
export var test_DummyTestForSnippetOnly0 = function () {
// >> gestures-double-tap
var label = new Label();
var observer = label.on(GestureTypes.doubleTap, function (args: GestureEventData) {
console.log('Double Tap');
});
// << gestures-double-tap
};
export var test_DummyTestForSnippetOnly01 = function () { export var test_DummyTestForSnippetOnly01 = function () {
// >> gestures-double-tap-alt // >> gestures-double-tap-alt
@ -19,15 +10,6 @@ export var test_DummyTestForSnippetOnly01 = function () {
// << gestures-double-tap-alt // << gestures-double-tap-alt
}; };
export var test_DummyTestForSnippetOnly1 = function () {
// >> gestures-long-press
var label = new Label();
var observer = label.on(GestureTypes.longPress, function (args: GestureEventData) {
console.log('Long Press');
});
// << gestures-long-press
};
export var test_DummyTestForSnippetOnly11 = function () { export var test_DummyTestForSnippetOnly11 = function () {
// >> gestures-long-press-alt // >> gestures-long-press-alt
var label = new Label(); var label = new Label();
@ -37,15 +19,6 @@ export var test_DummyTestForSnippetOnly11 = function () {
// << gestures-long-press-alt // << gestures-long-press-alt
}; };
export var test_DummyTestForSnippetOnly2 = function () {
// >> gestures-pan
var label = new Label();
var observer = label.on(GestureTypes.pan, function (args: PanGestureEventData) {
console.log('Pan deltaX:' + args.deltaX + '; deltaY:' + args.deltaY + ';');
});
// << gestures-pan
};
export var test_DummyTestForSnippetOnly22 = function () { export var test_DummyTestForSnippetOnly22 = function () {
// >> gestures-pan-alt // >> gestures-pan-alt
var label = new Label(); var label = new Label();
@ -55,15 +28,6 @@ export var test_DummyTestForSnippetOnly22 = function () {
// << gestures-pan-alt // << gestures-pan-alt
}; };
export var test_DummyTestForSnippetOnly3 = function () {
// >> gestures-pan-pinch
var label = new Label();
var observer = label.on(GestureTypes.pinch, function (args: PinchGestureEventData) {
console.log('Pinch scale: ' + args.scale);
});
// << gestures-pan-pinch
};
export var test_DummyTestForSnippetOnly33 = function () { export var test_DummyTestForSnippetOnly33 = function () {
// >> gestures-pan-pinch-alt // >> gestures-pan-pinch-alt
var label = new Label(); var label = new Label();
@ -73,15 +37,6 @@ export var test_DummyTestForSnippetOnly33 = function () {
// << gestures-pan-pinch-alt // << gestures-pan-pinch-alt
}; };
export var test_DummyTestForSnippetOnly4 = function () {
// >> gestures-rotation
var label = new Label();
var observer = label.on(GestureTypes.rotation, function (args: RotationGestureEventData) {
console.log('Rotation: ' + args.rotation);
});
// << gestures-rotation
};
export var test_DummyTestForSnippetOnly44 = function () { export var test_DummyTestForSnippetOnly44 = function () {
// >> gestures-rotation-alt // >> gestures-rotation-alt
var label = new Label(); var label = new Label();
@ -91,15 +46,6 @@ export var test_DummyTestForSnippetOnly44 = function () {
// << gestures-rotation-alt // << gestures-rotation-alt
}; };
export var test_DummyTestForSnippetOnly5 = function () {
// >> gestures-swipe
var label = new Label();
var observer = label.on(GestureTypes.swipe, function (args: SwipeGestureEventData) {
console.log('Swipe direction: ' + args.direction);
});
// << gestures-swipe
};
export var test_DummyTestForSnippetOnly55 = function () { export var test_DummyTestForSnippetOnly55 = function () {
// >> gestures-swipe-alt // >> gestures-swipe-alt
var label = new Label(); var label = new Label();
@ -109,15 +55,6 @@ export var test_DummyTestForSnippetOnly55 = function () {
// << gestures-swipe-alt // << gestures-swipe-alt
}; };
export var test_DummyTestForSnippetOnly6 = function () {
// >> gestures-tap
var label = new Label();
var observer = label.on(GestureTypes.tap, function (args: GestureEventData) {
console.log('Tap');
});
// << gestures-tap
};
export var test_DummyTestForSnippetOnly66 = function () { export var test_DummyTestForSnippetOnly66 = function () {
// >> gestures-tap-alt // >> gestures-tap-alt
var label = new Label(); var label = new Label();
@ -127,25 +64,6 @@ export var test_DummyTestForSnippetOnly66 = function () {
// << gestures-tap-alt // << gestures-tap-alt
}; };
export var test_DummyTestForSnippetOnly7 = function () {
// >> gestures-stop-observe
var label = new Label();
var observer = label.on(GestureTypes.tap, function (args: GestureEventData) {
console.log('Tap');
});
observer.disconnect();
// << gestures-stop-observe
};
export var test_DummyTestForSnippetOnly8 = function () {
// >> gestures-multiple
var label = new Label();
var observer = label.on(GestureTypes.tap | GestureTypes.doubleTap | GestureTypes.longPress, function (args: GestureEventData) {
console.log('Event: ' + args.eventName);
});
// << gestures-multiple
};
export var test_DummyTestForSnippetOnly88 = function () { export var test_DummyTestForSnippetOnly88 = function () {
// >> gestures-string // >> gestures-string
var label = new Label(); var label = new Label();

View File

@ -4,7 +4,7 @@ import { paddingLeftProperty, paddingTopProperty, paddingRightProperty, paddingB
import { textAlignmentProperty } from '../text-base'; import { textAlignmentProperty } from '../text-base';
import { CoreTypes } from '../../core-types'; import { CoreTypes } from '../../core-types';
import { profile } from '../../profiling'; import { profile } from '../../profiling';
import { TouchGestureEventData, GestureTypes, TouchAction } from '../gestures'; import { TouchGestureEventData, TouchAction, GestureTypes } from '../gestures';
import { Device } from '../../platform'; import { Device } from '../../platform';
import { SDK_VERSION } from '../../utils/constants'; import { SDK_VERSION } from '../../utils/constants';
import lazy from '../../utils/lazy'; import lazy from '../../utils/lazy';
@ -126,9 +126,9 @@ export class Button extends ButtonBase {
break; break;
} }
}); });
this.on(GestureTypes.touch, this._highlightedHandler); this.on(GestureTypes[GestureTypes.touch], this._highlightedHandler);
} else { } else {
this.off(GestureTypes.touch, this._highlightedHandler); this.off(GestureTypes[GestureTypes.touch], this._highlightedHandler);
} }
} }

View File

@ -595,7 +595,7 @@ export abstract class View extends ViewCommon {
* @param callback An optional parameter pointing to a specific listener. If not defined, all listeners for the event names will be removed. * @param callback An optional parameter pointing to a specific listener. If not defined, all listeners for the event names will be removed.
* @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. * @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.
*/ */
off(eventNames: string | GestureTypes, callback?: (args: EventData) => void, thisArg?: any); off(eventNames: string, callback?: (args: EventData) => void, thisArg?: any);
/** /**
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method). * A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
@ -603,7 +603,7 @@ export abstract class View extends ViewCommon {
* @param callback - Callback function which will be executed when event is raised. * @param callback - Callback function which will be executed when event is raised.
* @param thisArg - An optional parameter which will be used as `this` context for callback execution. * @param thisArg - An optional parameter which will be used as `this` context for callback execution.
*/ */
on(eventNames: string | GestureTypes, callback: (args: EventData) => void, thisArg?: any); on(eventNames: string, callback: (args: EventData) => void, thisArg?: any);
/** /**
* Raised when a loaded event occurs. * Raised when a loaded event occurs.

View File

@ -290,14 +290,9 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
return this._gestureObservers[type]; return this._gestureObservers[type];
} }
public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any) { public addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: any) {
if (typeof arg === 'number') {
this._observe(arg, callback as unknown as (data: GestureEventData) => void, thisArg);
return;
}
// Normalize "ontap" -> "tap" // Normalize "ontap" -> "tap"
const normalizedName = getEventOrGestureName(arg); const normalizedName = getEventOrGestureName(eventNames);
// Coerce "tap" -> GestureTypes.tap // Coerce "tap" -> GestureTypes.tap
// Coerce "loaded" -> undefined // Coerce "loaded" -> undefined
@ -312,14 +307,9 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
super.addEventListener(normalizedName, callback, thisArg); super.addEventListener(normalizedName, callback, thisArg);
} }
public removeEventListener(arg: string | GestureTypes, callback?: (data: EventData) => void, thisArg?: any) { public removeEventListener(eventNames: string, callback?: (data: EventData) => void, thisArg?: any) {
if (typeof arg === 'number') {
this._disconnectGestureObservers(arg);
return;
}
// Normalize "ontap" -> "tap" // Normalize "ontap" -> "tap"
const normalizedName = getEventOrGestureName(arg); const normalizedName = getEventOrGestureName(eventNames);
// Coerce "tap" -> GestureTypes.tap // Coerce "tap" -> GestureTypes.tap
// Coerce "loaded" -> undefined // Coerce "loaded" -> undefined

View File

@ -284,38 +284,42 @@ export interface RotationGestureEventData extends GestureEventDataWithState {
* @param separator(optional) - Text separator between gesture type strings. * @param separator(optional) - Text separator between gesture type strings.
*/ */
export function toString(type: GestureTypes, separator?: string): string { export function toString(type: GestureTypes, separator?: string): string {
const types = new Array<string>(); // We can get stronger typings with `keyof typeof GestureTypes`, but sadly
// indexing into an enum simply returns `string`, so we'd have to type-assert
// all of the below anyway. Even this `(typeof GestureTypes)[GestureTypes]` is
// more for documentation than for type-safety (it resolves to `string`, too).
const types = new Array<(typeof GestureTypes)[GestureTypes]>();
if (type & GestureTypes.tap) { if (type & GestureTypes.tap) {
types.push('tap'); types.push(GestureTypes[GestureTypes.tap]);
} }
if (type & GestureTypes.doubleTap) { if (type & GestureTypes.doubleTap) {
types.push('doubleTap'); types.push(GestureTypes[GestureTypes.doubleTap]);
} }
if (type & GestureTypes.pinch) { if (type & GestureTypes.pinch) {
types.push('pinch'); types.push(GestureTypes[GestureTypes.pinch]);
} }
if (type & GestureTypes.pan) { if (type & GestureTypes.pan) {
types.push('pan'); types.push(GestureTypes[GestureTypes.pan]);
} }
if (type & GestureTypes.swipe) { if (type & GestureTypes.swipe) {
types.push('swipe'); types.push(GestureTypes[GestureTypes.swipe]);
} }
if (type & GestureTypes.rotation) { if (type & GestureTypes.rotation) {
types.push('rotation'); types.push(GestureTypes[GestureTypes.rotation]);
} }
if (type & GestureTypes.longPress) { if (type & GestureTypes.longPress) {
types.push('longPress'); types.push(GestureTypes[GestureTypes.longPress]);
} }
if (type & GestureTypes.touch) { if (type & GestureTypes.touch) {
types.push('touch'); types.push(GestureTypes[GestureTypes.touch]);
} }
return types.join(separator); return types.join(separator);
@ -327,28 +331,8 @@ export function toString(type: GestureTypes, separator?: string): string {
* Returns a gesture type enum value from a string (case insensitive). * Returns a gesture type enum value from a string (case insensitive).
* @param type - A string representation of a gesture type (e.g. Tap). * @param type - A string representation of a gesture type (e.g. Tap).
*/ */
export function fromString(type: string): GestureTypes { export function fromString(type: string): GestureTypes | undefined {
const t = type.trim().toLowerCase(); return GestureTypes[type.trim()];
if (t === 'tap') {
return GestureTypes.tap;
} else if (t === 'doubletap') {
return GestureTypes.doubleTap;
} else if (t === 'pinch') {
return GestureTypes.pinch;
} else if (t === 'pan') {
return GestureTypes.pan;
} else if (t === 'swipe') {
return GestureTypes.swipe;
} else if (t === 'rotation') {
return GestureTypes.rotation;
} else if (t === 'longpress') {
return GestureTypes.longPress;
} else if (t === 'touch') {
return GestureTypes.touch;
}
return undefined;
} }
export abstract class GesturesObserverBase implements GesturesObserverDefinition { export abstract class GesturesObserverBase implements GesturesObserverDefinition {

View File

@ -114,7 +114,7 @@ export class TouchManager {
(<UILongPressGestureRecognizer>args.ios).minimumPressDuration = (<View>args.object)?.touchDelay || 0; (<UILongPressGestureRecognizer>args.ios).minimumPressDuration = (<View>args.object)?.touchDelay || 0;
} }
}); });
view.on(GestureTypes.longPress, (args: GestureEventDataWithState) => { view.on(GestureTypes[GestureTypes.longPress], (args: GestureEventDataWithState) => {
switch (args.state) { switch (args.state) {
case GestureStateTypes.began: case GestureStateTypes.began:
if (handleDown) { if (handleDown) {
@ -133,7 +133,7 @@ export class TouchManager {
} }
} else { } else {
if (handleDown || handleUp) { if (handleDown || handleUp) {
view.on(GestureTypes.touch, (args: TouchGestureEventData) => { view.on(GestureTypes[GestureTypes.touch], (args: TouchGestureEventData) => {
switch (args.action) { switch (args.action) {
case 'down': case 'down':
if (handleDown) { if (handleDown) {
@ -280,7 +280,7 @@ export class TouchManager {
TouchManager.visionHoverStyleCache['default'] = createHoverStyleFromOptions( TouchManager.visionHoverStyleCache['default'] = createHoverStyleFromOptions(
defaultOptions || { defaultOptions || {
effect: 'automatic', effect: 'automatic',
} },
); );
} }

View File

@ -2749,8 +2749,8 @@ export abstract class View extends ViewBase {
modal: View; modal: View;
// (undocumented) // (undocumented)
_modalParent?: View; _modalParent?: View;
off(eventNames: string | GestureTypes, callback?: (args: EventData) => void, thisArg?: any); off(eventNames: string, callback?: (args: EventData) => void, thisArg?: any);
on(eventNames: string | GestureTypes, callback: (args: EventData) => void, thisArg?: any); on(eventNames: string, callback: (args: EventData) => void, thisArg?: any);
on(event: "loaded", callback: (args: EventData) => void, thisArg?: any); on(event: "loaded", callback: (args: EventData) => void, thisArg?: any);
on(event: "unloaded", callback: (args: EventData) => void, thisArg?: any); on(event: "unloaded", callback: (args: EventData) => void, thisArg?: any);
on(event: "androidBackPressed", callback: (args: EventData) => void, thisArg?: any); on(event: "androidBackPressed", callback: (args: EventData) => void, thisArg?: any);