mouse_tracker.dart 16.8 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 6 7 8 9
// This library intentionally uses the LinkedHashMap constructor to declare that
// entries will be ordered. Using collection literals for this requires casting the
// resulting map, which has a runtime cost.
// ignore_for_file: prefer_collection_literals

10
import 'dart:collection' show LinkedHashMap;
11 12
import 'dart:ui';

13
import 'package:flutter/foundation.dart';
14
import 'package:flutter/gestures.dart';
15
import 'package:flutter/services.dart';
16

17 18
import 'object.dart';

19 20 21
export 'package:flutter/services.dart' show
  MouseCursor,
  SystemMouseCursors;
22

23
/// Signature for searching for [MouseTrackerAnnotation]s at the given offset.
24
///
25
/// It is used by the [MouseTracker] to fetch annotations for the mouse
26
/// position.
27
typedef MouseDetectorAnnotationFinder = HitTestResult Function(Offset offset);
28

29
// Various states of a connected mouse device used by [MouseTracker].
30 31
class _MouseState {
  _MouseState({
32
    required PointerEvent initialEvent,
33 34
  }) : assert(initialEvent != null),
       _latestEvent = initialEvent;
35

36
  // The list of annotations that contains this device.
37
  //
38 39 40
  // It uses [LinkedHashMap] to keep the insertion order.
  LinkedHashMap<MouseTrackerAnnotation, Matrix4> get annotations => _annotations;
  LinkedHashMap<MouseTrackerAnnotation, Matrix4> _annotations = LinkedHashMap<MouseTrackerAnnotation, Matrix4>();
41

42
  LinkedHashMap<MouseTrackerAnnotation, Matrix4> replaceAnnotations(LinkedHashMap<MouseTrackerAnnotation, Matrix4> value) {
43
    assert(value != null);
44
    final LinkedHashMap<MouseTrackerAnnotation, Matrix4> previous = _annotations;
45 46 47 48 49 50 51
    _annotations = value;
    return previous;
  }

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

  PointerEvent replaceLatestEvent(PointerEvent value) {
54
    assert(value != null);
55 56
    assert(value.device == _latestEvent.device);
    final PointerEvent previous = _latestEvent;
57
    _latestEvent = value;
58
    return previous;
59 60
  }

61
  int get device => latestEvent.device;
62 63 64

  @override
  String toString() {
65
    final String describeLatestEvent = 'latestEvent: ${describeIdentity(latestEvent)}';
66 67
    final String describeAnnotations = 'annotations: [list of ${annotations.length}]';
    return '${describeIdentity(this)}($describeLatestEvent, $describeAnnotations)';
68 69 70
  }
}

71 72 73 74 75 76
// The information in `MouseTracker._handleDeviceUpdate` 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.
77
@immutable
78
class _MouseTrackerUpdateDetails with Diagnosticable {
79 80 81
  /// When device update is triggered by a new frame.
  ///
  /// All parameters are required.
82
  const _MouseTrackerUpdateDetails.byNewFrame({
83 84
    required this.lastAnnotations,
    required this.nextAnnotations,
85
    required PointerEvent this.previousEvent,
86 87 88 89 90 91 92 93 94
  }) : 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.
95
  const _MouseTrackerUpdateDetails.byPointerEvent({
96 97
    required this.lastAnnotations,
    required this.nextAnnotations,
98
    this.previousEvent,
99
    required PointerEvent this.triggeringEvent,
100 101 102 103 104 105 106
  }) : assert(triggeringEvent != null),
       assert(lastAnnotations != null),
       assert(nextAnnotations != null);

  /// The annotations that the device is hovering before the update.
  ///
  /// It is never null.
107
  final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations;
108 109 110 111

  /// The annotations that the device is hovering after the update.
  ///
  /// It is never null.
112
  final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations;
113 114 115 116 117 118 119 120 121

  /// 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]).
122
  final PointerEvent? previousEvent;
123 124 125 126

  /// The event that triggered this update.
  ///
  /// It is non-null if and only if the update is triggered by a pointer event.
127
  final PointerEvent? triggeringEvent;
128 129 130

  /// The pointing device of this update.
  int get device {
131
    final int result = (previousEvent ?? triggeringEvent)!.device;
132 133 134 135 136 137 138 139
    assert(result != null);
    return result;
  }

  /// The last event that the device observed after the update.
  ///
  /// The [latestEvent] is never null.
  PointerEvent get latestEvent {
140
    final PointerEvent result = triggeringEvent ?? previousEvent!;
141 142 143 144 145 146 147 148 149 150
    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));
151 152
    properties.add(DiagnosticsProperty<Map<MouseTrackerAnnotation, Matrix4>>('lastAnnotations', lastAnnotations));
    properties.add(DiagnosticsProperty<Map<MouseTrackerAnnotation, Matrix4>>('nextAnnotations', nextAnnotations));
153 154 155
  }
}

