multitap.dart 10.7 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4 5 6 7 8
// 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.

import 'dart:async';
import 'dart:ui' show Point, Offset;

import 'arena.dart';
9
import 'binding.dart';
Hixie's avatar
Hixie committed
10 11 12 13
import 'constants.dart';
import 'events.dart';
import 'pointer_router.dart';
import 'recognizer.dart';
Hixie's avatar
Hixie committed
14 15 16 17 18 19 20

typedef void GestureDoubleTapCallback();

typedef void GestureMultiTapDownCallback(Point globalPosition, int pointer);
typedef void GestureMultiTapUpCallback(Point globalPosition, int pointer);
typedef void GestureMultiTapCallback(int pointer);
typedef void GestureMultiTapCancelCallback(int pointer);
Hixie's avatar
Hixie committed
21 22 23 24 25

/// TapTracker helps track individual tap sequences as part of a
/// larger gesture.
class _TapTracker {

Ian Hickson's avatar
Ian Hickson committed
26
  _TapTracker({ PointerDownEvent event, this.entry })
Hixie's avatar
Hixie committed
27
    : pointer = event.pointer,
Ian Hickson's avatar
Ian Hickson committed
28
      _initialPosition = event.position;
Hixie's avatar
Hixie committed
29 30 31 32 33 34 35

  final int pointer;
  final GestureArenaEntry entry;
  final Point _initialPosition;

  bool _isTrackingPointer = false;

36
  void startTrackingPointer(PointerRoute route) {
Hixie's avatar
Hixie committed
37 38
    if (!_isTrackingPointer) {
      _isTrackingPointer = true;
39
      Gesturer.instance.pointerRouter.addRoute(pointer, route);
Hixie's avatar
Hixie committed
40 41 42
    }
  }

43
  void stopTrackingPointer(PointerRoute route) {
Hixie's avatar
Hixie committed
44 45
    if (_isTrackingPointer) {
      _isTrackingPointer = false;
46
      Gesturer.instance.pointerRouter.removeRoute(pointer, route);
Hixie's avatar
Hixie committed
47 48 49
    }
  }

Ian Hickson's avatar
Ian Hickson committed
50
  bool isWithinTolerance(PointerEvent event, double tolerance) {
Hixie's avatar
Hixie committed
51 52 53 54 55 56 57
    Offset offset = event.position - _initialPosition;
    return offset.distance <= tolerance;
  }

}


58
class DoubleTapGestureRecognizer extends GestureRecognizer {
Hixie's avatar
Hixie committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  // Implementation notes:
  // The double tap recognizer can be in one of four states. There's no
  // explicit enum for the states, because they are already captured by
  // the state of existing fields.  Specifically:
  // Waiting on first tap: In this state, the _trackers list is empty, and
  // _firstTap is null.
  // First tap in progress: In this state, the _trackers list contains all
  // the states for taps that have begun but not completed. This list can
  // have more than one entry if two pointers begin to tap.
  // Waiting on second tap: In this state, one of the in-progress taps has
  // completed successfully. The _trackers list is again empty, and
  // _firstTap records the successful tap.
  // Second tap in progress: Much like the "first tap in progress" state, but
  // _firstTap is non-null.  If a tap completes successfully while in this
  // state, the callback is invoked and the state is reset.
  // There are various other scenarios that cause the state to reset:
  // - All in-progress taps are rejected (by time, distance, pointercancel, etc)
  // - The long timer between taps expires
  // - The gesture arena decides we have been rejected wholesale

Hixie's avatar
Hixie committed
79
  GestureDoubleTapCallback onDoubleTap;
Hixie's avatar
Hixie committed
80 81 82 83 84

