diff --git a/apps/automated/src/ui/gestures/gestures-tests.ts b/apps/automated/src/ui/gestures/gestures-tests.ts index 8ab92d7a76..8c82f3d77f 100644 --- a/apps/automated/src/ui/gestures/gestures-tests.ts +++ b/apps/automated/src/ui/gestures/gestures-tests.ts @@ -1,14 +1,5 @@ /* tslint:disable:no-unused-variable */ -import { GestureEventData, Label, GestureTypes, 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 -}; +import { GestureEventData, Label, PanGestureEventData, PinchGestureEventData, SwipeGestureEventData, RotationGestureEventData } from '@nativescript/core'; export var test_DummyTestForSnippetOnly01 = function () { // >> gestures-double-tap-alt @@ -19,15 +10,6 @@ export var test_DummyTestForSnippetOnly01 = function () { // << 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 () { // >> gestures-long-press-alt var label = new Label(); @@ -37,15 +19,6 @@ export var test_DummyTestForSnippetOnly11 = function () { // << 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 () { // >> gestures-pan-alt var label = new Label(); @@ -55,15 +28,6 @@ export var test_DummyTestForSnippetOnly22 = function () { // << 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 () { // >> gestures-pan-pinch-alt var label = new Label(); @@ -73,15 +37,6 @@ export var test_DummyTestForSnippetOnly33 = function () { // << 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 () { // >> gestures-rotation-alt var label = new Label(); @@ -91,15 +46,6 @@ export var test_DummyTestForSnippetOnly44 = function () { // << 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 () { // >> gestures-swipe-alt var label = new Label(); @@ -109,15 +55,6 @@ export var test_DummyTestForSnippetOnly55 = function () { // << 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 () { // >> gestures-tap-alt var label = new Label(); @@ -127,25 +64,6 @@ export var test_DummyTestForSnippetOnly66 = function () { // << 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 () { // >> gestures-string var label = new Label(); diff --git a/packages/core/ui/button/index.android.ts b/packages/core/ui/button/index.android.ts index 869d472368..f46eeb63e4 100644 --- a/packages/core/ui/button/index.android.ts +++ b/packages/core/ui/button/index.android.ts @@ -4,7 +4,7 @@ import { paddingLeftProperty, paddingTopProperty, paddingRightProperty, paddingB import { textAlignmentProperty } from '../text-base'; import { CoreTypes } from '../../core-types'; import { profile } from '../../profiling'; -import { TouchGestureEventData, GestureTypes, TouchAction } from '../gestures'; +import { TouchGestureEventData, TouchAction, GestureTypes } from '../gestures'; import { Device } from '../../platform'; import { SDK_VERSION } from '../../utils/constants'; import lazy from '../../utils/lazy'; @@ -126,9 +126,9 @@ export class Button extends ButtonBase { break; } }); - this.on(GestureTypes.touch, this._highlightedHandler); + this.on(GestureTypes[GestureTypes.touch], this._highlightedHandler); } else { - this.off(GestureTypes.touch, this._highlightedHandler); + this.off(GestureTypes[GestureTypes.touch], this._highlightedHandler); } } diff --git a/packages/core/ui/core/view/index.d.ts b/packages/core/ui/core/view/index.d.ts index 06d21abe73..adcea91b23 100644 --- a/packages/core/ui/core/view/index.d.ts +++ b/packages/core/ui/core/view/index.d.ts @@ -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 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). @@ -603,7 +603,7 @@ export abstract class View extends ViewCommon { * @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. */ - 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. diff --git a/packages/core/ui/core/view/view-common.ts b/packages/core/ui/core/view/view-common.ts index 8fd11d1f13..f792d92e2d 100644 --- a/packages/core/ui/core/view/view-common.ts +++ b/packages/core/ui/core/view/view-common.ts @@ -290,14 +290,9 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition { return this._gestureObservers[type]; } - public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any) { - if (typeof arg === 'number') { - this._observe(arg, callback as unknown as (data: GestureEventData) => void, thisArg); - return; - } - + public addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: any) { // Normalize "ontap" -> "tap" - const normalizedName = getEventOrGestureName(arg); + const normalizedName = getEventOrGestureName(eventNames); // Coerce "tap" -> GestureTypes.tap // Coerce "loaded" -> undefined @@ -312,14 +307,9 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition { super.addEventListener(normalizedName, callback, thisArg); } - public removeEventListener(arg: string | GestureTypes, callback?: (data: EventData) => void, thisArg?: any) { - if (typeof arg === 'number') { - this._disconnectGestureObservers(arg); - return; - } - + public removeEventListener(eventNames: string, callback?: (data: EventData) => void, thisArg?: any) { // Normalize "ontap" -> "tap" - const normalizedName = getEventOrGestureName(arg); + const normalizedName = getEventOrGestureName(eventNames); // Coerce "tap" -> GestureTypes.tap // Coerce "loaded" -> undefined diff --git a/packages/core/ui/gestures/gestures-common.ts b/packages/core/ui/gestures/gestures-common.ts index 434b80bd57..744bafd8d8 100644 --- a/packages/core/ui/gestures/gestures-common.ts +++ b/packages/core/ui/gestures/gestures-common.ts @@ -284,38 +284,42 @@ export interface RotationGestureEventData extends GestureEventDataWithState { * @param separator(optional) - Text separator between gesture type strings. */ export function toString(type: GestureTypes, separator?: string): string { - const types = new Array(); + // 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) { - types.push('tap'); + types.push(GestureTypes[GestureTypes.tap]); } if (type & GestureTypes.doubleTap) { - types.push('doubleTap'); + types.push(GestureTypes[GestureTypes.doubleTap]); } if (type & GestureTypes.pinch) { - types.push('pinch'); + types.push(GestureTypes[GestureTypes.pinch]); } if (type & GestureTypes.pan) { - types.push('pan'); + types.push(GestureTypes[GestureTypes.pan]); } if (type & GestureTypes.swipe) { - types.push('swipe'); + types.push(GestureTypes[GestureTypes.swipe]); } if (type & GestureTypes.rotation) { - types.push('rotation'); + types.push(GestureTypes[GestureTypes.rotation]); } if (type & GestureTypes.longPress) { - types.push('longPress'); + types.push(GestureTypes[GestureTypes.longPress]); } if (type & GestureTypes.touch) { - types.push('touch'); + types.push(GestureTypes[GestureTypes.touch]); } 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). * @param type - A string representation of a gesture type (e.g. Tap). */ -export function fromString(type: string): GestureTypes { - const t = type.trim().toLowerCase(); - - 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 function fromString(type: string): GestureTypes | undefined { + return GestureTypes[type.trim()]; } export abstract class GesturesObserverBase implements GesturesObserverDefinition { diff --git a/packages/core/ui/gestures/touch-manager.ts b/packages/core/ui/gestures/touch-manager.ts index bd0baca95c..dd89b3ffa0 100644 --- a/packages/core/ui/gestures/touch-manager.ts +++ b/packages/core/ui/gestures/touch-manager.ts @@ -114,7 +114,7 @@ export class TouchManager { (args.ios).minimumPressDuration = (args.object)?.touchDelay || 0; } }); - view.on(GestureTypes.longPress, (args: GestureEventDataWithState) => { + view.on(GestureTypes[GestureTypes.longPress], (args: GestureEventDataWithState) => { switch (args.state) { case GestureStateTypes.began: if (handleDown) { @@ -133,7 +133,7 @@ export class TouchManager { } } else { if (handleDown || handleUp) { - view.on(GestureTypes.touch, (args: TouchGestureEventData) => { + view.on(GestureTypes[GestureTypes.touch], (args: TouchGestureEventData) => { switch (args.action) { case 'down': if (handleDown) { @@ -280,7 +280,7 @@ export class TouchManager { TouchManager.visionHoverStyleCache['default'] = createHoverStyleFromOptions( defaultOptions || { effect: 'automatic', - } + }, ); } diff --git a/tools/scripts/api-reports/NativeScript.api.md b/tools/scripts/api-reports/NativeScript.api.md index 9fb24694f9..2f1d2e45ed 100644 --- a/tools/scripts/api-reports/NativeScript.api.md +++ b/tools/scripts/api-reports/NativeScript.api.md @@ -2749,8 +2749,8 @@ export abstract class View extends ViewBase { modal: View; // (undocumented) _modalParent?: View; - off(eventNames: string | GestureTypes, callback?: (args: EventData) => void, thisArg?: any); - on(eventNames: string | GestureTypes, callback: (args: EventData) => void, thisArg?: any); + off(eventNames: string, 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: "unloaded", callback: (args: EventData) => void, thisArg?: any); on(event: "androidBackPressed", callback: (args: EventData) => void, thisArg?: any);