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

5
import 'dart:collection' show LinkedHashMap;
6 7
import 'dart:ui';

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

11 12
import 'package:vector_math/vector_math_64.dart' show Matrix4;

13 14 15
import 'mouse_cursor.dart';
import 'object.dart';

16 17
/// Signature for listening to [PointerEnterEvent] events.
///
18
/// Used by [MouseTrackerAnnotation], [MouseRegion] and [RenderMouseRegion].
19 20 21 22
typedef PointerEnterEventListener = void Function(PointerEnterEvent event);

/// Signature for listening to [PointerExitEvent] events.
///
23
/// Used by [MouseTrackerAnnotation], [MouseRegion] and [RenderMouseRegion].
24 25 26 27
typedef PointerExitEventListener = void Function(PointerExitEvent event);

/// Signature for listening to [PointerHoverEvent] events.
///
28
/// Used by [MouseTrackerAnnotation], [MouseRegion] and [RenderMouseRegion].
29 30
typedef PointerHoverEventListener = void Function(PointerHoverEvent event);

31
/// The annotation object used to annotate regions that are interested in mouse
32 33
/// movements.
///
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/// To use an annotation, push it with [AnnotatedRegionLayer] during painting.
/// The annotation's callbacks or configurations will be used depending on the
/// relationship between annotations and mouse pointers.
///
/// A [RenderObject] who uses this class must not dispose this class in its
/// `detach`, even if it recreates a new one in `attach`, because the object
/// might be detached and attached during the same frame during a reparent, and
/// replacing the `MouseTrackerAnnotation` will cause an unnecessary `onExit` and
/// `onEnter`.
///
/// This class is also the type parameter of the annotation search started by
/// [BaseMouseTracker].
///
/// See also:
///
///  * [BaseMouseTracker], which uses [MouseTrackerAnnotation].
50
class MouseTrackerAnnotation with Diagnosticable {
51
  /// Creates an immutable [MouseTrackerAnnotation].
52 53
  ///
  /// All arguments are optional. The [cursor] must not be null.
54 55 56
  const MouseTrackerAnnotation({
    this.onEnter,
    this.onExit,
57 58
    this.cursor = MouseCursor.defer,
  }) : assert(cursor != null);
59

60
  /// Triggered when a mouse pointer, with or without buttons pressed, has
61
  /// entered the region.
62
  ///
63 64 65 66
  /// This callback is triggered when the pointer has started to be contained by
  /// the region, either due to a pointer event, or due to the movement or
  /// disappearance of the region. This method is always matched by a later
  /// [onExit].
67 68 69 70
  ///
  /// See also:
  ///
  ///  * [onExit], which is triggered when a mouse pointer exits the region.
71
  ///  * [MouseRegion.onEnter], which uses this callback.
72
  final PointerEnterEventListener? onEnter;
73

74
  /// Triggered when a mouse pointer, with or without buttons pressed, has
75
  /// exited the region.
76
  ///
77
  /// This callback is triggered when the pointer has stopped being contained
78 79
  /// by the region, either due to a pointer event, or due to the movement or
  /// disappearance of the region. This method always matches an earlier
80 81 82 83 84
  /// [onEnter].
  ///
  /// See also:
  ///
  ///  * [onEnter], which is triggered when a mouse pointer enters the region.
85
  ///  * [MouseRegion.onExit], which uses this callback, but is not triggered in
86
  ///    certain cases and does not always match its earlier [MouseRegion.onEnter].
87
  final PointerExitEventListener? onExit;
88

89 90 91 92 93
  /// The mouse cursor for mouse pointers that are hovering over the region.
  ///
  /// When a mouse enters the region, its cursor will be changed to the [cursor].
  /// When the mouse leaves the region, the cursor will be set by the region
  /// found at the new location.
94
  ///
95
  /// Defaults to [MouseCursor.defer], deferring the choice of cursor to the next
96
  /// region behind it in hit-test order.
97 98 99 100 101 102
  ///
  /// See also:
  ///
  ///  * [MouseRegion.cursor], which provide values to this field.
  final MouseCursor cursor;

103
  @override
104 105
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
106
    properties.add(FlagsSummary<Function?>(
107
      'callbacks',
108
      <String, Function?> {
109 110 111 112 113
        'enter': onEnter,
        'exit': onExit,
      },
      ifEmpty: '<none>',
    ));
114
    properties.add(DiagnosticsProperty<MouseCursor>('cursor', cursor, defaultValue: MouseCursor.defer));
115 116 117
  }
}