  Timer _doubleTapTimer;
  _TapTracker _firstTap;
  final Map<int, _TapTracker> _trackers = new Map<int, _TapTracker>();

Ian Hickson's avatar
Ian Hickson committed
85
  void addPointer(PointerEvent event) {
Florian Loitsch's avatar
Florian Loitsch committed
86
    // Ignore out-of-bounds second taps.
Hixie's avatar
Hixie committed
87
    if (_firstTap != null &&
88
        !_firstTap.isWithinTolerance(event, kDoubleTapSlop))
Hixie's avatar
Hixie committed
89 90 91 92
      return;
    _stopDoubleTapTimer();
    _TapTracker tracker = new _TapTracker(
      event: event,
93
      entry: Gesturer.instance.gestureArena.add(event.pointer, this)
Hixie's avatar
Hixie committed
94 95
    );
    _trackers[event.pointer] = tracker;
96
    tracker.startTrackingPointer(handleEvent);
Hixie's avatar
Hixie committed
97 98
  }

Ian Hickson's avatar
Ian Hickson committed
99
  void handleEvent(PointerEvent event) {
Hixie's avatar
Hixie committed
100 101
    _TapTracker tracker = _trackers[event.pointer];
    assert(tracker != null);
Ian Hickson's avatar
Ian Hickson committed
102 103 104 105 106 107 108
    if (event is PointerUpEvent) {
      if (_firstTap == null)
        _registerFirstTap(tracker);
      else
        _registerSecondTap(tracker);
    } else if (event is PointerMoveEvent) {
      if (!tracker.isWithinTolerance(event, kDoubleTapTouchSlop))
Hixie's avatar
Hixie committed
109
        _reject(tracker);
Ian Hickson's avatar
Ian Hickson committed
110 111
    } else if (event is PointerCancelEvent) {
      _reject(tracker);
Hixie's avatar
Hixie committed
112 113 114
    }
  }

115
  void acceptGesture(int pointer) { }
Hixie's avatar
Hixie committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

  void rejectGesture(int pointer) {
    _TapTracker tracker = _trackers[pointer];
    // If tracker isn't in the list, check if this is the first tap tracker
    if (tracker == null &&
        _firstTap != null &&
        _firstTap.pointer == pointer)
      tracker = _firstTap;
    // If tracker is still null, we rejected ourselves already
    if (tracker != null)
      _reject(tracker);
  }

  void _reject(_TapTracker tracker) {
    _trackers.remove(tracker.pointer);
    tracker.entry.resolve(GestureDisposition.rejected);
    _freezeTracker(tracker);
    // If the first tap is in progress, and we've run out of taps to track,
    // reset won't have any work to do.  But if we're in the second tap, we need
    // to clear intermediate state.
    if (_firstTap != null &&
        (_trackers.isEmpty || tracker == _firstTap))
      _reset();
  }

  void dispose() {
    _reset();
  }

  void _reset() {
    _stopDoubleTapTimer();
    if (_firstTap != null) {
      // Note, order is important below in order for the resolve -> reject logic
Florian Loitsch's avatar
Florian Loitsch committed
149
      // to work properly.
Hixie's avatar
Hixie committed
150 151 152
      _TapTracker tracker = _firstTap;
      _firstTap = null;
      _reject(tracker);
153
      Gesturer.instance.gestureArena.release(tracker.pointer);
Hixie's avatar
Hixie committed
154 155 156 157 158 159
    }
    _clearTrackers();
  }

  void _registerFirstTap(_TapTracker tracker) {
    _startDoubleTapTimer();
160
    Gesturer.instance.gestureArena.hold(tracker.pointer);
Hixie's avatar
Hixie committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    // Note, order is important below in order for the clear -> reject logic to
    // work properly.
    _freezeTracker(tracker);
    _trackers.remove(tracker.pointer);
    _clearTrackers();
    _firstTap = tracker;
  }

  void _registerSecondTap(_TapTracker tracker) {
    _firstTap.entry.resolve(GestureDisposition.accepted);
    tracker.entry.resolve(GestureDisposition.accepted);
    _freezeTracker(tracker);
    _trackers.remove(tracker.pointer);
    if (onDoubleTap != null)
      onDoubleTap();
    _reset();
  }

  void _clearTrackers() {
    List<_TapTracker> localTrackers = new List<_TapTracker>.from(_trackers.values);
    for (_TapTracker tracker in localTrackers)
      _reject(tracker);
    assert(_trackers.isEmpty);
  }

