long_press.dart 32.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

10 11 12 13 14 15
export 'dart:ui' show Offset, PointerDeviceKind;

export 'arena.dart' show GestureDisposition;
export 'events.dart' show PointerDownEvent, PointerEvent;
export 'velocity_tracker.dart' show Velocity;

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/// Callback signature for [LongPressGestureRecognizer.onLongPressDown].
///
/// Called when a pointer that might cause a long-press has contacted the
/// screen. The position at which the pointer contacted the screen is available
/// in the `details`.
///
/// See also:
///
///  * [GestureDetector.onLongPressDown], which matches this signature.
///  * [GestureLongPressStartCallback], the signature that gets called when the
///    pointer has been in contact with the screen long enough to be considered
///    a long-press.
typedef GestureLongPressDownCallback = void Function(LongPressDownDetails details);

/// Callback signature for [LongPressGestureRecognizer.onLongPressCancel].
///
/// Called when the pointer that previously triggered a
/// [GestureLongPressDownCallback] will not end up causing a long-press.
///
/// See also:
///
///  * [GestureDetector.onLongPressCancel], which matches this signature.
typedef GestureLongPressCancelCallback = void Function();

40 41 42
/// Callback signature for [LongPressGestureRecognizer.onLongPress].
///
/// Called when a pointer has remained in contact with the screen at the
43
/// same location for a long period of time.
44 45 46 47 48 49
///
/// See also:
///
///  * [GestureDetector.onLongPress], which matches this signature.
///  * [GestureLongPressStartCallback], which is the same signature but with
///    details of where the long press occurred.
50
typedef GestureLongPressCallback = void Function();
51

52 53 54 55
/// Callback signature for [LongPressGestureRecognizer.onLongPressUp].
///
/// Called when a pointer stops contacting the screen after a long press
/// gesture was detected.
56 57 58 59
///
/// See also:
///
///  * [GestureDetector.onLongPressUp], which matches this signature.
60 61
typedef GestureLongPressUpCallback = void Function();

62 63 64 65 66
/// Callback signature for [LongPressGestureRecognizer.onLongPressStart].
///
/// Called when a pointer has remained in contact with the screen at the
/// same location for a long period of time. Also reports the long press down
/// position.
67 68 69 70 71 72
///
/// See also:
///
///  * [GestureDetector.onLongPressStart], which matches this signature.
///  * [GestureLongPressCallback], which is the same signature without the
///    details.
73 74 75 76 77 78 79
typedef GestureLongPressStartCallback = void Function(LongPressStartDetails details);

/// Callback signature for [LongPressGestureRecognizer.onLongPressMoveUpdate].
///
/// Called when a pointer is moving after being held in contact at the same
/// location for a long period of time. Reports the new position and its offset
/// from the original down position.
80 81 82 83
///
/// See also:
///
///  * [GestureDetector.onLongPressMoveUpdate], which matches this signature.
84 85 86 87 88 89 90
typedef GestureLongPressMoveUpdateCallback = void Function(LongPressMoveUpdateDetails details);

/// Callback signature for [LongPressGestureRecognizer.onLongPressEnd].
///
/// Called when a pointer stops contacting the screen after a long press
/// gesture was detected. Also reports the position where the pointer stopped
/// contacting the screen.
91 92 93 94
///
/// See also:
///
///  * [GestureDetector.onLongPressEnd], which matches this signature.
95 96
typedef GestureLongPressEndCallback = void Function(LongPressEndDetails details);

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/// Details for callbacks that use [GestureLongPressDownCallback].
///
/// See also:
///
///  * [LongPressGestureRecognizer.onLongPressDown], whose callback passes
///    these details.
///  * [LongPressGestureRecognizer.onSecondaryLongPressDown], whose callback
///    passes these details.
///  * [LongPressGestureRecognizer.onTertiaryLongPressDown], whose callback
///    passes these details.
class LongPressDownDetails {
  /// Creates the details for a [GestureLongPressDownCallback].
  ///
  /// The `globalPosition` argument must not be null.
  ///
  /// If the `localPosition` argument is not specified, it will default to the
  /// global position.
  const LongPressDownDetails({
    this.globalPosition = Offset.zero,
    Offset? localPosition,
    this.kind,
  }) : assert(globalPosition != null),
       localPosition = localPosition ?? globalPosition;

  /// The global position at which the pointer contacted the screen.
  final Offset globalPosition;

