handleSelectionChange method

void handleSelectionChange()

Handle changes to the listenable to compute a new SwayzeHeaderState.

The Autoscroll and the reactions to selections in HeaderState create a cycle of updating selections and viewport size.

WHile autoscrolling: SwayzeSelectionController changes -> HeaderState updates cols/rows -> AutoScrollController updates selection -> HeaderState updates cols/row

In order to break the cycle we scheduleMicrotask on HeaderState's reaction to SwayzeSelectionController changes.

This means that now, wHile autoscrolling: SwayzeSelectionController changes <

Implementation

void handleSelectionChange() {
  final selections = [
    ...parent.selection.userSelectionState.selections,
    ...parent.selection.dataSelections,
  ];

  final currentElasticEdge = IntVector2(
    columns.value.elasticCount,
    rows.value.elasticCount,
  );

  final elasticEdge = selections.fold<IntVector2>(
    const IntVector2.symmetric(0),
    (acc, selection) {
      final rowEdge = selection.bottom;
      final columnEdge = selection.right;

      return acc.copyWith(
        x: columnEdge == null ? acc.dx : max(acc.dx, columnEdge),
        y: rowEdge == null ? acc.dy : max(acc.dy, rowEdge),
      );
    },
  );

  if (currentElasticEdge == elasticEdge) {
    return;
  }

  scheduleMicrotask(() {
    columns.updateElasticCount(elasticEdge.dx);
    rows.updateElasticCount(elasticEdge.dy);
  });
}