updateWithEvent method

void updateWithEvent(
  1. PointerEvent event,
  2. HitTestResult? hitTestResult
)

Perform a device update for one device according to the given new event.

The updateWithEvent is typically called by RendererBinding during the handler of a pointer event. All pointer events should call this method, and let MouseTracker filter which to react to.

The hitTestResult serves as an optional optimization, and is the hit test result already performed by RendererBinding for other gestures. It can be null, but when it's not null, it should be identical to the result from directly calling hitTestInView given in the constructor (which means that it should not use the cached result for PointerMoveEvent).

The updateWithEvent is one of the two ways of updating mouse states, the other one being updateAllDevices.

Implementation

void updateWithEvent(PointerEvent event, HitTestResult? hitTestResult) {
  if (event.kind != PointerDeviceKind.mouse) {
    return;
  }
  if (event is PointerSignalEvent) {
    return;
  }
  final HitTestResult result;
  if (event is PointerRemovedEvent) {
    result = HitTestResult();
  } else {
    final int viewId = event.viewId;
    result = hitTestResult ?? _hitTestInView(event.position, viewId);
  }
  final int device = event.device;
  final _MouseState? existingState = _mouseStates[device];
  if (!_shouldMarkStateDirty(existingState, event)) {
    return;
  }

  _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) {
        if (event is PointerRemovedEvent) {
          return;
        }
        _mouseStates[device] = _MouseState(initialEvent: event);
      } else {
        assert(event is! PointerAddedEvent);
        if (event is PointerRemovedEvent) {
          _mouseStates.remove(event.device);
        }
      }
      final _MouseState targetState = _mouseStates[device] ?? existingState!;

      final PointerEvent lastEvent = targetState.replaceLatestEvent(event);
      final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations = event is PointerRemovedEvent ?
          LinkedHashMap<MouseTrackerAnnotation, Matrix4>() :
          _hitTestInViewResultToAnnotations(result);
      final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations = targetState.replaceAnnotations(nextAnnotations);

      _handleDeviceUpdate(_MouseTrackerUpdateDetails.byPointerEvent(
        lastAnnotations: lastAnnotations,
        nextAnnotations: nextAnnotations,
        previousEvent: lastEvent,
        triggeringEvent: event,
      ));
    });
  });
}