  /// The kind of the device that initiated the event.
  final PointerDeviceKind? kind;

  /// The local position at which the pointer contacted the screen.
  final Offset localPosition;
}

131 132 133 134 135 136 137 138 139 140 141
/// Details for callbacks that use [GestureLongPressStartCallback].
///
/// See also:
///
///  * [LongPressGestureRecognizer.onLongPressStart], which uses [GestureLongPressStartCallback].
///  * [LongPressMoveUpdateDetails], the details for [GestureLongPressMoveUpdateCallback]
///  * [LongPressEndDetails], the details for [GestureLongPressEndCallback].
class LongPressStartDetails {
  /// Creates the details for a [GestureLongPressStartCallback].
  ///
  /// The [globalPosition] argument must not be null.
142 143
  const LongPressStartDetails({
    this.globalPosition = Offset.zero,
144
    Offset? localPosition,
145 146
  }) : assert(globalPosition != null),
       localPosition = localPosition ?? globalPosition;
147

148
  /// The global position at which the pointer initially contacted the screen.
149
  final Offset globalPosition;
150

151
  /// The local position at which the pointer initially contacted the screen.
152
  final Offset localPosition;
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
}

/// Details for callbacks that use [GestureLongPressMoveUpdateCallback].
///
/// See also:
///
///  * [LongPressGestureRecognizer.onLongPressMoveUpdate], which uses [GestureLongPressMoveUpdateCallback].
///  * [LongPressEndDetails], the details for [GestureLongPressEndCallback]
///  * [LongPressStartDetails], the details for [GestureLongPressStartCallback].
class LongPressMoveUpdateDetails {
  /// Creates the details for a [GestureLongPressMoveUpdateCallback].
  ///
  /// The [globalPosition] and [offsetFromOrigin] arguments must not be null.
  const LongPressMoveUpdateDetails({
    this.globalPosition = Offset.zero,
168
    Offset? localPosition,
169
    this.offsetFromOrigin = Offset.zero,
170
    Offset? localOffsetFromOrigin,
171
  }) : assert(globalPosition != null),
172 173 174
       assert(offsetFromOrigin != null),
       localPosition = localPosition ?? globalPosition,
       localOffsetFromOrigin = localOffsetFromOrigin ?? offsetFromOrigin;
175 176 177 178

  /// The global position of the pointer when it triggered this update.
  final Offset globalPosition;

179 180 181
  /// The local position of the pointer when it triggered this update.
  final Offset localPosition;

182 183 184 185
  /// A delta offset from the point where the long press drag initially contacted
  /// the screen to the point where the pointer is currently located (the
  /// present [globalPosition]) when this callback is triggered.
  final Offset offsetFromOrigin;
186 187 188 189 190

  /// A local delta offset from the point where the long press drag initially contacted
  /// the screen to the point where the pointer is currently located (the
  /// present [localPosition]) when this callback is triggered.
  final Offset localOffsetFromOrigin;
191 192 193 194 195 196 197
}

/// Details for callbacks that use [GestureLongPressEndCallback].
///
/// See also:
///
///  * [LongPressGestureRecognizer.onLongPressEnd], which uses [GestureLongPressEndCallback].
198
///  * [LongPressMoveUpdateDetails], the details for [GestureLongPressMoveUpdateCallback].
199 200 201 202 203
///  * [LongPressStartDetails], the details for [GestureLongPressStartCallback].
class LongPressEndDetails {
  /// Creates the details for a [GestureLongPressEndCallback].
  ///
  /// The [globalPosition] argument must not be null.
204 205
  const LongPressEndDetails({
    this.globalPosition = Offset.zero,
206
    Offset? localPosition,
207
    this.velocity = Velocity.zero,
208 209
  }) : assert(globalPosition != null),
       localPosition = localPosition ?? globalPosition;
210 211 212

  /// The global position at which the pointer lifted from the screen.
  final Offset globalPosition;
213 214 215

  /// The local position at which the pointer contacted the screen.
  final Offset localPosition;
216 217 218 219 220

  /// The pointer's velocity when it stopped contacting the screen.
  ///
  /// Defaults to zero if not specified in the constructor.
  final Velocity velocity;
221 222
}

