Commit 2225405f authored by Adam Barth's avatar Adam Barth

Add a position to the onTap callback

Fixes #1807
parent e77cad81
...@@ -169,7 +169,7 @@ class TextureButtonState extends State<TextureButton> { ...@@ -169,7 +169,7 @@ class TextureButtonState extends State<TextureButton> {
) )
) )
), ),
onTapDown: () { onTapDown: (_) {
setState(() { setState(() {
_highlight = true; _highlight = true;
}); });
......
...@@ -11,28 +11,37 @@ import 'events.dart'; ...@@ -11,28 +11,37 @@ import 'events.dart';
import 'pointer_router.dart'; import 'pointer_router.dart';
import 'recognizer.dart'; import 'recognizer.dart';
typedef void GestureTapDownCallback(ui.Point globalPosition);
typedef void GestureTapUpCallback(ui.Point globalPosition);
typedef void GestureTapCallback(); typedef void GestureTapCallback();
typedef void GestureTapCancelCallback();
/// TapGestureRecognizer is a tap recognizer that tracks only one primary /// TapGestureRecognizer is a tap recognizer that tracks only one primary
/// pointer per gesture. That is, during tap recognition, extra pointer events /// pointer per gesture. That is, during tap recognition, extra pointer events
/// are ignored: down-1, down-2, up-1, up-2 produces only one tap on up-1. /// are ignored: down-1, down-2, up-1, up-2 produces only one tap on up-1.
class TapGestureRecognizer extends PrimaryPointerGestureRecognizer { class TapGestureRecognizer extends PrimaryPointerGestureRecognizer {
TapGestureRecognizer({ PointerRouter router, this.onTap }) TapGestureRecognizer({
: super(router: router); PointerRouter router,
this.onTapDown,
this.onTapUp,
this.onTap,
this.onTapCancel
}) : super(router: router);
GestureTapDownCallback onTapDown;
GestureTapDownCallback onTapUp;
GestureTapCallback onTap; GestureTapCallback onTap;
GestureTapCallback onTapDown; GestureTapCancelCallback onTapCancel;
GestureTapCallback onTapCancel;
bool _wonArena = false; bool _wonArena = false;
bool _didTap = false; Point _finalPosition;
void handlePrimaryPointer(PointerInputEvent event) { void handlePrimaryPointer(PointerInputEvent event) {
if (event.type == 'pointerdown') { if (event.type == 'pointerdown') {
if (onTapDown != null) if (onTapDown != null)
onTapDown(); onTapDown(event.position);
} else if (event.type == 'pointerup') { } else if (event.type == 'pointerup') {
_didTap = true; _finalPosition = event.position;
_check(); _check();
} }
} }
...@@ -50,15 +59,17 @@ class TapGestureRecognizer extends PrimaryPointerGestureRecognizer { ...@@ -50,15 +59,17 @@ class TapGestureRecognizer extends PrimaryPointerGestureRecognizer {
if (pointer == primaryPointer) { if (pointer == primaryPointer) {
assert(state == GestureRecognizerState.defunct); assert(state == GestureRecognizerState.defunct);
_wonArena = false; _wonArena = false;
_didTap = false; _finalPosition = null;
if (onTapCancel != null) if (onTapCancel != null)
onTapCancel(); onTapCancel();
} }
} }
void _check() { void _check() {
if (_wonArena && _didTap) { if (_wonArena && _finalPosition != null) {
resolve(GestureDisposition.accepted); resolve(GestureDisposition.accepted);
if (onTapUp != null)
onTapUp(_finalPosition);
if (onTap != null) if (onTap != null)
onTap(); onTap();
} }
...@@ -76,7 +87,7 @@ class _TapTracker { ...@@ -76,7 +87,7 @@ class _TapTracker {
assert(event.type == 'pointerdown'); assert(event.type == 'pointerdown');
} }
int pointer; final int pointer;
GestureArenaEntry entry; GestureArenaEntry entry;
ui.Point _initialPosition; ui.Point _initialPosition;
bool _isTrackingPointer; bool _isTrackingPointer;
...@@ -102,7 +113,7 @@ class _TapTracker { ...@@ -102,7 +113,7 @@ class _TapTracker {
} }
enum TapResolution { enum _TapResolution {
tap, tap,
cancel cancel
} }
...@@ -113,17 +124,15 @@ enum TapResolution { ...@@ -113,17 +124,15 @@ enum TapResolution {
class _TapGesture extends _TapTracker { class _TapGesture extends _TapTracker {
_TapGesture({ this.gestureRecognizer, PointerInputEvent event }) _TapGesture({ this.gestureRecognizer, PointerInputEvent event })
: super(event: event) { : super(event: event) {
entry = GestureArena.instance.add(event.pointer, gestureRecognizer); entry = GestureArena.instance.add(event.pointer, gestureRecognizer);
_wonArena = false;
_didTap = false;
startTrackingPointer(gestureRecognizer.router, handleEvent); startTrackingPointer(gestureRecognizer.router, handleEvent);
} }
MultiTapGestureRecognizer gestureRecognizer; final MultiTapGestureRecognizer gestureRecognizer;
bool _wonArena; bool _wonArena = false;
bool _didTap; ui.Point _finalPosition;
void handleEvent(PointerInputEvent event) { void handleEvent(PointerInputEvent event) {
assert(event.pointer == pointer); assert(event.pointer == pointer);
...@@ -133,7 +142,7 @@ class _TapGesture extends _TapTracker { ...@@ -133,7 +142,7 @@ class _TapGesture extends _TapTracker {
cancel(); cancel();
} else if (event.type == 'pointerup') { } else if (event.type == 'pointerup') {
stopTrackingPointer(gestureRecognizer.router, handleEvent); stopTrackingPointer(gestureRecognizer.router, handleEvent);
_didTap = true; _finalPosition = event.position;
_check(); _check();
} }
} }
...@@ -145,7 +154,7 @@ class _TapGesture extends _TapTracker { ...@@ -145,7 +154,7 @@ class _TapGesture extends _TapTracker {
void reject() { void reject() {
stopTrackingPointer(gestureRecognizer.router, handleEvent); stopTrackingPointer(gestureRecognizer.router, handleEvent);
gestureRecognizer._resolveTap(pointer, TapResolution.cancel); gestureRecognizer._resolveTap(pointer, _TapResolution.cancel, null);
} }
void cancel() { void cancel() {
...@@ -158,8 +167,8 @@ class _TapGesture extends _TapTracker { ...@@ -158,8 +167,8 @@ class _TapGesture extends _TapTracker {
} }
void _check() { void _check() {
if (_wonArena && _didTap) if (_wonArena && _finalPosition != null)
gestureRecognizer._resolveTap(pointer, TapResolution.tap); gestureRecognizer._resolveTap(pointer, _TapResolution.tap, _finalPosition);
} }
} }
...@@ -169,12 +178,19 @@ class _TapGesture extends _TapTracker { ...@@ -169,12 +178,19 @@ class _TapGesture extends _TapTracker {
/// does so independently of others: down-1, down-2, up-1, up-2 produces two /// does so independently of others: down-1, down-2, up-1, up-2 produces two
/// taps, on up-1 and up-2. /// taps, on up-1 and up-2.
class MultiTapGestureRecognizer extends DisposableArenaMember { class MultiTapGestureRecognizer extends DisposableArenaMember {
MultiTapGestureRecognizer({ this.router, this.onTap, this.onTapDown, this.onTapCancel }); MultiTapGestureRecognizer({
this.router,
this.onTapDown,
this.onTapUp,
this.onTap,
this.onTapCancel
});
PointerRouter router; PointerRouter router;
GestureTapDownCallback onTapDown;
GestureTapDownCallback onTapUp;
GestureTapCallback onTap; GestureTapCallback onTap;
GestureTapCallback onTapDown; GestureTapCancelCallback onTapCancel;
GestureTapCallback onTapCancel;
Map<int, _TapGesture> _gestureMap = new Map<int, _TapGesture>(); Map<int, _TapGesture> _gestureMap = new Map<int, _TapGesture>();
...@@ -185,7 +201,7 @@ class MultiTapGestureRecognizer extends DisposableArenaMember { ...@@ -185,7 +201,7 @@ class MultiTapGestureRecognizer extends DisposableArenaMember {
event: event event: event
); );
if (onTapDown != null) if (onTapDown != null)
onTapDown(); onTapDown(event.position);
} }
void acceptGesture(int pointer) { void acceptGesture(int pointer) {
...@@ -198,9 +214,11 @@ class MultiTapGestureRecognizer extends DisposableArenaMember { ...@@ -198,9 +214,11 @@ class MultiTapGestureRecognizer extends DisposableArenaMember {
_gestureMap[pointer]?.reject(); _gestureMap[pointer]?.reject();
} }
void _resolveTap(int pointer, TapResolution resolution) { void _resolveTap(int pointer, _TapResolution resolution, ui.Point globalPosition) {
_gestureMap.remove(pointer); _gestureMap.remove(pointer);
if (resolution == TapResolution.tap) { if (resolution == _TapResolution.tap) {
if (onTapUp != null)
onTapUp(globalPosition);
if (onTap != null) if (onTap != null)
onTap(); onTap();
} else { } else {
......
...@@ -41,7 +41,7 @@ class InkWell extends StatefulComponent { ...@@ -41,7 +41,7 @@ class InkWell extends StatefulComponent {
final _HighlightChangedCallback onHighlightChanged; final _HighlightChangedCallback onHighlightChanged;
final Color defaultColor; final Color defaultColor;
final Color highlightColor; final Color highlightColor;
_InkWellState createState() => new _InkWellState(); _InkWellState createState() => new _InkWellState();
} }
...@@ -237,7 +237,7 @@ class _RenderInkSplashes extends RenderProxyBox { ...@@ -237,7 +237,7 @@ class _RenderInkSplashes extends RenderProxyBox {
_longPress = null; _longPress = null;
} }
void _handleTapDown() { void _handleTapDown(_) {
if (onHighlightChanged != null) if (onHighlightChanged != null)
onHighlightChanged(true); onHighlightChanged(true);
} }
......
...@@ -9,9 +9,10 @@ import 'basic.dart'; ...@@ -9,9 +9,10 @@ import 'basic.dart';
import 'framework.dart'; import 'framework.dart';
export 'package:flutter/gestures.dart' show export 'package:flutter/gestures.dart' show
GestureTapDownCallback,
GestureTapUpCallback,
GestureTapCallback, GestureTapCallback,
GestureTapCallback, GestureTapCancelCallback,
GestureTapCallback,
GestureShowPressCallback, GestureShowPressCallback,
GestureLongPressCallback, GestureLongPressCallback,
GestureDragStartCallback, GestureDragStartCallback,
...@@ -31,10 +32,11 @@ class GestureDetector extends StatefulComponent { ...@@ -31,10 +32,11 @@ class GestureDetector extends StatefulComponent {
const GestureDetector({ const GestureDetector({
Key key, Key key,
this.child, this.child,
this.onTap,
this.onDoubleTap,
this.onTapDown, this.onTapDown,
this.onTapUp,
this.onTap,
this.onTapCancel, this.onTapCancel,
this.onDoubleTap,
this.onShowPress, this.onShowPress,
this.onLongPress, this.onLongPress,
this.onVerticalDragStart, this.onVerticalDragStart,
...@@ -53,9 +55,10 @@ class GestureDetector extends StatefulComponent { ...@@ -53,9 +55,10 @@ class GestureDetector extends StatefulComponent {
final Widget child; final Widget child;
final GestureTapDownCallback onTapDown;
final GestureTapDownCallback onTapUp;
final GestureTapCallback onTap; final GestureTapCallback onTap;
final GestureTapCallback onTapDown; final GestureTapCancelCallback onTapCancel;
final GestureTapCallback onTapCancel;
final GestureTapCallback onDoubleTap; final GestureTapCallback onDoubleTap;
final GestureShowPressCallback onShowPress; final GestureShowPressCallback onShowPress;
...@@ -125,13 +128,14 @@ class _GestureDetectorState extends State<GestureDetector> { ...@@ -125,13 +128,14 @@ class _GestureDetectorState extends State<GestureDetector> {
} }
void _syncTap() { void _syncTap() {
if (config.onTap == null && config.onTapDown == null && config.onTapCancel == null) { if (config.onTapDown == null && config.onTapUp == null && config.onTap == null && config.onTapCancel == null) {
_tap = _ensureDisposed(_tap); _tap = _ensureDisposed(_tap);
} else { } else {
_tap ??= new TapGestureRecognizer(router: _router); _tap ??= new TapGestureRecognizer(router: _router);
_tap _tap
..onTap = config.onTap
..onTapDown = config.onTapDown ..onTapDown = config.onTapDown
..onTapUp = config.onTapUp
..onTap = config.onTap
..onTapCancel = config.onTapCancel; ..onTapCancel = config.onTapCancel;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment