updateWithEvent method Null safety

void updateWithEvent(
  1. PointerEvent event,
  2. ValueGetter<HitTestResult> getResult
)

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. It is typically called by 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.

Implementation

void updateWithEvent(PointerEvent event, ValueGetter<HitTestResult> getResult) {
  if (event.kind != PointerDeviceKind.mouse) {
    return;
  }
  if (event is PointerSignalEvent) {
    return;
  }
  final HitTestResult result = event is PointerRemovedEvent ? HitTestResult() : getResult();
  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>() :
          _hitTestResultToAnnotations(result);
      final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations = targetState.replaceAnnotations(nextAnnotations);

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