updateAnnotationResize method

void updateAnnotationResize(
  1. Offset delta
)

Updates the annotation size during a resize operation.

This method works with any resizable annotation by calling its setSize method.

Implementation

void updateAnnotationResize(Offset delta) {
  final annotationId = _resizingAnnotationId.value;
  final handle = _resizeHandle.value;
  if (annotationId == null || handle == null) return;

  final annotation = _annotations[annotationId];
  if (annotation == null || !annotation.isResizable) return;

  final startPos = _resizeStartPosition;
  final startSize = _resizeStartSize;
  if (startPos == null || startSize == null) return;

  runInAction(() {
    // Calculate new position and size based on handle being dragged
    var newX = annotation.position.dx;
    var newY = annotation.position.dy;
    var newWidth = annotation.size.width;
    var newHeight = annotation.size.height;

    switch (handle) {
      case ResizeHandle.topLeft:
        newX += delta.dx;
        newY += delta.dy;
        newWidth -= delta.dx;
        newHeight -= delta.dy;
      case ResizeHandle.topCenter:
        newY += delta.dy;
        newHeight -= delta.dy;
      case ResizeHandle.topRight:
        newY += delta.dy;
        newWidth += delta.dx;
        newHeight -= delta.dy;
      case ResizeHandle.centerLeft:
        newX += delta.dx;
        newWidth -= delta.dx;
      case ResizeHandle.centerRight:
        newWidth += delta.dx;
      case ResizeHandle.bottomLeft:
        newX += delta.dx;
        newWidth -= delta.dx;
        newHeight += delta.dy;
      case ResizeHandle.bottomCenter:
        newHeight += delta.dy;
      case ResizeHandle.bottomRight:
        newWidth += delta.dx;
        newHeight += delta.dy;
    }

    // Apply minimum size constraints (each annotation can further constrain in setSize)
    const minWidth = 100.0;
    const minHeight = 60.0;

    // If new size would be below minimum, adjust position back
    if (newWidth < minWidth) {
      if (handle == ResizeHandle.topLeft ||
          handle == ResizeHandle.centerLeft ||
          handle == ResizeHandle.bottomLeft) {
        newX = annotation.position.dx + annotation.size.width - minWidth;
      }
      newWidth = minWidth;
    }

    if (newHeight < minHeight) {
      if (handle == ResizeHandle.topLeft ||
          handle == ResizeHandle.topCenter ||
          handle == ResizeHandle.topRight) {
        newY = annotation.position.dy + annotation.size.height - minHeight;
      }
      newHeight = minHeight;
    }

    // Update position if changed
    final newPosition = Offset(newX, newY);
    if (newPosition != annotation.position) {
      annotation.position = newPosition;
      annotation.visualPosition = _parentController.config
          .snapAnnotationsToGridIfEnabled(newPosition);
    }

    // Update size - annotation's setSize handles any type-specific constraints
    annotation.setSize(Size(newWidth, newHeight));

    // Mark annotation dirty
    _parentController.internalMarkAnnotationDirty(annotationId);
  });
}