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

5

6
import 'dart:ui' show Offset, PointerDeviceKind;
7

8
import 'package:flutter/foundation.dart';
9
import 'package:vector_math/vector_math_64.dart';
10

11
import 'constants.dart';
12
import 'gesture_settings.dart';
13

14
export 'dart:ui' show Offset, PointerDeviceKind;
15

16 17 18 19 20
export 'package:flutter/foundation.dart' show DiagnosticPropertiesBuilder;
export 'package:vector_math/vector_math_64.dart' show Matrix4;

export 'gesture_settings.dart' show DeviceGestureSettings;

21 22
/// The bit of [PointerEvent.buttons] that corresponds to a cross-device
/// behavior of "primary operation".
23
///
24
/// More specifically, it includes:
25
///
26 27 28
///  * [kTouchContact]: The pointer contacts the touch screen.
///  * [kStylusContact]: The stylus contacts the screen.
///  * [kPrimaryMouseButton]: The primary mouse button.
29 30 31
///
/// See also:
///
32 33
///  * [kSecondaryButton], which describes a cross-device behavior of
///    "secondary operation".
34 35
///  * [kTertiaryButton], which describes a cross-device behavior of
///    "tertiary operation".
36 37
const int kPrimaryButton = 0x01;

38 39 40 41 42 43
/// The bit of [PointerEvent.buttons] that corresponds to a cross-device
/// behavior of "secondary operation".
///
/// It is equivalent to:
///
///  * [kPrimaryStylusButton]: The stylus contacts the screen.
44
///  * [kSecondaryMouseButton]: The secondary mouse button.
45 46 47 48 49
///
/// See also:
///
///  * [kPrimaryButton], which describes a cross-device behavior of
///    "primary operation".
50 51
///  * [kTertiaryButton], which describes a cross-device behavior of
///    "tertiary operation".
52 53
const int kSecondaryButton = 0x02;

54 55 56 57
/// The bit of [PointerEvent.buttons] that corresponds to the primary mouse button.
///
/// The primary mouse button is typically the left button on the top of the
/// mouse but can be reconfigured to be a different physical button.
58 59 60
///
/// See also:
///
61 62
///  * [kPrimaryButton], which has the same value but describes its cross-device
///    concept.
63
const int kPrimaryMouseButton = kPrimaryButton;
64 65 66 67 68

/// The bit of [PointerEvent.buttons] that corresponds to the secondary mouse button.
///
/// The secondary mouse button is typically the right button on the top of the
/// mouse but can be reconfigured to be a different physical button.
69 70 71 72 73 74
///
/// See also:
///
///  * [kSecondaryButton], which has the same value but describes its cross-device
///    concept.
const int kSecondaryMouseButton = kSecondaryButton;
75

76 77
/// The bit of [PointerEvent.buttons] that corresponds to when a stylus
/// contacting the screen.
78 79 80
///
/// See also:
///
81 82
///  * [kPrimaryButton], which has the same value but describes its cross-device
///    concept.
83 84
const int kStylusContact = kPrimaryButton;

85 86 87 88
/// The bit of [PointerEvent.buttons] that corresponds to the primary stylus button.
///
/// The primary stylus button is typically the top of the stylus and near the
/// tip but can be reconfigured to be a different physical button.
89 90 91 92 93 94
///
/// See also:
///
///  * [kSecondaryButton], which has the same value but describes its cross-device
///    concept.
const int kPrimaryStylusButton = kSecondaryButton;
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/// The bit of [PointerEvent.buttons] that corresponds to a cross-device
/// behavior of "tertiary operation".
///
/// It is equivalent to:
///
///  * [kMiddleMouseButton]: The tertiary mouseButton.
///  * [kSecondaryStylusButton]: The secondary button on a stylus. This is considered
///    a tertiary button as the primary button of a stylus already corresponds to a
///    "secondary operation" (where stylus contact is the primary operation).
///
/// See also:
///
///  * [kPrimaryButton], which describes a cross-device behavior of
///    "primary operation".
///  * [kSecondaryButton], which describes a cross-device behavior of
///    "secondary operation".
const int kTertiaryButton = 0x04;

114 115 116 117 118
/// The bit of [PointerEvent.buttons] that corresponds to the middle mouse button.
///
/// The middle mouse button is typically between the left and right buttons on
/// the top of the mouse but can be reconfigured to be a different physical
/// button.
119 120 121 122 123 124
///
/// See also:
///
///  * [kTertiaryButton], which has the same value but describes its cross-device
///    concept.
const int kMiddleMouseButton = kTertiaryButton;
125 126 127

/// The bit of [PointerEvent.buttons] that corresponds to the secondary stylus button.
///
128
/// The secondary stylus button is typically on the end of the stylus farthest
129
/// from the tip but can be reconfigured to be a different physical button.
130 131 132 133 134 135
///
/// See also:
///
///  * [kTertiaryButton], which has the same value but describes its cross-device
///    concept.
const int kSecondaryStylusButton = kTertiaryButton;
136 137 138 139 140

/// The bit of [PointerEvent.buttons] that corresponds to the back mouse button.
///
/// The back mouse button is typically on the left side of the mouse but can be
/// reconfigured to be a different physical button.
141
const int kBackMouseButton = 0x08;
142 143 144 145 146 147 148

/// The bit of [PointerEvent.buttons] that corresponds to the forward mouse button.
///
/// The forward mouse button is typically on the right side of the mouse but can
/// be reconfigured to be a different physical button.
const int kForwardMouseButton = 0x10;

149 150
/// The bit of [PointerEvent.buttons] that corresponds to the pointer contacting
/// a touch screen.
151 152 153
///
/// See also:
///
154 155
///  * [kPrimaryButton], which has the same value but describes its cross-device
///    concept.
156 157
const int kTouchContact = kPrimaryButton;

158 159
/// The bit of [PointerEvent.buttons] that corresponds to the nth mouse button.
///
160 161
/// The `number` argument can be at most 62 in Flutter for mobile and desktop,
/// and at most 32 on Flutter for web.
162 163 164 165 166 167 168 169
///
/// See [kPrimaryMouseButton], [kSecondaryMouseButton], [kMiddleMouseButton],
/// [kBackMouseButton], and [kForwardMouseButton] for semantic names for some
/// mouse buttons.
int nthMouseButton(int number) => (kPrimaryMouseButton << (number - 1)) & kMaxUnsignedSMI;

/// The bit of [PointerEvent.buttons] that corresponds to the nth stylus button.
///
170 171
/// The `number` argument can be at most 62 in Flutter for mobile and desktop,
/// and at most 32 on Flutter for web.
172 173 174 175
///
/// See [kPrimaryStylusButton] and [kSecondaryStylusButton] for semantic names
/// for some stylus buttons.
int nthStylusButton(int number) => (kPrimaryStylusButton << (number - 1)) & kMaxUnsignedSMI;
176

177 178
/// Returns the button of `buttons` with the smallest integer.
///
179
/// The `buttons` parameter is a bit field where each set bit represents a button.
180 181 182 183 184 185 186
/// This function returns the set bit closest to the least significant bit.
///
/// It returns zero when `buttons` is zero.
///
/// Example:
///
/// ```dart
187 188 189 190
/// assert(smallestButton(0x01) == 0x01);
/// assert(smallestButton(0x11) == 0x01);
/// assert(smallestButton(0x10) == 0x10);
/// assert(smallestButton(0) == 0);
191 192 193 194
/// ```
///
/// See also:
///
195
///  * [isSingleButton], which checks if a `buttons` contains exactly one button.
196 197 198 199
int smallestButton(int buttons) => buttons & (-buttons);

/// Returns whether `buttons` contains one and only one button.
///
200
/// The `buttons` parameter is a bit field where each set bit represents a button.
201 202 203 204 205 206 207 208 209 210 211 212 213 214
/// This function returns whether there is only one set bit in the given integer.
///
/// It returns false when `buttons` is zero.
///
/// Example:
///
/// ```dart
///   assert(isSingleButton(0x1) == true);
///   assert(isSingleButton(0x11) == false);
///   assert(isSingleButton(0) == false);
/// ```
///
/// See also:
///
215 216
///  * [smallestButton], which returns the button in a `buttons` bit field with
///    the smallest integer button.
217 218
bool isSingleButton(int buttons) => buttons != 0 && (smallestButton(buttons) == buttons);

