• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:ui' show Offset;
6
7import 'package:flutter/foundation.dart';
8
9import 'velocity_tracker.dart';
10
11/// Details object for callbacks that use [GestureDragDownCallback].
12///
13/// See also:
14///
15///  * [DragGestureRecognizer.onDown], which uses [GestureDragDownCallback].
16///  * [DragStartDetails], the details for [GestureDragStartCallback].
17///  * [DragUpdateDetails], the details for [GestureDragUpdateCallback].
18///  * [DragEndDetails], the details for [GestureDragEndCallback].
19class DragDownDetails {
20  /// Creates details for a [GestureDragDownCallback].
21  ///
22  /// The [globalPosition] argument must not be null.
23  DragDownDetails({
24    this.globalPosition = Offset.zero,
25    Offset localPosition,
26  }) : assert(globalPosition != null),
27       localPosition = localPosition ?? globalPosition;
28
29  /// The global position at which the pointer contacted the screen.
30  ///
31  /// Defaults to the origin if not specified in the constructor.
32  ///
33  /// See also:
34  ///
35  ///  * [localPosition], which is the [globalPosition] transformed to the
36  ///    coordinate space of the event receiver.
37  final Offset globalPosition;
38
39  /// The local position in the coordinate system of the event receiver at
40  /// which the pointer contacted the screen.
41  ///
42  /// Defaults to [globalPosition] if not specified in the constructor.
43  final Offset localPosition;
44
45  @override
46  String toString() => '$runtimeType($globalPosition)';
47}
48
49/// Signature for when a pointer has contacted the screen and might begin to
50/// move.
51///
52/// The `details` object provides the position of the touch.
53///
54/// See [DragGestureRecognizer.onDown].
55typedef GestureDragDownCallback = void Function(DragDownDetails details);
56
57/// Details object for callbacks that use [GestureDragStartCallback].
58///
59/// See also:
60///
61///  * [DragGestureRecognizer.onStart], which uses [GestureDragStartCallback].
62///  * [DragDownDetails], the details for [GestureDragDownCallback].
63///  * [DragUpdateDetails], the details for [GestureDragUpdateCallback].
64///  * [DragEndDetails], the details for [GestureDragEndCallback].
65class DragStartDetails {
66  /// Creates details for a [GestureDragStartCallback].
67  ///
68  /// The [globalPosition] argument must not be null.
69  DragStartDetails({
70    this.sourceTimeStamp,
71    this.globalPosition = Offset.zero,
72    Offset localPosition,
73  }) : assert(globalPosition != null),
74       localPosition = localPosition ?? globalPosition;
75
76  /// Recorded timestamp of the source pointer event that triggered the drag
77  /// event.
78  ///
79  /// Could be null if triggered from proxied events such as accessibility.
80  final Duration sourceTimeStamp;
81
82  /// The global position at which the pointer contacted the screen.
83  ///
84  /// Defaults to the origin if not specified in the constructor.
85  ///
86  /// See also:
87  ///
88  ///  * [localPosition], which is the [globalPosition] transformed to the
89  ///    coordinate space of the event receiver.
90  final Offset globalPosition;
91
92  /// The local position in the coordinate system of the event receiver at
93  /// which the pointer contacted the screen.
94  ///
95  /// Defaults to [globalPosition] if not specified in the constructor.
96  final Offset localPosition;
97
98  // TODO(ianh): Expose the current position, so that you can have a no-jump
99  // drag even when disambiguating (though of course it would lag the finger
100  // instead).
101
102  @override
103  String toString() => '$runtimeType($globalPosition)';
104}
105
106/// Signature for when a pointer has contacted the screen and has begun to move.
107///
108/// The `details` object provides the position of the touch when it first
109/// touched the surface.
110///
111/// See [DragGestureRecognizer.onStart].
112typedef GestureDragStartCallback = void Function(DragStartDetails details);
113
114/// Details object for callbacks that use [GestureDragUpdateCallback].
115///
116/// See also:
117///
118///  * [DragGestureRecognizer.onUpdate], which uses [GestureDragUpdateCallback].
119///  * [DragDownDetails], the details for [GestureDragDownCallback].
120///  * [DragStartDetails], the details for [GestureDragStartCallback].
121///  * [DragEndDetails], the details for [GestureDragEndCallback].
122class DragUpdateDetails {
123  /// Creates details for a [DragUpdateDetails].
124  ///
125  /// The [delta] argument must not be null.
126  ///
127  /// If [primaryDelta] is non-null, then its value must match one of the
128  /// coordinates of [delta] and the other coordinate must be zero.
129  ///
130  /// The [globalPosition] argument must be provided and must not be null.
131  DragUpdateDetails({
132    this.sourceTimeStamp,
133    this.delta = Offset.zero,
134    this.primaryDelta,
135    @required this.globalPosition,
136    Offset localPosition,
137  }) : assert(delta != null),
138       assert(primaryDelta == null
139           || (primaryDelta == delta.dx && delta.dy == 0.0)
140           || (primaryDelta == delta.dy && delta.dx == 0.0)),
141       localPosition = localPosition ?? globalPosition;
142
143  /// Recorded timestamp of the source pointer event that triggered the drag
144  /// event.
145  ///
146  /// Could be null if triggered from proxied events such as accessibility.
147  final Duration sourceTimeStamp;
148
149  /// The amount the pointer has moved in the coordinate space of the event
150  /// receiver since the previous update.
151  ///
152  /// If the [GestureDragUpdateCallback] is for a one-dimensional drag (e.g.,
153  /// a horizontal or vertical drag), then this offset contains only the delta
154  /// in that direction (i.e., the coordinate in the other direction is zero).
155  ///
156  /// Defaults to zero if not specified in the constructor.
157  final Offset delta;
158
159  /// The amount the pointer has moved along the primary axis in the coordinate
160  /// space of the event receiver since the previous
161  /// update.
162  ///
163  /// If the [GestureDragUpdateCallback] is for a one-dimensional drag (e.g.,
164  /// a horizontal or vertical drag), then this value contains the component of
165  /// [delta] along the primary axis (e.g., horizontal or vertical,
166  /// respectively). Otherwise, if the [GestureDragUpdateCallback] is for a
167  /// two-dimensional drag (e.g., a pan), then this value is null.
168  ///
169  /// Defaults to null if not specified in the constructor.
170  final double primaryDelta;
171
172  /// The pointer's global position when it triggered this update.
173  ///
174  /// See also:
175  ///
176  ///  * [localPosition], which is the [globalPosition] transformed to the
177  ///    coordinate space of the event receiver.
178  final Offset globalPosition;
179
180  /// The local position in the coordinate system of the event receiver at
181  /// which the pointer contacted the screen.
182  ///
183  /// Defaults to [globalPosition] if not specified in the constructor.
184  final Offset localPosition;
185
186  @override
187  String toString() => '$runtimeType($delta)';
188}
189
190/// Signature for when a pointer that is in contact with the screen and moving
191/// has moved again.
192///
193/// The `details` object provides the position of the touch and the distance it
194/// has traveled since the last update.
195///
196/// See [DragGestureRecognizer.onUpdate].
197typedef GestureDragUpdateCallback = void Function(DragUpdateDetails details);
198
199/// Details object for callbacks that use [GestureDragEndCallback].
200///
201/// See also:
202///
203///  * [DragGestureRecognizer.onEnd], which uses [GestureDragEndCallback].
204///  * [DragDownDetails], the details for [GestureDragDownCallback].
205///  * [DragStartDetails], the details for [GestureDragStartCallback].
206///  * [DragUpdateDetails], the details for [GestureDragUpdateCallback].
207class DragEndDetails {
208  /// Creates details for a [GestureDragEndCallback].
209  ///
210  /// The [velocity] argument must not be null.
211  DragEndDetails({
212    this.velocity = Velocity.zero,
213    this.primaryVelocity,
214  }) : assert(velocity != null),
215       assert(primaryVelocity == null
216           || primaryVelocity == velocity.pixelsPerSecond.dx
217           || primaryVelocity == velocity.pixelsPerSecond.dy);
218
219  /// The velocity the pointer was moving when it stopped contacting the screen.
220  ///
221  /// Defaults to zero if not specified in the constructor.
222  final Velocity velocity;
223
224  /// The velocity the pointer was moving along the primary axis when it stopped
225  /// contacting the screen, in logical pixels per second.
226  ///
227  /// If the [GestureDragEndCallback] is for a one-dimensional drag (e.g., a
228  /// horizontal or vertical drag), then this value contains the component of
229  /// [velocity] along the primary axis (e.g., horizontal or vertical,
230  /// respectively). Otherwise, if the [GestureDragEndCallback] is for a
231  /// two-dimensional drag (e.g., a pan), then this value is null.
232  ///
233  /// Defaults to null if not specified in the constructor.
234  final double primaryVelocity;
235
236  @override
237  String toString() => '$runtimeType($velocity)';
238}
239