156 157
/// Tracks the relationship between mouse devices and annotations, and
/// triggers mouse events and cursor changes accordingly.
158
///
159 160 161 162
/// The [MouseTracker] tracks the relationship between mouse devices and
/// [MouseTrackerAnnotation], notified by [updateWithEvent] and
/// [updateAllDevices]. At every update, [MouseTracker] triggers the following
/// changes if applicable:
163
///
164 165 166
///  * Dispatches mouse-related pointer events (pointer enter, hover, and exit).
///  * Changes mouse cursors.
///  * Notifies when [mouseIsConnected] changes.
167
///
168 169 170
/// This class is a [ChangeNotifier] that notifies its listeners if the value of
/// [mouseIsConnected] changes.
///
171 172 173 174
/// An instance of [MouseTracker] is owned by the global singleton
/// [RendererBinding].
class MouseTracker extends ChangeNotifier {
  final MouseCursorManager _mouseCursorMixin = MouseCursorManager(
175
    SystemMouseCursors.basic,
176
  );
177

178 179
  // Tracks the state of connected mouse devices.
  //
180
  // It is the source of truth for the list of connected mouse devices, and
181 182 183 184
  // consists of two parts:
  //
  //  * The mouse devices that are connected.
  //  * In which annotations each device is contained.
185 186
  final Map<int, _MouseState> _mouseStates = <int, _MouseState>{};

187 188 189 190 191 192 193
  // 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();
194
    if (mouseWasConnected != mouseIsConnected) {
195
      notifyListeners();
196
    }
197 198 199
  }

  bool _debugDuringDeviceUpdate = false;
200
  // Used to wrap any procedure that might call `_handleDeviceUpdate`.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  //
  // 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;
    }());
  }

217
  // Whether an observed event might update a device.
218
  static bool _shouldMarkStateDirty(_MouseState? state, PointerEvent event) {
219
    if (state == null) {
220
      return true;
221
    }
222
    assert(event != null);
223
    final PointerEvent lastEvent = state.latestEvent;
224
    assert(event.device == lastEvent.device);
225 226
    // An Added can only follow a Removed, and a Removed can only be followed
    // by an Added.
227
    assert((event is PointerAddedEvent) == (lastEvent is PointerRemovedEvent));
228 229

    // Ignore events that are unrelated to mouse tracking.
230
    if (event is PointerSignalEvent) {
231
      return false;
232
    }
233
    return lastEvent is PointerAddedEvent
234 235 236 237
      || event is PointerRemovedEvent
      || lastEvent.position != event.position;
  }

238 239
  LinkedHashMap<MouseTrackerAnnotation, Matrix4> _hitTestResultToAnnotations(HitTestResult result) {
    assert(result != null);
240
    final LinkedHashMap<MouseTrackerAnnotation, Matrix4> annotations = LinkedHashMap<MouseTrackerAnnotation, Matrix4>();
241
    for (final HitTestEntry entry in result.path) {
242 243 244
      final Object target = entry.target;
      if (target is MouseTrackerAnnotation) {
        annotations[target] = entry.transform!;
245 246 247 248 249
      }
    }
    return annotations;
  }

250 251
  // Find the annotations that is hovered by the device of the `state`, and
  // their respective global transform matrices.
252
  //
253
  // If the device is not connected or not a mouse, an empty map is returned
254 255 256 257
  // without calling `hitTest`.
  LinkedHashMap<MouseTrackerAnnotation, Matrix4> _findAnnotations(_MouseState state, MouseDetectorAnnotationFinder hitTest) {
    assert(state != null);
    assert(hitTest != null);
258 259
    final Offset globalPosition = state.latestEvent.position;
    final int device = state.device;
260
    if (!_mouseStates.containsKey(device)) {
261
      return LinkedHashMap<MouseTrackerAnnotation, Matrix4>();
262
    }
263 264

    return _hitTestResultToAnnotations(hitTest(globalPosition));
265 266
  }

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  // A callback that is called on the update of a device.
  //
  // An event (not necessarily a pointer event) that might change the
  // relationship between mouse devices and [MouseTrackerAnnotation]s is called
  // a _device update_. This method should be called at each such update.
  //
  // 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.
  //
  // Calls of this method must be wrapped in `_deviceUpdatePhase`.
  void _handleDeviceUpdate(_MouseTrackerUpdateDetails details) {
284
    assert(_debugDuringDeviceUpdate);
285 286 287 288
    _handleDeviceUpdateMouseEvents(details);
    _mouseCursorMixin.handleDeviceCursorUpdate(
      details.device,
      details.triggeringEvent,
289
      details.nextAnnotations.keys.map((MouseTrackerAnnotation annotation) => annotation.cursor),
290
    );
291 292
  }

293 294 295
  /// Whether or not at least one mouse is connected and has produced events.
  bool get mouseIsConnected => _mouseStates.isNotEmpty;

296 297 298
  /// Trigger a device update with a new event and its corresponding hit test
  /// result.
  ///
299
  /// The [updateWithEvent] indicates that an event has been observed, and is
300
  /// called during the handler of the event. It is typically called by
