fix: update tap event data (#8415)

* Update tap event data object

Adds a:

- TabGestureEventData interface, which can be used for both tap and doubleTap events.

- The event object returned by both tap and doubleTap events now have getX(), getY(), and getPointersCount() methods. These facilitate the same function as those of the touch event object.

* ui(gesture): getX,getY in DIP

Updates the getX() and getY() methods of Tap, doubleTap and touch events to return DIP instead of DP.

* ui(gesture): tap event data includes location

Tap and doubleTap event data now include getX and getY methods for event location. These are in DIP format.

getPointerCount is also available.

* Fix tslint errors

* fix minor formatting issues for api-extrector
This commit is contained in:
Sam Donald
2020-03-19 21:51:23 +11:00
committed by GitHub
parent 4a67a3b73f
commit 8dbb623944
6 changed files with 397 additions and 378 deletions

View File

@@ -870,17 +870,17 @@ export class Frame extends View {
// @public
export interface GestureEventData extends EventData {
android: any
ios: any /* UIGestureRecognizer */;
type: GestureTypes;
view: View;
}
android: any
ios: any /* UIGestureRecognizer */;
type: GestureTypes;
view: View;
}
// @public
export interface GestureEventDataWithState extends GestureEventData {
// (undocumented)
state: number;
}
}
// @public
export class GesturesObserver {
@@ -897,7 +897,7 @@ export class GesturesObserver {
observe(type: GestureTypes);
type: GestureTypes;
}
}
// @public
export enum GestureStateTypes {
@@ -905,7 +905,7 @@ export enum GestureStateTypes {
cancelled,
changed,
ended
}
}
// @public
export enum GestureTypes {
@@ -917,7 +917,7 @@ export enum GestureTypes {
swipe,
tap,
touch
}
}
// @public
export class GridLayout extends LayoutBase {
@@ -1011,49 +1011,49 @@ export const Http: {
// @public
export interface HttpContent {
raw: any;
raw: any;
toArrayBuffer: () => ArrayBuffer;
toArrayBuffer: () => ArrayBuffer;
toFile: (destinationFilePath?: string) => File;
toFile: (destinationFilePath?: string) => File;
toImage: () => Promise<ImageSource>;
toImage: () => Promise<ImageSource>;
toJSON: (encoding?: HttpResponseEncoding) => any;
toJSON: (encoding?: HttpResponseEncoding) => any;
toString: (encoding?: HttpResponseEncoding) => string;
toString: (encoding?: HttpResponseEncoding) => string;
}
// @public
export interface HttpRequestOptions {
content?: string | FormData | ArrayBuffer;
content?: string | FormData | ArrayBuffer;
dontFollowRedirects?: boolean;
dontFollowRedirects?: boolean;
headers?: any;
headers?: any;
method: string;
method: string;
timeout?: number;
timeout?: number;
url: string;
url: string;
}
// @public
export interface HttpResponse {
content?: HttpContent;
content?: HttpContent;
headers: Headers;
headers: Headers;
statusCode: number;
statusCode: number;
}
// @public (undocumented)
export enum HttpResponseEncoding {
// (undocumented)
GBK,
// (undocumented)
UTF8
// (undocumented)
GBK,
// (undocumented)
UTF8
}
// @public
@@ -1646,7 +1646,7 @@ export interface PanGestureEventData extends GestureEventDataWithState {
deltaX: number;
// (undocumented)
deltaY: number;
}
}
// @public
export interface ParserEvent {
@@ -1702,7 +1702,7 @@ export interface PinchGestureEventData extends GestureEventDataWithState {
// (undocumented)
scale: number;
}
}
// @public
export class Placeholder extends View {
@@ -1767,7 +1767,7 @@ export class Repeater extends CustomLayoutView {
export interface RotationGestureEventData extends GestureEventDataWithState {
// (undocumented)
rotation: number;
}
}
// @public
export module Screen {
@@ -2172,13 +2172,13 @@ export enum SwipeDirection {
left,
right,
up
}
}
// @public
export interface SwipeGestureEventData extends GestureEventData {
// (undocumented)
direction: SwipeDirection;
}
}
// @public
export class Switch extends View {
@@ -2417,6 +2417,15 @@ export class TabViewItem extends ViewBase {
public view: View;
}
// @public
export interface TapGestureEventData extends GestureEventData {
getPointerCount(): number;
getX(): number;
getY(): number;
}
// @public
export interface Template {
(): View;
@@ -2536,20 +2545,12 @@ export interface TimerInfo {
}
// @public
export interface TouchGestureEventData extends GestureEventData {
export interface TouchGestureEventData extends TapGestureEventData {
action: "up" | "move" | "down" | "cancel";
// Warning: (ae-forgotten-export) The symbol "Pointer" needs to be exported by the entry point index.d.ts
getActivePointers(): Array<Pointer>;
getAllPointers(): Array<Pointer>;
getPointerCount(): number;
getX(): number;
getY(): number;
}
}
// @public (undocumented)
export const Trace: {

View File

@@ -1,5 +1,5 @@
// Definitions.
import { DoubleTapGestureEventData, GestureEventData, GestureEventDataWithState, PanGestureEventData, RotationGestureEventData, SwipeGestureEventData } from ".";
import { GestureEventData, TapGestureEventData, SwipeGestureEventData, PanGestureEventData, RotationGestureEventData, GestureEventDataWithState } from ".";
import { View, EventData } from "../core/view";
// Types.
@@ -71,14 +71,14 @@ function initializeTapAndDoubleTapGestureListener() {
if (this._target.getGestureObservers(GestureTypes.doubleTap)) {
this._tapTimeoutId = timer.setTimeout(() => {
if (this._type & GestureTypes.tap) {
const args = _getArgs(GestureTypes.tap, this._target, motionEvent);
const args = _getTapArgs(GestureTypes.tap, this._target, motionEvent);
_executeCallback(this._observer, args);
}
timer.clearTimeout(this._tapTimeoutId);
}, TapAndDoubleTapGestureListenerImpl.DoubleTapTimeout);
} else {
if (this._type & GestureTypes.tap) {
const args = _getArgs(GestureTypes.tap, this._target, motionEvent);
const args = _getTapArgs(GestureTypes.tap, this._target, motionEvent);
_executeCallback(this._observer, args);
}
}
@@ -89,7 +89,7 @@ function initializeTapAndDoubleTapGestureListener() {
timer.clearTimeout(this._tapTimeoutId);
}
if (this._type & GestureTypes.doubleTap) {
const args = _getDoubleTapArgs(this._target, motionEvent);
const args = _getTapArgs(GestureTypes.doubleTap, this._target, motionEvent);
_executeCallback(this._observer, args);
}
}
@@ -372,15 +372,18 @@ export class GesturesObserver extends GesturesObserverBase {
}
}
function _getArgs(type: GestureTypes, view: View, e: android.view.MotionEvent): GestureEventData {
return <GestureEventData>{
type: type,
view: view,
android: e,
ios: undefined,
object: view,
eventName: toString(type),
};
function _getTapArgs(type: GestureTypes, view: View, e: android.view.MotionEvent): TapGestureEventData {
return <TapGestureEventData>{
type: type,
view: view,
android: e,
ios: undefined,
object: view,
eventName: toString(type),
getPointerCount: () => e.getPointerCount(),
getX: () => layout.toDeviceIndependentPixels(e.getX()),
getY: () => layout.toDeviceIndependentPixels(e.getY())
};
}
function _getLongPressArgs(type: GestureTypes, view: View, state: GestureStateTypes, e: android.view.MotionEvent): GestureEventDataWithState {
@@ -395,19 +398,6 @@ function _getLongPressArgs(type: GestureTypes, view: View, state: GestureStateTy
};
}
function _getDoubleTapArgs(view: View, e: android.view.MotionEvent): DoubleTapGestureEventData {
return <DoubleTapGestureEventData>{
type: GestureTypes.doubleTap,
view: view,
android: e,
getX: () => e.getX() / layout.getDisplayDensity(),
getY: () => e.getY() / layout.getDisplayDensity(),
ios: undefined,
object: view,
eventName: toString(GestureTypes.doubleTap),
};
}
function _getSwipeArgs(direction: SwipeDirection, view: View,
initialEvent: android.view.MotionEvent, currentEvent: android.view.MotionEvent): SwipeGestureEventData {
return <SwipeGestureEventData>{

View File

@@ -1,309 +1,329 @@
/**
/**
* Contains the GesturesObserver class, which lets you observe and respond to user gestures.
* @module "ui/gestures"
*/ /** */
import { View, EventData } from "../core/view";
import { View, EventData } from "../core/view";
/**
* Defines an enum with supported gesture types.
*/
export enum GestureTypes {
/**
* Denotes tap (click) gesture.
*/
tap,
/**
* Denotes double tap gesture.
*/
doubleTap,
/**
* Denotes pinch gesture.
*/
pinch,
/**
* Denotes pan gesture.
*/
pan,
/**
* Denotes swipe gesture.
*/
swipe,
/**
* Denotes rotation gesture.
*/
rotation,
/**
* Denotes long press gesture.
*/
longPress,
/**
* Denotes touch action.
*/
touch
}
/**
* Defines an enum with supported gesture types.
*/
export enum GestureTypes {
/**
* Denotes tap (click) gesture.
*/
tap,
/**
* Denotes double tap gesture.
*/
doubleTap,
/**
* Denotes pinch gesture.
*/
pinch,
/**
* Denotes pan gesture.
*/
pan,
/**
* Denotes swipe gesture.
*/
swipe,
/**
* Denotes rotation gesture.
*/
rotation,
/**
* Denotes long press gesture.
*/
longPress,
/**
* Denotes touch action.
*/
touch
}
/**
* Defines an enum with supported gesture states.
*/
export enum GestureStateTypes {
/**
* Gesture canceled.
*/
cancelled,
/**
* Gesture began.
*/
began,
/**
* Gesture changed.
*/
changed,
/**
* Gesture ended.
*/
ended
}
/**
* Defines an enum for swipe gesture direction.
*/
export enum SwipeDirection {
/**
* Denotes right direction for swipe gesture.
*/
right,
/**
* Denotes left direction for swipe gesture.
*/
left,
/**
* Denotes up direction for swipe gesture.
*/
up,
/**
* Denotes down direction for swipe gesture.
*/
down
}
/**
* Defines a touch action
*/
export module TouchAction {
/**
* Down action.
*/
export const down: string;
/**
* Up action.
*/
export const up: string;
/**
* Move action.
*/
export const move: string;
/**
* Cancel action.
*/
export const cancel: string;
}
/**
* Provides gesture event data.
*/
export interface GestureEventData extends EventData {
/**
* Gets the type of the gesture.
*/
type: GestureTypes;
/**
* Gets the view which originates the gesture.
*/
view: View;
/**
* Gets the underlying native iOS specific [UIGestureRecognizer](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/).
*/
ios: any /* UIGestureRecognizer */;
/**
* Gets the underlying native android specific [gesture detector](http://developer.android.com/reference/android/view/GestureDetector.html).
*/
android: any
}
/**
* Defines an enum with supported gesture states.
*/
export enum GestureStateTypes {
/**
* Gesture canceled.
*/
cancelled,
/**
* Gesture began.
*/
began,
/**
* Gesture changed.
*/
changed,
/**
* Gesture ended.
*/
ended
}
/**
* Defines an enum for swipe gesture direction.
*/
export enum SwipeDirection {
/**
* Denotes right direction for swipe gesture.
*/
right,
/**
* Denotes left direction for swipe gesture.
*/
left,
/**
* Denotes up direction for swipe gesture.
*/
up,
/**
* Denotes down direction for swipe gesture.
*/
down
}
/**
* Defines a touch action
*/
export module TouchAction {
/**
* Down action.
*/
export const down: string;
/**
* Up action.
*/
export const up: string;
/**
* Move action.
*/
export const move: string;
/**
* Cancel action.
*/
export const cancel: string;
}
/**
* Provides gesture event data.
*/
export interface GestureEventData extends EventData {
/**
* Gets the type of the gesture.
*/
type: GestureTypes;
/**
* Gets the view which originates the gesture.
*/
view: View;
/**
* Gets the underlying native iOS specific [UIGestureRecognizer](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/).
*/
ios: any /* UIGestureRecognizer */;
/**
* Gets the underlying native android specific [gesture detector](http://developer.android.com/reference/android/view/GestureDetector.html).
*/
android: any
}
/**
* Provides gesture event data.
*/
export interface TouchGestureEventData extends GestureEventData {
/**
* Gets action of the touch. Possible values: 'up', 'move', 'down', 'cancel'
*/
action: "up" | "move" | "down" | "cancel";
/**
* Gets the X coordinate of this event inside the view that triggered the event.
*/
getX(): number;
/**
* Gets the Y coordinate of this event inside the view that triggered the event.
*/
getY(): number;
/**
* Gets the number of pointers in the event.
*/
/**
* Provides gesture event data.
*/
export interface TapGestureEventData extends GestureEventData {
/**
* Gets the number of pointers in the event.
*/
getPointerCount(): number;
/**
* Gets the pointers that triggered the event.
* Note: In Android there is aways only one active pointer.
*/
getActivePointers(): Array<Pointer>;
/**
* Gets all pointers.
*/
getAllPointers(): Array<Pointer>;
}
/**
* Pointer is an object representing a finger (or other object) that is touching the screen.
*/
export interface Pointer {
/**
* The id of the pointer.
*/
android: any;
/**
* The UITouch object associated to the touch
*/
ios: any;
/**
* Gets the X coordinate of the pointer inside the view that triggered the event.
* Gets the X coordinate of this event inside the view that triggered the event
*/
getX(): number;
/**
* Gets the Y coordinate of the pointer inside the view that triggered the event.
* Gets the Y coordinate of the event inside the view that triggered the event.
*/
getY(): number;
}
/**
* Provides gesture event data.
*/
export interface GestureEventDataWithState extends GestureEventData {
state: number;
}
}
/**
* Provides gesture event data.
*/
export interface TouchGestureEventData extends TapGestureEventData {
/**
* Gets action of the touch. Possible values: 'up', 'move', 'down', 'cancel'
*/
action: "up" | "move" | "down" | "cancel";
/**
* Gets the pointers that triggered the event.
* Note: In Android there is aways only one active pointer.
*/
getActivePointers(): Array<Pointer>;
/**
* Gets all pointers.
*/
getAllPointers(): Array<Pointer>;
}
/**
* Pointer is an object representing a finger (or other object) that is touching the screen.
*/
export interface Pointer {
/**
* The id of the pointer.
*/
android: any;
/**
* The UITouch object associated to the touch
*/
ios: any;
/**
* Gets the X coordinate of the pointer inside the view that triggered the event.
*/
getX(): number;
/**
* Provides gesture event data for pinch gesture.
*/
export interface PinchGestureEventData extends GestureEventDataWithState {
scale: number;
/**
* Gets the Y coordinate of the pointer inside the view that triggered the event.
*/
getY(): number;
getFocusX(): number;
getFocusY(): number;
}
/**
* Gests the X coordinate of the pointer inside the view that triggered the event.
* @returns The X coordinate in _Device Pixels_.
*/
getXPixels(): number
/**
* Provides gesture event data for double tap gesture.
*/
export interface DoubleTapGestureEventData extends GestureEventData {
getX(): number;
getY(): number;
}
/**
* Gets the X coordinate of the pointer inside the view that triggered the event.
* @returns The X coordinate in _Device Independent Pixels_.
*/
getXDIP(): number
/**
* Provides gesture event data for swipe gesture.
*/
export interface SwipeGestureEventData extends GestureEventData {
direction: SwipeDirection;
}
/**
* Gests the Y coordinate of the pointer inside the view that triggered the event.
* @returns The Y coordinate in _Device Pixels_.
*/
getYPixels(): number
/**
* Provides gesture event data for pan gesture.
*/
export interface PanGestureEventData extends GestureEventDataWithState {
deltaX: number;
deltaY: number;
}
/**
* Provides gesture event data for rotation gesture.
*/
export interface RotationGestureEventData extends GestureEventDataWithState {
rotation: number;
}
/**
* Provides options for the GesturesObserver.
*/
export class GesturesObserver {
/**
* Creates an instance of GesturesObserver class.
* @param target - The view for which the observer is created.
* @param callback - A function that will be executed when a gesture is received.
* @param context - default this argument for the callbacks.
*/
constructor(target: View, callback: (args: GestureEventData) => void, context: any);
/**
* Registers a gesture observer to a view and gesture.
* @param type - Type of the gesture.
*/
observe(type: GestureTypes);
/**
* Disconnects the gesture observer.
*/
disconnect();
/**
* Gesture type attached to the observer.
*/
type: GestureTypes;
/**
* A function that will be executed when a gesture is received.
*/
callback: (args: GestureEventData) => void;
/**
* A context which will be used as `this` in callback execution.
*/
context: any;
/**
* An internal Android specific method used to pass the motion event to the correct gesture observer.
*/
androidOnTouchEvent: (motionEvent: any /* android.view.MotionEvent */) => void;
}
/**
* A short-hand function that is used to create a gesture observer for a view and gesture.
* @param target - View which will be watched for originating a specific gesture.
* @param type - Type of the gesture.
* @param callback - A function that will be executed when a gesture is received.
* @param context - this argument for the callback.
*/
export function observe(target: View, type: GestureTypes, callback: (args: GestureEventData) => void, context?: any): GesturesObserver;
/**
* Returns a string representation of a gesture type.
* @param type - Type of the gesture.
* @param separator(optional) - Text separator between gesture type strings.
*/
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;
/**
* Gets the Y coordinate of the pointer inside the view that triggered the event.
* @returns The Y coordinate in _Device Independent Pixels_.
*/
getYDIP(): number
}
/**
* Provides gesture event data.
*/
export interface GestureEventDataWithState extends GestureEventData {
state: number;
}
/**
* Provides gesture event data for pinch gesture.
*/
export interface PinchGestureEventData extends GestureEventDataWithState {
scale: number;
getFocusX(): number;
getFocusY(): number;
}
/**
* Provides gesture event data for swipe gesture.
*/
export interface SwipeGestureEventData extends GestureEventData {
direction: SwipeDirection;
}
/**
* Provides gesture event data for pan gesture.
*/
export interface PanGestureEventData extends GestureEventDataWithState {
deltaX: number;
deltaY: number;
}
/**
* Provides gesture event data for rotation gesture.
*/
export interface RotationGestureEventData extends GestureEventDataWithState {
rotation: number;
}
/**
* Provides options for the GesturesObserver.
*/
export class GesturesObserver {
/**
* Creates an instance of GesturesObserver class.
* @param target - The view for which the observer is created.
* @param callback - A function that will be executed when a gesture is received.
* @param context - default this argument for the callbacks.
*/
constructor(target: View, callback: (args: GestureEventData) => void, context: any);
/**
* Registers a gesture observer to a view and gesture.
* @param type - Type of the gesture.
*/
observe(type: GestureTypes);
/**
* Disconnects the gesture observer.
*/
disconnect();
/**
* Gesture type attached to the observer.
*/
type: GestureTypes;
/**
* A function that will be executed when a gesture is received.
*/
callback: (args: GestureEventData) => void;
/**
* A context which will be used as `this` in callback execution.
*/
context: any;
/**
* An internal Android specific method used to pass the motion event to the correct gesture observer.
*/
androidOnTouchEvent: (motionEvent: any /* android.view.MotionEvent */) => void;
}
/**
* A short-hand function that is used to create a gesture observer for a view and gesture.
* @param target - View which will be watched for originating a specific gesture.
* @param type - Type of the gesture.
* @param callback - A function that will be executed when a gesture is received.
* @param context - this argument for the callback.
*/
export function observe(target: View, type: GestureTypes, callback: (args: GestureEventData) => void, context?: any): GesturesObserver;
/**
* Returns a string representation of a gesture type.
* @param type - Type of the gesture.
* @param separator(optional) - Text separator between gesture type strings.
*/
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;

View File

@@ -1,10 +1,14 @@
// Definitions.
import { DoubleTapGestureEventData, GestureEventData, GestureEventDataWithState, SwipeGestureEventData, PanGestureEventData, RotationGestureEventData, PinchGestureEventData } from ".";
import { GestureEventData, TapGestureEventData, GestureEventDataWithState, SwipeGestureEventData, PanGestureEventData, RotationGestureEventData, PinchGestureEventData } from ".";
import { View, EventData } from "../core/view";
// Types.
import { GesturesObserverBase, toString, TouchAction, GestureStateTypes, GestureTypes, SwipeDirection } from "./gestures-common";
// Import layout from utils directly to avoid circular references
import { layout } from "../../utils/utils";
export * from "./gestures-common";
export function observe(target: View, type: GestureTypes, callback: (args: GestureEventData) => void, context?: any): GesturesObserver {
@@ -123,12 +127,14 @@ export class GesturesObserver extends GesturesObserverBase {
const nativeView = <UIView>target.nativeViewProtected;
if (type & GestureTypes.tap) {
nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.tap));
nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.tap, args => {
this._executeCallback(_getTapData(args));
}));
}
if (type & GestureTypes.doubleTap) {
nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.doubleTap, args => {
this._executeCallback(_getDoubleTapData(args));
this._executeCallback(_getTapData(args));
}));
}
@@ -300,20 +306,21 @@ function _getSwipeDirection(direction: UISwipeGestureRecognizerDirection): Swipe
}
}
function _getDoubleTapData(args: GestureEventData): DoubleTapGestureEventData {
const recognizer = <UITapGestureRecognizer>args.ios;
const location: CGPoint = recognizer.locationInView(args.view.nativeViewProtected);
function _getTapData(args: GestureEventData): TapGestureEventData {
const recognizer = <UITapGestureRecognizer>args.ios;
const center = recognizer.locationInView(args.view.nativeViewProtected);
return <DoubleTapGestureEventData>{
type: args.type,
view: args.view,
ios: args.ios,
android: undefined,
getX: () => location.x,
getY: () => location.y,
object: args.view,
eventName: toString(args.type)
};
return <TapGestureEventData>{
type: args.type,
view: args.view,
ios: args.ios,
android: undefined,
eventName: args.eventName,
object: args.object,
getPointerCount: () => recognizer.numberOfTouches,
getX: () => layout.toDeviceIndependentPixels(center.x),
getY: () => layout.toDeviceIndependentPixels(center.y)
};
}
function _getPinchData(args: GestureEventData): PinchGestureEventData {
@@ -461,7 +468,7 @@ class Pointer implements Pointer {
}
getY(): number {
return this.location.y;
return this.location.y;
}
}

View File

@@ -10,7 +10,7 @@ export { View, Template, KeyedTemplate, ShownModallyData } from "./core/view";
export { DatePicker } from "./date-picker";
export { EditableTextBase } from "./editable-text-base";
export { Frame, NavigationEntry, NavigationContext, NavigationTransition, BackstackEntry, ViewEntry } from "./frame";
export { GestureEventData, GestureEventDataWithState, GestureStateTypes, GestureTypes, GesturesObserver, PanGestureEventData, PinchGestureEventData, RotationGestureEventData, SwipeDirection, SwipeGestureEventData, TouchGestureEventData } from "./gestures";
export { GestureEventData, GestureEventDataWithState, GestureStateTypes, GestureTypes, GesturesObserver, TapGestureEventData, PanGestureEventData, PinchGestureEventData, RotationGestureEventData, SwipeDirection, SwipeGestureEventData, TouchGestureEventData } from "./gestures";
export { HtmlView } from "./html-view";
export { Image } from "./image";
export { Cache as ImageCache, DownloadError, DownloadRequest, DownloadedData } from "./image-cache";

View File

@@ -22,12 +22,13 @@ export {
GestureStateTypes,
GestureTypes,
GesturesObserver,
TapGestureEventData,
PanGestureEventData,
PinchGestureEventData,
RotationGestureEventData,
SwipeDirection,
SwipeGestureEventData,
TouchGestureEventData
TouchGestureEventData,
} from "./gestures";
export { HtmlView } from "./html-view";