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

import 'package:flutter/foundation.dart';

import 'velocity_tracker.dart';

9 10 11 12
export 'dart:ui' show Offset, PointerDeviceKind;

export 'velocity_tracker.dart' show Velocity;

13 14 15 16 17 18 19 20 21 22
/// Details object for callbacks that use [GestureDragDownCallback].
///
/// See also:
///
///  * [DragGestureRecognizer.onDown], which uses [GestureDragDownCallback].
///  * [DragStartDetails], the details for [GestureDragStartCallback].
///  * [DragUpdateDetails], the details for [GestureDragUpdateCallback].
///  * [DragEndDetails], the details for [GestureDragEndCallback].
class DragDownDetails {
  /// Creates details for a [GestureDragDownCallback].
23 24
  DragDownDetails({
    this.globalPosition = Offset.zero,
25
    Offset? localPosition,
26
  }) : localPosition = localPosition ?? globalPosition;
27 28 29 30

  /// The global position at which the pointer contacted the screen.
  ///
  /// Defaults to the origin if not specified in the constructor.
31 32 33 34 35
  ///
  /// See also:
  ///
  ///  * [localPosition], which is the [globalPosition] transformed to the
  ///    coordinate space of the event receiver.
36 37
  final Offset globalPosition;

38 39 40 41 42 43
  /// The local position in the coordinate system of the event receiver at
  /// which the pointer contacted the screen.
  ///
  /// Defaults to [globalPosition] if not specified in the constructor.
  final Offset localPosition;

44
  @override
45
  String toString() => '${objectRuntimeType(this, 'DragDownDetails')}($globalPosition)';
46 47 48 49 50 51 52 53
}

/// Signature for when a pointer has contacted the screen and might begin to
/// move.
///
/// The `details` object provides the position of the touch.
///
/// See [DragGestureRecognizer.onDown].
54
typedef GestureDragDownCallback = void Function(DragDownDetails details);
55 56 57 58 59 60 61 62 63 64 65

/// Details object for callbacks that use [GestureDragStartCallback].
///
/// See also:
///
///  * [DragGestureRecognizer.onStart], which uses [GestureDragStartCallback].
///  * [DragDownDetails], the details for [GestureDragDownCallback].
///  * [DragUpdateDetails], the details for [GestureDragUpdateCallback].
///  * [DragEndDetails], the details for [GestureDragEndCallback].
class DragStartDetails {
  /// Creates details for a [GestureDragStartCallback].
66 67 68
  DragStartDetails({
    this.sourceTimeStamp,
    this.globalPosition = Offset.zero,
69
    Offset? localPosition,
70
    this.kind,
71
  }) : localPosition = localPosition ?? globalPosition;
72

73 74 75 76
  /// Recorded timestamp of the source pointer event that triggered the drag
  /// event.
  ///
  /// Could be null if triggered from proxied events such as accessibility.
77
  final Duration? sourceTimeStamp;
78

79 80 81
  /// The global position at which the pointer contacted the screen.
  ///
  /// Defaults to the origin if not specified in the constructor.
82 83 84 85 86
  ///
  /// See also:
  ///
  ///  * [localPosition], which is the [globalPosition] transformed to the
  ///    coordinate space of the event receiver.
87 88
  final Offset globalPosition;

89 90 91 92 93 94
  /// The local position in the coordinate system of the event receiver at
  /// which the pointer contacted the screen.
  ///
  /// Defaults to [globalPosition] if not specified in the constructor.
  final Offset localPosition;

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

98 99 100 101
  // TODO(ianh): Expose the current position, so that you can have a no-jump
  // drag even when disambiguating (though of course it would lag the finger
  // instead).

102
  @override
103
  String toString() => '${objectRuntimeType(this, 'DragStartDetails')}($globalPosition)';
104 105
}

106
/// {@template flutter.gestures.dragdetails.GestureDragStartCallback}
107 108 109 110
/// Signature for when a pointer has contacted the screen and has begun to move.
///
/// The `details` object provides the position of the touch when it first
/// touched the surface.
111
/// {@endtemplate}
112 113
///
/// See [DragGestureRecognizer.onStart].
114
typedef GestureDragStartCallback = void Function(DragStartDetails details);
115 116 117 118 119 120 121 122 123 124