301 302 303 304 305 306
  /// [RendererBinding], and should be called with all events received, and let
  /// [MouseTracker] filter which to react to.
  ///
  /// The `getResult` is a function to return the hit test result at the
  /// position of the event. It should not simply return cached hit test
  /// result, because the cache does not change throughout a tap sequence.
307
  void updateWithEvent(PointerEvent event, ValueGetter<HitTestResult> getResult) {
308
    if (event.kind != PointerDeviceKind.mouse) {
309
      return;
310 311
    }
    if (event is PointerSignalEvent) {
312
      return;
313
    }
314
    final HitTestResult result = event is PointerRemovedEvent ? HitTestResult() : getResult();
315
    final int device = event.device;
316
    final _MouseState? existingState = _mouseStates[device];
317
    if (!_shouldMarkStateDirty(existingState, event)) {
318
      return;
319
    }
320

321 322 323 324 325 326
    _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) {
327
          if (event is PointerRemovedEvent) {
328
            return;
329
          }
330 331 332
          _mouseStates[device] = _MouseState(initialEvent: event);
        } else {
          assert(event is! PointerAddedEvent);
333
          if (event is PointerRemovedEvent) {
334
            _mouseStates.remove(event.device);
335
          }
336
        }
337
        final _MouseState targetState = _mouseStates[device] ?? existingState!;
338 339

        final PointerEvent lastEvent = targetState.replaceLatestEvent(event);
340
        final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations = event is PointerRemovedEvent ?
341
            LinkedHashMap<MouseTrackerAnnotation, Matrix4>() :
342
            _hitTestResultToAnnotations(result);
343
        final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations = targetState.replaceAnnotations(nextAnnotations);
344

345
        _handleDeviceUpdate(_MouseTrackerUpdateDetails.byPointerEvent(
346 347 348 349 350 351 352
          lastAnnotations: lastAnnotations,
          nextAnnotations: nextAnnotations,
          previousEvent: lastEvent,
          triggeringEvent: event,
        ));
      });
    });
353 354
  }

355 356 357 358
  /// 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
359
  /// `hitTest` is a function that acquires the hit test result at a given
360 361 362
  /// position, and must not be empty.
  ///
  /// For each connected device, the [updateAllDevices] will make a hit test on
363 364
  /// the device's last seen position, and check if necessary changes need to be
  /// made.
365
  void updateAllDevices(MouseDetectorAnnotationFinder hitTest) {
366 367 368
    _deviceUpdatePhase(() {
      for (final _MouseState dirtyState in _mouseStates.values) {
        final PointerEvent lastEvent = dirtyState.latestEvent;
369
        final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations = _findAnnotations(dirtyState, hitTest);
370
        final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations = dirtyState.replaceAnnotations(nextAnnotations);
371

372
        _handleDeviceUpdate(_MouseTrackerUpdateDetails.byNewFrame(
373 374 375 376
          lastAnnotations: lastAnnotations,
          nextAnnotations: nextAnnotations,
          previousEvent: lastEvent,
        ));
377
      }
378
    });
379 380
  }

381 382 383 384 385 386 387 388 389 390 391 392
  /// Returns the active mouse cursor for a device.
  ///
  /// The return value is the last [MouseCursor] activated onto this device, even
  /// if the activation failed.
  ///
  /// This function is only active when asserts are enabled. In release builds,
  /// it always returns null.
  @visibleForTesting
  MouseCursor? debugDeviceActiveCursor(int device) {
    return _mouseCursorMixin.debugDeviceActiveCursor(device);
  }

393
  // Handles device update and dispatches mouse event callbacks.
394
  static void _handleDeviceUpdateMouseEvents(_MouseTrackerUpdateDetails details) {
395 396
    final PointerEvent latestEvent = details.latestEvent;

397 398
    final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations = details.lastAnnotations;
    final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations = details.nextAnnotations;
399

400 401 402 403
    // Order is important for mouse event callbacks. The
    // `_hitTestResultToAnnotations` returns annotations in the visual order
    // from front to back, called the "hit-test order". The algorithm here is
    // explained in https://github.com/flutter/flutter/issues/41420
404

405
    // Send exit events to annotations that are in last but not in next, in
406
    // hit-test order.
407 408
    final PointerExitEvent baseExitEvent = PointerExitEvent.fromMouseEvent(latestEvent);
    lastAnnotations.forEach((MouseTrackerAnnotation annotation, Matrix4 transform) {
409
      if (!nextAnnotations.containsKey(annotation)) {
410
        if (annotation.validForMouseTracker && annotation.onExit != null) {
411
          annotation.onExit!(baseExitEvent.transformed(lastAnnotations[annotation]));
412
        }
413
      }
414
    });
415

416
    // Send enter events to annotations that are not in last but in next, in
417
    // reverse hit-test order.
418 419 420 421 422
    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) {
423
      if (annotation.validForMouseTracker && annotation.onEnter != null) {
424
        annotation.onEnter!(baseEnterEvent.transformed(nextAnnotations[annotation]));
425
      }
426 427 428
    }
  }
}