"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/packages/flutter/lib/src/services/text_editing.dart" (13 Nov 2020, 4789 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 
    6 import 'dart:ui' show hashValues, TextAffinity, TextPosition, TextRange;
    7 
    8 import 'package:flutter/foundation.dart';
    9 
   10 export 'dart:ui' show TextAffinity, TextPosition, TextRange;
   11 
   12 /// A range of text that represents a selection.
   13 @immutable
   14 class TextSelection extends TextRange {
   15   /// Creates a text selection.
   16   ///
   17   /// The [baseOffset] and [extentOffset] arguments must not be null.
   18   const TextSelection({
   19     required this.baseOffset,
   20     required this.extentOffset,
   21     this.affinity = TextAffinity.downstream,
   22     this.isDirectional = false,
   23   }) : super(
   24          start: baseOffset < extentOffset ? baseOffset : extentOffset,
   25          end: baseOffset < extentOffset ? extentOffset : baseOffset,
   26        );
   27 
   28   /// Creates a collapsed selection at the given offset.
   29   ///
   30   /// A collapsed selection starts and ends at the same offset, which means it
   31   /// contains zero characters but instead serves as an insertion point in the
   32   /// text.
   33   ///
   34   /// The [offset] argument must not be null.
   35   const TextSelection.collapsed({
   36     required int offset,
   37     this.affinity = TextAffinity.downstream,
   38   }) : baseOffset = offset,
   39        extentOffset = offset,
   40        isDirectional = false,
   41        super.collapsed(offset);
   42 
   43   /// Creates a collapsed selection at the given text position.
   44   ///
   45   /// A collapsed selection starts and ends at the same offset, which means it
   46   /// contains zero characters but instead serves as an insertion point in the
   47   /// text.
   48   TextSelection.fromPosition(TextPosition position)
   49     : baseOffset = position.offset,
   50       extentOffset = position.offset,
   51       affinity = position.affinity,
   52       isDirectional = false,
   53       super.collapsed(position.offset);
   54 
   55   /// The offset at which the selection originates.
   56   ///
   57   /// Might be larger than, smaller than, or equal to extent.
   58   final int baseOffset;
   59 
   60   /// The offset at which the selection terminates.
   61   ///
   62   /// When the user uses the arrow keys to adjust the selection, this is the
   63   /// value that changes. Similarly, if the current theme paints a caret on one
   64   /// side of the selection, this is the location at which to paint the caret.
   65   ///
   66   /// Might be larger than, smaller than, or equal to base.
   67   final int extentOffset;
   68 
   69   /// If the text range is collapsed and has more than one visual location
   70   /// (e.g., occurs at a line break), which of the two locations to use when
   71   /// painting the caret.
   72   final TextAffinity affinity;
   73 
   74   /// Whether this selection has disambiguated its base and extent.
   75   ///
   76   /// On some platforms, the base and extent are not disambiguated until the
   77   /// first time the user adjusts the selection. At that point, either the start
   78   /// or the end of the selection becomes the base and the other one becomes the
   79   /// extent and is adjusted.
   80   final bool isDirectional;
   81 
   82   /// The position at which the selection originates.
   83   ///
   84   /// Might be larger than, smaller than, or equal to extent.
   85   TextPosition get base => TextPosition(offset: baseOffset, affinity: affinity);
   86 
   87   /// The position at which the selection terminates.
   88   ///
   89   /// When the user uses the arrow keys to adjust the selection, this is the
   90   /// value that changes. Similarly, if the current theme paints a caret on one
   91   /// side of the selection, this is the location at which to paint the caret.
   92   ///
   93   /// Might be larger than, smaller than, or equal to base.
   94   TextPosition get extent => TextPosition(offset: extentOffset, affinity: affinity);
   95 
   96   @override
   97   String toString() {
   98     return '${objectRuntimeType(this, 'TextSelection')}(baseOffset: $baseOffset, extentOffset: $extentOffset, affinity: $affinity, isDirectional: $isDirectional)';
   99   }
  100 
  101   @override
  102   bool operator ==(Object other) {
  103     if (identical(this, other))
  104       return true;
  105     return other is TextSelection
  106         && other.baseOffset == baseOffset
  107         && other.extentOffset == extentOffset
  108         && other.affinity == affinity
  109         && other.isDirectional == isDirectional;
  110   }
  111 
  112   @override
  113   int get hashCode => hashValues(
  114     baseOffset.hashCode,
  115     extentOffset.hashCode,
  116     affinity.hashCode,
  117     isDirectional.hashCode,
  118   );
  119 
  120   /// Creates a new [TextSelection] based on the current selection, with the
  121   /// provided parameters overridden.
  122   TextSelection copyWith({
  123     int? baseOffset,
  124     int? extentOffset,
  125     TextAffinity? affinity,
  126     bool? isDirectional,
  127   }) {
  128     return TextSelection(
  129       baseOffset: baseOffset ?? this.baseOffset,
  130       extentOffset: extentOffset ?? this.extentOffset,
  131       affinity: affinity ?? this.affinity,
  132       isDirectional: isDirectional ?? this.isDirectional,
  133     );
  134   }
  135 }