  void _freezeTracker(_TapTracker tracker) {
187
    tracker.stopTrackingPointer(handleEvent);
Hixie's avatar
Hixie committed
188 189 190 191 192 193 194 195 196 197 198 199 200
  }

  void _startDoubleTapTimer() {
    _doubleTapTimer ??= new Timer(kDoubleTapTimeout, () => _reset());
  }

  void _stopDoubleTapTimer() {
    if (_doubleTapTimer != null) {
      _doubleTapTimer.cancel();
      _doubleTapTimer = null;
    }
  }

201
  String toStringShort() => 'double tap';
Hixie's avatar
Hixie committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}


enum _TapResolution {
  tap,
  cancel
}

/// TapGesture represents a full gesture resulting from a single tap sequence,
/// as part of a [MultiTapGestureRecognizer]. Tap gestures are passive, meaning
/// that they will not preempt any other arena member in play.
class _TapGesture extends _TapTracker {

  _TapGesture({
    MultiTapGestureRecognizer gestureRecognizer,
Ian Hickson's avatar
Ian Hickson committed
217
    PointerEvent event,
Hixie's avatar
Hixie committed
218
    Duration longTapDelay
Hixie's avatar
Hixie committed
219
  }) : gestureRecognizer = gestureRecognizer,
Hixie's avatar
Hixie committed
220
       _lastPosition = event.position,
Ian Hickson's avatar
Ian Hickson committed
221 222
       super(
    event: event,
223
    entry: Gesturer.instance.gestureArena.add(event.pointer, gestureRecognizer)
Ian Hickson's avatar
Ian Hickson committed
224
  ) {
225
    startTrackingPointer(handleEvent);
Hixie's avatar
Hixie committed
226 227 228 229 230 231
    if (longTapDelay > Duration.ZERO) {
      _timer = new Timer(longTapDelay, () {
        _timer = null;
        gestureRecognizer._handleLongTap(event.pointer, _lastPosition);
      });
    }
Hixie's avatar
Hixie committed
232 233 234 235 236
  }

  final MultiTapGestureRecognizer gestureRecognizer;

  bool _wonArena = false;
Hixie's avatar
Hixie committed
237 238 239
  Timer _timer;

  Point _lastPosition;
Hixie's avatar
Hixie committed
240 241
  Point _finalPosition;

Ian Hickson's avatar
Ian Hickson committed
242
  void handleEvent(PointerEvent event) {
Hixie's avatar
Hixie committed
243
    assert(event.pointer == pointer);
Ian Hickson's avatar
Ian Hickson committed
244
    if (event is PointerMoveEvent) {
Hixie's avatar
Hixie committed
245 246 247 248
      if (!isWithinTolerance(event, kTouchSlop))
        cancel();
      else
        _lastPosition = event.position;
Ian Hickson's avatar
Ian Hickson committed
249
    } else if (event is PointerCancelEvent) {
Hixie's avatar
Hixie committed
250
      cancel();
Ian Hickson's avatar
Ian Hickson committed
251
    } else if (event is PointerUpEvent) {
252
      stopTrackingPointer(handleEvent);
Hixie's avatar
Hixie committed
253 254 255 256 257
      _finalPosition = event.position;
      _check();
    }
  }

258
  void stopTrackingPointer(PointerRoute route) {
Hixie's avatar
Hixie committed
259 260
    _timer?.cancel();
    _timer = null;
261
    super.stopTrackingPointer(route);
Hixie's avatar
Hixie committed
262 263
  }

Hixie's avatar
Hixie committed
264 265 266 267 268 269
  void accept() {
    _wonArena = true;
    _check();
  }

  void reject() {
270
    stopTrackingPointer(handleEvent);
Hixie's avatar
Hixie committed
271 272 273 274 275 276 277 278 279
    gestureRecognizer._resolveTap(pointer, _TapResolution.cancel, null);
  }

  void cancel() {
    // If we won the arena already, then entry is resolved, so resolving
    // again is a no-op. But we still need to clean up our own state.
    if (_wonArena)
      reject();
    else
280
      entry.resolve(GestureDisposition.rejected); // eventually calls reject()
Hixie's avatar
Hixie committed
281 282 283 284 285 286 287 288 289 290 291 292 293
  }

