CellUserSelectionModel.fromAnchorFocus constructor

CellUserSelectionModel.fromAnchorFocus({
  1. String? id,
  2. required IntVector2 anchor,
  3. required IntVector2 focus,
  4. SelectionStyle? style,
})

Create a CellUserSelectionModel given its opposite corners (anchor and focus).

If id is omitted, an uuid is generated.

Since this selection is a Range2D, we convert anchor and focus into range's leftTop and rightBottom values.

Implementation

factory CellUserSelectionModel.fromAnchorFocus({
  String? id,
  required IntVector2 anchor,
  required IntVector2 focus,
  SelectionStyle? style,
}) {
  final leftTop = IntVector2(
    min(anchor.dx, focus.dx),
    min(anchor.dy, focus.dy),
  );

  // Focus and anchor are inclusive, rightBottom on Range2D is not.
  final rightBottom = IntVector2(
    max(anchor.dx, focus.dx) + 1,
    max(anchor.dy, focus.dy) + 1,
  );

  // Define where the anchor is from their positions
  late Corner anchorCorner;
  if (anchor.dx <= focus.dx && anchor.dy <= focus.dy) {
    anchorCorner = Corner.leftTop;
  } else if (anchor.dx >= focus.dx && anchor.dy >= focus.dy) {
    anchorCorner = Corner.rightBottom;
  } else if (anchor.dx < focus.dx) {
    anchorCorner = Corner.leftBottom;
  } else {
    anchorCorner = Corner.rightTop;
  }

  return CellUserSelectionModel._(
    id: id,
    leftTop: leftTop,
    rightBottom: rightBottom,
    anchorCorner: anchorCorner,
    style: style,
  );
}