handleDirectionallyExtendSelection method

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

Extend current selection in a certain text granularity.

Implementation

@protected
SelectionResult handleDirectionallyExtendSelection(DirectionallyExtendSelectionEvent event) {
  assert((currentSelectionStartIndex == -1) == (currentSelectionEndIndex == -1));
  if (currentSelectionStartIndex == -1) {
    switch (event.direction) {
      case SelectionExtendDirection.previousLine:
      case SelectionExtendDirection.backward:
        currentSelectionStartIndex = currentSelectionEndIndex = selectables.length;
      case SelectionExtendDirection.nextLine:
      case SelectionExtendDirection.forward:
      currentSelectionStartIndex = currentSelectionEndIndex = 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;
}