223 224
/// Recognizes when the user has pressed down at the same location for a long
/// period of time.
225 226 227 228 229
///
/// The gesture must not deviate in position from its touch down point for 500ms
/// until it's recognized. Once the gesture is accepted, the finger can be
/// moved, triggering [onLongPressMoveUpdate] callbacks, unless the
/// [postAcceptSlopTolerance] constructor argument is specified.
230
///
231
/// [LongPressGestureRecognizer] may compete on pointer events of
232 233
/// [kPrimaryButton], [kSecondaryButton], and/or [kTertiaryButton] if at least
/// one corresponding callback is non-null. If it has no callbacks, it is a no-op.
234
class LongPressGestureRecognizer extends PrimaryPointerGestureRecognizer {
235 236
  /// Creates a long-press gesture recognizer.
  ///
237 238 239 240 241 242 243 244 245
  /// Consider assigning the [onLongPressStart] callback after creating this
  /// object.
  ///
  /// The [postAcceptSlopTolerance] argument can be used to specify a maximum
  /// allowed distance for the gesture to deviate from the starting point once
  /// the long press has triggered. If the gesture deviates past that point,
  /// subsequent callbacks ([onLongPressMoveUpdate], [onLongPressUp],
  /// [onLongPressEnd]) will stop. Defaults to null, which means the gesture
  /// can be moved without limit once the long press is accepted.
246 247 248
  ///
  /// The [duration] argument can be used to overwrite the default duration
  /// after which the long press will be recognized.
249 250
  ///
  /// {@macro flutter.gestures.GestureRecognizer.supportedDevices}
251
  LongPressGestureRecognizer({
252
    Duration? duration,
253 254 255
    // TODO(goderbauer): remove ignore when https://github.com/dart-lang/linter/issues/3349 is fixed.
    // ignore: avoid_init_to_null
    super.postAcceptSlopTolerance = null,
256 257 258 259
    @Deprecated(
      'Migrate to supportedDevices. '
      'This feature was deprecated after v2.3.0-1.0.pre.',
    )
260 261 262
    super.kind,
    super.supportedDevices,
    super.debugOwner,
263
  }) : super(
264 265
         deadline: duration ?? kLongPressTimeout,
       );
266 267

  bool _longPressAccepted = false;
268
  OffsetPair? _longPressOrigin;
269 270
  // The buttons sent by `PointerDownEvent`. If a `PointerMoveEvent` comes with a
  // different set of buttons, the gesture is canceled.
271
  int? _initialButtons;
272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  /// Called when a pointer has contacted the screen at a particular location
  /// with a primary button, which might be the start of a long-press.
  ///
  /// This triggers after the pointer down event.
  ///
  /// If this recognizer doesn't win the arena, [onLongPressCancel] is called
  /// next. Otherwise, [onLongPressStart] is called next.
  ///
  /// See also:
  ///
  ///  * [kPrimaryButton], the button this callback responds to.
  ///  * [onSecondaryLongPressDown], a similar callback but for a secondary button.
  ///  * [onTertiaryLongPressDown], a similar callback but for a tertiary button.
  ///  * [LongPressDownDetails], which is passed as an argument to this callback.
  ///  * [GestureDetector.onLongPressDown], which exposes this callback in a widget.
  GestureLongPressDownCallback? onLongPressDown;

