tap.dart 2.35 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'arena.dart';
6
import 'constants.dart';
7
import 'events.dart';
8
import 'recognizer.dart';
9

Hixie's avatar
Hixie committed
10 11
typedef void GestureTapDownCallback(Point globalPosition);
typedef void GestureTapUpCallback(Point globalPosition);
12
typedef void GestureTapCallback();
13
typedef void GestureTapCancelCallback();
14

15 16 17 18
/// TapGestureRecognizer is a tap recognizer that tracks only one primary
/// 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.
class TapGestureRecognizer extends PrimaryPointerGestureRecognizer {
19
  TapGestureRecognizer() : super(deadline: kPressTimeout);
20 21

  GestureTapDownCallback onTapDown;
Hixie's avatar
Hixie committed
22
  GestureTapUpCallback onTapUp;
23
  GestureTapCallback onTap;
24
  GestureTapCancelCallback onTapCancel;
25

Hixie's avatar
Hixie committed
26
  bool _sentTapDown = false;
27
  bool _wonArena = false;
28
  Point _finalPosition;
29

Ian Hickson's avatar
Ian Hickson committed
30 31
  void handlePrimaryPointer(PointerEvent event) {
    if (event is PointerUpEvent) {
32
      _finalPosition = event.position;
Hixie's avatar
Hixie committed
33
      _checkUp();
34 35 36
    }
  }

Ian Hickson's avatar
Ian Hickson committed
37 38 39 40 41
  void resolve(GestureDisposition disposition) {
    if (_wonArena && disposition == GestureDisposition.rejected) {
      if (onTapCancel != null)
        onTapCancel();
      _reset();
42
    }
Ian Hickson's avatar
Ian Hickson committed
43 44 45
    super.resolve(disposition);
  }

Hixie's avatar
Hixie committed
46 47 48 49
  void didExceedDeadline() {
    _checkDown();
  }

50 51 52
  void acceptGesture(int pointer) {
    super.acceptGesture(pointer);
    if (pointer == primaryPointer) {
Hixie's avatar
Hixie committed
53
      _checkDown();
54
      _wonArena = true;
Hixie's avatar
Hixie committed
55
      _checkUp();
56 57 58 59 60 61 62 63 64
    }
  }

  void rejectGesture(int pointer) {
    super.rejectGesture(pointer);
    if (pointer == primaryPointer) {
      assert(state == GestureRecognizerState.defunct);
      if (onTapCancel != null)
        onTapCancel();
65
      _reset();
66 67 68
    }
  }

Hixie's avatar
Hixie committed
69 70 71 72 73 74 75 76 77
  void _checkDown() {
    if (!_sentTapDown) {
      if (onTapDown != null)
        onTapDown(initialPosition);
      _sentTapDown = true;
    }
  }

  void _checkUp() {
78
    if (_wonArena && _finalPosition != null) {
79
      resolve(GestureDisposition.accepted);
80 81
      if (onTapUp != null)
        onTapUp(_finalPosition);
82 83
      if (onTap != null)
        onTap();
84
      _reset();
85 86
    }
  }
87 88

  void _reset() {
Hixie's avatar
Hixie committed
89
    _sentTapDown = false;
90 91 92
    _wonArena = false;
    _finalPosition = null;
  }
93 94

  String toStringShort() => 'tap';
95
}