snapToEdge method

void snapToEdge()

Implementation

void snapToEdge() {
  if (!isSnaping) return;
  if (_screenSize == Size.zero) return;

  final double x = _position.offset.dx;
  final double y = _position.offset.dy;

  final double leftDistance = x;
  final double rightDistance = _screenSize.width - (x + _settings.collapsedWidth);
  final double topDistance = y;
  final double bottomDistance =
      _screenSize.height - (y + _settings.collapsedHeight) - bottomSafePadding;

  double newX = x;
  double newY = y;
  PipAnchor newAnchor;

  if (leftDistance < rightDistance) {
    newX = margin;
  } else {
    newX = _screenSize.width - _settings.collapsedWidth - margin;
  }

  if (topDistance < bottomDistance) {
    newY = margin;
    newAnchor = leftDistance < rightDistance ? PipAnchor.topLeft : PipAnchor.topRight;
  } else {
    newY = _screenSize.height - _settings.collapsedHeight - margin - bottomSafePadding;
    newAnchor = leftDistance < rightDistance ? PipAnchor.bottomLeft : PipAnchor.bottomRight;
  }

  _position = _position.copyWith(
    offset: Offset(newX, newY),
    anchor: newAnchor,
  );

  notifyListeners();
}