  /// Called when a pointer that previously triggered [onLongPressDown] will
  /// not end up causing a long-press.
  ///
  /// This triggers once the gesture loses the arena if [onLongPressDown] has
  /// previously been triggered.
  ///
  /// If this recognizer wins the arena, [onLongPressStart] and [onLongPress]
  /// are called instead.
  ///
  /// If the gesture is deactivated due to [postAcceptSlopTolerance] having
  /// been exceeded, this callback will not be called, since the gesture will
  /// have already won the arena at that point.
  ///
  /// See also:
  ///
  ///  * [kPrimaryButton], the button this callback responds to.
  GestureLongPressCancelCallback? onLongPressCancel;

308
  /// Called when a long press gesture by a primary button has been recognized.
309
  ///
310 311 312 313 314
  /// This is equivalent to (and is called immediately after) [onLongPressStart].
  /// The only difference between the two is that this callback does not
  /// contain details of the position at which the pointer initially contacted
  /// the screen.
  ///
315 316
  /// See also:
  ///
317
  ///  * [kPrimaryButton], the button this callback responds to.
318
  GestureLongPressCallback? onLongPress;
319

320
  /// Called when a long press gesture by a primary button has been recognized.
321
  ///
322 323 324 325 326
  /// This is equivalent to (and is called immediately before) [onLongPress].
  /// The only difference between the two is that this callback contains
  /// details of the position at which the pointer initially contacted the
  /// screen, whereas [onLongPress] does not.
  ///
327 328
  /// See also:
  ///
329 330
  ///  * [kPrimaryButton], the button this callback responds to.
  ///  * [LongPressStartDetails], which is passed as an argument to this callback.
331
  GestureLongPressStartCallback? onLongPressStart;
332

333 334 335 336 337 338 339
  /// Called when moving after the long press by a primary button is recognized.
  ///
  /// See also:
  ///
  ///  * [kPrimaryButton], the button this callback responds to.
  ///  * [LongPressMoveUpdateDetails], which is passed as an argument to this
  ///    callback.
340
  GestureLongPressMoveUpdateCallback? onLongPressMoveUpdate;
341

342 343
  /// Called when the pointer stops contacting the screen after a long-press
  /// by a primary button.
344
  ///
345 346 347 348 349
  /// This is equivalent to (and is called immediately after) [onLongPressEnd].
  /// The only difference between the two is that this callback does not
  /// contain details of the state of the pointer when it stopped contacting
  /// the screen.
  ///
350 351
  /// See also:
  ///
352
  ///  * [kPrimaryButton], the button this callback responds to.
353
  GestureLongPressUpCallback? onLongPressUp;
354

355 356
  /// Called when the pointer stops contacting the screen after a long-press
  /// by a primary button.
357
  ///
358 359 360 361 362
  /// This is equivalent to (and is called immediately before) [onLongPressUp].
  /// The only difference between the two is that this callback contains
  /// details of the state of the pointer when it stopped contacting the
  /// screen, whereas [onLongPressUp] does not.
  ///
363 364
  /// See also:
  ///
365 366 367
  ///  * [kPrimaryButton], the button this callback responds to.
  ///  * [LongPressEndDetails], which is passed as an argument to this
  ///    callback.
368
  GestureLongPressEndCallback? onLongPressEnd;
369

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
  /// Called when a pointer has contacted the screen at a particular location
  /// with a secondary button, which might be the start of a long-press.
  ///
  /// This triggers after the pointer down event.
  ///
  /// If this recognizer doesn't win the arena, [onSecondaryLongPressCancel] is
  /// called next. Otherwise, [onSecondaryLongPressStart] is called next.
  ///
  /// See also:
  ///
  ///  * [kSecondaryButton], the button this callback responds to.
  ///  * [onLongPressDown], a similar callback but for a primary button.
  ///  * [onTertiaryLongPressDown], a similar callback but for a tertiary button.
  ///  * [LongPressDownDetails], which is passed as an argument to this callback.
  ///  * [GestureDetector.onSecondaryLongPressDown], which exposes this callback
  ///    in a widget.
  GestureLongPressDownCallback? onSecondaryLongPressDown;

  /// Called when a pointer that previously triggered [onSecondaryLongPressDown]
  /// will not end up causing a long-press.
  ///
  /// This triggers once the gesture loses the arena if
  /// [onSecondaryLongPressDown] has previously been triggered.
  ///
  /// If this recognizer wins the arena, [onSecondaryLongPressStart] and
  /// [onSecondaryLongPress] are called instead.
  ///
  /// If the gesture is deactivated due to [postAcceptSlopTolerance] having
  /// been exceeded, this callback will not be called, since the gesture will
  /// have already won the arena at that point.
  ///
  /// See also:
  ///
  ///  * [kSecondaryButton], the button this callback responds to.
  GestureLongPressCancelCallback? onSecondaryLongPressCancel;

406 407 408
  /// Called when a long press gesture by a secondary button has been
  /// recognized.
  ///
409 410 411 412 413
  /// This is equivalent to (and is called immediately after)
  /// [onSecondaryLongPressStart]. The only difference between the two is that
  /// this callback does not contain details of the position at which the
  /// pointer initially contacted the screen.
  ///
414 415 416
  /// See also:
  ///
  ///  * [kSecondaryButton], the button this callback responds to.
417
  GestureLongPressCallback? onSecondaryLongPress;
418 419 420

