alignSelected method

bool alignSelected(
  1. CanvasAlignment alignment
)

Aligns selected objects inside the selection bounds.

Implementation

bool alignSelected(CanvasAlignment alignment) {
  if (_selectedObjectIds.length < 2) return false;
  final bounds = selectedBounds;
  if (bounds == null) return false;
  _remember();
  final changed = <CanvasObject>[];
  for (final id in _selectedObjectIds) {
    final index = _objects.indexWhere((object) => object.id == id);
    if (index < 0 || _objects[index].locked) continue;
    final objectBounds = _boundsFor(_objects[index]);
    final (dx, dy) = switch (alignment) {
      CanvasAlignment.left => (bounds.left - objectBounds.left, 0.0),
      CanvasAlignment.center => (bounds.centerX - objectBounds.centerX, 0.0),
      CanvasAlignment.right => (bounds.right - objectBounds.right, 0.0),
      CanvasAlignment.top => (0.0, bounds.top - objectBounds.top),
      CanvasAlignment.middle => (0.0, bounds.centerY - objectBounds.centerY),
      CanvasAlignment.bottom => (0.0, bounds.bottom - objectBounds.bottom),
    };
    final moved = _constrainObject(
      _objects[index],
      _moveObject(_objects[index], dx, dy),
      moving: true,
    );
    _objects[index] = moved;
    changed.add(moved);
  }
  if (changed.isEmpty) return false;
  render();
  onMove?.call(List.unmodifiable(changed));
  _emitChange();
  return true;
}