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

@@ -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,19 +2545,11 @@ 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)

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,14 +372,17 @@ export class GesturesObserver extends GesturesObserverBase {
}
}
function _getArgs(type: GestureTypes, view: View, e: android.view.MotionEvent): GestureEventData {
return <GestureEventData>{
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())
};
}
@@ -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,4 +1,4 @@
/**
/**
* Contains the GesturesObserver class, which lets you observe and respond to user gestures.
* @module "ui/gestures"
*/ /** */
@@ -137,27 +137,30 @@ export interface GestureEventData extends EventData {
/**
* 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;
export interface TapGestureEventData extends GestureEventData {
/**
* Gets the number of pointers in the event.
*/
getPointerCount(): number;
/**
* Gets the X coordinate of this event inside the view that triggered the event
*/
getX(): number;
/**
* Gets the Y coordinate of the event inside the view that triggered the event.
*/
getY(): 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.
@@ -193,6 +196,30 @@ export interface Pointer {
* Gets the Y coordinate of the pointer inside the view that triggered the event.
*/
getY(): number;
/**
* Gests the X coordinate of the pointer inside the view that triggered the event.
* @returns The X coordinate in _Device Pixels_.
*/
getXPixels(): 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
/**
* Gests the Y coordinate of the pointer inside the view that triggered the event.
* @returns The Y coordinate in _Device Pixels_.
*/
getYPixels(): number
/**
* Gets the Y coordinate of the pointer inside the view that triggered the event.
* @returns The Y coordinate in _Device Independent Pixels_.
*/
getYDIP(): number
}
/**
@@ -212,14 +239,6 @@ export interface PinchGestureEventData extends GestureEventDataWithState {
getFocusY(): number;
}
/**
* Provides gesture event data for double tap gesture.
*/
export interface DoubleTapGestureEventData extends GestureEventData {
getX(): number;
getY(): number;
}
/**
* Provides gesture event data for swipe gesture.
*/
@@ -307,3 +326,4 @@ 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;

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,19 +306,20 @@ function _getSwipeDirection(direction: UISwipeGestureRecognizerDirection): Swipe
}
}
function _getDoubleTapData(args: GestureEventData): DoubleTapGestureEventData {
function _getTapData(args: GestureEventData): TapGestureEventData {
const recognizer = <UITapGestureRecognizer>args.ios;
const location: CGPoint = recognizer.locationInView(args.view.nativeViewProtected);
const center = recognizer.locationInView(args.view.nativeViewProtected);
return <DoubleTapGestureEventData>{
return <TapGestureEventData>{
type: args.type,
view: args.view,
ios: args.ios,
android: undefined,
getX: () => location.x,
getY: () => location.y,
object: args.view,
eventName: toString(args.type)
eventName: args.eventName,
object: args.object,
getPointerCount: () => recognizer.numberOfTouches,
getX: () => layout.toDeviceIndependentPixels(center.x),
getY: () => layout.toDeviceIndependentPixels(center.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";