tap.dart 2.54 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 'pointer_router.dart';
9
import 'recognizer.dart';
10

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

16 17 18 19
/// 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 {
20 21
  TapGestureRecognizer({
    PointerRouter router,
Ian Hickson's avatar
Ian Hickson committed
22
    GestureArena gestureArena,
23 24 25 26
    this.onTapDown,
    this.onTapUp,
    this.onTap,
    this.onTapCancel
Ian Hickson's avatar
Ian Hickson committed
27 28 29 30 31
  }) : super(
    router: router,
    gestureArena: gestureArena,
    deadline: kPressTimeout
  );
32 33

  GestureTapDownCallback onTapDown;
Hixie's avatar
Hixie committed
34
  GestureTapUpCallback onTapUp;
35
  GestureTapCallback onTap;
36
  GestureTapCancelCallback onTapCancel;
37

Hixie's avatar
Hixie committed
38
  bool _sentTapDown = false;
39
  bool _wonArena = false;
40
  Point _finalPosition;
41

Ian Hickson's avatar
Ian Hickson committed
42 43
  void handlePrimaryPointer(PointerEvent event) {
    if (event is PointerUpEvent) {
44
      _finalPosition = event.position;
Hixie's avatar
Hixie committed
45
      _checkUp();
46 47 48
    }
  }

Ian Hickson's avatar
Ian Hickson committed
49 50 51 52 53
  void resolve(GestureDisposition disposition) {
    if (_wonArena && disposition == GestureDisposition.rejected) {
      if (onTapCancel != null)
        onTapCancel();
      _reset();
54
    }
Ian Hickson's avatar
Ian Hickson committed
55 56 57
    super.resolve(disposition);
  }

Hixie's avatar
Hixie committed
58 59 60 61
  void didExceedDeadline() {
    _checkDown();
  }

62 63 64
  void acceptGesture(int pointer) {
    super.acceptGesture(pointer);
    if (pointer == primaryPointer) {
Hixie's avatar
Hixie committed
65
      _checkDown();
66
      _wonArena = true;
Hixie's avatar
Hixie committed
67
      _checkUp();
68 69 70 71 72 73 74 75 76
    }
  }

  void rejectGesture(int pointer) {
    super.rejectGesture(pointer);
    if (pointer == primaryPointer) {
      assert(state == GestureRecognizerState.defunct);
      if (onTapCancel != null)
        onTapCancel();
77
      _reset();
78 79 80
    }
  }

Hixie's avatar
Hixie committed
81 82 83 84 85 86 87 88 89
  void _checkDown() {
    if (!_sentTapDown) {
      if (onTapDown != null)
        onTapDown(initialPosition);
      _sentTapDown = true;
    }
  }

  void _checkUp() {
90
    if (_wonArena && _finalPosition != null) {
91
      resolve(GestureDisposition.accepted);
92 93
      if (onTapUp != null)
        onTapUp(_finalPosition);
94 95
      if (onTap != null)
        onTap();
96
      _reset();
97 98
    }
  }
99 100

  void _reset() {
Hixie's avatar
Hixie committed
101
    _sentTapDown = false;
102 103 104
    _wonArena = false;
    _finalPosition = null;
  }
105
}