/// Details object for callbacks that use [GestureDragUpdateCallback].
///
/// See also:
///
///  * [DragGestureRecognizer.onUpdate], which uses [GestureDragUpdateCallback].
///  * [DragDownDetails], the details for [GestureDragDownCallback].
///  * [DragStartDetails], the details for [GestureDragStartCallback].
///  * [DragEndDetails], the details for [GestureDragEndCallback].
class DragUpdateDetails {
125
  /// Creates details for a [GestureDragUpdateCallback].
126 127 128 129
  ///
  /// If [primaryDelta] is non-null, then its value must match one of the
  /// coordinates of [delta] and the other coordinate must be zero.
  DragUpdateDetails({
130
    this.sourceTimeStamp,
131
    this.delta = Offset.zero,
132
    this.primaryDelta,
133 134
    required this.globalPosition,
    Offset? localPosition,
135
  }) : assert(
136
         primaryDelta == null
137
           || (primaryDelta == delta.dx && delta.dy == 0.0)
138 139
           || (primaryDelta == delta.dy && delta.dx == 0.0),
       ),
140
       localPosition = localPosition ?? globalPosition;
141

142 143 144 145
  /// Recorded timestamp of the source pointer event that triggered the drag
  /// event.
  ///
  /// Could be null if triggered from proxied events such as accessibility.
146
  final Duration? sourceTimeStamp;
147

148 149
  /// The amount the pointer has moved in the coordinate space of the event
  /// receiver since the previous update.
150 151 152 153 154 155 156 157
  ///
  /// If the [GestureDragUpdateCallback] is for a one-dimensional drag (e.g.,
  /// a horizontal or vertical drag), then this offset contains only the delta
  /// in that direction (i.e., the coordinate in the other direction is zero).
  ///
  /// Defaults to zero if not specified in the constructor.
  final Offset delta;

158 159
  /// The amount the pointer has moved along the primary axis in the coordinate
  /// space of the event receiver since the previous
160 161 162 163 164 165 166 167 168
  /// update.
  ///
  /// If the [GestureDragUpdateCallback] is for a one-dimensional drag (e.g.,
  /// a horizontal or vertical drag), then this value contains the component of
  /// [delta] along the primary axis (e.g., horizontal or vertical,
  /// respectively). Otherwise, if the [GestureDragUpdateCallback] is for a
  /// two-dimensional drag (e.g., a pan), then this value is null.
  ///
  /// Defaults to null if not specified in the constructor.
169
  final double? primaryDelta;
170 171

  /// The pointer's global position when it triggered this update.
172 173 174 175 176
  ///
  /// See also:
  ///
  ///  * [localPosition], which is the [globalPosition] transformed to the
  ///    coordinate space of the event receiver.
177
  final Offset globalPosition;
178 179 180 181 182 183

  /// The local position in the coordinate system of the event receiver at
  /// which the pointer contacted the screen.
  ///
  /// Defaults to [globalPosition] if not specified in the constructor.
  final Offset localPosition;
184 185

  @override
186
  String toString() => '${objectRuntimeType(this, 'DragUpdateDetails')}($delta)';
187 188
}

189
/// {@template flutter.gestures.dragdetails.GestureDragUpdateCallback}
190 191 192 193
/// Signature for when a pointer that is in contact with the screen and moving
/// has moved again.
///
/// The `details` object provides the position of the touch and the distance it
194
/// has traveled since the last update.
195
/// {@endtemplate}
196 197
///
/// See [DragGestureRecognizer.onUpdate].
198
typedef GestureDragUpdateCallback = void Function(DragUpdateDetails details);
199 200 201 202 203 204 205 206 207 208 209 210

/// Details object for callbacks that use [GestureDragEndCallback].
///
/// See also:
///
///  * [DragGestureRecognizer.onEnd], which uses [GestureDragEndCallback].
///  * [DragDownDetails], the details for [GestureDragDownCallback].
///  * [DragStartDetails], the details for [GestureDragStartCallback].
///  * [DragUpdateDetails], the details for [GestureDragUpdateCallback].
class DragEndDetails {
  /// Creates details for a [GestureDragEndCallback].
  ///
211 212 213
  /// If [primaryVelocity] is non-null, its value must match one of the
  /// coordinates of `velocity.pixelsPerSecond` and the other coordinate
  /// must be zero.
214
  DragEndDetails({
215
    this.velocity = Velocity.zero,
216
    this.primaryVelocity,
217
  }) : assert(
218
         primaryVelocity == null
219 220
           || (primaryVelocity == velocity.pixelsPerSecond.dx && velocity.pixelsPerSecond.dy == 0)
           || (primaryVelocity == velocity.pixelsPerSecond.dy && velocity.pixelsPerSecond.dx == 0),
221
       );
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

  /// The velocity the pointer was moving when it stopped contacting the screen.
  ///
  /// Defaults to zero if not specified in the constructor.
  final Velocity velocity;

  /// The velocity the pointer was moving along the primary axis when it stopped
  /// contacting the screen, in logical pixels per second.
  ///
  /// If the [GestureDragEndCallback] is for a one-dimensional drag (e.g., a
  /// horizontal or vertical drag), then this value contains the component of
  /// [velocity] along the primary axis (e.g., horizontal or vertical,
  /// respectively). Otherwise, if the [GestureDragEndCallback] is for a
  /// two-dimensional drag (e.g., a pan), then this value is null.
  ///
  /// Defaults to null if not specified in the constructor.
238
  final double? primaryVelocity;
239 240

  @override
241
  String toString() => '${objectRuntimeType(this, 'DragEndDetails')}($velocity)';
242
}