tap.dart 2.52 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.

Hixie's avatar
Hixie committed
5
import 'dart:ui' show Point, Offset;
6

7
import 'arena.dart';
8
import 'constants.dart';
9
import 'events.dart';
10
import 'pointer_router.dart';
11
import 'recognizer.dart';
12

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

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

  GestureTapDownCallback onTapDown;
  GestureTapDownCallback onTapUp;
32
  GestureTapCallback onTap;
33
  GestureTapCancelCallback onTapCancel;
34

Hixie's avatar
Hixie committed
35
  bool _sentTapDown = false;
36
  bool _wonArena = false;
37
  Point _finalPosition;
38 39

  void handlePrimaryPointer(PointerInputEvent event) {
Hixie's avatar
Hixie committed
40
    if (event.type == 'pointerup') {
41
      _finalPosition = event.position;
Hixie's avatar
Hixie committed
42
      _checkUp();
43 44 45
    }
  }

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

Hixie's avatar
Hixie committed
55 56 57 58
  void didExceedDeadline() {
    _checkDown();
  }

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

  void rejectGesture(int pointer) {
    super.rejectGesture(pointer);
    if (pointer == primaryPointer) {
      assert(state == GestureRecognizerState.defunct);
      if (onTapCancel != null)
        onTapCancel();
74
      _reset();
75 76 77
    }
  }

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

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

  void _reset() {
Hixie's avatar
Hixie committed
98
    _sentTapDown = false;
99 100 101
    _wonArena = false;
    _finalPosition = null;
  }
102
}