  /// Called when a long press gesture by a secondary button has been recognized.
  ///
421 422 423 424 425
  /// This is equivalent to (and is called immediately before)
  /// [onSecondaryLongPress]. The only difference between the two is that this
  /// callback contains details of the position at which the pointer initially
  /// contacted the screen, whereas [onSecondaryLongPress] does not.
  ///
426 427 428 429 430
  /// See also:
  ///
  ///  * [kSecondaryButton], the button this callback responds to.
  ///  * [LongPressStartDetails], which is passed as an argument to this
  ///    callback.
431
  GestureLongPressStartCallback? onSecondaryLongPressStart;
432 433 434 435 436 437 438 439 440

  /// Called when moving after the long press by a secondary button is
  /// recognized.
  ///
  /// See also:
  ///
  ///  * [kSecondaryButton], the button this callback responds to.
  ///  * [LongPressMoveUpdateDetails], which is passed as an argument to this
  ///    callback.
441
  GestureLongPressMoveUpdateCallback? onSecondaryLongPressMoveUpdate;
442 443 444 445

  /// Called when the pointer stops contacting the screen after a long-press by
  /// a secondary button.
  ///
446 447 448 449 450
  /// This is equivalent to (and is called immediately after)
  /// [onSecondaryLongPressEnd]. The only difference between the two is that
  /// this callback does not contain details of the state of the pointer when
  /// it stopped contacting the screen.
  ///
451 452 453
  /// See also:
  ///
  ///  * [kSecondaryButton], the button this callback responds to.
454
  GestureLongPressUpCallback? onSecondaryLongPressUp;
455 456 457 458

  /// Called when the pointer stops contacting the screen after a long-press by
  /// a secondary button.
  ///
459 460 461 462 463
  /// This is equivalent to (and is called immediately before)
  /// [onSecondaryLongPressUp]. The only difference between the two is that
  /// this callback contains details of the state of the pointer when it
  /// stopped contacting the screen, whereas [onSecondaryLongPressUp] does not.
  ///
464 465 466 467
  /// See also:
  ///
  ///  * [kSecondaryButton], the button this callback responds to.
  ///  * [LongPressEndDetails], which is passed as an argument to this callback.
468
  GestureLongPressEndCallback? onSecondaryLongPressEnd;
469

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
  /// Called when a pointer has contacted the screen at a particular location
  /// with a tertiary button, which might be the start of a long-press.
  ///
  /// This triggers after the pointer down event.
  ///
  /// If this recognizer doesn't win the arena, [onTertiaryLongPressCancel] is
  /// called next. Otherwise, [onTertiaryLongPressStart] is called next.
  ///
  /// See also:
  ///
  ///  * [kTertiaryButton], the button this callback responds to.
  ///  * [onLongPressDown], a similar callback but for a primary button.
  ///  * [onSecondaryLongPressDown], a similar callback but for a secondary button.
  ///  * [LongPressDownDetails], which is passed as an argument to this callback.
  ///  * [GestureDetector.onTertiaryLongPressDown], which exposes this callback
  ///    in a widget.
  GestureLongPressDownCallback? onTertiaryLongPressDown;

  /// Called when a pointer that previously triggered [onTertiaryLongPressDown]
  /// will not end up causing a long-press.
  ///
  /// This triggers once the gesture loses the arena if
  /// [onTertiaryLongPressDown] has previously been triggered.
  ///
  /// If this recognizer wins the arena, [onTertiaryLongPressStart] and
  /// [onTertiaryLongPress] are called instead.
  ///
  /// If the gesture is deactivated due to [postAcceptSlopTolerance] having
  /// been exceeded, this callback will not be called, since the gesture will
  /// have already won the arena at that point.
  ///
  /// See also:
  ///
  ///  * [kTertiaryButton], the button this callback responds to.
  GestureLongPressCancelCallback? onTertiaryLongPressCancel;

506 507 508
  /// Called when a long press gesture by a tertiary button has been
  /// recognized.
  ///
509 510 511 512 513
  /// This is equivalent to (and is called immediately after)
  /// [onTertiaryLongPressStart]. The only difference between the two is that
  /// this callback does not contain details of the position at which the
  /// pointer initially contacted the screen.
  ///
514 515 516 517 518 519 520
  /// See also:
  ///
  ///  * [kTertiaryButton], the button this callback responds to.
  GestureLongPressCallback? onTertiaryLongPress;

  /// Called when a long press gesture by a tertiary button has been recognized.
  ///
521 522 523 524 525
  /// This is equivalent to (and is called immediately before)
  /// [onTertiaryLongPress]. The only difference between the two is that this
  /// callback contains details of the position at which the pointer initially
  /// contacted the screen, whereas [onTertiaryLongPress] does not.
  ///
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
  /// See also:
  ///
  ///  * [kTertiaryButton], the button this callback responds to.
  ///  * [LongPressStartDetails], which is passed as an argument to this
  ///    callback.
  GestureLongPressStartCallback? onTertiaryLongPressStart;