118
/// Signature for searching for [MouseTrackerAnnotation]s at the given offset.
119
///
120
/// It is used by the [BaseMouseTracker] to fetch annotations for the mouse
121
/// position.
122
typedef MouseDetectorAnnotationFinder = HitTestResult Function(Offset offset);
123

124
// Various states of a connected mouse device used by [BaseMouseTracker].
125 126
class _MouseState {
  _MouseState({
127
    required PointerEvent initialEvent,
128 129
  }) : assert(initialEvent != null),
       _latestEvent = initialEvent;
130

131
  // The list of annotations that contains this device.
132
  //
133 134 135
  // It uses [LinkedHashMap] to keep the insertion order.
  LinkedHashMap<MouseTrackerAnnotation, Matrix4> get annotations => _annotations;
  LinkedHashMap<MouseTrackerAnnotation, Matrix4> _annotations = LinkedHashMap<MouseTrackerAnnotation, Matrix4>();
136

137
  LinkedHashMap<MouseTrackerAnnotation, Matrix4> replaceAnnotations(LinkedHashMap<MouseTrackerAnnotation, Matrix4> value) {
138
    assert(value != null);
139
    final LinkedHashMap<MouseTrackerAnnotation, Matrix4> previous = _annotations;
140 141 142 143 144 145 146
    _annotations = value;
    return previous;
  }

  // The most recently processed mouse event observed from this device.
  PointerEvent get latestEvent => _latestEvent;
  PointerEvent _latestEvent;
147 148

  PointerEvent replaceLatestEvent(PointerEvent value) {
149
    assert(value != null);
150 151
    assert(value.device == _latestEvent.device);
    final PointerEvent previous = _latestEvent;
152
    _latestEvent = value;
153
    return previous;
154 155
  }

156
  int get device => latestEvent.device;
157 158 159

  @override
  String toString() {
160
    final String describeLatestEvent = 'latestEvent: ${describeIdentity(latestEvent)}';
161 162
    final String describeAnnotations = 'annotations: [list of ${annotations.length}]';
    return '${describeIdentity(this)}($describeLatestEvent, $describeAnnotations)';
163 164 165
  }
}

166 167 168 169 170 171 172 173 174 175 176 177
/// Used by [BaseMouseTracker] to provide the details of an update of a mouse
/// device.
///
/// This class contains the information needed to handle the update that might
/// change the state of a mouse device, or the [MouseTrackerAnnotation]s that
/// the mouse device is hovering.
@immutable
class MouseTrackerUpdateDetails with Diagnosticable {
  /// When device update is triggered by a new frame.
  ///
  /// All parameters are required.
  const MouseTrackerUpdateDetails.byNewFrame({
178 179 180
    required this.lastAnnotations,
    required this.nextAnnotations,
    required this.previousEvent,
181 182 183 184 185 186 187 188 189 190
  }) : assert(previousEvent != null),
       assert(lastAnnotations != null),
       assert(nextAnnotations != null),
       triggeringEvent = null;

  /// When device update is triggered by a pointer event.
  ///
  /// The [lastAnnotations], [nextAnnotations], and [triggeringEvent] are
  /// required.
  const MouseTrackerUpdateDetails.byPointerEvent({
191 192
    required this.lastAnnotations,
    required this.nextAnnotations,
193
    this.previousEvent,
194
    required PointerEvent this.triggeringEvent,
195 196 197 198 199 200 201
  }) : assert(triggeringEvent != null),
       assert(lastAnnotations != null),
       assert(nextAnnotations != null);

  /// The annotations that the device is hovering before the update.
  ///
  /// It is never null.
202
  final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations;
203 204 205 206

  /// The annotations that the device is hovering after the update.
  ///
  /// It is never null.
207
  final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations;
208 209 210 211 212 213 214 215 216

  /// The last event that the device observed before the update.
  ///
  /// If the update is triggered by a frame, the [previousEvent] is never null,
  /// since the pointer must have been added before.
  ///
  /// If the update is triggered by a pointer event, the [previousEvent] is not
  /// null except for cases where the event is the first event observed by the
  /// pointer (which is not necessarily a [PointerAddedEvent]).
217
  final PointerEvent? previousEvent;
218 219 220 221

  /// The event that triggered this update.
  ///
  /// It is non-null if and only if the update is triggered by a pointer event.
222
  final PointerEvent? triggeringEvent;
223 224 225

  /// The pointing device of this update.
  int get device {
226
    final int result = (previousEvent ?? triggeringEvent)!.device;
227 228 229 230 231 232 233 234
    assert(result != null);
    return result;
  }

