handleDeviceCursorUpdate method Null safety

void handleDeviceCursorUpdate(
  1. int device,
  2. PointerEvent? triggeringEvent,
  3. Iterable<MouseCursor> cursorCandidates
)

Handles the changes that cause a pointer device to have a new list of mouse cursor candidates.

This change can be caused by a pointer event, in which case triggeringEvent should not be null, or by other changes, such as when a widget has moved under a still mouse, which is detected after the current frame is complete. In either case, cursorCandidates should be the list of cursors at the location of the mouse in hit-test order.

Implementation

void handleDeviceCursorUpdate(
  int device,
  PointerEvent? triggeringEvent,
  Iterable<MouseCursor> cursorCandidates,
) {
  if (triggeringEvent is PointerRemovedEvent) {
    _lastSession.remove(device);
    return;
  }

  final MouseCursorSession? lastSession = _lastSession[device];
  final MouseCursor nextCursor = _DeferringMouseCursor.firstNonDeferred(cursorCandidates)
    ?? fallbackMouseCursor;
  assert(nextCursor is! _DeferringMouseCursor);
  if (lastSession?.cursor == nextCursor) {
    return;
  }

  final MouseCursorSession nextSession = nextCursor.createSession(device);
  _lastSession[device] = nextSession;

  lastSession?.dispose();
  nextSession.activate();
}