  /// Called when moving after the long press by a tertiary button is
  /// recognized.
  ///
  /// See also:
  ///
  ///  * [kTertiaryButton], the button this callback responds to.
  ///  * [LongPressMoveUpdateDetails], which is passed as an argument to this
  ///    callback.
  GestureLongPressMoveUpdateCallback? onTertiaryLongPressMoveUpdate;

  /// Called when the pointer stops contacting the screen after a long-press by
  /// a tertiary button.
  ///
546 547 548 549 550
  /// This is equivalent to (and is called immediately after)
  /// [onTertiaryLongPressEnd]. The only difference between the two is that
  /// this callback does not contain details of the state of the pointer when
  /// it stopped contacting the screen.
  ///
551 552 553 554 555 556 557 558
  /// See also:
  ///
  ///  * [kTertiaryButton], the button this callback responds to.
  GestureLongPressUpCallback? onTertiaryLongPressUp;

  /// Called when the pointer stops contacting the screen after a long-press by
  /// a tertiary button.
  ///
559 560 561 562 563
  /// This is equivalent to (and is called immediately before)
  /// [onTertiaryLongPressUp]. The only difference between the two is that
  /// this callback contains details of the state of the pointer when it
  /// stopped contacting the screen, whereas [onTertiaryLongPressUp] does not.
  ///
564 565 566 567 568 569
  /// See also:
  ///
  ///  * [kTertiaryButton], the button this callback responds to.
  ///  * [LongPressEndDetails], which is passed as an argument to this callback.
  GestureLongPressEndCallback? onTertiaryLongPressEnd;

570
  VelocityTracker? _velocityTracker;
571

572 573 574 575
  @override
  bool isPointerAllowed(PointerDownEvent event) {
    switch (event.buttons) {
      case kPrimaryButton:
576 577 578
        if (onLongPressDown == null &&
            onLongPressCancel == null &&
            onLongPressStart == null &&
579 580 581
            onLongPress == null &&
            onLongPressMoveUpdate == null &&
            onLongPressEnd == null &&
582
            onLongPressUp == null) {
583
          return false;
584
        }
585
        break;
586
      case kSecondaryButton:
587 588 589
        if (onSecondaryLongPressDown == null &&
            onSecondaryLongPressCancel == null &&
            onSecondaryLongPressStart == null &&
590 591 592
            onSecondaryLongPress == null &&
            onSecondaryLongPressMoveUpdate == null &&
            onSecondaryLongPressEnd == null &&
593
            onSecondaryLongPressUp == null) {
594
          return false;
595
        }
596
        break;
597
      case kTertiaryButton:
598 599 600
        if (onTertiaryLongPressDown == null &&
            onTertiaryLongPressCancel == null &&
            onTertiaryLongPressStart == null &&
601 602 603
            onTertiaryLongPress == null &&
            onTertiaryLongPressMoveUpdate == null &&
            onTertiaryLongPressEnd == null &&
604
            onTertiaryLongPressUp == null) {
605
          return false;
606
        }
607
        break;
608 609 610 611 612 613
      default:
        return false;
    }
    return super.isPointerAllowed(event);
  }

614
  @override
615
  void didExceedDeadline() {
616
    // Exceeding the deadline puts the gesture in the accepted state.
617
    resolve(GestureDisposition.accepted);
618
    _longPressAccepted = true;
619
    super.acceptGesture(primaryPointer!);
620
    _checkLongPressStart();
621 622
  }

623
  @override
Ian Hickson's avatar
Ian Hickson committed
624
  void handlePrimaryPointer(PointerEvent event) {
625 626
    if (!event.synthesized) {
      if (event is PointerDownEvent) {
627
        _velocityTracker = VelocityTracker.withKind(event.kind);
628
        _velocityTracker!.addPosition(event.timeStamp, event.localPosition);
629 630 631
      }
      if (event is PointerMoveEvent) {
        assert(_velocityTracker != null);
632
        _velocityTracker!.addPosition(event.timeStamp, event.localPosition);
633 634 635
      }
    }

636
    if (event is PointerUpEvent) {
637
      if (_longPressAccepted == true) {
638
        _checkLongPressEnd(event);
639
      } else {
640
        // Pointer is lifted before timeout.
641 642
        resolve(GestureDisposition.rejected);
      }
643 644
      _reset();
    } else if (event is PointerCancelEvent) {
645
      _checkLongPressCancel();
646 647
      _reset();
    } else if (event is PointerDownEvent) {
648
      // The first touch.
649
      _longPressOrigin = OffsetPair.fromEventPosition(event);
650
      _initialButtons = event.buttons;
651
      _checkLongPressDown(event);
652 653 654
    } else if (event is PointerMoveEvent) {
      if (event.buttons != _initialButtons) {
        resolve(GestureDisposition.rejected);
655
        stopTrackingPointer(primaryPointer!);
656 657 658 659 660 661
      } else if (_longPressAccepted) {
        _checkLongPressMoveUpdate(event);
      }
    }
  }

662 663 664 665 666 667 668 669 670
  void _checkLongPressDown(PointerDownEvent event) {
    assert(_longPressOrigin != null);
    final LongPressDownDetails details = LongPressDownDetails(
      globalPosition: _longPressOrigin!.global,
      localPosition: _longPressOrigin!.local,
      kind: getKindForPointer(event.pointer),
    );
    switch (_initialButtons) {
      case kPrimaryButton:
671
        if (onLongPressDown != null) {
672
          invokeCallback<void>('onLongPressDown', () => onLongPressDown!(details));
673
        }
674 675
        break;
      case kSecondaryButton:
676
        if (onSecondaryLongPressDown != null) {
677
          invokeCallback<void>('onSecondaryLongPressDown', () => onSecondaryLongPressDown!(details));
678
        }
679 680
        break;
      case kTertiaryButton:
681
        if (onTertiaryLongPressDown != null) {
682
          invokeCallback<void>('onTertiaryLongPressDown', () => onTertiaryLongPressDown!(details));
683
        }
684 685 686 687 688 689 690 691 692 693
        break;
      default:
        assert(false, 'Unhandled button $_initialButtons');
    }
  }