  /// The last event that the device observed after the update.
  ///
  /// The [latestEvent] is never null.
  PointerEvent get latestEvent {
235
    final PointerEvent result = triggeringEvent ?? previousEvent!;
236 237 238 239 240 241 242 243 244 245
    assert(result != null);
    return result;
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(IntProperty('device', device));
    properties.add(DiagnosticsProperty<PointerEvent>('previousEvent', previousEvent));
    properties.add(DiagnosticsProperty<PointerEvent>('triggeringEvent', triggeringEvent));
246 247
    properties.add(DiagnosticsProperty<Map<MouseTrackerAnnotation, Matrix4>>('lastAnnotations', lastAnnotations));
    properties.add(DiagnosticsProperty<Map<MouseTrackerAnnotation, Matrix4>>('nextAnnotations', nextAnnotations));
248 249 250 251 252 253
  }
}

/// A base class that tracks the relationship between mouse devices and
/// [MouseTrackerAnnotation]s.
///
254 255 256 257 258 259 260
/// An event (not necessarily a pointer event) that might change the relationship
/// between mouse devices and [MouseTrackerAnnotation]s is called a _device
/// update_.
///
/// [MouseTracker] is notified of device updates by [updateWithEvent] or
/// [updateAllDevices], and processes effects as defined in [handleDeviceUpdate]
/// by subclasses.
261
///
262 263 264
/// This class is a [ChangeNotifier] that notifies its listeners if the value of
/// [mouseIsConnected] changes.
///
265 266 267 268
/// See also:
///
///   * [MouseTracker], which is a subclass of [BaseMouseTracker] with definition
///     of how to process mouse event callbacks and mouse cursors.
269 270
///   * [MouseTrackerCursorMixin], which is a mixin for [BaseMouseTracker] that
///     defines how to process mouse cursors.
271
abstract class BaseMouseTracker extends ChangeNotifier {
272 273 274
  /// Whether or not at least one mouse is connected and has produced events.
  bool get mouseIsConnected => _mouseStates.isNotEmpty;

275 276
  // Tracks the state of connected mouse devices.
  //
277 278 279 280 281
  // It is the source of truth for the list of connected mouse devices, and is
  // consists of two parts:
  //
  //  * The mouse devices that are connected.
  //  * In which annotations each device is contained.
282 283
  final Map<int, _MouseState> _mouseStates = <int, _MouseState>{};

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
  // Used to wrap any procedure that might change `mouseIsConnected`.
  //
  // This method records `mouseIsConnected`, runs `task`, and calls
  // [notifyListeners] at the end if the `mouseIsConnected` has changed.
  void _monitorMouseConnection(VoidCallback task) {
    final bool mouseWasConnected = mouseIsConnected;
    task();
    if (mouseWasConnected != mouseIsConnected)
      notifyListeners();
  }

  bool _debugDuringDeviceUpdate = false;
  // Used to wrap any procedure that might call [handleDeviceUpdate].
  //
  // In debug mode, this method uses `_debugDuringDeviceUpdate` to prevent
  // `_deviceUpdatePhase` being recursively called.
  void _deviceUpdatePhase(VoidCallback task) {
    assert(!_debugDuringDeviceUpdate);
    assert(() {
      _debugDuringDeviceUpdate = true;
      return true;
    }());
    task();
    assert(() {
      _debugDuringDeviceUpdate = false;
      return true;
    }());
  }

313
  // Whether an observed event might update a device.
314
  static bool _shouldMarkStateDirty(_MouseState? state, PointerEvent event) {
315 316
    if (state == null)
      return true;
317
    assert(event != null);
318
    final PointerEvent lastEvent = state.latestEvent;
319
    assert(event.device == lastEvent.device);
320 321
    // An Added can only follow a Removed, and a Removed can only be followed
    // by an Added.
322
    assert((event is PointerAddedEvent) == (lastEvent is PointerRemovedEvent));
323 324

    // Ignore events that are unrelated to mouse tracking.
325
    if (event is PointerSignalEvent)
326 327
      return false;
    return lastEvent is PointerAddedEvent
328 329 330 331
      || event is PointerRemovedEvent
      || lastEvent.position != event.position;
  }

332 333 334 335 336 337
  LinkedHashMap<MouseTrackerAnnotation, Matrix4> _hitTestResultToAnnotations(HitTestResult result) {
    assert(result != null);
    final LinkedHashMap<MouseTrackerAnnotation, Matrix4> annotations = <MouseTrackerAnnotation, Matrix4>{}
        as LinkedHashMap<MouseTrackerAnnotation, Matrix4>;
    for (final HitTestEntry entry in result.path) {
      if (entry.target is MouseTrackerAnnotation) {
338
        annotations[entry.target as MouseTrackerAnnotation] = entry.transform!;
339 340 341 342 343
      }
    }
    return annotations;
  }

344 345
  // Find the annotations that is hovered by the device of the `state`, and
  // their respective global transform matrices.
346
  //
347
  // If the device is not connected or not a mouse, an empty map is returned
348 349 350 351
  // without calling `hitTest`.
  LinkedHashMap<MouseTrackerAnnotation, Matrix4> _findAnnotations(_MouseState state, MouseDetectorAnnotationFinder hitTest) {
    assert(state != null);
    assert(hitTest != null);
352 353
    final Offset globalPosition = state.latestEvent.position;
    final int device = state.device;
354 355
    if (!_mouseStates.containsKey(device))
      return <MouseTrackerAnnotation, Matrix4>{} as LinkedHashMap<MouseTrackerAnnotation, Matrix4>;
356 357

    return _hitTestResultToAnnotations(hitTest(globalPosition));
358 359 360 361
  }