  void _check() {
    if (_wonArena && _finalPosition != null)
      gestureRecognizer._resolveTap(pointer, _TapResolution.tap, _finalPosition);
  }

}

/// MultiTapGestureRecognizer is a tap recognizer that treats taps
/// independently. That is, each pointer sequence that could resolve to a tap
/// does so independently of others: down-1, down-2, up-1, up-2 produces two
/// taps, on up-1 and up-2.
294
class MultiTapGestureRecognizer extends GestureRecognizer {
Hixie's avatar
Hixie committed
295
  MultiTapGestureRecognizer({
296 297
    this.longTapDelay: Duration.ZERO
  });
Hixie's avatar
Hixie committed
298

Hixie's avatar
Hixie committed
299 300 301 302 303 304
  GestureMultiTapDownCallback onTapDown;
  GestureMultiTapUpCallback onTapUp;
  GestureMultiTapCallback onTap;
  GestureMultiTapCancelCallback onTapCancel;
  Duration longTapDelay;
  GestureMultiTapDownCallback onLongTapDown;
Hixie's avatar
Hixie committed
305

Hixie's avatar
Hixie committed
306
  final Map<int, _TapGesture> _gestureMap = new Map<int, _TapGesture>();
Hixie's avatar
Hixie committed
307

Ian Hickson's avatar
Ian Hickson committed
308
  void addPointer(PointerEvent event) {
Hixie's avatar
Hixie committed
309 310 311
    assert(!_gestureMap.containsKey(event.pointer));
    _gestureMap[event.pointer] = new _TapGesture(
      gestureRecognizer: this,
Hixie's avatar
Hixie committed
312 313
      event: event,
      longTapDelay: longTapDelay
Hixie's avatar
Hixie committed
314 315
    );
    if (onTapDown != null)
Hixie's avatar
Hixie committed
316
      onTapDown(event.position, event.pointer);
Hixie's avatar
Hixie committed
317 318 319 320 321
  }

  void acceptGesture(int pointer) {
    assert(_gestureMap.containsKey(pointer));
    _gestureMap[pointer]?.accept();
Hixie's avatar
Hixie committed
322
    assert(!_gestureMap.containsKey(pointer));
Hixie's avatar
Hixie committed
323 324 325 326 327
  }

  void rejectGesture(int pointer) {
    assert(_gestureMap.containsKey(pointer));
    _gestureMap[pointer]?.reject();
Hixie's avatar
Hixie committed
328
    assert(!_gestureMap.containsKey(pointer));
Hixie's avatar
Hixie committed
329 330 331 332 333 334
  }

  void _resolveTap(int pointer, _TapResolution resolution, Point globalPosition) {
    _gestureMap.remove(pointer);
    if (resolution == _TapResolution.tap) {
      if (onTapUp != null)
Hixie's avatar
Hixie committed
335
        onTapUp(globalPosition, pointer);
Hixie's avatar
Hixie committed
336
      if (onTap != null)
Hixie's avatar
Hixie committed
337
        onTap(pointer);
Hixie's avatar
Hixie committed
338 339
    } else {
      if (onTapCancel != null)
Hixie's avatar
Hixie committed
340
        onTapCancel(pointer);
Hixie's avatar
Hixie committed
341 342 343
    }
  }

Hixie's avatar
Hixie committed
344 345 346 347 348 349
  void _handleLongTap(int pointer, Point lastPosition) {
    assert(_gestureMap.containsKey(pointer));
    if (onLongTapDown != null)
      onLongTapDown(lastPosition, pointer);
  }

Hixie's avatar
Hixie committed
350 351 352 353 354 355 356 357
  void dispose() {
    List<_TapGesture> localGestures = new List<_TapGesture>.from(_gestureMap.values);
    for (_TapGesture gesture in localGestures)
      gesture.cancel();
    // Rejection of each gesture should cause it to be removed from our map
    assert(_gestureMap.isEmpty);
  }

358
  String toStringShort() => 'multitap';
Hixie's avatar
Hixie committed
359
}