  void _checkLongPressCancel() {
    if (state == GestureRecognizerState.possible) {
      switch (_initialButtons) {
        case kPrimaryButton:
694
          if (onLongPressCancel != null) {
695
            invokeCallback<void>('onLongPressCancel', onLongPressCancel!);
696
          }
697 698
          break;
        case kSecondaryButton:
699
          if (onSecondaryLongPressCancel != null) {
700
            invokeCallback<void>('onSecondaryLongPressCancel', onSecondaryLongPressCancel!);
701
          }
702 703
          break;
        case kTertiaryButton:
704
          if (onTertiaryLongPressCancel != null) {
705
            invokeCallback<void>('onTertiaryLongPressCancel', onTertiaryLongPressCancel!);
706
          }
707 708 709 710 711 712 713
          break;
        default:
          assert(false, 'Unhandled button $_initialButtons');
      }
    }
  }

714
  void _checkLongPressStart() {
715 716 717 718
    switch (_initialButtons) {
      case kPrimaryButton:
        if (onLongPressStart != null) {
          final LongPressStartDetails details = LongPressStartDetails(
719 720
            globalPosition: _longPressOrigin!.global,
            localPosition: _longPressOrigin!.local,
721
          );
722
          invokeCallback<void>('onLongPressStart', () => onLongPressStart!(details));
723 724
        }
        if (onLongPress != null) {
725
          invokeCallback<void>('onLongPress', onLongPress!);
726 727 728 729 730
        }
        break;
      case kSecondaryButton:
        if (onSecondaryLongPressStart != null) {
          final LongPressStartDetails details = LongPressStartDetails(
731 732
            globalPosition: _longPressOrigin!.global,
            localPosition: _longPressOrigin!.local,
733
          );
734
          invokeCallback<void>('onSecondaryLongPressStart', () => onSecondaryLongPressStart!(details));
735 736
        }
        if (onSecondaryLongPress != null) {
737
          invokeCallback<void>('onSecondaryLongPress', onSecondaryLongPress!);
738 739
        }
        break;
740 741 742 743 744 745
      case kTertiaryButton:
        if (onTertiaryLongPressStart != null) {
          final LongPressStartDetails details = LongPressStartDetails(
            globalPosition: _longPressOrigin!.global,
            localPosition: _longPressOrigin!.local,
          );
746
          invokeCallback<void>('onTertiaryLongPressStart', () => onTertiaryLongPressStart!(details));
747 748 749 750 751
        }
        if (onTertiaryLongPress != null) {
          invokeCallback<void>('onTertiaryLongPress', onTertiaryLongPress!);
        }
        break;
752 753
      default:
        assert(false, 'Unhandled button $_initialButtons');
754
    }
755 756 757 758 759
  }