Ian Hickson's avatar
Ian Hickson committed
219
/// Base class for touch, stylus, or mouse events.
220 221 222 223 224
///
/// Pointer events operate in the coordinate space of the screen, scaled to
/// logical pixels. Logical pixels approximate a grid with about 38 pixels per
/// centimeter, or 96 pixels per inch.
///
225
/// This allows gestures to be recognized independent of the precise hardware
226 227 228 229 230 231 232 233 234 235 236 237 238
/// characteristics of the device. In particular, features such as touch slop
/// (see [kTouchSlop]) can be defined in terms of roughly physical lengths so
/// that the user can shift their finger by the same distance on a high-density
/// display as on a low-resolution device.
///
/// For similar reasons, pointer events are not affected by any transforms in
/// the rendering layer. This means that deltas may need to be scaled before
/// being applied to movement within the rendering. For example, if a scrolling
/// list is shown scaled by 2x, the pointer deltas will have to be scaled by the
/// inverse amount if the list is to appear to scroll with the user's finger.
///
/// See also:
///
239 240
///  * [dart:ui.FlutterView.devicePixelRatio], which defines the device's
///    current resolution.
241 242
///  * [Listener], a widget that calls callbacks in response to common pointer
///    events.
243
@immutable
244
abstract class PointerEvent with Diagnosticable {
245 246
  /// Abstract const constructor. This constructor enables subclasses to provide
  /// const constructors so that they can be used in const expressions.
Ian Hickson's avatar
Ian Hickson committed
247
  const PointerEvent({
248
    this.embedderId = 0,
249 250 251 252 253 254 255 256 257 258 259 260 261 262
    this.timeStamp = Duration.zero,
    this.pointer = 0,
    this.kind = PointerDeviceKind.touch,
    this.device = 0,
    this.position = Offset.zero,
    this.delta = Offset.zero,
    this.buttons = 0,
    this.down = false,
    this.obscured = false,
    this.pressure = 1.0,
    this.pressureMin = 1.0,
    this.pressureMax = 1.0,
    this.distance = 0.0,
    this.distanceMax = 0.0,
263
    this.size = 0.0,
264 265 266 267 268 269
    this.radiusMajor = 0.0,
    this.radiusMinor = 0.0,
    this.radiusMin = 0.0,
    this.radiusMax = 0.0,
    this.orientation = 0.0,
    this.tilt = 0.0,
270
    this.platformData = 0,
271
    this.synthesized = false,
272 273
    this.transform,
    this.original,
274
  });
275

276 277 278 279 280 281 282 283
  /// Unique identifier that ties the [PointerEvent] to the embedder event that created it.
  ///
  /// No two pointer events can have the same [embedderId] on platforms that set it.
  /// This is different from [pointer] identifier - used for hit-testing,
  /// whereas [embedderId] is used to identify the platform event.
  ///
  /// On Android this is ID of the underlying [MotionEvent](https://developer.android.com/reference/android/view/MotionEvent).
  final int embedderId;
284

Ian Hickson's avatar
Ian Hickson committed
285 286 287
  /// Time of event dispatch, relative to an arbitrary timeline.
  final Duration timeStamp;

288 289
  /// Unique identifier for the pointer, not reused. Changes for each new
  /// pointer down event.
Kris Giesing's avatar
Kris Giesing committed
290
  final int pointer;
Ian Hickson's avatar
Ian Hickson committed
291 292 293 294

  /// The kind of input device for which the event was generated.
  final PointerDeviceKind kind;

295 296 297
  /// Unique identifier for the pointing device, reused across interactions.
  final int device;

Ian Hickson's avatar
Ian Hickson committed
298 299
  /// Coordinate of the position of the pointer, in logical pixels in the global
  /// coordinate space.
300 301 302 303 304
  ///
  /// See also:
  ///
  ///  * [localPosition], which is the [position] transformed into the local
  ///    coordinate system of the event receiver.
305
  final Offset position;
Ian Hickson's avatar
Ian Hickson committed
306

307 308 309 310 311 312
  /// The [position] transformed into the event receiver's local coordinate
  /// system according to [transform].
  ///
  /// If this event has not been transformed, [position] is returned as-is.
  /// See also:
  ///
Dan Field's avatar
Dan Field committed
313 314
  ///  * [position], which is the position in the global coordinate system of
  ///    the screen.
315
  Offset get localPosition => position;
316

Ian Hickson's avatar
Ian Hickson committed
317
  /// Distance in logical pixels that the pointer moved since the last
318
  /// [PointerMoveEvent] or [PointerHoverEvent].
319 320
  ///
  /// This value is always 0.0 for down, up, and cancel events.
321 322 323 324 325
  ///
  /// See also:
  ///
  ///  * [localDelta], which is the [delta] transformed into the local
  ///    coordinate space of the event receiver.
Ian Hickson's avatar
Ian Hickson committed
326 327
  final Offset delta;

328 329 330 331 332 333 334 335 336
  /// The [delta] transformed into the event receiver's local coordinate
  /// system according to [transform].
  ///
  /// If this event has not been transformed, [delta] is returned as-is.
  ///
  /// See also:
  ///
  ///  * [delta], which is the distance the pointer moved in the global
  ///    coordinate system of the screen.
337
  Offset get localDelta => delta;
338

339 340 341 342
  /// Bit field using the *Button constants such as [kPrimaryMouseButton],
  /// [kSecondaryStylusButton], etc.
  ///
  /// For example, if this has the value 6 and the
Ian Hickson's avatar
Ian Hickson committed
343 344
  /// [kind] is [PointerDeviceKind.invertedStylus], then this indicates an
  /// upside-down stylus with both its primary and secondary buttons pressed.
Kris Giesing's avatar
Kris Giesing committed
345
  final int buttons;
Ian Hickson's avatar
Ian Hickson committed
346

347 348 349 350
  /// Set if the pointer is currently down.
  ///
  /// For touch and stylus pointers, this means the object (finger, pen) is in
  /// contact with the input surface. For mice, it means a button is pressed.
Kris Giesing's avatar
Kris Giesing committed
351
  final bool down;
Ian Hickson's avatar
Ian Hickson committed
352

353
  /// Set if an application from a different security domain is in any way
354 355 356
  /// obscuring this application's window.
  ///
  /// This is not currently implemented.
Kris Giesing's avatar
Kris Giesing committed
357
  final bool obscured;
Ian Hickson's avatar
Ian Hickson committed
358

359 360 361 362 363 364
  /// The pressure of the touch.
  ///
  /// This value is a number ranging from 0.0, indicating a touch with no
  /// discernible pressure, to 1.0, indicating a touch with "normal" pressure,
  /// and possibly beyond, indicating a stronger touch. For devices that do not
  /// detect pressure (e.g. mice), returns 1.0.
Kris Giesing's avatar
Kris Giesing committed
365
  final double pressure;
Ian Hickson's avatar
Ian Hickson committed
366

367 368 369 370
  /// The minimum value that [pressure] can return for this pointer.
  ///
  /// For devices that do not detect pressure (e.g. mice), returns 1.0.
  /// This will always be a number less than or equal to 1.0.
Kris Giesing's avatar
Kris Giesing committed
371
  final double pressureMin;
Ian Hickson's avatar
Ian Hickson committed
372

373 374 375 376
  /// The maximum value that [pressure] can return for this pointer.
  ///
  /// For devices that do not detect pressure (e.g. mice), returns 1.0.
  /// This will always be a greater than or equal to 1.0.
Kris Giesing's avatar
Kris Giesing committed
377
  final double pressureMax;
Ian Hickson's avatar
Ian Hickson committed
378

379 380 381 382 383
  /// The distance of the detected object from the input surface.
  ///
  /// For instance, this value could be the distance of a stylus or finger
  /// from a touch screen, in arbitrary units on an arbitrary (not necessarily
  /// linear) scale. If the pointer is down, this is 0.0 by definition.
Kris Giesing's avatar
Kris Giesing committed
384
  final double distance;
Ian Hickson's avatar
Ian Hickson committed
385

386 387 388
  /// The minimum value that [distance] can return for this pointer.
  ///
  /// This value is always 0.0.
389
  double get distanceMin => 0.0;
Ian Hickson's avatar
Ian Hickson committed
390

391 392 393 394
  /// The maximum value that [distance] can return for this pointer.
  ///
  /// If this input device cannot detect "hover touch" input events,
  /// then this will be 0.0.
Kris Giesing's avatar
Kris Giesing committed
395
  final double distanceMax;
Ian Hickson's avatar
Ian Hickson committed
396

397 398 399 400 401 402 403 404 405 406 407
  /// The area of the screen being pressed.
  ///
  /// This value is scaled to a range between 0 and 1. It can be used to
  /// determine fat touch events. This value is only set on Android and is
  /// a device specific approximation within the range of detectable values.
  /// So, for example, the value of 0.1 could mean a touch with the tip of
  /// the finger, 0.2 a touch with full finger, and 0.3 the full palm.
  ///
  /// Because this value uses device-specific range and is uncalibrated,
  /// it is of limited use and is primarily retained in order to be able
  /// to reconstruct original pointer events for [AndroidView].
408 409
  final double size;

Ian Hickson's avatar
Ian Hickson committed
410
  /// The radius of the contact ellipse along the major axis, in logical pixels.
Kris Giesing's avatar
Kris Giesing committed
411
  final double radiusMajor;
Ian Hickson's avatar
Ian Hickson committed
412 413

