diff --git a/packages/flutter/lib/src/gestures/mouse_tracking.dart b/packages/flutter/lib/src/gestures/mouse_tracking.dart index a21f0a5c83..ba309bfe46 100644 --- a/packages/flutter/lib/src/gestures/mouse_tracking.dart +++ b/packages/flutter/lib/src/gestures/mouse_tracking.dart @@ -145,7 +145,7 @@ typedef _UpdatedDeviceHandler = void Function(_MouseState mouseState, LinkedHash // Various states of a connected mouse device used by [MouseTracker]. class _MouseState { _MouseState({ - @required PointerAddedEvent initialEvent, + @required PointerEvent initialEvent, }) : assert(initialEvent != null), _latestEvent = initialEvent; @@ -289,6 +289,7 @@ class MouseTracker extends ChangeNotifier { return; final PointerEvent previousEvent = existingState?.latestEvent; + final Offset lastHoverPosition = previousEvent is! PointerHoverEvent ? null : previousEvent.position; _updateDevices( targetEvent: event, handleUpdatedDevice: (_MouseState mouseState, LinkedHashSet previousAnnotations) { @@ -296,7 +297,7 @@ class MouseTracker extends ChangeNotifier { _dispatchDeviceCallbacks( lastAnnotations: previousAnnotations, nextAnnotations: mouseState.annotations, - handledEvent: previousEvent, + lastHoverPosition: lastHoverPosition, unhandledEvent: event, trackedAnnotations: _trackedAnnotations, ); @@ -327,10 +328,12 @@ class MouseTracker extends ChangeNotifier { void _updateAllDevices() { _updateDevices( handleUpdatedDevice: (_MouseState mouseState, LinkedHashSet previousAnnotations) { + final PointerEvent latestEvent = mouseState.latestEvent; + final Offset lastHoverPosition = latestEvent is PointerHoverEvent ? latestEvent.position : null; _dispatchDeviceCallbacks( lastAnnotations: previousAnnotations, nextAnnotations: mouseState.annotations, - handledEvent: mouseState.latestEvent, + lastHoverPosition: lastHoverPosition, unhandledEvent: mouseState.latestEvent, trackedAnnotations: _trackedAnnotations, ); @@ -385,11 +388,11 @@ class MouseTracker extends ChangeNotifier { _MouseState targetState; if (targetEvent != null) { targetState = _mouseStates[targetEvent.device]; - assert((targetState == null) == (targetEvent is PointerAddedEvent)); - if (targetEvent is PointerAddedEvent) { + if (targetState == null) { targetState = _MouseState(initialEvent: targetEvent); _mouseStates[targetState.device] = targetState; } else { + assert(targetEvent is! PointerAddedEvent); targetState.latestEvent = targetEvent; // Update mouseState to the latest devices that have not been removed, // so that [mouseIsConnected], which is decided by `_mouseStates`, is @@ -424,17 +427,18 @@ class MouseTracker extends ChangeNotifier { // Dispatch callbacks related to a device after all necessary information // has been collected. // - // The `unhandledEvent` can be null. Other arguments must not be null. + // The `lastHoverPosition` can be null, which means the last event is not a + // hover. Other arguments must not be null. static void _dispatchDeviceCallbacks({ @required LinkedHashSet lastAnnotations, @required LinkedHashSet nextAnnotations, - @required PointerEvent handledEvent, + @required Offset lastHoverPosition, @required PointerEvent unhandledEvent, @required Set trackedAnnotations, }) { assert(lastAnnotations != null); assert(nextAnnotations != null); - // handledEvent can be null + // lastHoverPosition can be null assert(unhandledEvent != null); assert(trackedAnnotations != null); // Order is important for mouse event callbacks. The `findAnnotations` @@ -477,8 +481,7 @@ class MouseTracker extends ChangeNotifier { // or the position has changed assert(trackedAnnotations.contains(annotation)); if (!lastAnnotations.contains(annotation) - || handledEvent is! PointerHoverEvent - || handledEvent.position != unhandledEvent.position) { + || lastHoverPosition != unhandledEvent.position) { if (annotation.onHover != null) { annotation.onHover(unhandledEvent); } diff --git a/packages/flutter_test/lib/src/test_pointer.dart b/packages/flutter_test/lib/src/test_pointer.dart index 55bb909b53..dc46d5ca89 100644 --- a/packages/flutter_test/lib/src/test_pointer.dart +++ b/packages/flutter_test/lib/src/test_pointer.dart @@ -337,16 +337,12 @@ class TestGesture { _dispatcher = dispatcher, _hitTester = hitTester, _pointer = TestPointer(pointer, kind, device, buttons), - _added = false, _result = null; /// Dispatch a pointer down event at the given `downLocation`, caching the /// hit test result. - /// - /// If the pointer has not been added, an added event will be dispatched first. - Future down(Offset downLocation) { + Future down(Offset downLocation) async { return TestAsyncUtils.guard(() async { - await _ensureAdded(location: downLocation); _result = _hitTester(downLocation); return _dispatcher(_pointer.down(downLocation), _result); }); @@ -354,12 +350,9 @@ class TestGesture { /// Dispatch a pointer down event at the given `downLocation`, caching the /// hit test result with a custom down event. - /// - /// If the pointer has not been added, an added event will be dispatched first. - Future downWithCustomEvent(Offset downLocation, PointerDownEvent event) { + Future downWithCustomEvent(Offset downLocation, PointerDownEvent event) async { + _pointer.setDownInfo(event, downLocation); return TestAsyncUtils.guard(() async { - await _ensureAdded(location: downLocation); - _pointer.setDownInfo(event, downLocation); _result = _hitTester(downLocation); return _dispatcher(event, _result); }); @@ -369,22 +362,10 @@ class TestGesture { final HitTester _hitTester; final TestPointer _pointer; HitTestResult _result; - bool _added; - - Future _ensureAdded({ Offset location }) async { - if (!_added) { - await addPointer(location: location ?? _pointer.location); - } - } /// In a test, send a move event that moves the pointer by the given offset. - /// - /// If the pointer has not been added, and the subject event is not an added - /// event, an added event will be dispatched first. @visibleForTesting - Future updateWithCustomEvent(PointerEvent event, { Duration timeStamp = Duration.zero }) async { - if (event is! PointerAddedEvent) - await _ensureAdded(location: event.position); + Future updateWithCustomEvent(PointerEvent event, { Duration timeStamp = Duration.zero }) { _pointer.setDownInfo(event, event.position); return TestAsyncUtils.guard(() { return _dispatcher(event, _result); @@ -392,34 +373,21 @@ class TestGesture { } /// In a test, send a pointer add event for this pointer. - /// - /// If a pointer has been added, the pointer will be removed first. Future addPointer({ Duration timeStamp = Duration.zero, Offset location }) { - return TestAsyncUtils.guard(() async { - if (_added) { - await removePointer(timeStamp: timeStamp); - } - _added = true; + return TestAsyncUtils.guard(() { return _dispatcher(_pointer.addPointer(timeStamp: timeStamp, location: location ?? _pointer.location), null); }); } /// In a test, send a pointer remove event for this pointer. - /// - /// If no pointer has been added, the call will be a no-op. Future removePointer({ Duration timeStamp = Duration.zero, Offset location }) { - return TestAsyncUtils.guard(() async { - if (!_added) - return; - _added = false; - await _dispatcher(_pointer.removePointer(timeStamp: timeStamp, location: location ?? _pointer.location), null); + return TestAsyncUtils.guard(() { + return _dispatcher(_pointer.removePointer(timeStamp: timeStamp, location: location ?? _pointer.location), null); }); } /// Send a move event moving the pointer by the given offset. /// - /// If the pointer has not been added, an added event will be dispatched first. - /// /// If the pointer is down, then a move event is dispatched. If the pointer is /// up, then a hover event is dispatched. Touch devices are not able to send /// hover events. @@ -429,14 +397,11 @@ class TestGesture { /// Send a move event moving the pointer to the given location. /// - /// If the pointer has not been added, an added event will be dispatched first. - /// /// If the pointer is down, then a move event is dispatched. If the pointer is /// up, then a hover event is dispatched. Touch devices are not able to send /// hover events. Future moveTo(Offset location, { Duration timeStamp = Duration.zero }) { - return TestAsyncUtils.guard(() async { - await _ensureAdded(location: location); + return TestAsyncUtils.guard(() { if (_pointer._isDown) { assert(_result != null, 'Move events with the pointer down must be preceded by a down ' @@ -451,11 +416,8 @@ class TestGesture { } /// End the gesture by releasing the pointer. - /// - /// If the pointer has not been added, an added event will be dispatched first. Future up() { return TestAsyncUtils.guard(() async { - await _ensureAdded(); assert(_pointer._isDown); await _dispatcher(_pointer.up(), _result); assert(!_pointer._isDown); @@ -466,11 +428,8 @@ class TestGesture { /// End the gesture by canceling the pointer (as would happen if the /// system showed a modal dialog on top of the Flutter application, /// for instance). - /// - /// If the pointer has not been added, an added event will be dispatched first. Future cancel() { return TestAsyncUtils.guard(() async { - await _ensureAdded(); assert(_pointer._isDown); await _dispatcher(_pointer.cancel(), _result); assert(!_pointer._isDown);