  void _checkLongPressMoveUpdate(PointerEvent event) {
    final LongPressMoveUpdateDetails details = LongPressMoveUpdateDetails(
      globalPosition: event.position,
760
      localPosition: event.localPosition,
761 762
      offsetFromOrigin: event.position - _longPressOrigin!.global,
      localOffsetFromOrigin: event.localPosition - _longPressOrigin!.local,
763
    );
764 765 766
    switch (_initialButtons) {
      case kPrimaryButton:
        if (onLongPressMoveUpdate != null) {
767
          invokeCallback<void>('onLongPressMoveUpdate', () => onLongPressMoveUpdate!(details));
768 769 770 771
        }
        break;
      case kSecondaryButton:
        if (onSecondaryLongPressMoveUpdate != null) {
772
          invokeCallback<void>('onSecondaryLongPressMoveUpdate', () => onSecondaryLongPressMoveUpdate!(details));
773 774
        }
        break;
775 776
      case kTertiaryButton:
        if (onTertiaryLongPressMoveUpdate != null) {
777
          invokeCallback<void>('onTertiaryLongPressMoveUpdate', () => onTertiaryLongPressMoveUpdate!(details));
778 779
        }
        break;
780 781 782
      default:
        assert(false, 'Unhandled button $_initialButtons');
    }
783 784 785
  }

  void _checkLongPressEnd(PointerEvent event) {
786
    final VelocityEstimate? estimate = _velocityTracker!.getVelocityEstimate();
787 788 789
    final Velocity velocity = estimate == null
        ? Velocity.zero
        : Velocity(pixelsPerSecond: estimate.pixelsPerSecond);
790 791
    final LongPressEndDetails details = LongPressEndDetails(
      globalPosition: event.position,
792
      localPosition: event.localPosition,
793
      velocity: velocity,
794
    );
795 796

    _velocityTracker = null;
797 798 799
    switch (_initialButtons) {
      case kPrimaryButton:
        if (onLongPressEnd != null) {
800
          invokeCallback<void>('onLongPressEnd', () => onLongPressEnd!(details));
801 802
        }
        if (onLongPressUp != null) {
803
          invokeCallback<void>('onLongPressUp', onLongPressUp!);
804 805 806 807
        }
        break;
      case kSecondaryButton:
        if (onSecondaryLongPressEnd != null) {
808
          invokeCallback<void>('onSecondaryLongPressEnd', () => onSecondaryLongPressEnd!(details));
809 810
        }
        if (onSecondaryLongPressUp != null) {
811
          invokeCallback<void>('onSecondaryLongPressUp', onSecondaryLongPressUp!);
812 813
        }
        break;
814 815 816 817 818 819 820 821
      case kTertiaryButton:
        if (onTertiaryLongPressEnd != null) {
          invokeCallback<void>('onTertiaryLongPressEnd', () => onTertiaryLongPressEnd!(details));
        }
        if (onTertiaryLongPressUp != null) {
          invokeCallback<void>('onTertiaryLongPressUp', onTertiaryLongPressUp!);
        }
        break;
822 823 824
      default:
        assert(false, 'Unhandled button $_initialButtons');
    }
825 826 827 828 829 830
  }

  void _reset() {
    _longPressAccepted = false;
    _longPressOrigin = null;
    _initialButtons = null;
831
    _velocityTracker = null;
832 833 834 835
  }

  @override
  void resolve(GestureDisposition disposition) {
836 837 838 839 840 841 842 843
    if (disposition == GestureDisposition.rejected) {
      if (_longPressAccepted) {
        // This can happen if the gesture has been canceled. For example when
        // the buttons have changed.
        _reset();
      } else {
        _checkLongPressCancel();
      }
844
    }
845
    super.resolve(disposition);
846
  }
847

848 849 850 851 852 853
  @override
  void acceptGesture(int pointer) {
    // Winning the arena isn't important here since it may happen from a sweep.
    // Explicitly exceeding the deadline puts the gesture in accepted state.
  }

854
  @override
855
  String get debugDescription => 'long press';
856
}