  /// The radius of the contact ellipse along the minor axis, in logical pixels.
Kris Giesing's avatar
Kris Giesing committed
414
  final double radiusMinor;
Ian Hickson's avatar
Ian Hickson committed
415

416
  /// The minimum value that could be reported for [radiusMajor] and [radiusMinor]
Ian Hickson's avatar
Ian Hickson committed
417
  /// for this pointer, in logical pixels.
Kris Giesing's avatar
Kris Giesing committed
418
  final double radiusMin;
Ian Hickson's avatar
Ian Hickson committed
419

420
  /// The maximum value that could be reported for [radiusMajor] and [radiusMinor]
Ian Hickson's avatar
Ian Hickson committed
421
  /// for this pointer, in logical pixels.
Kris Giesing's avatar
Kris Giesing committed
422
  final double radiusMax;
Ian Hickson's avatar
Ian Hickson committed
423

424
  /// The orientation angle of the detected object, in radians.
Ian Hickson's avatar
Ian Hickson committed
425
  ///
426 427 428
  /// For [PointerDeviceKind.touch] events:
  ///
  /// The angle of the contact ellipse, in radians in the range:
Ian Hickson's avatar
Ian Hickson committed
429
  ///
430
  ///     -pi/2 < orientation <= pi/2
Ian Hickson's avatar
Ian Hickson committed
431 432 433 434 435 436 437
  ///
  /// ...giving the angle of the major axis of the ellipse with the y-axis
  /// (negative angles indicating an orientation along the top-left /
  /// bottom-right diagonal, positive angles indicating an orientation along the
  /// top-right / bottom-left diagonal, and zero indicating an orientation
  /// parallel with the y-axis).
  ///
438
  /// For [PointerDeviceKind.stylus] and [PointerDeviceKind.invertedStylus] events:
Ian Hickson's avatar
Ian Hickson committed
439 440 441
  ///
  /// The angle of the stylus, in radians in the range:
  ///
442
  ///     -pi < orientation <= pi
Ian Hickson's avatar
Ian Hickson committed
443 444 445 446 447 448 449 450
  ///
  /// ...giving the angle of the axis of the stylus projected onto the input
  /// surface, relative to the positive y-axis of that surface (thus 0.0
  /// indicates the stylus, if projected onto that surface, would go from the
  /// contact point vertically up in the positive y-axis direction, pi would
  /// indicate that the stylus would go down in the negative y-axis direction;
  /// pi/4 would indicate that the stylus goes up and to the right, -pi/2 would
  /// indicate that the stylus goes to the left, etc).
Kris Giesing's avatar
Kris Giesing committed
451 452
  final double orientation;

453 454 455
  /// The tilt angle of the detected object, in radians.
  ///
  /// For [PointerDeviceKind.stylus] and [PointerDeviceKind.invertedStylus] events:
Ian Hickson's avatar
Ian Hickson committed
456 457 458
  ///
  /// The angle of the stylus, in radians in the range:
  ///
459
  ///     0 <= tilt <= pi/2
Ian Hickson's avatar
Ian Hickson committed
460 461 462 463 464 465
  ///
  /// ...giving the angle of the axis of the stylus, relative to the axis
  /// perpendicular to the input surface (thus 0.0 indicates the stylus is
  /// orthogonal to the plane of the input surface, while pi/2 indicates that
  /// the stylus is flat on that surface).
  final double tilt;
466

467 468 469
  /// Opaque platform-specific data associated with the event.
  final int platformData;

470 471
  /// Set if the event was synthesized by Flutter.
  ///
472
  /// We occasionally synthesize PointerEvents that aren't exact translations
Dan Field's avatar
Dan Field committed
473
  /// of [PointerData] from the engine to cover small cross-OS discrepancies
Ian Hickson's avatar
Ian Hickson committed
474
  /// in pointer behaviors.
475 476
  ///
  /// For instance, on end events, Android always drops any location changes
477
  /// that happened between its reporting intervals when emitting the end events.
478 479 480 481 482 483
  ///
  /// On iOS, minor incorrect location changes from the previous move events
  /// can be reported on end events. We synthesize a [PointerEvent] to cover
  /// the difference between the 2 events in that case.
  final bool synthesized;

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
  /// The transformation used to transform this event from the global coordinate
  /// space into the coordinate space of the event receiver.
  ///
  /// This value affects what is returned by [localPosition] and [localDelta].
  /// If this value is null, it is treated as the identity transformation.
  ///
  /// Unlike a paint transform, this transform usually does not contain any
  /// "perspective" components, meaning that the third row and the third column
  /// of the matrix should be equal to "0, 0, 1, 0". This ensures that
  /// [localPosition] describes the point in the local coordinate system of the
  /// event receiver at which the user is actually touching the screen.
  ///
  /// See also:
  ///
  ///  * [transformed], which transforms this event into a different coordinate
  ///    space.
500
  final Matrix4? transform;
501 502 503 504 505 506 507 508 509 510

  /// The original un-transformed [PointerEvent] before any [transform]s were
  /// applied.
  ///
  /// If [transform] is null or the identity transformation this may be null.
  ///
  /// When multiple event receivers in different coordinate spaces receive an
  /// event, they all receive the event transformed to their local coordinate
  /// space. The [original] property can be used to determine if all those
  /// transformed events actually originated from the same pointer interaction.
511
  final PointerEvent? original;
512 513 514 515 516 517 518

  /// Transforms the event from the global coordinate space into the coordinate
  /// space of an event receiver.
  ///
  /// The coordinate space of the event receiver is described by `transform`. A
  /// null value for `transform` is treated as the identity transformation.
  ///
519 520 521
  /// The resulting event will store the base event as [original], delegates
  /// most properties to [original], except for [localPosition] and [localDelta],
  /// which are calculated based on [transform] on first use and cached.
522
  ///
523 524 525 526 527 528 529 530 531
  /// The method may return the same object instance if for example the
  /// transformation has no effect on the event. Otherwise, the resulting event
  /// will be a subclass of, but not exactly, the original event class (e.g.
  /// [PointerDownEvent.transformed] may return a subclass of [PointerDownEvent]).
  ///
  /// Transforms are not commutative, and are based on [original] events.
  /// If this method is called on a transformed event, the provided `transform`
  /// will override (instead of multiplied onto) the existing [transform] and
  /// used to calculate the new [localPosition] and [localDelta].
532
  PointerEvent transformed(Matrix4? transform);
533

534
  /// Creates a copy of event with the specified properties replaced.
535 536 537
  ///
  /// Calling this method on a transformed event will return a new transformed
  /// event based on the current [transform] and the provided properties.
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
  PointerEvent copyWith({
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  });

563 564 565 566 567
  /// Returns the transformation of `position` into the coordinate system
  /// described by `transform`.
  ///
  /// The z-value of `position` is assumed to be 0.0. If `transform` is null,
  /// `position` is returned as-is.
568
  static Offset transformPosition(Matrix4? transform, Offset position) {
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
    if (transform == null) {
      return position;
    }
    final Vector3 position3 = Vector3(position.dx, position.dy, 0.0);
    final Vector3 transformed3 = transform.perspectiveTransform(position3);
    return Offset(transformed3.x, transformed3.y);
  }

  /// Transforms `untransformedDelta` into the coordinate system described by
  /// `transform`.
  ///
  /// It uses the provided `untransformedEndPosition` and
  /// `transformedEndPosition` of the provided delta to increase accuracy.
  ///
  /// If `transform` is null, `untransformedDelta` is returned.
  static Offset transformDeltaViaPositions({
585 586 587 588
    required Offset untransformedEndPosition,
    Offset? transformedEndPosition,
    required Offset untransformedDelta,
    required Matrix4? transform,
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
  }) {
    if (transform == null) {
      return untransformedDelta;
    }
    // We could transform the delta directly with the transformation matrix.
    // While that is mathematically equivalent, in practice we are seeing a
    // greater precision error with that approach. Instead, we are transforming
    // start and end point of the delta separately and calculate the delta in
    // the new space for greater accuracy.
    transformedEndPosition ??= transformPosition(transform, untransformedEndPosition);
    final Offset transformedStartPosition = transformPosition(transform, untransformedEndPosition - untransformedDelta);
    return transformedEndPosition - transformedStartPosition;
  }