  /// A callback that is called on the update of a device.
  ///
362 363
  /// This method should be called only by [BaseMouseTracker], each time when the
  /// relationship between a device and annotations has changed.
364
  ///
365 366 367
  /// By default the [handleDeviceUpdate] does nothing effective. Subclasses
  /// should override this method to first call to their inherited
  /// [handleDeviceUpdate] method, and then process the update as desired.
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
  ///
  /// The update can be caused by two kinds of triggers:
  ///
  ///   * Triggered by the addition, movement, or removal of a pointer. Such
  ///     calls occur during the handler of the event, indicated by
  ///     `details.triggeringEvent` being non-null.
  ///   * Triggered by the appearance, movement, or disappearance of an annotation.
  ///     Such calls occur after each new frame, during the post-frame callbacks,
  ///     indicated by `details.triggeringEvent` being null.
  ///
  /// Calling of this method must be wrapped in `_deviceUpdatePhase`.
  @protected
  @mustCallSuper
  void handleDeviceUpdate(MouseTrackerUpdateDetails details) {
    assert(_debugDuringDeviceUpdate);
383 384
  }

385 386 387 388 389 390 391 392 393 394 395 396 397
  /// Trigger a device update with a new event and its corresponding hit test
  /// result.
  ///
  /// The [updateWithEvent] indicates that an event has been observed, and
  /// is called during the handler of the event. The `getResult` should return
  /// the hit test result at the position of the event.
  ///
  /// The [updateWithEvent] will generate the new state for the pointer based on
  /// given information, and call [handleDeviceUpdate] based on the state changes.
  void updateWithEvent(PointerEvent event, ValueGetter<HitTestResult> getResult) {
    assert(event != null);
    final HitTestResult result = event is PointerRemovedEvent ? HitTestResult() : getResult();
    assert(result != null);
398 399 400
    if (event.kind != PointerDeviceKind.mouse)
      return;
    if (event is PointerSignalEvent)
401
      return;
402
    final int device = event.device;
403
    final _MouseState? existingState = _mouseStates[device];
404 405
    if (!_shouldMarkStateDirty(existingState, event))
      return;
406

407 408 409 410 411 412
    _monitorMouseConnection(() {
      _deviceUpdatePhase(() {
        // Update mouseState to the latest devices that have not been removed,
        // so that [mouseIsConnected], which is decided by `_mouseStates`, is
        // correct during the callbacks.
        if (existingState == null) {
413
          assert(event is! PointerRemovedEvent);
414 415 416 417 418 419
          _mouseStates[device] = _MouseState(initialEvent: event);
        } else {
          assert(event is! PointerAddedEvent);
          if (event is PointerRemovedEvent)
            _mouseStates.remove(event.device);
        }
420
        final _MouseState targetState = _mouseStates[device] ?? existingState!;
421 422

        final PointerEvent lastEvent = targetState.replaceLatestEvent(event);
423 424 425
        final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations = event is PointerRemovedEvent ?
            <MouseTrackerAnnotation, Matrix4>{} as LinkedHashMap<MouseTrackerAnnotation, Matrix4> :
            _hitTestResultToAnnotations(result);
426
        final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations = targetState.replaceAnnotations(nextAnnotations);
427 428 429 430 431 432 433 434 435

        handleDeviceUpdate(MouseTrackerUpdateDetails.byPointerEvent(
          lastAnnotations: lastAnnotations,
          nextAnnotations: nextAnnotations,
          previousEvent: lastEvent,
          triggeringEvent: event,
        ));
      });
    });
436 437
  }

438 439 440 441 442 443 444 445 446 447 448 449
  /// Trigger a device update for all detected devices.
  ///
  /// The [updateAllDevices] is typically called during the post frame phase,
  /// indicating a frame has passed and all objects have potentially moved. The
  /// `hitTest` is a function that can acquire the hit test result at a given
  /// position, and must not be empty.
  ///
  /// For each connected device, the [updateAllDevices] will make a hit test on
  /// the device's last seen position, generate the new state for the pointer
  /// based on given information, and call [handleDeviceUpdate] based on the
  /// state changes.
  void updateAllDevices(MouseDetectorAnnotationFinder hitTest) {
450 451 452
    _deviceUpdatePhase(() {
      for (final _MouseState dirtyState in _mouseStates.values) {
        final PointerEvent lastEvent = dirtyState.latestEvent;
453
        final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations = _findAnnotations(dirtyState, hitTest);
454
        final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations = dirtyState.replaceAnnotations(nextAnnotations);
455 456 457 458 459 460

        handleDeviceUpdate(MouseTrackerUpdateDetails.byNewFrame(
          lastAnnotations: lastAnnotations,
          nextAnnotations: nextAnnotations,
          previousEvent: lastEvent,
        ));
461
      }
462
    });
463
  }
464
}
465

