From 1a690d2e7637e2a5ff3dbdcc3bd6cd576dd2ec1e Mon Sep 17 00:00:00 2001 From: shirakaba <14055146+shirakaba@users.noreply.github.com> Date: Fri, 3 May 2024 23:06:00 +0900 Subject: [PATCH 1/3] fix(core): stop accepting GestureTypes as eventName --- .../src/ui/gestures/gestures-tests.ts | 84 +------------------ apps/ui/src/events/gestures-page.ts | 18 ++-- packages/core/ui/button/index.android.ts | 4 +- packages/core/ui/core/view/index.d.ts | 4 +- packages/core/ui/core/view/view-common.ts | 18 +--- packages/core/ui/gestures/touch-manager.ts | 6 +- tools/scripts/api-reports/NativeScript.api.md | 4 +- 7 files changed, 23 insertions(+), 115 deletions(-) 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/apps/ui/src/events/gestures-page.ts b/apps/ui/src/events/gestures-page.ts index b8077b437d..270feac23c 100644 --- a/apps/ui/src/events/gestures-page.ts +++ b/apps/ui/src/events/gestures-page.ts @@ -65,55 +65,55 @@ export function createPage() { tapAndDoubleTapLabel.automationText = 'Gestures detection disabled'; }); - tapLabel.on(GestureTypes[GestureTypes.tap], function (args: GestureEventData) { + tapLabel.on('tap', function (args: GestureEventData) { tapLabel.text = 'Tap gesture detected, ' + (args.object === tapLabel); }); const observer1 = tapLabel.getGestureObservers(GestureTypes.tap)[0]; - doubleTapLabel.on(GestureTypes[GestureTypes.doubleTap], function (args: GestureEventData) { + doubleTapLabel.on('doubleTap', function (args: GestureEventData) { doubleTapLabel.text = 'Double Tap gesture detected, ' + (args.object === doubleTapLabel); }); const observer2 = doubleTapLabel.getGestureObservers(GestureTypes.doubleTap)[0]; - longPressLabel.on(GestureTypes[GestureTypes.longPress], function (args: GestureEventData) { + longPressLabel.on('longPress', function (args: GestureEventData) { longPressLabel.text = 'Long Press gesture detected, ' + (args.object === longPressLabel); }); const observer3 = longPressLabel.getGestureObservers(GestureTypes.longPress)[0]; - swipeLabel.on(GestureTypes[GestureTypes.swipe], function (args: SwipeGestureEventData) { + swipeLabel.on('swipe', function (args: SwipeGestureEventData) { swipeLabel.text = 'Swipe Direction: ' + args.direction + ', ' + (args.object === swipeLabel); // + getStateAsString(args.state); }); const observer4 = swipeLabel.getGestureObservers(GestureTypes.swipe)[0]; - panLabel.on(GestureTypes[GestureTypes.pan], function (args: PanGestureEventData) { + panLabel.on('pan', function (args: PanGestureEventData) { panLabel.text = 'Pan deltaX:' + Math.round(args.deltaX) + '; deltaY:' + Math.round(args.deltaY) + ';' + ', ' + (args.object === panLabel) + getStateAsString(args.state); }); const observer5 = panLabel.getGestureObservers(GestureTypes.pan)[0]; - pinchLabel.on(GestureTypes[GestureTypes.pinch], function (args: PinchGestureEventData) { + pinchLabel.on('pinch', function (args: PinchGestureEventData) { pinchLabel.text = 'Pinch Scale: ' + Math.round(args.scale) + ', ' + (args.object === pinchLabel) + getStateAsString(args.state); }); const observer6 = pinchLabel.getGestureObservers(GestureTypes.pinch)[0]; - rotationLabel.on(GestureTypes[GestureTypes.rotation], function (args: RotationGestureEventData) { + rotationLabel.on('rotation', function (args: RotationGestureEventData) { rotationLabel.text = 'Rotation: ' + Math.round(args.rotation) + ', ' + (args.object === rotationLabel) + getStateAsString(args.state); }); const observer7 = rotationLabel.getGestureObservers(GestureTypes.rotation)[0]; - tapAndDoubleTapLabel.on(GestureTypes[GestureTypes.doubleTap], function (args: GestureEventData) { + tapAndDoubleTapLabel.on('doubleTap', function (args: GestureEventData) { tapAndDoubleTapLabel.text = 'Last action: Double tap gesture, ' + (args.object === tapAndDoubleTapLabel); }); const observer8 = tapAndDoubleTapLabel.getGestureObservers(GestureTypes.doubleTap)[0]; - tapAndDoubleTapLabel.on(GestureTypes[GestureTypes.tap], function (args: GestureEventData) { + tapAndDoubleTapLabel.on('tap', function (args: GestureEventData) { tapAndDoubleTapLabel.text = 'Last action: Tap gesture, ' + (args.object === tapAndDoubleTapLabel); }); diff --git a/packages/core/ui/button/index.android.ts b/packages/core/ui/button/index.android.ts index 869d472368..f5e0139019 100644 --- a/packages/core/ui/button/index.android.ts +++ b/packages/core/ui/button/index.android.ts @@ -126,9 +126,9 @@ export class Button extends ButtonBase { break; } }); - this.on(GestureTypes.touch, this._highlightedHandler); + this.on('touch', this._highlightedHandler); } else { - this.off(GestureTypes.touch, this._highlightedHandler); + this.off('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/touch-manager.ts b/packages/core/ui/gestures/touch-manager.ts index bd0baca95c..1288f99514 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('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('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); From 7637aa056cf2212d5b756ee89669556d85acb1c5 Mon Sep 17 00:00:00 2001 From: shirakaba <14055146+shirakaba@users.noreply.github.com> Date: Sat, 4 May 2024 00:39:40 +0900 Subject: [PATCH 2/3] chore(core): introduce GestureTypeNames --- apps/ui/src/events/gestures-page.ts | 20 ++++---- packages/core/ui/button/index.android.ts | 6 +-- packages/core/ui/gestures/gestures-common.ts | 54 +++++++++----------- packages/core/ui/gestures/touch-manager.ts | 6 +-- packages/core/ui/index.ts | 2 +- 5 files changed, 40 insertions(+), 48 deletions(-) diff --git a/apps/ui/src/events/gestures-page.ts b/apps/ui/src/events/gestures-page.ts index 270feac23c..fedbd4e68b 100644 --- a/apps/ui/src/events/gestures-page.ts +++ b/apps/ui/src/events/gestures-page.ts @@ -1,4 +1,4 @@ -import { Screen, StackLayout, Page, Label, Button, GestureEventData, RotationGestureEventData, GestureTypes, SwipeGestureEventData, PanGestureEventData, PinchGestureEventData, GestureStateTypes } from '@nativescript/core'; +import { Screen, StackLayout, Page, Label, Button, GestureEventData, GestureTypeNames, RotationGestureEventData, GestureTypes, SwipeGestureEventData, PanGestureEventData, PinchGestureEventData, GestureStateTypes } from '@nativescript/core'; export function createPage() { const stack = new StackLayout(); @@ -65,55 +65,55 @@ export function createPage() { tapAndDoubleTapLabel.automationText = 'Gestures detection disabled'; }); - tapLabel.on('tap', function (args: GestureEventData) { + tapLabel.on(GestureTypeNames.tap, function (args: GestureEventData) { tapLabel.text = 'Tap gesture detected, ' + (args.object === tapLabel); }); const observer1 = tapLabel.getGestureObservers(GestureTypes.tap)[0]; - doubleTapLabel.on('doubleTap', function (args: GestureEventData) { + doubleTapLabel.on(GestureTypeNames.doubleTap, function (args: GestureEventData) { doubleTapLabel.text = 'Double Tap gesture detected, ' + (args.object === doubleTapLabel); }); const observer2 = doubleTapLabel.getGestureObservers(GestureTypes.doubleTap)[0]; - longPressLabel.on('longPress', function (args: GestureEventData) { + longPressLabel.on(GestureTypeNames.longPress, function (args: GestureEventData) { longPressLabel.text = 'Long Press gesture detected, ' + (args.object === longPressLabel); }); const observer3 = longPressLabel.getGestureObservers(GestureTypes.longPress)[0]; - swipeLabel.on('swipe', function (args: SwipeGestureEventData) { + swipeLabel.on(GestureTypeNames.swipe, function (args: SwipeGestureEventData) { swipeLabel.text = 'Swipe Direction: ' + args.direction + ', ' + (args.object === swipeLabel); // + getStateAsString(args.state); }); const observer4 = swipeLabel.getGestureObservers(GestureTypes.swipe)[0]; - panLabel.on('pan', function (args: PanGestureEventData) { + panLabel.on(GestureTypeNames.pan, function (args: PanGestureEventData) { panLabel.text = 'Pan deltaX:' + Math.round(args.deltaX) + '; deltaY:' + Math.round(args.deltaY) + ';' + ', ' + (args.object === panLabel) + getStateAsString(args.state); }); const observer5 = panLabel.getGestureObservers(GestureTypes.pan)[0]; - pinchLabel.on('pinch', function (args: PinchGestureEventData) { + pinchLabel.on(GestureTypeNames.pinch, function (args: PinchGestureEventData) { pinchLabel.text = 'Pinch Scale: ' + Math.round(args.scale) + ', ' + (args.object === pinchLabel) + getStateAsString(args.state); }); const observer6 = pinchLabel.getGestureObservers(GestureTypes.pinch)[0]; - rotationLabel.on('rotation', function (args: RotationGestureEventData) { + rotationLabel.on(GestureTypeNames.rotation, function (args: RotationGestureEventData) { rotationLabel.text = 'Rotation: ' + Math.round(args.rotation) + ', ' + (args.object === rotationLabel) + getStateAsString(args.state); }); const observer7 = rotationLabel.getGestureObservers(GestureTypes.rotation)[0]; - tapAndDoubleTapLabel.on('doubleTap', function (args: GestureEventData) { + tapAndDoubleTapLabel.on(GestureTypeNames.doubleTap, function (args: GestureEventData) { tapAndDoubleTapLabel.text = 'Last action: Double tap gesture, ' + (args.object === tapAndDoubleTapLabel); }); const observer8 = tapAndDoubleTapLabel.getGestureObservers(GestureTypes.doubleTap)[0]; - tapAndDoubleTapLabel.on('tap', function (args: GestureEventData) { + tapAndDoubleTapLabel.on(GestureTypeNames.tap, function (args: GestureEventData) { tapAndDoubleTapLabel.text = 'Last action: Tap gesture, ' + (args.object === tapAndDoubleTapLabel); }); diff --git a/packages/core/ui/button/index.android.ts b/packages/core/ui/button/index.android.ts index f5e0139019..069a869c13 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, GestureTypeNames } 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('touch', this._highlightedHandler); + this.on(GestureTypeNames.touch, this._highlightedHandler); } else { - this.off('touch', this._highlightedHandler); + this.off(GestureTypeNames.touch, this._highlightedHandler); } } diff --git a/packages/core/ui/gestures/gestures-common.ts b/packages/core/ui/gestures/gestures-common.ts index 434b80bd57..fa5237a4af 100644 --- a/packages/core/ui/gestures/gestures-common.ts +++ b/packages/core/ui/gestures/gestures-common.ts @@ -61,6 +61,18 @@ export enum GestureTypes { touch = 1 << 7, } +/** @see GestureTypes */ +export const GestureTypeNames = { + tap: 'tap', + doubleTap: 'doubleTap', + pinch: 'pinch', + pan: 'pan', + swipe: 'swipe', + rotation: 'rotation', + longPress: 'longPress', + touch: 'touch', +} as const; + /** * Defines an enum with supported gesture states. */ @@ -284,38 +296,38 @@ 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(); + const types = new Array(); if (type & GestureTypes.tap) { - types.push('tap'); + types.push(GestureTypeNames.tap); } if (type & GestureTypes.doubleTap) { - types.push('doubleTap'); + types.push(GestureTypeNames.doubleTap); } if (type & GestureTypes.pinch) { - types.push('pinch'); + types.push(GestureTypeNames.pinch); } if (type & GestureTypes.pan) { - types.push('pan'); + types.push(GestureTypeNames.pan); } if (type & GestureTypes.swipe) { - types.push('swipe'); + types.push(GestureTypeNames.swipe); } if (type & GestureTypes.rotation) { - types.push('rotation'); + types.push(GestureTypeNames.rotation); } if (type & GestureTypes.longPress) { - types.push('longPress'); + types.push(GestureTypeNames.longPress); } if (type & GestureTypes.touch) { - types.push('touch'); + types.push(GestureTypeNames.touch); } return types.join(separator); @@ -327,28 +339,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 GestureTypeNames[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 1288f99514..530ecdcb45 100644 --- a/packages/core/ui/gestures/touch-manager.ts +++ b/packages/core/ui/gestures/touch-manager.ts @@ -7,7 +7,7 @@ import { Animation } from '../animation'; import { AnimationDefinition } from '../animation/animation-interfaces'; import { View } from '../core/view'; import { isObject, isFunction } from '../../utils/types'; -import { GestureEvents, GestureStateTypes, GestureTypes } from './gestures-common'; +import { GestureEvents, GestureStateTypes, GestureTypeNames, GestureTypes } from './gestures-common'; export type TouchAnimationFn = (view: View) => void; export type TouchAnimationOptions = { @@ -114,7 +114,7 @@ export class TouchManager { (args.ios).minimumPressDuration = (args.object)?.touchDelay || 0; } }); - view.on('longPress', (args: GestureEventDataWithState) => { + view.on(GestureTypeNames.longPress, (args: GestureEventDataWithState) => { switch (args.state) { case GestureStateTypes.began: if (handleDown) { @@ -133,7 +133,7 @@ export class TouchManager { } } else { if (handleDown || handleUp) { - view.on('touch', (args: TouchGestureEventData) => { + view.on(GestureTypeNames.touch, (args: TouchGestureEventData) => { switch (args.action) { case 'down': if (handleDown) { diff --git a/packages/core/ui/index.ts b/packages/core/ui/index.ts index 893eb4c336..32b23d0b34 100644 --- a/packages/core/ui/index.ts +++ b/packages/core/ui/index.ts @@ -27,7 +27,7 @@ export * from './editable-text-base'; export { Frame, setActivityCallbacks } from './frame'; export type { NavigationEntry, NavigationContext, NavigationTransition, BackstackEntry, ViewEntry, AndroidActivityCallbacks } from './frame'; -export { GesturesObserver, TouchAction, GestureTypes, GestureStateTypes, SwipeDirection, GestureEvents, TouchManager } from './gestures'; +export { GesturesObserver, TouchAction, GestureTypes, GestureTypeNames, GestureStateTypes, SwipeDirection, GestureEvents, TouchManager } from './gestures'; export type { GestureEventData, GestureEventDataWithState, TapGestureEventData, PanGestureEventData, PinchGestureEventData, RotationGestureEventData, SwipeGestureEventData, TouchGestureEventData, TouchAnimationOptions, VisionHoverOptions } from './gestures'; export { HtmlView } from './html-view'; From 988558a2de3c5001112da6e608b92c80bad80b5d Mon Sep 17 00:00:00 2001 From: shirakaba <14055146+shirakaba@users.noreply.github.com> Date: Sat, 4 May 2024 01:04:17 +0900 Subject: [PATCH 3/3] chore(core): get strings directly out of GestureTypes enum keys --- apps/ui/src/events/gestures-page.ts | 20 +++++------ packages/core/ui/button/index.android.ts | 6 ++-- packages/core/ui/gestures/gestures-common.ts | 36 ++++++++------------ packages/core/ui/gestures/touch-manager.ts | 6 ++-- packages/core/ui/index.ts | 2 +- 5 files changed, 31 insertions(+), 39 deletions(-) diff --git a/apps/ui/src/events/gestures-page.ts b/apps/ui/src/events/gestures-page.ts index fedbd4e68b..b8077b437d 100644 --- a/apps/ui/src/events/gestures-page.ts +++ b/apps/ui/src/events/gestures-page.ts @@ -1,4 +1,4 @@ -import { Screen, StackLayout, Page, Label, Button, GestureEventData, GestureTypeNames, RotationGestureEventData, GestureTypes, SwipeGestureEventData, PanGestureEventData, PinchGestureEventData, GestureStateTypes } from '@nativescript/core'; +import { Screen, StackLayout, Page, Label, Button, GestureEventData, RotationGestureEventData, GestureTypes, SwipeGestureEventData, PanGestureEventData, PinchGestureEventData, GestureStateTypes } from '@nativescript/core'; export function createPage() { const stack = new StackLayout(); @@ -65,55 +65,55 @@ export function createPage() { tapAndDoubleTapLabel.automationText = 'Gestures detection disabled'; }); - tapLabel.on(GestureTypeNames.tap, function (args: GestureEventData) { + tapLabel.on(GestureTypes[GestureTypes.tap], function (args: GestureEventData) { tapLabel.text = 'Tap gesture detected, ' + (args.object === tapLabel); }); const observer1 = tapLabel.getGestureObservers(GestureTypes.tap)[0]; - doubleTapLabel.on(GestureTypeNames.doubleTap, function (args: GestureEventData) { + doubleTapLabel.on(GestureTypes[GestureTypes.doubleTap], function (args: GestureEventData) { doubleTapLabel.text = 'Double Tap gesture detected, ' + (args.object === doubleTapLabel); }); const observer2 = doubleTapLabel.getGestureObservers(GestureTypes.doubleTap)[0]; - longPressLabel.on(GestureTypeNames.longPress, function (args: GestureEventData) { + longPressLabel.on(GestureTypes[GestureTypes.longPress], function (args: GestureEventData) { longPressLabel.text = 'Long Press gesture detected, ' + (args.object === longPressLabel); }); const observer3 = longPressLabel.getGestureObservers(GestureTypes.longPress)[0]; - swipeLabel.on(GestureTypeNames.swipe, function (args: SwipeGestureEventData) { + swipeLabel.on(GestureTypes[GestureTypes.swipe], function (args: SwipeGestureEventData) { swipeLabel.text = 'Swipe Direction: ' + args.direction + ', ' + (args.object === swipeLabel); // + getStateAsString(args.state); }); const observer4 = swipeLabel.getGestureObservers(GestureTypes.swipe)[0]; - panLabel.on(GestureTypeNames.pan, function (args: PanGestureEventData) { + panLabel.on(GestureTypes[GestureTypes.pan], function (args: PanGestureEventData) { panLabel.text = 'Pan deltaX:' + Math.round(args.deltaX) + '; deltaY:' + Math.round(args.deltaY) + ';' + ', ' + (args.object === panLabel) + getStateAsString(args.state); }); const observer5 = panLabel.getGestureObservers(GestureTypes.pan)[0]; - pinchLabel.on(GestureTypeNames.pinch, function (args: PinchGestureEventData) { + pinchLabel.on(GestureTypes[GestureTypes.pinch], function (args: PinchGestureEventData) { pinchLabel.text = 'Pinch Scale: ' + Math.round(args.scale) + ', ' + (args.object === pinchLabel) + getStateAsString(args.state); }); const observer6 = pinchLabel.getGestureObservers(GestureTypes.pinch)[0]; - rotationLabel.on(GestureTypeNames.rotation, function (args: RotationGestureEventData) { + rotationLabel.on(GestureTypes[GestureTypes.rotation], function (args: RotationGestureEventData) { rotationLabel.text = 'Rotation: ' + Math.round(args.rotation) + ', ' + (args.object === rotationLabel) + getStateAsString(args.state); }); const observer7 = rotationLabel.getGestureObservers(GestureTypes.rotation)[0]; - tapAndDoubleTapLabel.on(GestureTypeNames.doubleTap, function (args: GestureEventData) { + tapAndDoubleTapLabel.on(GestureTypes[GestureTypes.doubleTap], function (args: GestureEventData) { tapAndDoubleTapLabel.text = 'Last action: Double tap gesture, ' + (args.object === tapAndDoubleTapLabel); }); const observer8 = tapAndDoubleTapLabel.getGestureObservers(GestureTypes.doubleTap)[0]; - tapAndDoubleTapLabel.on(GestureTypeNames.tap, function (args: GestureEventData) { + tapAndDoubleTapLabel.on(GestureTypes[GestureTypes.tap], function (args: GestureEventData) { tapAndDoubleTapLabel.text = 'Last action: Tap gesture, ' + (args.object === tapAndDoubleTapLabel); }); diff --git a/packages/core/ui/button/index.android.ts b/packages/core/ui/button/index.android.ts index 069a869c13..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, TouchAction, GestureTypeNames } 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(GestureTypeNames.touch, this._highlightedHandler); + this.on(GestureTypes[GestureTypes.touch], this._highlightedHandler); } else { - this.off(GestureTypeNames.touch, this._highlightedHandler); + this.off(GestureTypes[GestureTypes.touch], this._highlightedHandler); } } diff --git a/packages/core/ui/gestures/gestures-common.ts b/packages/core/ui/gestures/gestures-common.ts index fa5237a4af..744bafd8d8 100644 --- a/packages/core/ui/gestures/gestures-common.ts +++ b/packages/core/ui/gestures/gestures-common.ts @@ -61,18 +61,6 @@ export enum GestureTypes { touch = 1 << 7, } -/** @see GestureTypes */ -export const GestureTypeNames = { - tap: 'tap', - doubleTap: 'doubleTap', - pinch: 'pinch', - pan: 'pan', - swipe: 'swipe', - rotation: 'rotation', - longPress: 'longPress', - touch: 'touch', -} as const; - /** * Defines an enum with supported gesture states. */ @@ -296,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(GestureTypeNames.tap); + types.push(GestureTypes[GestureTypes.tap]); } if (type & GestureTypes.doubleTap) { - types.push(GestureTypeNames.doubleTap); + types.push(GestureTypes[GestureTypes.doubleTap]); } if (type & GestureTypes.pinch) { - types.push(GestureTypeNames.pinch); + types.push(GestureTypes[GestureTypes.pinch]); } if (type & GestureTypes.pan) { - types.push(GestureTypeNames.pan); + types.push(GestureTypes[GestureTypes.pan]); } if (type & GestureTypes.swipe) { - types.push(GestureTypeNames.swipe); + types.push(GestureTypes[GestureTypes.swipe]); } if (type & GestureTypes.rotation) { - types.push(GestureTypeNames.rotation); + types.push(GestureTypes[GestureTypes.rotation]); } if (type & GestureTypes.longPress) { - types.push(GestureTypeNames.longPress); + types.push(GestureTypes[GestureTypes.longPress]); } if (type & GestureTypes.touch) { - types.push(GestureTypeNames.touch); + types.push(GestureTypes[GestureTypes.touch]); } return types.join(separator); @@ -340,7 +332,7 @@ export function toString(type: GestureTypes, separator?: string): string { * @param type - A string representation of a gesture type (e.g. Tap). */ export function fromString(type: string): GestureTypes | undefined { - return GestureTypeNames[type.trim()]; + 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 530ecdcb45..dd89b3ffa0 100644 --- a/packages/core/ui/gestures/touch-manager.ts +++ b/packages/core/ui/gestures/touch-manager.ts @@ -7,7 +7,7 @@ import { Animation } from '../animation'; import { AnimationDefinition } from '../animation/animation-interfaces'; import { View } from '../core/view'; import { isObject, isFunction } from '../../utils/types'; -import { GestureEvents, GestureStateTypes, GestureTypeNames, GestureTypes } from './gestures-common'; +import { GestureEvents, GestureStateTypes, GestureTypes } from './gestures-common'; export type TouchAnimationFn = (view: View) => void; export type TouchAnimationOptions = { @@ -114,7 +114,7 @@ export class TouchManager { (args.ios).minimumPressDuration = (args.object)?.touchDelay || 0; } }); - view.on(GestureTypeNames.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(GestureTypeNames.touch, (args: TouchGestureEventData) => { + view.on(GestureTypes[GestureTypes.touch], (args: TouchGestureEventData) => { switch (args.action) { case 'down': if (handleDown) { diff --git a/packages/core/ui/index.ts b/packages/core/ui/index.ts index 32b23d0b34..893eb4c336 100644 --- a/packages/core/ui/index.ts +++ b/packages/core/ui/index.ts @@ -27,7 +27,7 @@ export * from './editable-text-base'; export { Frame, setActivityCallbacks } from './frame'; export type { NavigationEntry, NavigationContext, NavigationTransition, BackstackEntry, ViewEntry, AndroidActivityCallbacks } from './frame'; -export { GesturesObserver, TouchAction, GestureTypes, GestureTypeNames, GestureStateTypes, SwipeDirection, GestureEvents, TouchManager } from './gestures'; +export { GesturesObserver, TouchAction, GestureTypes, GestureStateTypes, SwipeDirection, GestureEvents, TouchManager } from './gestures'; export type { GestureEventData, GestureEventDataWithState, TapGestureEventData, PanGestureEventData, PinchGestureEventData, RotationGestureEventData, SwipeGestureEventData, TouchGestureEventData, TouchAnimationOptions, VisionHoverOptions } from './gestures'; export { HtmlView } from './html-view';