handleDirectionallyExtendSelection method

  1. @protected
SelectionResult handleDirectionallyExtendSelection(
  1. DirectionallyExtendSelectionEvent event
)

Extend current selection in a certain TextGranularity.

Implementation

@protected
SelectionResult handleDirectionallyExtendSelection(DirectionallyExtendSelectionEvent event) {
  assert((currentSelectionStartIndex == -1) == (currentSelectionEndIndex == -1));
  if (currentSelectionStartIndex == -1) {
    currentSelectionStartIndex = currentSelectionEndIndex = switch (event.direction) {
      SelectionExtendDirection.previousLine || SelectionExtendDirection.backward => selectables.length - 1,
      SelectionExtendDirection.nextLine || SelectionExtendDirection.forward => 0,
    };
  }
  int targetIndex = event.isEnd ? currentSelectionEndIndex : currentSelectionStartIndex;
  SelectionResult result = dispatchSelectionEventToChild(selectables[targetIndex], event);
  switch (event.direction) {
    case SelectionExtendDirection.previousLine:
      assert(result == SelectionResult.end || result == SelectionResult.previous);
      if (result == SelectionResult.previous) {
        if (targetIndex > 0) {
          targetIndex -= 1;
          result = dispatchSelectionEventToChild(
            selectables[targetIndex],
            event.copyWith(direction: SelectionExtendDirection.backward),
          );
          assert(result == SelectionResult.end);
        }
      }
    case SelectionExtendDirection.nextLine:
      assert(result == SelectionResult.end || result == SelectionResult.next);
      if (result == SelectionResult.next) {
        if (targetIndex < selectables.length - 1) {
          targetIndex += 1;
          result = dispatchSelectionEventToChild(
            selectables[targetIndex],
            event.copyWith(direction: SelectionExtendDirection.forward),
          );
          assert(result == SelectionResult.end);
        }
      }
    case SelectionExtendDirection.forward:
    case SelectionExtendDirection.backward:
      assert(result == SelectionResult.end);
  }
  if (event.isEnd) {
    currentSelectionEndIndex = targetIndex;
  } else {
    currentSelectionStartIndex = targetIndex;
  }
  return result;
}