cockpitRecorderSnapBallPosition function

Offset cockpitRecorderSnapBallPosition(
  1. Offset position,
  2. Size bounds,
  3. Size ballSize
)

Computes the resting position of the floating ball after a drag ends: clamped into bounds vertically and docked to the nearer horizontal edge. Pure so drag docking is unit-testable without a widget tree.

Implementation

Offset cockpitRecorderSnapBallPosition(
  Offset position,
  Size bounds,
  Size ballSize,
) {
  final maxY = (bounds.height - ballSize.height)
      .clamp(0.0, double.infinity)
      .toDouble();
  final maxX = (bounds.width - ballSize.width)
      .clamp(0.0, double.infinity)
      .toDouble();
  final docksLeft = position.dx + ballSize.width / 2 < bounds.width / 2;
  return Offset(
    docksLeft ? 0.0 : maxX,
    position.dy.clamp(0.0, maxY).toDouble(),
  );
}