466 467 468 469 470 471 472 473 474 475
// A mixin for [BaseMouseTracker] that dispatches mouse events on device update.
//
// See also:
//
//  * [MouseTracker], which uses this mixin.
mixin _MouseTrackerEventMixin on BaseMouseTracker {
  // Handles device update and dispatches mouse event callbacks.
  static void _handleDeviceUpdateMouseEvents(MouseTrackerUpdateDetails details) {
    final PointerEvent latestEvent = details.latestEvent;

476 477
    final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations = details.lastAnnotations;
    final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations = details.nextAnnotations;
478

479 480 481 482 483 484
    // Order is important for mouse event callbacks. The `findAnnotations`
    // returns annotations in the visual order from front to back. We call
    // it the "visual order", and the opposite one "reverse visual order".
    // The algorithm here is explained in
    // https://github.com/flutter/flutter/issues/41420

485 486
    // Send exit events to annotations that are in last but not in next, in
    // visual order.
487 488 489 490
    final PointerExitEvent baseExitEvent = PointerExitEvent.fromMouseEvent(latestEvent);
    lastAnnotations.forEach((MouseTrackerAnnotation annotation, Matrix4 transform) {
      if (!nextAnnotations.containsKey(annotation))
        if (annotation.onExit != null)
491
          annotation.onExit!(baseExitEvent.transformed(lastAnnotations[annotation]));
492
    });
493

494 495
    // Send enter events to annotations that are not in last but in next, in
    // reverse visual order.
496 497 498 499 500
    final List<MouseTrackerAnnotation> enteringAnnotations = nextAnnotations.keys.where(
      (MouseTrackerAnnotation annotation) => !lastAnnotations.containsKey(annotation),
    ).toList();
    final PointerEnterEvent baseEnterEvent = PointerEnterEvent.fromMouseEvent(latestEvent);
    for (final MouseTrackerAnnotation annotation in enteringAnnotations.reversed) {
501
      if (annotation.onEnter != null)
502
        annotation.onEnter!(baseEnterEvent.transformed(nextAnnotations[annotation]));
503 504 505
    }
  }

506 507 508 509 510
  @protected
  @override
  void handleDeviceUpdate(MouseTrackerUpdateDetails details) {
    super.handleDeviceUpdate(details);
    _handleDeviceUpdateMouseEvents(details);
511
  }
512
}
513

514
/// Tracks the relationship between mouse devices and annotations, and
515 516
/// triggers mouse events and cursor changes accordingly.
///
517
/// The [MouseTracker] tracks the relationship between mouse devices and
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
/// [MouseTrackerAnnotation]s, and when such relationship changes, triggers
/// the following changes if applicable:
///
///  * Dispatches mouse-related pointer events (pointer enter, hover, and exit).
///  * Notifies changes of [mouseIsConnected].
///  * Changes mouse cursors.
///
/// An instance of [MouseTracker] is owned by the global singleton of
/// [RendererBinding].
///
/// This class is a [ChangeNotifier] that notifies its listeners if the value of
/// [mouseIsConnected] changes.
///
/// See also:
///
///   * [BaseMouseTracker], which introduces more details about the timing of
///     device updates.
class MouseTracker extends BaseMouseTracker with MouseTrackerCursorMixin, _MouseTrackerEventMixin {
536
}