  /// Removes the "perspective" component from `transform`.
  ///
  /// When applying the resulting transform matrix to a point with a
  /// z-coordinate of zero (which is generally assumed for all points
  /// represented by an [Offset]), the other coordinates will get transformed as
  /// before, but the new z-coordinate is going to be zero again. This is
  /// achieved by setting the third column and third row of the matrix to
  /// "0, 0, 1, 0".
  static Matrix4 removePerspectiveTransform(Matrix4 transform) {
    final Vector4 vector = Vector4(0, 0, 1, 0);
    return transform.clone()
      ..setColumn(2, vector)
      ..setRow(2, vector);
  }
617
}
Ian Hickson's avatar
Ian Hickson committed
618

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
// A mixin that adds implementation for [debugFillProperties] and [toStringFull]
// to [PointerEvent].
mixin _PointerEventDescription on PointerEvent {

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<Offset>('position', position));
    properties.add(DiagnosticsProperty<Offset>('localPosition', localPosition, defaultValue: position, level: DiagnosticLevel.debug));
    properties.add(DiagnosticsProperty<Offset>('delta', delta, defaultValue: Offset.zero, level: DiagnosticLevel.debug));
    properties.add(DiagnosticsProperty<Offset>('localDelta', localDelta, defaultValue: delta, level: DiagnosticLevel.debug));
    properties.add(DiagnosticsProperty<Duration>('timeStamp', timeStamp, defaultValue: Duration.zero, level: DiagnosticLevel.debug));
    properties.add(IntProperty('pointer', pointer, level: DiagnosticLevel.debug));
    properties.add(EnumProperty<PointerDeviceKind>('kind', kind, level: DiagnosticLevel.debug));
    properties.add(IntProperty('device', device, defaultValue: 0, level: DiagnosticLevel.debug));
    properties.add(IntProperty('buttons', buttons, defaultValue: 0, level: DiagnosticLevel.debug));
    properties.add(DiagnosticsProperty<bool>('down', down, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('pressure', pressure, defaultValue: 1.0, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('pressureMin', pressureMin, defaultValue: 1.0, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('pressureMax', pressureMax, defaultValue: 1.0, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('distance', distance, defaultValue: 0.0, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('distanceMin', distanceMin, defaultValue: 0.0, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('distanceMax', distanceMax, defaultValue: 0.0, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('size', size, defaultValue: 0.0, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('radiusMajor', radiusMajor, defaultValue: 0.0, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('radiusMinor', radiusMinor, defaultValue: 0.0, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('radiusMin', radiusMin, defaultValue: 0.0, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('radiusMax', radiusMax, defaultValue: 0.0, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('orientation', orientation, defaultValue: 0.0, level: DiagnosticLevel.debug));
    properties.add(DoubleProperty('tilt', tilt, defaultValue: 0.0, level: DiagnosticLevel.debug));
    properties.add(IntProperty('platformData', platformData, defaultValue: 0, level: DiagnosticLevel.debug));
    properties.add(FlagProperty('obscured', value: obscured, ifTrue: 'obscured', level: DiagnosticLevel.debug));
    properties.add(FlagProperty('synthesized', value: synthesized, ifTrue: 'synthesized', level: DiagnosticLevel.debug));
    properties.add(IntProperty('embedderId', embedderId, defaultValue: 0, level: DiagnosticLevel.debug));
  }

  /// Returns a complete textual description of this event.
  String toStringFull() {
    return toString(minLevel: DiagnosticLevel.fine);
  }
}

661
abstract class _AbstractPointerEvent implements PointerEvent { }
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

// The base class for transformed pointer event classes.
//
// A _TransformedPointerEvent stores an [original] event and the [transform]
// matrix. It defers all field getters to the original event, except for
// [localPosition] and [localDelta], which are calculated when first used.
abstract class _TransformedPointerEvent extends _AbstractPointerEvent with Diagnosticable, _PointerEventDescription {

  @override
  PointerEvent get original;

  @override
  Matrix4 get transform;

  @override
  int get embedderId => original.embedderId;

  @override
  Duration get timeStamp => original.timeStamp;

  @override
  int get pointer => original.pointer;

  @override
  PointerDeviceKind get kind => original.kind;

  @override
  int get device => original.device;

  @override
  Offset get position => original.position;

  @override
  Offset get delta => original.delta;

  @override
  int get buttons => original.buttons;

  @override
  bool get down => original.down;

  @override
  bool get obscured => original.obscured;

  @override
  double get pressure => original.pressure;

  @override
  double get pressureMin => original.pressureMin;

  @override
  double get pressureMax => original.pressureMax;

  @override
  double get distance => original.distance;

  @override
  double get distanceMin => 0.0;

  @override
  double get distanceMax => original.distanceMax;

  @override
  double get size => original.size;

  @override
  double get radiusMajor => original.radiusMajor;

  @override
  double get radiusMinor => original.radiusMinor;

  @override
  double get radiusMin => original.radiusMin;

  @override
  double get radiusMax => original.radiusMax;

  @override
  double get orientation => original.orientation;

  @override
  double get tilt => original.tilt;

  @override
  int get platformData => original.platformData;

  @override
  bool get synthesized => original.synthesized;

  @override
  late final Offset localPosition = PointerEvent.transformPosition(transform, position);

  @override
  late final Offset localDelta = PointerEvent.transformDeltaViaPositions(
    transform: transform,
    untransformedDelta: delta,
    untransformedEndPosition: position,
    transformedEndPosition: localPosition,
  );
}

mixin _CopyPointerAddedEvent on PointerEvent {
  @override
  PointerAddedEvent copyWith({
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
    return PointerAddedEvent(
      timeStamp: timeStamp ?? this.timeStamp,
      kind: kind ?? this.kind,
      device: device ?? this.device,
      position: position ?? this.position,
      obscured: obscured ?? this.obscured,
      pressureMin: pressureMin ?? this.pressureMin,
      pressureMax: pressureMax ?? this.pressureMax,
      distance: distance ?? this.distance,
      distanceMax: distanceMax ?? this.distanceMax,
      radiusMin: radiusMin ?? this.radiusMin,
      radiusMax: radiusMax ?? this.radiusMax,
      orientation: orientation ?? this.orientation,
      tilt: tilt ?? this.tilt,
      embedderId: embedderId ?? this.embedderId,
    ).transformed(transform);
  }
}

808 809 810 811
/// The device has started tracking the pointer.
///
/// For example, the pointer might be hovering above the device, having not yet
/// made contact with the surface of the device.
812
class PointerAddedEvent extends PointerEvent with _PointerEventDescription, _CopyPointerAddedEvent {
813 814
  /// Creates a pointer added event.
  ///
815
  /// All of the arguments must be non-null.
Ian Hickson's avatar
Ian Hickson committed
816
  const PointerAddedEvent({
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
    super.timeStamp,
    super.pointer,
    super.kind,
    super.device,
    super.position,
    super.obscured,
    super.pressureMin,
    super.pressureMax,
    super.distance,
    super.distanceMax,
    super.radiusMin,
    super.radiusMax,
    super.orientation,
    super.tilt,
    super.embedderId,
Ian Hickson's avatar
Ian Hickson committed
832
  }) : super(
833
         pressure: 0.0,
834
       );
835 836

  @override
837
  PointerAddedEvent transformed(Matrix4? transform) {
838 839 840
    if (transform == null || transform == this.transform) {
      return this;
    }
841
    return _TransformedPointerAddedEvent(original as PointerAddedEvent? ?? this, transform);
842
  }
843 844 845 846 847
}

class _TransformedPointerAddedEvent extends _TransformedPointerEvent with _CopyPointerAddedEvent implements PointerAddedEvent {
  _TransformedPointerAddedEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);
848 849

  @override
850 851 852 853 854 855 856 857 858 859 860 861
  final PointerAddedEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerAddedEvent transformed(Matrix4? transform) => original.transformed(transform);
}

mixin _CopyPointerRemovedEvent on PointerEvent {
  @override
  PointerRemovedEvent copyWith({
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
885
    return PointerRemovedEvent(
886 887 888 889 890 891 892 893 894 895 896
      timeStamp: timeStamp ?? this.timeStamp,
      kind: kind ?? this.kind,
      device: device ?? this.device,
      position: position ?? this.position,
      obscured: obscured ?? this.obscured,
      pressureMin: pressureMin ?? this.pressureMin,
      pressureMax: pressureMax ?? this.pressureMax,
      distanceMax: distanceMax ?? this.distanceMax,
      radiusMin: radiusMin ?? this.radiusMin,
      radiusMax: radiusMax ?? this.radiusMax,
      embedderId: embedderId ?? this.embedderId,
897
    ).transformed(transform);
898
  }
Ian Hickson's avatar
Ian Hickson committed
899 900
}

901 902 903 904
/// The device is no longer tracking the pointer.
///
/// For example, the pointer might have drifted out of the device's hover
/// detection range or might have been disconnected from the system entirely.
905
class PointerRemovedEvent extends PointerEvent with _PointerEventDescription, _CopyPointerRemovedEvent {
906 907
  /// Creates a pointer removed event.
  ///
908
  /// All of the arguments must be non-null.
Ian Hickson's avatar
Ian Hickson committed
909
  const PointerRemovedEvent({
910 911 912 913 914 915 916 917 918 919 920 921 922
    super.timeStamp,
    super.pointer,
    super.kind,
    super.device,
    super.position,
    super.obscured,
    super.pressureMin,
    super.pressureMax,
    super.distanceMax,
    super.radiusMin,
    super.radiusMax,
    PointerRemovedEvent? super.original,
    super.embedderId,
Ian Hickson's avatar
Ian Hickson committed
923
  }) : super(
924
         pressure: 0.0,
925
       );
926 927

  @override
928
  PointerRemovedEvent transformed(Matrix4? transform) {
929 930 931
    if (transform == null || transform == this.transform) {
      return this;
    }
932
    return _TransformedPointerRemovedEvent(original as PointerRemovedEvent? ?? this, transform);
933
  }
934 935 936 937 938
}

class _TransformedPointerRemovedEvent extends _TransformedPointerEvent with _CopyPointerRemovedEvent implements PointerRemovedEvent {
  _TransformedPointerRemovedEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);
939 940

  @override
941 942 943 944 945 946 947 948 949 950 951 952
  final PointerRemovedEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerRemovedEvent transformed(Matrix4? transform) => original.transformed(transform);
}

mixin _CopyPointerHoverEvent on PointerEvent {
  @override
  PointerHoverEvent copyWith({
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
976
    return PointerHoverEvent(
977 978 979 980
      timeStamp: timeStamp ?? this.timeStamp,
      kind: kind ?? this.kind,
      device: device ?? this.device,
      position: position ?? this.position,
981 982
      delta: delta ?? this.delta,
      buttons: buttons ?? this.buttons,
983 984 985
      obscured: obscured ?? this.obscured,
      pressureMin: pressureMin ?? this.pressureMin,
      pressureMax: pressureMax ?? this.pressureMax,
986
      distance: distance ?? this.distance,
987
      distanceMax: distanceMax ?? this.distanceMax,
988 989 990
      size: size ?? this.size,
      radiusMajor: radiusMajor ?? this.radiusMajor,
      radiusMinor: radiusMinor ?? this.radiusMinor,
991 992
      radiusMin: radiusMin ?? this.radiusMin,
      radiusMax: radiusMax ?? this.radiusMax,
993 994 995
      orientation: orientation ?? this.orientation,
      tilt: tilt ?? this.tilt,
      synthesized: synthesized ?? this.synthesized,
996
      embedderId: embedderId ?? this.embedderId,
997
    ).transformed(transform);
998
  }
Ian Hickson's avatar
Ian Hickson committed
999 1000
}

1001 1002 1003 1004 1005
/// The pointer has moved with respect to the device while the pointer is not
/// in contact with the device.
///
/// See also:
///
1006 1007 1008
///  * [PointerEnterEvent], which reports when the pointer has entered an
///    object.
///  * [PointerExitEvent], which reports when the pointer has left an object.
1009 1010
///  * [PointerMoveEvent], which reports movement while the pointer is in
///    contact with the device.
1011 1012
///  * [Listener.onPointerHover], which allows callers to be notified of these
///    events in a widget tree.
1013
class PointerHoverEvent extends PointerEvent with _PointerEventDescription, _CopyPointerHoverEvent {
1014 1015
  /// Creates a pointer hover event.
  ///
1016
  /// All of the arguments must be non-null.
1017
  const PointerHoverEvent({
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
    super.timeStamp,
    super.kind,
    super.pointer,
    super.device,
    super.position,
    super.delta,
    super.buttons,
    super.obscured,
    super.pressureMin,
    super.pressureMax,
    super.distance,
    super.distanceMax,
    super.size,
    super.radiusMajor,
    super.radiusMinor,
    super.radiusMin,
    super.radiusMax,
    super.orientation,
    super.tilt,
    super.synthesized,
    super.embedderId,
1039
  }) : super(
1040
         down: false,
1041
         pressure: 0.0,
1042
       );
1043 1044

  @override
1045
  PointerHoverEvent transformed(Matrix4? transform) {
1046 1047 1048
    if (transform == null || transform == this.transform) {
      return this;
    }
1049
    return _TransformedPointerHoverEvent(original as PointerHoverEvent? ?? this, transform);
1050
  }
1051 1052 1053 1054 1055
}

class _TransformedPointerHoverEvent extends _TransformedPointerEvent with _CopyPointerHoverEvent implements PointerHoverEvent {
  _TransformedPointerHoverEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);
1056 1057

  @override
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
  final PointerHoverEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerHoverEvent transformed(Matrix4? transform) => original.transformed(transform);
}

mixin _CopyPointerEnterEvent on PointerEvent {
  @override
  PointerEnterEvent copyWith({
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
1093
    return PointerEnterEvent(
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
      timeStamp: timeStamp ?? this.timeStamp,
      kind: kind ?? this.kind,
      device: device ?? this.device,
      position: position ?? this.position,
      delta: delta ?? this.delta,
      buttons: buttons ?? this.buttons,
      obscured: obscured ?? this.obscured,
      pressureMin: pressureMin ?? this.pressureMin,
      pressureMax: pressureMax ?? this.pressureMax,
      distance: distance ?? this.distance,
      distanceMax: distanceMax ?? this.distanceMax,
      size: size ?? this.size,
      radiusMajor: radiusMajor ?? this.radiusMajor,
      radiusMinor: radiusMinor ?? this.radiusMinor,
      radiusMin: radiusMin ?? this.radiusMin,
      radiusMax: radiusMax ?? this.radiusMax,
      orientation: orientation ?? this.orientation,
      tilt: tilt ?? this.tilt,
      synthesized: synthesized ?? this.synthesized,
      embedderId: embedderId ?? this.embedderId,
1114
    ).transformed(transform);
1115
  }
1116 1117
}

1118 1119
/// The pointer has moved with respect to the device while the pointer is or is
/// not in contact with the device, and it has entered a target object.
1120 1121 1122 1123 1124 1125 1126 1127
///
/// See also:
///
///  * [PointerHoverEvent], which reports when the pointer has moved while
///    within an object.
///  * [PointerExitEvent], which reports when the pointer has left an object.
///  * [PointerMoveEvent], which reports movement while the pointer is in
///    contact with the device.
1128
///  * [MouseRegion.onEnter], which allows callers to be notified of these
1129
///    events in a widget tree.
1130
class PointerEnterEvent extends PointerEvent with _PointerEventDescription, _CopyPointerEnterEvent {
1131 1132 1133 1134
  /// Creates a pointer enter event.
  ///
  /// All of the arguments must be non-null.
  const PointerEnterEvent({
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
    super.timeStamp,
    super.pointer,
    super.kind,
    super.device,
    super.position,
    super.delta,
    super.buttons,
    super.obscured,
    super.pressureMin,
    super.pressureMax,
    super.distance,
    super.distanceMax,
    super.size,
    super.radiusMajor,
    super.radiusMinor,
    super.radiusMin,
    super.radiusMax,
    super.orientation,
    super.tilt,
    super.down,
    super.synthesized,
    super.embedderId,
1157 1158 1159
  }) : // Dart doesn't support comparing enums with == in const contexts yet.
       // https://github.com/dart-lang/language/issues/1811
       assert(!identical(kind, PointerDeviceKind.trackpad)),
1160
       super(
1161
         pressure: 0.0,
1162
       );
1163

1164 1165 1166
  /// Creates an enter event from a [PointerEvent].
  ///
  /// This is used by the [MouseTracker] to synthesize enter events.
1167
  factory PointerEnterEvent.fromMouseEvent(PointerEvent event) => PointerEnterEvent(
1168
    timeStamp: event.timeStamp,
1169
    pointer: event.pointer,
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
    kind: event.kind,
    device: event.device,
    position: event.position,
    delta: event.delta,
    buttons: event.buttons,
    obscured: event.obscured,
    pressureMin: event.pressureMin,
    pressureMax: event.pressureMax,
    distance: event.distance,
    distanceMax: event.distanceMax,
    size: event.size,
    radiusMajor: event.radiusMajor,
    radiusMinor: event.radiusMinor,
    radiusMin: event.radiusMin,
    radiusMax: event.radiusMax,
    orientation: event.orientation,
    tilt: event.tilt,
    down: event.down,
    synthesized: event.synthesized,
1189
  ).transformed(event.transform);
1190 1191

  @override
1192
  PointerEnterEvent transformed(Matrix4? transform) {
1193 1194 1195
    if (transform == null || transform == this.transform) {
      return this;
    }
1196
    return _TransformedPointerEnterEvent(original as PointerEnterEvent? ?? this, transform);
1197
  }
1198 1199 1200 1201 1202
}

class _TransformedPointerEnterEvent extends _TransformedPointerEvent with _CopyPointerEnterEvent implements PointerEnterEvent {
  _TransformedPointerEnterEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);
1203 1204

  @override
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
  final PointerEnterEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerEnterEvent transformed(Matrix4? transform) => original.transformed(transform);
}

mixin _CopyPointerExitEvent on PointerEvent {
  @override
  PointerExitEvent copyWith({
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
1240
    return PointerExitEvent(
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
      timeStamp: timeStamp ?? this.timeStamp,
      kind: kind ?? this.kind,
      device: device ?? this.device,
      position: position ?? this.position,
      delta: delta ?? this.delta,
      buttons: buttons ?? this.buttons,
      obscured: obscured ?? this.obscured,
      pressureMin: pressureMin ?? this.pressureMin,
      pressureMax: pressureMax ?? this.pressureMax,
      distance: distance ?? this.distance,
      distanceMax: distanceMax ?? this.distanceMax,
      size: size ?? this.size,
      radiusMajor: radiusMajor ?? this.radiusMajor,
      radiusMinor: radiusMinor ?? this.radiusMinor,
      radiusMin: radiusMin ?? this.radiusMin,
      radiusMax: radiusMax ?? this.radiusMax,
      orientation: orientation ?? this.orientation,
      tilt: tilt ?? this.tilt,
      synthesized: synthesized ?? this.synthesized,
      embedderId: embedderId ?? this.embedderId,
1261
    ).transformed(transform);
1262
  }
1263 1264
}

1265
/// The pointer has moved with respect to the device while the pointer is or is
1266
/// not in contact with the device, and exited a target object.
1267 1268 1269 1270 1271 1272 1273 1274
///
/// See also:
///
///  * [PointerHoverEvent], which reports when the pointer has moved while
///    within an object.
///  * [PointerEnterEvent], which reports when the pointer has entered an object.
///  * [PointerMoveEvent], which reports movement while the pointer is in
///    contact with the device.
1275
///  * [MouseRegion.onExit], which allows callers to be notified of these
1276
///    events in a widget tree.
1277
class PointerExitEvent extends PointerEvent with _PointerEventDescription, _CopyPointerExitEvent {
1278 1279 1280 1281
  /// Creates a pointer exit event.
  ///
  /// All of the arguments must be non-null.
  const PointerExitEvent({
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
    super.timeStamp,
    super.kind,
    super.pointer,
    super.device,
    super.position,
    super.delta,
    super.buttons,
    super.obscured,
    super.pressureMin,
    super.pressureMax,
    super.distance,
    super.distanceMax,
    super.size,
    super.radiusMajor,
    super.radiusMinor,
    super.radiusMin,
    super.radiusMax,
    super.orientation,
    super.tilt,
    super.down,
    super.synthesized,
    super.embedderId,
1304 1305
  }) : assert(!identical(kind, PointerDeviceKind.trackpad)),
       super(
1306
         pressure: 0.0,
1307
       );
1308

1309 1310 1311
  /// Creates an exit event from a [PointerEvent].
  ///
  /// This is used by the [MouseTracker] to synthesize exit events.
1312
  factory PointerExitEvent.fromMouseEvent(PointerEvent event) => PointerExitEvent(
1313
    timeStamp: event.timeStamp,
1314
    pointer: event.pointer,
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
    kind: event.kind,
    device: event.device,
    position: event.position,
    delta: event.delta,
    buttons: event.buttons,
    obscured: event.obscured,
    pressureMin: event.pressureMin,
    pressureMax: event.pressureMax,
    distance: event.distance,
    distanceMax: event.distanceMax,
    size: event.size,
    radiusMajor: event.radiusMajor,
    radiusMinor: event.radiusMinor,
    radiusMin: event.radiusMin,
    radiusMax: event.radiusMax,
    orientation: event.orientation,
    tilt: event.tilt,
    down: event.down,
    synthesized: event.synthesized,
1334
  ).transformed(event.transform);
1335 1336

  @override
1337
  PointerExitEvent transformed(Matrix4? transform) {
1338 1339 1340
    if (transform == null || transform == this.transform) {
      return this;
    }
1341
    return _TransformedPointerExitEvent(original as PointerExitEvent? ?? this, transform);
1342
  }
1343

1344 1345 1346 1347 1348 1349
}

class _TransformedPointerExitEvent extends _TransformedPointerEvent with _CopyPointerExitEvent implements PointerExitEvent {
  _TransformedPointerExitEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);

1350
  @override
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
  final PointerExitEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerExitEvent transformed(Matrix4? transform) => original.transformed(transform);
}

mixin _CopyPointerDownEvent on PointerEvent {
  @override
  PointerDownEvent copyWith({
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
1386
    return PointerDownEvent(
1387
      timeStamp: timeStamp ?? this.timeStamp,
1388
      pointer: pointer ?? this.pointer,
1389 1390 1391 1392 1393
      kind: kind ?? this.kind,
      device: device ?? this.device,
      position: position ?? this.position,
      buttons: buttons ?? this.buttons,
      obscured: obscured ?? this.obscured,
1394
      pressure: pressure ?? this.pressure,
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
      pressureMin: pressureMin ?? this.pressureMin,
      pressureMax: pressureMax ?? this.pressureMax,
      distanceMax: distanceMax ?? this.distanceMax,
      size: size ?? this.size,
      radiusMajor: radiusMajor ?? this.radiusMajor,
      radiusMinor: radiusMinor ?? this.radiusMinor,
      radiusMin: radiusMin ?? this.radiusMin,
      radiusMax: radiusMax ?? this.radiusMax,
      orientation: orientation ?? this.orientation,
      tilt: tilt ?? this.tilt,
      embedderId: embedderId ?? this.embedderId,
1406
    ).transformed(transform);
1407
  }
1408 1409
}

1410
/// The pointer has made contact with the device.
1411 1412 1413 1414 1415
///
/// See also:
///
///  * [Listener.onPointerDown], which allows callers to be notified of these
///    events in a widget tree.
1416
class PointerDownEvent extends PointerEvent with _PointerEventDescription, _CopyPointerDownEvent {
1417 1418
  /// Creates a pointer down event.
  ///
1419
  /// All of the arguments must be non-null.
Ian Hickson's avatar
Ian Hickson committed
1420
  const PointerDownEvent({
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
    super.timeStamp,
    super.pointer,
    super.kind,
    super.device,
    super.position,
    super.buttons = kPrimaryButton,
    super.obscured,
    super.pressure,
    super.pressureMin,
    super.pressureMax,
    super.distanceMax,
    super.size,
    super.radiusMajor,
    super.radiusMinor,
    super.radiusMin,
    super.radiusMax,
    super.orientation,
    super.tilt,
    super.embedderId,
1440 1441
  }) : assert(!identical(kind, PointerDeviceKind.trackpad)),
       super(
1442 1443 1444
         down: true,
         distance: 0.0,
       );
1445 1446

  @override
1447
  PointerDownEvent transformed(Matrix4? transform) {
1448 1449 1450
    if (transform == null || transform == this.transform) {
      return this;
    }
1451
    return _TransformedPointerDownEvent(original as PointerDownEvent? ?? this, transform);
1452
  }
1453 1454 1455 1456 1457
}

class _TransformedPointerDownEvent extends _TransformedPointerEvent with _CopyPointerDownEvent implements PointerDownEvent {
  _TransformedPointerDownEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);
1458 1459

  @override
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
  final PointerDownEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerDownEvent transformed(Matrix4? transform) => original.transformed(transform);
}

mixin _CopyPointerMoveEvent on PointerEvent {
  @override
  PointerMoveEvent copyWith({
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
1495
    return PointerMoveEvent(
1496 1497 1498 1499 1500
      timeStamp: timeStamp ?? this.timeStamp,
      pointer: pointer ?? this.pointer,
      kind: kind ?? this.kind,
      device: device ?? this.device,
      position: position ?? this.position,
1501
      delta: delta ?? this.delta,
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
      buttons: buttons ?? this.buttons,
      obscured: obscured ?? this.obscured,
      pressure: pressure ?? this.pressure,
      pressureMin: pressureMin ?? this.pressureMin,
      pressureMax: pressureMax ?? this.pressureMax,
      distanceMax: distanceMax ?? this.distanceMax,
      size: size ?? this.size,
      radiusMajor: radiusMajor ?? this.radiusMajor,
      radiusMinor: radiusMinor ?? this.radiusMinor,
      radiusMin: radiusMin ?? this.radiusMin,
      radiusMax: radiusMax ?? this.radiusMax,
      orientation: orientation ?? this.orientation,
      tilt: tilt ?? this.tilt,
1515
      synthesized: synthesized ?? this.synthesized,
1516
      embedderId: embedderId ?? this.embedderId,
1517
    ).transformed(transform);
1518
  }
Ian Hickson's avatar
Ian Hickson committed
1519 1520
}

1521 1522 1523 1524 1525 1526 1527
/// The pointer has moved with respect to the device while the pointer is in
/// contact with the device.
///
/// See also:
///
///  * [PointerHoverEvent], which reports movement while the pointer is not in
///    contact with the device.
1528 1529
///  * [Listener.onPointerMove], which allows callers to be notified of these
///    events in a widget tree.
1530
class PointerMoveEvent extends PointerEvent with _PointerEventDescription, _CopyPointerMoveEvent {
1531 1532
  /// Creates a pointer move event.
  ///
1533
  /// All of the arguments must be non-null.
Ian Hickson's avatar
Ian Hickson committed
1534
  const PointerMoveEvent({
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
    super.timeStamp,
    super.pointer,
    super.kind,
    super.device,
    super.position,
    super.delta,
    super.buttons = kPrimaryButton,
    super.obscured,
    super.pressure,
    super.pressureMin,
    super.pressureMax,
    super.distanceMax,
    super.size,
    super.radiusMajor,
    super.radiusMinor,
    super.radiusMin,
    super.radiusMax,
    super.orientation,
    super.tilt,
    super.platformData,
    super.synthesized,
    super.embedderId,
1557 1558
  }) : assert(!identical(kind, PointerDeviceKind.trackpad)),
       super(
1559 1560 1561
         down: true,
         distance: 0.0,
       );
1562 1563

  @override
1564
  PointerMoveEvent transformed(Matrix4? transform) {
1565 1566 1567 1568
    if (transform == null || transform == this.transform) {
      return this;
    }

1569
    return _TransformedPointerMoveEvent(original as PointerMoveEvent? ?? this, transform);
1570
  }
1571 1572 1573 1574 1575
}

class _TransformedPointerMoveEvent extends _TransformedPointerEvent with _CopyPointerMoveEvent implements PointerMoveEvent {
  _TransformedPointerMoveEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);
1576 1577

  @override
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
  final PointerMoveEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerMoveEvent transformed(Matrix4? transform) => original.transformed(transform);
}

mixin _CopyPointerUpEvent on PointerEvent {
  @override
  PointerUpEvent copyWith({
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? localPosition,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
1614
    return PointerUpEvent(
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
      timeStamp: timeStamp ?? this.timeStamp,
      pointer: pointer ?? this.pointer,
      kind: kind ?? this.kind,
      device: device ?? this.device,
      position: position ?? this.position,
      buttons: buttons ?? this.buttons,
      obscured: obscured ?? this.obscured,
      pressure: pressure ?? this.pressure,
      pressureMin: pressureMin ?? this.pressureMin,
      pressureMax: pressureMax ?? this.pressureMax,
1625
      distance: distance ?? this.distance,
1626 1627 1628 1629 1630 1631 1632 1633 1634
      distanceMax: distanceMax ?? this.distanceMax,
      size: size ?? this.size,
      radiusMajor: radiusMajor ?? this.radiusMajor,
      radiusMinor: radiusMinor ?? this.radiusMinor,
      radiusMin: radiusMin ?? this.radiusMin,
      radiusMax: radiusMax ?? this.radiusMax,
      orientation: orientation ?? this.orientation,
      tilt: tilt ?? this.tilt,
      embedderId: embedderId ?? this.embedderId,
1635
    ).transformed(transform);
1636
  }
Ian Hickson's avatar
Ian Hickson committed
1637 1638
}

1639
/// The pointer has stopped making contact with the device.
1640 1641 1642 1643 1644
///
/// See also:
///
///  * [Listener.onPointerUp], which allows callers to be notified of these
///    events in a widget tree.
1645
class PointerUpEvent extends PointerEvent with _PointerEventDescription, _CopyPointerUpEvent {
1646 1647
  /// Creates a pointer up event.
  ///
1648
  /// All of the arguments must be non-null.
Ian Hickson's avatar
Ian Hickson committed
1649
  const PointerUpEvent({
1650 1651 1652 1653 1654 1655 1656
    super.timeStamp,
    super.pointer,
    super.kind,
    super.device,
    super.position,
    super.buttons,
    super.obscured,
1657 1658
    // Allow pressure customization here because PointerUpEvent can contain
    // non-zero pressure. See https://github.com/flutter/flutter/issues/31340
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
    super.pressure = 0.0,
    super.pressureMin,
    super.pressureMax,
    super.distance,
    super.distanceMax,
    super.size,
    super.radiusMajor,
    super.radiusMinor,
    super.radiusMin,
    super.radiusMax,
    super.orientation,
    super.tilt,
    super.embedderId,
1672 1673
  }) : assert(!identical(kind, PointerDeviceKind.trackpad)),
       super(
1674 1675
         down: false,
       );
1676 1677

  @override
1678
  PointerUpEvent transformed(Matrix4? transform) {
1679 1680 1681
    if (transform == null || transform == this.transform) {
      return this;
    }
1682
    return _TransformedPointerUpEvent(original as PointerUpEvent? ?? this, transform);
1683
  }
1684 1685 1686 1687 1688
}

class _TransformedPointerUpEvent extends _TransformedPointerEvent with _CopyPointerUpEvent implements PointerUpEvent {
  _TransformedPointerUpEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);
1689 1690

  @override
1691 1692 1693 1694 1695 1696 1697
  final PointerUpEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerUpEvent transformed(Matrix4? transform) => original.transformed(transform);
Ian Hickson's avatar
Ian Hickson committed
1698 1699
}

1700 1701 1702 1703 1704
/// An event that corresponds to a discrete pointer signal.
///
/// Pointer signals are events that originate from the pointer but don't change
/// the state of the pointer itself, and are discrete rather than needing to be
/// interpreted in the context of a series of events.
1705 1706 1707 1708 1709
///
/// See also:
///
///  * [Listener.onPointerSignal], which allows callers to be notified of these
///    events in a widget tree.
1710 1711
///  * [PointerSignalResolver], which provides an opt-in mechanism whereby
///    participating agents may disambiguate an event's target.
1712 1713 1714 1715
abstract class PointerSignalEvent extends PointerEvent {
  /// Abstract const constructor. This constructor enables subclasses to provide
  /// const constructors so that they can be used in const expressions.
  const PointerSignalEvent({
1716 1717 1718 1719 1720 1721
    super.timeStamp,
    super.pointer,
    super.kind = PointerDeviceKind.mouse,
    super.device,
    super.position,
    super.embedderId,
1722
  });
1723 1724
}

1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
mixin _CopyPointerScrollEvent on PointerEvent {
  /// The amount to scroll, in logical pixels.
  Offset get scrollDelta;

  @override
  PointerScrollEvent copyWith({
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
    return PointerScrollEvent(
      timeStamp: timeStamp ?? this.timeStamp,
      kind: kind ?? this.kind,
      device: device ?? this.device,
      position: position ?? this.position,
      scrollDelta: scrollDelta,
      embedderId: embedderId ?? this.embedderId,
    ).transformed(transform);
  }
}

1765 1766 1767 1768
/// The pointer issued a scroll event.
///
/// Scrolling the scroll wheel on a mouse is an example of an event that
/// would create a [PointerScrollEvent].
1769 1770 1771 1772 1773
///
/// See also:
///
///  * [Listener.onPointerSignal], which allows callers to be notified of these
///    events in a widget tree.
1774 1775
///  * [PointerSignalResolver], which provides an opt-in mechanism whereby
///    participating agents may disambiguate an event's target.
1776
class PointerScrollEvent extends PointerSignalEvent with _PointerEventDescription, _CopyPointerScrollEvent {
1777 1778 1779 1780
  /// Creates a pointer scroll event.
  ///
  /// All of the arguments must be non-null.
  const PointerScrollEvent({
1781 1782 1783 1784
    super.timeStamp,
    super.kind,
    super.device,
    super.position,
1785
    this.scrollDelta = Offset.zero,
1786
    super.embedderId,
1787 1788 1789 1790
  }) : assert(timeStamp != null),
       assert(kind != null),
       assert(device != null),
       assert(position != null),
1791 1792
       assert(scrollDelta != null),
       assert(!identical(kind, PointerDeviceKind.trackpad));
1793

1794
  @override
1795 1796
  final Offset scrollDelta;

1797
  @override
1798
  PointerScrollEvent transformed(Matrix4? transform) {
1799 1800 1801
    if (transform == null || transform == this.transform) {
      return this;
    }
1802
    return _TransformedPointerScrollEvent(original as PointerScrollEvent? ?? this, transform);
1803 1804
  }

1805
  @override
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<Offset>('scrollDelta', scrollDelta));
  }
}

class _TransformedPointerScrollEvent extends _TransformedPointerEvent with _CopyPointerScrollEvent implements PointerScrollEvent {
  _TransformedPointerScrollEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);

  @override
  final PointerScrollEvent original;

  @override
  final Matrix4 transform;

  @override
  Offset get scrollDelta => original.scrollDelta;

  @override
  PointerScrollEvent transformed(Matrix4? transform) => original.transformed(transform);

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<Offset>('scrollDelta', scrollDelta));
  }
}

1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
mixin _CopyPointerScrollInertiaCancelEvent on PointerEvent {
  @override
  PointerScrollInertiaCancelEvent copyWith({
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
    return PointerScrollInertiaCancelEvent(
      timeStamp: timeStamp ?? this.timeStamp,
      kind: kind ?? this.kind,
      device: device ?? this.device,
      position: position ?? this.position,
      embedderId: embedderId ?? this.embedderId,
    ).transformed(transform);
  }
}

/// The pointer issued a scroll-inertia cancel event.
///
/// Touching the trackpad immediately after a scroll is an example of an event
/// that would create a [PointerScrollInertiaCancelEvent].
///
/// See also:
///
///  * [Listener.onPointerSignal], which allows callers to be notified of these
///    events in a widget tree.
///  * [PointerSignalResolver], which provides an opt-in mechanism whereby
///    participating agents may disambiguate an event's target.
class PointerScrollInertiaCancelEvent extends PointerSignalEvent with _PointerEventDescription, _CopyPointerScrollInertiaCancelEvent {
  /// Creates a pointer scroll-inertia cancel event.
  ///
  /// All of the arguments must be non-null.
  const PointerScrollInertiaCancelEvent({
    super.timeStamp,
    super.kind,
    super.device,
    super.position,
    super.embedderId,
  }) : assert(timeStamp != null),
       assert(kind != null),
       assert(device != null),
       assert(position != null);

  @override
  PointerScrollInertiaCancelEvent transformed(Matrix4? transform) {
    if (transform == null || transform == this.transform) {
      return this;
    }
    return _TransformedPointerScrollInertiaCancelEvent(original as PointerScrollInertiaCancelEvent? ?? this, transform);
  }
}

class _TransformedPointerScrollInertiaCancelEvent extends _TransformedPointerEvent with _CopyPointerScrollInertiaCancelEvent implements PointerScrollInertiaCancelEvent {
  _TransformedPointerScrollInertiaCancelEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);

  @override
  final PointerScrollInertiaCancelEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerScrollInertiaCancelEvent transformed(Matrix4? transform) => original.transformed(transform);
}

1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
mixin _CopyPointerScaleEvent on PointerEvent {
  /// The scale (zoom factor) of the event.
  double get scale;

  @override
  PointerScaleEvent copyWith({
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
    double? scale,
  }) {
    return PointerScaleEvent(
      timeStamp: timeStamp ?? this.timeStamp,
      kind: kind ?? this.kind,
      device: device ?? this.device,
      position: position ?? this.position,
      embedderId: embedderId ?? this.embedderId,
      scale: scale ?? this.scale,
    ).transformed(transform);
  }
}

/// The pointer issued a scale event.
///
/// Pinching-to-zoom in the browser is an example of an event
/// that would create a [PointerScaleEvent].
///
/// See also:
///
///  * [Listener.onPointerSignal], which allows callers to be notified of these
///    events in a widget tree.
///  * [PointerSignalResolver], which provides an opt-in mechanism whereby
///    participating agents may disambiguate an event's target.
class PointerScaleEvent extends PointerSignalEvent with _PointerEventDescription, _CopyPointerScaleEvent {
  /// Creates a pointer scale event.
  ///
  /// All of the arguments must be non-null.
  const PointerScaleEvent({
    super.timeStamp,
    super.kind,
    super.device,
    super.position,
    super.embedderId,
    this.scale = 1.0,
  }) : assert(timeStamp != null),
       assert(kind != null),
       assert(device != null),
       assert(position != null),
       assert(embedderId != null),
       assert(scale != null);

  @override
  final double scale;

  @override
  PointerScaleEvent transformed(Matrix4? transform) {
    if (transform == null || transform == this.transform) {
      return this;
    }
    return _TransformedPointerScaleEvent(original as PointerScaleEvent? ?? this, transform);
  }
}

class _TransformedPointerScaleEvent extends _TransformedPointerEvent with _CopyPointerScaleEvent implements PointerScaleEvent {
  _TransformedPointerScaleEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);

  @override
  final PointerScaleEvent original;

  @override
  final Matrix4 transform;

  @override
  double get scale => original.scale;

  @override
  PointerScaleEvent transformed(Matrix4? transform) => original.transformed(transform);
}

2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
mixin _CopyPointerPanZoomStartEvent on PointerEvent {
  @override
  PointerPanZoomStartEvent copyWith({
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
2045
    assert(kind == null || identical(kind, PointerDeviceKind.trackpad));
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
    return PointerPanZoomStartEvent(
      timeStamp: timeStamp ?? this.timeStamp,
      device: device ?? this.device,
      position: position ?? this.position,
      embedderId: embedderId ?? this.embedderId,
    ).transformed(transform);
  }
}

/// A pan/zoom has begun on this pointer.
///
/// See also:
///
///  * [Listener.onPointerPanZoomStart], which allows callers to be notified of these
///    events in a widget tree.
class PointerPanZoomStartEvent extends PointerEvent with _PointerEventDescription, _CopyPointerPanZoomStartEvent {
  /// Creates a pointer pan/zoom start event.
  ///
  /// All of the arguments must be non-null.
  const PointerPanZoomStartEvent({
2066 2067 2068 2069 2070 2071
    super.timeStamp,
    super.device,
    super.pointer,
    super.position,
    super.embedderId,
    super.synthesized,
2072 2073 2074 2075 2076
  }) : assert(timeStamp != null),
       assert(device != null),
       assert(pointer != null),
       assert(position != null),
       assert(embedderId != null),
2077 2078
       assert(synthesized != null),
       super(kind: PointerDeviceKind.trackpad);
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147

  @override
  PointerPanZoomStartEvent transformed(Matrix4? transform) {
    if (transform == null || transform == this.transform) {
      return this;
    }
    return _TransformedPointerPanZoomStartEvent(original as PointerPanZoomStartEvent? ?? this, transform);
  }
}

class _TransformedPointerPanZoomStartEvent extends _TransformedPointerEvent with _CopyPointerPanZoomStartEvent implements PointerPanZoomStartEvent {
  _TransformedPointerPanZoomStartEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);

  @override
  final PointerPanZoomStartEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerPanZoomStartEvent transformed(Matrix4? transform) => original.transformed(transform);
}

mixin _CopyPointerPanZoomUpdateEvent on PointerEvent {
  /// The total pan offset of the pan/zoom.
  Offset get pan;
  /// The total pan offset of the pan/zoom, transformed into local coordinates.
  Offset get localPan;
  /// The amount the pan offset changed since the last event.
  Offset get panDelta;
  /// The amount the pan offset changed since the last event, transformed into local coordinates.
  Offset get localPanDelta;
  /// The scale (zoom factor) of the pan/zoom.
  double get scale;
  /// The amount the pan/zoom has rotated in radians so far.
  double get rotation;

  @override
  PointerPanZoomUpdateEvent copyWith({
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
    Offset? pan,
    Offset? localPan,
    Offset? panDelta,
    Offset? localPanDelta,
    double? scale,
    double? rotation,
  }) {
2148
    assert(kind == null || identical(kind, PointerDeviceKind.trackpad));
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
    return PointerPanZoomUpdateEvent(
      timeStamp: timeStamp ?? this.timeStamp,
      device: device ?? this.device,
      position: position ?? this.position,
      embedderId: embedderId ?? this.embedderId,
      pan: pan ?? this.pan,
      panDelta: panDelta ?? this.panDelta,
      scale: scale ?? this.scale,
      rotation: rotation ?? this.rotation,
    ).transformed(transform);
  }
}

/// The active pan/zoom on this pointer has updated.
///
/// See also:
///
///  * [Listener.onPointerPanZoomUpdate], which allows callers to be notified of these
///    events in a widget tree.
class PointerPanZoomUpdateEvent extends PointerEvent with _PointerEventDescription, _CopyPointerPanZoomUpdateEvent {
  /// Creates a pointer pan/zoom update event.
  ///
  /// All of the arguments must be non-null.
  const PointerPanZoomUpdateEvent({
2173 2174 2175 2176 2177
    super.timeStamp,
    super.device,
    super.pointer,
    super.position,
    super.embedderId,
2178 2179 2180 2181
    this.pan = Offset.zero,
    this.panDelta = Offset.zero,
    this.scale = 1.0,
    this.rotation = 0.0,
2182
    super.synthesized,
2183 2184 2185 2186 2187 2188 2189 2190 2191
  }) : assert(timeStamp != null),
       assert(device != null),
       assert(pointer != null),
       assert(position != null),
       assert(embedderId != null),
       assert(pan != null),
       assert(panDelta != null),
       assert(scale != null),
       assert(rotation != null),
2192 2193 2194
       assert(synthesized != null),
       super(kind: PointerDeviceKind.trackpad);

2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
  @override
  final Offset pan;
  @override
  Offset get localPan => pan;
  @override
  final Offset panDelta;
  @override
  Offset get localPanDelta => panDelta;
  @override
  final double scale;
  @override
  final double rotation;

  @override
  PointerPanZoomUpdateEvent transformed(Matrix4? transform) {
    if (transform == null || transform == this.transform) {
      return this;
    }
    return _TransformedPointerPanZoomUpdateEvent(original as PointerPanZoomUpdateEvent? ?? this, transform);
  }
}

class _TransformedPointerPanZoomUpdateEvent extends _TransformedPointerEvent with _CopyPointerPanZoomUpdateEvent implements PointerPanZoomUpdateEvent {
  _TransformedPointerPanZoomUpdateEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);

  @override
  Offset get pan => original.pan;

  @override
  late final Offset localPan = PointerEvent.transformPosition(transform, pan);

  @override
  Offset get panDelta => original.panDelta;

  @override
  late final Offset localPanDelta = PointerEvent.transformDeltaViaPositions(
    transform: transform,
    untransformedDelta: panDelta,
    untransformedEndPosition: pan,
    transformedEndPosition: localPan,
  );

  @override
  double get scale => original.scale;

  @override
  double get rotation => original.rotation;

  @override
  final PointerPanZoomUpdateEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerPanZoomUpdateEvent transformed(Matrix4? transform) => original.transformed(transform);
}

mixin _CopyPointerPanZoomEndEvent on PointerEvent {
  @override
  PointerPanZoomEndEvent copyWith({
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
2280
    assert(kind == null || identical(kind, PointerDeviceKind.trackpad));
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
    return PointerPanZoomEndEvent(
      timeStamp: timeStamp ?? this.timeStamp,
      device: device ?? this.device,
      position: position ?? this.position,
      embedderId: embedderId ?? this.embedderId,
    ).transformed(transform);
  }
}

/// The pan/zoom on this pointer has ended.
///
/// See also:
///
///  * [Listener.onPointerPanZoomEnd], which allows callers to be notified of these
///    events in a widget tree.
class PointerPanZoomEndEvent extends PointerEvent with _PointerEventDescription, _CopyPointerPanZoomEndEvent {
  /// Creates a pointer pan/zoom end event.
  ///
  /// All of the arguments must be non-null.
  const PointerPanZoomEndEvent({
2301 2302 2303 2304 2305 2306
    super.timeStamp,
    super.device,
    super.pointer,
    super.position,
    super.embedderId,
    super.synthesized,
2307 2308 2309 2310 2311
  }) : assert(timeStamp != null),
       assert(device != null),
       assert(pointer != null),
       assert(position != null),
       assert(embedderId != null),
2312 2313
       assert(synthesized != null),
       super(kind: PointerDeviceKind.trackpad);
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337

  @override
  PointerPanZoomEndEvent transformed(Matrix4? transform) {
    if (transform == null || transform == this.transform) {
      return this;
    }
    return _TransformedPointerPanZoomEndEvent(original as PointerPanZoomEndEvent? ?? this, transform);
  }
}

class _TransformedPointerPanZoomEndEvent extends _TransformedPointerEvent with _CopyPointerPanZoomEndEvent implements PointerPanZoomEndEvent {
  _TransformedPointerPanZoomEndEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);

  @override
  final PointerPanZoomEndEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerPanZoomEndEvent transformed(Matrix4? transform) => original.transformed(transform);
}

2338 2339 2340
mixin _CopyPointerCancelEvent on PointerEvent {
  @override
  PointerCancelEvent copyWith({
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363
    Duration? timeStamp,
    int? pointer,
    PointerDeviceKind? kind,
    int? device,
    Offset? position,
    Offset? delta,
    int? buttons,
    bool? obscured,
    double? pressure,
    double? pressureMin,
    double? pressureMax,
    double? distance,
    double? distanceMax,
    double? size,
    double? radiusMajor,
    double? radiusMinor,
    double? radiusMin,
    double? radiusMax,
    double? orientation,
    double? tilt,
    bool? synthesized,
    int? embedderId,
  }) {
2364
    return PointerCancelEvent(
2365
      timeStamp: timeStamp ?? this.timeStamp,
2366
      pointer: pointer ?? this.pointer,
2367 2368 2369
      kind: kind ?? this.kind,
      device: device ?? this.device,
      position: position ?? this.position,
2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
      buttons: buttons ?? this.buttons,
      obscured: obscured ?? this.obscured,
      pressureMin: pressureMin ?? this.pressureMin,
      pressureMax: pressureMax ?? this.pressureMax,
      distance: distance ?? this.distance,
      distanceMax: distanceMax ?? this.distanceMax,
      size: size ?? this.size,
      radiusMajor: radiusMajor ?? this.radiusMajor,
      radiusMinor: radiusMinor ?? this.radiusMinor,
      radiusMin: radiusMin ?? this.radiusMin,
      radiusMax: radiusMax ?? this.radiusMax,
      orientation: orientation ?? this.orientation,
      tilt: tilt ?? this.tilt,
2383
      embedderId: embedderId ?? this.embedderId,
2384
    ).transformed(transform);
2385 2386 2387
  }
}

2388
/// The input from the pointer is no longer directed towards this receiver.
2389 2390 2391 2392 2393
///
/// See also:
///
///  * [Listener.onPointerCancel], which allows callers to be notified of these
///    events in a widget tree.
2394
class PointerCancelEvent extends PointerEvent with _PointerEventDescription, _CopyPointerCancelEvent {
2395 2396
  /// Creates a pointer cancel event.
  ///
2397
  /// All of the arguments must be non-null.
Ian Hickson's avatar
Ian Hickson committed
2398
  const PointerCancelEvent({
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
    super.timeStamp,
    super.pointer,
    super.kind,
    super.device,
    super.position,
    super.buttons,
    super.obscured,
    super.pressureMin,
    super.pressureMax,
    super.distance,
    super.distanceMax,
    super.size,
    super.radiusMajor,
    super.radiusMinor,
    super.radiusMin,
    super.radiusMax,
    super.orientation,
    super.tilt,
    super.embedderId,
2418 2419
  }) : assert(!identical(kind, PointerDeviceKind.trackpad)),
       super(
2420
         down: false,
2421
         pressure: 0.0,
2422
       );
2423 2424

  @override
2425
  PointerCancelEvent transformed(Matrix4? transform) {
2426 2427 2428
    if (transform == null || transform == this.transform) {
      return this;
    }
2429
    return _TransformedPointerCancelEvent(original as PointerCancelEvent? ?? this, transform);
2430
  }
Ian Hickson's avatar
Ian Hickson committed
2431
}
2432

2433
/// Determine the appropriate hit slop pixels based on the [kind] of pointer.
2434
double computeHitSlop(PointerDeviceKind kind, DeviceGestureSettings? settings) {
2435 2436
  switch (kind) {
    case PointerDeviceKind.mouse:
2437
      return kPrecisePointerHitSlop;
2438 2439 2440 2441
    case PointerDeviceKind.stylus:
    case PointerDeviceKind.invertedStylus:
    case PointerDeviceKind.unknown:
    case PointerDeviceKind.touch:
2442
    case PointerDeviceKind.trackpad:
2443
      return settings?.touchSlop ?? kTouchSlop;
2444 2445 2446
  }
}

2447
/// Determine the appropriate pan slop pixels based on the [kind] of pointer.
2448
double computePanSlop(PointerDeviceKind kind, DeviceGestureSettings? settings) {
2449 2450
  switch (kind) {
    case PointerDeviceKind.mouse:
2451
      return kPrecisePointerPanSlop;
2452 2453 2454 2455
    case PointerDeviceKind.stylus:
    case PointerDeviceKind.invertedStylus:
    case PointerDeviceKind.unknown:
    case PointerDeviceKind.touch:
2456
    case PointerDeviceKind.trackpad:
2457
      return settings?.panSlop ?? kPanSlop;
2458 2459 2460
  }
}

2461
/// Determine the appropriate scale slop pixels based on the [kind] of pointer.
2462 2463 2464
double computeScaleSlop(PointerDeviceKind kind) {
  switch (kind) {
    case PointerDeviceKind.mouse:
2465
      return kPrecisePointerScaleSlop;
2466 2467 2468 2469
    case PointerDeviceKind.stylus:
    case PointerDeviceKind.invertedStylus:
    case PointerDeviceKind.unknown:
    case PointerDeviceKind.touch:
2470
    case PointerDeviceKind.trackpad:
2471 2472 2473
      return kScaleSlop;
  }
}
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487

class _TransformedPointerCancelEvent extends _TransformedPointerEvent with _CopyPointerCancelEvent implements PointerCancelEvent {
  _TransformedPointerCancelEvent(this.original, this.transform)
    : assert(original != null), assert(transform != null);

  @override
  final PointerCancelEvent original;

  @override
  final Matrix4 transform;

  @override
  PointerCancelEvent transformed(Matrix4? transform) => original.transformed(transform);
}