forked from flutter/flutter
/
events.dart
2148 lines (1975 loc) · 70 KB
/
events.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
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
617
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
661
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui' show Offset, PointerDeviceKind;
import 'package:flutter/foundation.dart';
import 'package:vector_math/vector_math_64.dart';
import 'constants.dart';
export 'dart:ui' show Offset, PointerDeviceKind;
/// The bit of [PointerEvent.buttons] that corresponds to a cross-device
/// behavior of "primary operation".
///
/// More specifically, it includes:
///
/// * [kTouchContact]: The pointer contacts the touch screen.
/// * [kStylusContact]: The stylus contacts the screen.
/// * [kPrimaryMouseButton]: The primary mouse button.
///
/// See also:
///
/// * [kSecondaryButton], which describes a cross-device behavior of
/// "secondary operation".
/// * [kTertiaryButton], which describes a cross-device behavior of
/// "tertiary operation".
const int kPrimaryButton = 0x01;
/// 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.
/// * [kSecondaryMouseButton]: The secondary mouse button.
///
/// See also:
///
/// * [kPrimaryButton], which describes a cross-device behavior of
/// "primary operation".
/// * [kTertiaryButton], which describes a cross-device behavior of
/// "tertiary operation".
const int kSecondaryButton = 0x02;
/// 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.
///
/// See also:
///
/// * [kPrimaryButton], which has the same value but describes its cross-device
/// concept.
const int kPrimaryMouseButton = kPrimaryButton;
/// 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.
///
/// See also:
///
/// * [kSecondaryButton], which has the same value but describes its cross-device
/// concept.
const int kSecondaryMouseButton = kSecondaryButton;
/// The bit of [PointerEvent.buttons] that corresponds to when a stylus
/// contacting the screen.
///
/// See also:
///
/// * [kPrimaryButton], which has the same value but describes its cross-device
/// concept.
const int kStylusContact = kPrimaryButton;
/// 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.
///
/// See also:
///
/// * [kSecondaryButton], which has the same value but describes its cross-device
/// concept.
const int kPrimaryStylusButton = kSecondaryButton;
/// 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;
/// 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.
///
/// See also:
///
/// * [kTertiaryButton], which has the same value but describes its cross-device
/// concept.
const int kMiddleMouseButton = kTertiaryButton;
/// The bit of [PointerEvent.buttons] that corresponds to the secondary stylus button.
///
/// The secondary stylus button is typically on the end of the stylus farthest
/// from the tip but can be reconfigured to be a different physical button.
///
/// See also:
///
/// * [kTertiaryButton], which has the same value but describes its cross-device
/// concept.
const int kSecondaryStylusButton = kTertiaryButton;
/// 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.
const int kBackMouseButton = 0x08;
/// 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;
/// The bit of [PointerEvent.buttons] that corresponds to the pointer contacting
/// a touch screen.
///
/// See also:
///
/// * [kPrimaryButton], which has the same value but describes its cross-device
/// concept.
const int kTouchContact = kPrimaryButton;
/// The bit of [PointerEvent.buttons] that corresponds to the nth mouse button.
///
/// The `number` argument can be at most 62 in Flutter for mobile and desktop,
/// and at most 32 on Flutter for web.
///
/// 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.
///
/// The `number` argument can be at most 62 in Flutter for mobile and desktop,
/// and at most 32 on Flutter for web.
///
/// See [kPrimaryStylusButton] and [kSecondaryStylusButton] for semantic names
/// for some stylus buttons.
int nthStylusButton(int number) => (kPrimaryStylusButton << (number - 1)) & kMaxUnsignedSMI;
/// Returns the button of `buttons` with the smallest integer.
///
/// The `buttons` parameter is a bit field where each set bit represents a button.
/// This function returns the set bit closest to the least significant bit.
///
/// It returns zero when `buttons` is zero.
///
/// Example:
///
/// ```dart
/// assert(rightmostButton(0x1) == 0x1);
/// assert(rightmostButton(0x11) == 0x1);
/// assert(rightmostButton(0) == 0);
/// ```
///
/// See also:
///
/// * [isSingleButton], which checks if a `buttons` contains exactly one button.
int smallestButton(int buttons) => buttons & (-buttons);
/// Returns whether `buttons` contains one and only one button.
///
/// The `buttons` parameter is a bit field where each set bit represents a button.
/// 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:
///
/// * [smallestButton], which returns the button in a `buttons` bit field with
/// the smallest integer button.
bool isSingleButton(int buttons) => buttons != 0 && (smallestButton(buttons) == buttons);
/// Base class for touch, stylus, or mouse events.
///
/// 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.
///
/// This allows gestures to be recognized independent of the precise hardware
/// 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:
///
/// * [dart:ui.FlutterView.devicePixelRatio], which defines the device's
/// current resolution.
/// * [Listener], a widget that calls callbacks in response to common pointer
/// events.
@immutable
abstract class PointerEvent with Diagnosticable {
/// Abstract const constructor. This constructor enables subclasses to provide
/// const constructors so that they can be used in const expressions.
const PointerEvent({
this.embedderId = 0,
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,
this.size = 0.0,
this.radiusMajor = 0.0,
this.radiusMinor = 0.0,
this.radiusMin = 0.0,
this.radiusMax = 0.0,
this.orientation = 0.0,
this.tilt = 0.0,
this.platformData = 0,
this.synthesized = false,
this.transform,
this.original,
});
/// 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;
/// Time of event dispatch, relative to an arbitrary timeline.
final Duration timeStamp;
/// Unique identifier for the pointer, not reused. Changes for each new
/// pointer down event.
final int pointer;
/// The kind of input device for which the event was generated.
final PointerDeviceKind kind;
/// Unique identifier for the pointing device, reused across interactions.
final int device;
/// Coordinate of the position of the pointer, in logical pixels in the global
/// coordinate space.
///
/// See also:
///
/// * [localPosition], which is the [position] transformed into the local
/// coordinate system of the event receiver.
final Offset position;
/// 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:
///
/// * [position], which is the position in the global coordinate system of
/// the screen.
Offset get localPosition => position;
/// Distance in logical pixels that the pointer moved since the last
/// [PointerMoveEvent] or [PointerHoverEvent].
///
/// This value is always 0.0 for down, up, and cancel events.
///
/// See also:
///
/// * [localDelta], which is the [delta] transformed into the local
/// coordinate space of the event receiver.
final Offset delta;
/// 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.
Offset get localDelta => delta;
/// Bit field using the *Button constants such as [kPrimaryMouseButton],
/// [kSecondaryStylusButton], etc.
///
/// For example, if this has the value 6 and the
/// [kind] is [PointerDeviceKind.invertedStylus], then this indicates an
/// upside-down stylus with both its primary and secondary buttons pressed.
final int buttons;
/// 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.
final bool down;
/// Set if an application from a different security domain is in any way
/// obscuring this application's window.
///
/// This is not currently implemented.
final bool obscured;
/// 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.
final double pressure;
/// 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.
final double pressureMin;
/// 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.
final double pressureMax;
/// 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.
final double distance;
/// The minimum value that [distance] can return for this pointer.
///
/// This value is always 0.0.
double get distanceMin => 0.0;
/// 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.
final double distanceMax;
/// 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].
final double size;
/// The radius of the contact ellipse along the major axis, in logical pixels.
final double radiusMajor;
/// The radius of the contact ellipse along the minor axis, in logical pixels.
final double radiusMinor;
/// The minimum value that could be reported for [radiusMajor] and [radiusMinor]
/// for this pointer, in logical pixels.
final double radiusMin;
/// The maximum value that could be reported for [radiusMajor] and [radiusMinor]
/// for this pointer, in logical pixels.
final double radiusMax;
/// The orientation angle of the detected object, in radians.
///
/// For [PointerDeviceKind.touch] events:
///
/// The angle of the contact ellipse, in radians in the range:
///
/// -pi/2 < orientation <= pi/2
///
/// ...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).
///
/// For [PointerDeviceKind.stylus] and [PointerDeviceKind.invertedStylus] events:
///
/// The angle of the stylus, in radians in the range:
///
/// -pi < orientation <= pi
///
/// ...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).
final double orientation;
/// The tilt angle of the detected object, in radians.
///
/// For [PointerDeviceKind.stylus] and [PointerDeviceKind.invertedStylus] events:
///
/// The angle of the stylus, in radians in the range:
///
/// 0 <= tilt <= pi/2
///
/// ...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;
/// Opaque platform-specific data associated with the event.
final int platformData;
/// Set if the event was synthesized by Flutter.
///
/// We occasionally synthesize PointerEvents that aren't exact translations
/// of [PointerData] from the engine to cover small cross-OS discrepancies
/// in pointer behaviors.
///
/// For instance, on end events, Android always drops any location changes
/// that happened between its reporting intervals when emitting the end events.
///
/// 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;
/// 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.
final Matrix4? transform;
/// 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.
final PointerEvent? original;
/// 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.
///
/// 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.
///
/// 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].
PointerEvent transformed(Matrix4? transform);
/// Creates a copy of event with the specified properties replaced.
///
/// Calling this method on a transformed event will return a new transformed
/// event based on the current [transform] and the provided properties.
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,
});
/// 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.
static Offset transformPosition(Matrix4? transform, Offset position) {
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({
required Offset untransformedEndPosition,
Offset? transformedEndPosition,
required Offset untransformedDelta,
required Matrix4? transform,
}) {
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);
}
}
// 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);
}
}
abstract class _AbstractPointerEvent implements PointerEvent {}
// 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);
}
}
/// 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.
class PointerAddedEvent extends PointerEvent with _PointerEventDescription, _CopyPointerAddedEvent {
/// Creates a pointer added event.
///
/// All of the arguments must be non-null.
const PointerAddedEvent({
Duration timeStamp = Duration.zero,
int pointer = 0,
PointerDeviceKind kind = PointerDeviceKind.touch,
int device = 0,
Offset position = Offset.zero,
bool obscured = false,
double pressureMin = 1.0,
double pressureMax = 1.0,
double distance = 0.0,
double distanceMax = 0.0,
double radiusMin = 0.0,
double radiusMax = 0.0,
double orientation = 0.0,
double tilt = 0.0,
int embedderId = 0,
}) : super(
timeStamp: timeStamp,
pointer: pointer,
kind: kind,
device: device,
position: position,
obscured: obscured,
pressure: 0.0,
pressureMin: pressureMin,
pressureMax: pressureMax,
distance: distance,
distanceMax: distanceMax,
radiusMin: radiusMin,
radiusMax: radiusMax,
orientation: orientation,
tilt: tilt,
embedderId: embedderId,
);
@override
PointerAddedEvent transformed(Matrix4? transform) {
if (transform == null || transform == this.transform) {
return this;
}
return _TransformedPointerAddedEvent(original as PointerAddedEvent? ?? this, transform);
}
}
class _TransformedPointerAddedEvent extends _TransformedPointerEvent with _CopyPointerAddedEvent implements PointerAddedEvent {
_TransformedPointerAddedEvent(this.original, this.transform)
: assert(original != null), assert(transform != null);
@override
final PointerAddedEvent original;
@override
final Matrix4 transform;
@override
PointerAddedEvent transformed(Matrix4? transform) => original.transformed(transform);
}
mixin _CopyPointerRemovedEvent on PointerEvent {
@override
PointerRemovedEvent 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 PointerRemovedEvent(
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,
).transformed(transform);
}
}
/// 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.
class PointerRemovedEvent extends PointerEvent with _PointerEventDescription, _CopyPointerRemovedEvent {
/// Creates a pointer removed event.
///
/// All of the arguments must be non-null.
const PointerRemovedEvent({
Duration timeStamp = Duration.zero,
int pointer = 0,
PointerDeviceKind kind = PointerDeviceKind.touch,
int device = 0,
Offset position = Offset.zero,
bool obscured = false,
double pressureMin = 1.0,
double pressureMax = 1.0,
double distanceMax = 0.0,
double radiusMin = 0.0,
double radiusMax = 0.0,
PointerRemovedEvent? original,
int embedderId = 0,
}) : super(
timeStamp: timeStamp,
pointer: pointer,
kind: kind,
device: device,
position: position,
obscured: obscured,
pressure: 0.0,
pressureMin: pressureMin,
pressureMax: pressureMax,
distanceMax: distanceMax,
radiusMin: radiusMin,
radiusMax: radiusMax,
original: original,
embedderId: embedderId,
);
@override
PointerRemovedEvent transformed(Matrix4? transform) {
if (transform == null || transform == this.transform) {
return this;
}
return _TransformedPointerRemovedEvent(original as PointerRemovedEvent? ?? this, transform);
}
}
class _TransformedPointerRemovedEvent extends _TransformedPointerEvent with _CopyPointerRemovedEvent implements PointerRemovedEvent {
_TransformedPointerRemovedEvent(this.original, this.transform)
: assert(original != null), assert(transform != null);
@override
final PointerRemovedEvent original;
@override
final Matrix4 transform;
@override
PointerRemovedEvent transformed(Matrix4? transform) => original.transformed(transform);
}
mixin _CopyPointerHoverEvent on PointerEvent {
@override
PointerHoverEvent 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 PointerHoverEvent(
timeStamp: timeStamp ?? this.timeStamp,
kind: kind ?? this.kind,
device: device ?? this.device,