Beispiel #1
0
        public TextSelectionOverlay(TextEditingValue value                  = null,
                                    BuildContext context                    = null, Widget debugRequiredFor = null,
                                    LayerLink layerLink                     = null,
                                    RenderEditable renderObject             = null,
                                    TextSelectionControls selectionControls = null,
                                    TextSelectionDelegate selectionDelegate = null,
                                    DragStartBehavior dragStartBehavior     = DragStartBehavior.start)
        {
            D.assert(value != null);
            D.assert(context != null);
            this.context           = context;
            this.debugRequiredFor  = debugRequiredFor;
            this.layerLink         = layerLink;
            this.renderObject      = renderObject;
            this.selectionControls = selectionControls;
            this.selectionDelegate = selectionDelegate;
            this._value            = value;
            OverlayState overlay = Overlay.of(context);

            D.assert(overlay != null, () => $"No Overlay widget exists above {context}.\n" +
                     "Usually the Navigator created by WidgetsApp provides the overlay. Perhaps your " +
                     "app content was created above the Navigator with the WidgetsApp builder parameter.");
            this._toolbarController = new AnimationController(duration: fadeDuration, vsync: overlay);
            this.dragStartBehavior  = dragStartBehavior;
        }
Beispiel #2
0
 bool _showSuffixWidget(TextEditingValue text)
 {
     return(widget.suffix != null && _shouldShowAttachment(
                attachment: widget.suffixMode,
                hasText: text.text.isNotEmpty()
                ));
 }
Beispiel #3
0
 bool _showPrefixWidget(TextEditingValue text)
 {
     return(this.widget.prefix != null && this._shouldShowAttachment(
                attachment: this.widget.prefixMode,
                hasText: text.text.isNotEmpty()
                ));
 }
Beispiel #4
0
 bool _showClearButton(TextEditingValue text)
 {
     return(_shouldShowAttachment(
                attachment: widget.clearButtonMode,
                hasText: text.text.isNotEmpty()
                ));
 }
Beispiel #5
0
 public _Editable(TextSpan textSpan                  = null, TextEditingValue value          = null,
                  Color cursorColor                  = null, ValueNotifier <bool> showCursor = null, bool hasFocus = false,
                  int?maxLines                       = null, Color selectionColor   = null, float textScaleFactor  = 1.0f,
                  TextDirection?textDirection        = null, bool obscureText       = false, TextAlign textAlign   = TextAlign.left,
                  bool autocorrect                   = false, ViewportOffset offset = null, SelectionChangedHandler onSelectionChanged = null,
                  CaretChangedHandler onCaretChanged = null, bool rendererIgnoresPointer = false,
                  Key key = null) : base(key)
 {
     this.textSpan               = textSpan;
     this.value                  = value;
     this.cursorColor            = cursorColor;
     this.showCursor             = showCursor;
     this.hasFocus               = hasFocus;
     this.maxLines               = maxLines;
     this.selectionColor         = selectionColor;
     this.textScaleFactor        = textScaleFactor;
     this.textAlign              = textAlign;
     this.textDirection          = textDirection;
     this.obscureText            = obscureText;
     this.autocorrect            = autocorrect;
     this.offset                 = offset;
     this.onSelectionChanged     = onSelectionChanged;
     this.onCaretChanged         = onCaretChanged;
     this.rendererIgnoresPointer = rendererIgnoresPointer;
 }
Beispiel #6
0
        void _formatAndSetValue(TextEditingValue value)
        {
            var textChanged = (this._value == null ? null : this._value.text) != (value == null ? null : value.text);

            if (this.widget.inputFormatters != null && this.widget.inputFormatters.isNotEmpty())
            {
                foreach (var formatter in this.widget.inputFormatters)
                {
                    value = formatter.formatEditUpdate(this._value, value);
                }

                this._value = value;
                this._updateRemoteEditingValueIfNeeded();
                this._updateImePosIfNeed();
            }
            else
            {
                this._value = value;
            }

            if (textChanged && this.widget.onChanged != null)
            {
                this.widget.onChanged(value.text);
            }
        }
Beispiel #7
0
        public override TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue)
        {
            var stringInfo = new StringInfo(value: newValue.text);

            if (this.maxLength != null && this.maxLength > 0 && stringInfo.LengthInTextElements > this.maxLength)
            {
                if (Input.compositionString.Length > 0)
                {
                    return(newValue);
                }

                var truncated = stringInfo.SubstringByTextElements(0, lengthInTextElements: this.maxLength.Value);

                TextSelection newSelection = newValue.selection.copyWith(
                    baseOffset: Mathf.Min(a: newValue.selection.start, b: truncated.Length),
                    extentOffset: Mathf.Min(a: newValue.selection.end, b: truncated.Length)
                    );

                return(new TextEditingValue(
                           text: truncated,
                           selection: newSelection,
                           composing: TextRange.empty
                           ));
            }

            return(newValue);
        }
Beispiel #8
0
        public override TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue)
        {
            TextEditingValue filteredValue = _filterFormatter.formatEditUpdate(oldValue, newValue);

            return(filteredValue.copyWith(
                       text: Regex.Replace(filteredValue.text, @"[\D]", separator)
                       ));
        }
        new bool canSelectAll(TextSelectionDelegate selectionDelegate)
        {
            TextEditingValue value = selectionDelegate.textEditingValue;

            return(selectionDelegate.selectAllEnabled &&
                   value.text.isNotEmpty() &&
                   !(value.selection.start == 0 && value.selection.end == value.text.Length));
        }
Beispiel #10
0
 void _closeInputConnectionIfNeeded()
 {
     if (this._hasInputConnection)
     {
         this._textInputConnection.close();
         this._textInputConnection             = null;
         this._lastKnownRemoteTextEditingValue = null;
     }
 }
Beispiel #11
0
        public override TextEditingValue formatEditUpdate(
            TextEditingValue oldValue,
            TextEditingValue newValue
            )
        {
            int           newTextLength      = newValue.text.Length;
            int           selectionIndex     = newValue.selection.end;
            int           usedSubstringIndex = 0;
            StringBuilder newText            = new StringBuilder();

            if (newTextLength >= 1)
            {
                newText.Append("(");
                if (newValue.selection.end >= 1)
                {
                    selectionIndex++;
                }
            }

            if (newTextLength >= 4)
            {
                newText.Append(newValue.text.Substring(0, usedSubstringIndex = 3) + ") ");
                if (newValue.selection.end >= 3)
                {
                    selectionIndex += 2;
                }
            }

            if (newTextLength >= 7)
            {
                newText.Append(newValue.text.Substring(3, usedSubstringIndex = 6) + "-");
                if (newValue.selection.end >= 6)
                {
                    selectionIndex++;
                }
            }

            if (newTextLength >= 11)
            {
                newText.Append(newValue.text.Substring(6, usedSubstringIndex = 10) + " ");
                if (newValue.selection.end >= 10)
                {
                    selectionIndex++;
                }
            }

            // Dump the rest.
            if (newTextLength >= usedSubstringIndex)
            {
                newText.Append(newValue.text.Substring(usedSubstringIndex));
            }
            return(new TextEditingValue(
                       text: newText.ToString(),
                       selection: TextSelection.collapsed(offset: selectionIndex)
                       ));
        }
Beispiel #12
0
        public override TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue)
        {
            if (!Regex.IsMatch(newValue.text, regexPattern))
            {
                IsMatch = false;
                return(new TextEditingValue(oldValue.text,
                                            selection: oldValue.selection,
                                            composing: TextRange.empty));
            }

            IsMatch = true;
            return(newValue);
        }
Beispiel #13
0
        public void handleCopy(TextSelectionDelegate selectionDelegate)
        {
            TextEditingValue value = selectionDelegate.textEditingValue;

            Clipboard.setData(new ClipboardData(
                                  text: value.selection.textInside(value.text)
                                  ));
            selectionDelegate.textEditingValue = new TextEditingValue(
                text: value.text,
                selection: TextSelection.collapsed(offset: value.selection.end)
                );
            selectionDelegate.bringIntoView(selectionDelegate.textEditingValue.selection.extendPos);
            selectionDelegate.hideToolbar();
        }
Beispiel #14
0
        public void updateEditingValue(TextEditingValue value)
        {
            if (value.text != this._value.text)
            {
                this._hideSelectionOverlayIfNeeded();
                this._showCaretOnScreen();
                if (this.widget.obscureText && value.text.Length == this._value.text.Length + 1)
                {
                    this._obscureShowCharTicksPending = _kObscureShowLatestCharCursorTicks;
                    this._obscureLatestCharIndex      = this._value.selection.baseOffset;
                }
            }

            this._lastKnownRemoteTextEditingValue = value;
            this._formatAndSetValue(value);
        }
Beispiel #15
0
        public void update(TextEditingValue newValue)
        {
            if (this._value == newValue)
            {
                return;
            }

            this._value = newValue;
            if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.persistentCallbacks)
            {
                SchedulerBinding.instance.addPostFrameCallback((duration) => this._markNeedsBuild());
            }
            else
            {
                this._markNeedsBuild();
            }
        }
Beispiel #16
0
        void _updateRemoteEditingValueIfNeeded()
        {
            if (!this._hasInputConnection)
            {
                return;
            }

            var localValue = this._value;

            if (localValue == this._lastKnownRemoteTextEditingValue)
            {
                return;
            }

            this._lastKnownRemoteTextEditingValue = localValue;
            this._textInputConnection.setEditingState(localValue);
        }
Beispiel #17
0
 void _openInputConnection()
 {
     if (!this._hasInputConnection)
     {
         TextEditingValue localValue = this._value;
         this._lastKnownRemoteTextEditingValue = localValue;
         this._textInputConnection             = Window.instance.textInput.attach(this, new TextInputConfiguration(
                                                                                      inputType: this.widget.keyboardType,
                                                                                      obscureText: this.widget.obscureText,
                                                                                      autocorrect: this.widget.autocorrect,
                                                                                      inputAction: this.widget.textInputAction ?? ((this.widget.keyboardType == TextInputType.multiline) ?
                                                                                                                                   TextInputAction.newline: TextInputAction.done)
                                                                                      ));
         this._textInputConnection.setEditingState(localValue);
     }
     this._textInputConnection.show();
 }
Beispiel #18
0
        public void handlePaste(TextSelectionDelegate selectionDelegate)
        {
            TextEditingValue value = selectionDelegate.textEditingValue; // Snapshot the input before using `await`.

            Clipboard.getData(Clipboard.kTextPlain).Then((data) => {
                if (data != null)
                {
                    selectionDelegate.textEditingValue = new TextEditingValue(
                        text: value.selection.textBefore(value.text)
                        + data.text
                        + value.selection.textAfter(value.text),
                        selection: TextSelection.collapsed(
                            offset: value.selection.start + data.text.Length
                            )
                        );
                }

                selectionDelegate.bringIntoView(selectionDelegate.textEditingValue.selection.extendPos);
                selectionDelegate.hideToolbar();
            });
        }
Beispiel #19
0
        public override void didChangeDependencies()
        {
            base.didChangeDependencies();
            if (_selectedDate != null)
            {
                MaterialLocalizations localizations = MaterialLocalizations.of(context);
                _inputText = localizations.formatCompactDate(_selectedDate.Value);
                TextEditingValue textEditingValue = _controller.value.copyWith(text: _inputText);

                if (widget.autofocus && !_autoSelected)
                {
                    textEditingValue = textEditingValue.copyWith(selection: new TextSelection(
                                                                     baseOffset: 0,
                                                                     extentOffset: _inputText.Length
                                                                     ));
                    _autoSelected = true;
                }

                _controller.value = textEditingValue;
            }
        }
Beispiel #20
0
        public TextSelectionOverlay(TextEditingValue value                  = null,
                                    BuildContext context                    = null, Widget debugRequiredFor = null,
                                    LayerLink layerLink                     = null,
                                    RenderEditable renderObject             = null,
                                    TextSelectionControls selectionControls = null,
                                    TextSelectionDelegate selectionDelegate = null)
        {
            D.assert(value != null);
            D.assert(context != null);
            this.context           = context;
            this.debugRequiredFor  = debugRequiredFor;
            this.layerLink         = layerLink;
            this.renderObject      = renderObject;
            this.selectionControls = selectionControls;
            this.selectionDelegate = selectionDelegate;
            this._value            = value;
            OverlayState overlay = Overlay.of(context);

            D.assert(overlay != null);
            this._handleController  = new AnimationController(duration: _fadeDuration, vsync: overlay);
            this._toolbarController = new AnimationController(duration: _fadeDuration, vsync: overlay);
        }
Beispiel #21
0
        void _openInputConnection()
        {
            if (!this._hasInputConnection)
            {
                TextEditingValue localValue = this._value;
                this._lastKnownRemoteTextEditingValue = localValue;
                this._textInputConnection             = TextInput.attach(this, new TextInputConfiguration(
                                                                             inputType: this.widget.keyboardType,
                                                                             obscureText: this.widget.obscureText,
                                                                             autocorrect: this.widget.autocorrect,
                                                                             inputAction: this.widget.textInputAction ?? ((this.widget.keyboardType == TextInputType.multiline)
                                     ? TextInputAction.newline
                                     : TextInputAction.done),
                                                                             textCapitalization: this.widget.textCapitalization,
                                                                             unityTouchKeyboard: this.widget.unityTouchKeyboard

                                                                             ));
                this._textInputConnection.setEditingState(localValue);
                this._updateImePosIfNeed();
            }

            this._textInputConnection.show();
        }
Beispiel #22
0
        public TextSelectionOverlay(
            TextEditingValue value                  = null,
            BuildContext context                    = null,
            Widget debugRequiredFor                 = null,
            LayerLink toolbarLayerLink              = null,
            LayerLink startHandleLayerLink          = null,
            LayerLink endHandleLayerLink            = null,
            RenderEditable renderObject             = null,
            TextSelectionControls selectionControls = null,
            bool?handlesVisible = false,
            TextSelectionDelegate selectionDelegate = null,
            DragStartBehavior dragStartBehavior     = DragStartBehavior.start,
            VoidCallback onSelectionHandleTapped    = null)
        {
            D.assert(value != null);
            D.assert(context != null);
            D.assert(handlesVisible != null);
            _handlesVisible              = handlesVisible.Value;
            this.context                 = context;
            this.debugRequiredFor        = debugRequiredFor;
            this.toolbarLayerLink        = toolbarLayerLink;
            this.startHandleLayerLink    = startHandleLayerLink;
            this.endHandleLayerLink      = endHandleLayerLink;
            this.renderObject            = renderObject;
            this.selectionControls       = selectionControls;
            this.selectionDelegate       = selectionDelegate;
            this.onSelectionHandleTapped = onSelectionHandleTapped;
            _value = value;
            OverlayState overlay = Overlay.of(context, rootOverlay: true);

            D.assert(overlay != null, () => $"No Overlay widget exists above {context}.\n" +
                     "Usually the Navigator created by WidgetsApp provides the overlay. Perhaps your " +
                     "app content was created above the Navigator with the WidgetsApp builder parameter.");
            _toolbarController     = new AnimationController(duration: fadeDuration, vsync: overlay);
            this.dragStartBehavior = dragStartBehavior;
        }
Beispiel #23
0
        void _handleShortcuts(KeyCode pressedKeyCode)
        {
            switch (pressedKeyCode)
            {
            case KeyCode.C:
                if (!this.selection.isCollapsed)
                {
                    Clipboard.setData(
                        new ClipboardData(text: this.selection.textInside(this.text.text)));
                }
                break;

            case KeyCode.X:
                if (!this.selection.isCollapsed)
                {
                    Clipboard.setData(
                        new ClipboardData(text: this.selection.textInside(this.text.text)));
                    this.textSelectionDelegate.textEditingValue = new TextEditingValue(
                        text: this.selection.textBefore(this.text.text)
                        + this.selection.textAfter(this.text.text),
                        selection: TextSelection.collapsed(offset: this.selection.start)
                        );
                }
                break;

            case KeyCode.V:
                TextEditingValue value = this.textSelectionDelegate.textEditingValue;
                Clipboard.getData(Clipboard.kTextPlain).Then(data => {
                    if (data != null)
                    {
                        this.textSelectionDelegate.textEditingValue = new TextEditingValue(
                            text: value.selection.textBefore(value.text)
                            + data.text
                            + value.selection.textAfter(value.text),
                            selection: TextSelection.collapsed(
                                offset: value.selection.start + data.text.Length
                                )
                            );
                    }
                });

                break;

            case KeyCode.A:
                this._baseOffset   = 0;
                this._extentOffset = this.textSelectionDelegate.textEditingValue.text.Length;
                this.onSelectionChanged(
                    new TextSelection(
                        baseOffset: 0,
                        extentOffset: this.textSelectionDelegate.textEditingValue.text.Length
                        ),
                    this,
                    SelectionChangedCause.keyboard
                    );
                break;

            default:
                D.assert(false);
                break;
            }
        }
Beispiel #24
0
 static int _codeUnitLengthAt(TextEditingValue value)
 {
     return(value.selection.start > 1 && char.IsSurrogate(value.text[value.selection.start - 1]) ? 2 : 1);
 }
Beispiel #25
0
        public TextEditingValue getValueForAction(TextInputAction operation)
        {
            TextPosition     newPosition  = null;
            TextPosition     newExtend    = null;
            TextEditingValue newValue     = null;
            TextSelection    newSelection = null;
            TextPosition     startPos     = new TextPosition(this._value.selection.start, this._value.selection.affinity);

            switch (operation)
            {
            case TextInputAction.moveLeft:
                newValue = this._value.moveLeft();
                break;

            case TextInputAction.moveRight:
                newValue = this._value.moveRight();
                break;

            case TextInputAction.moveUp:
                newPosition = this.renderEditable.getPositionUp(startPos);
                break;

            case TextInputAction.moveDown:
                newPosition = this.renderEditable.getPositionDown(startPos);
                break;

            case TextInputAction.moveLineStart:
                newPosition = this.renderEditable.getParagraphStart(startPos, TextAffinity.downstream);
                break;

            case TextInputAction.moveLineEnd:
                newPosition = this.renderEditable.getParagraphEnd(startPos, TextAffinity.upstream);
                break;

            case TextInputAction.moveWordRight:
                newPosition = this.renderEditable.getWordRight(startPos);
                break;

            case TextInputAction.moveWordLeft:
                newPosition = this.renderEditable.getWordLeft(startPos);
                break;

//                case TextInputAction.MoveToStartOfNextWord:      MoveToStartOfNextWord(); break;
//                case TextInputAction.MoveToEndOfPreviousWord:        MoveToEndOfPreviousWord(); break;
            case TextInputAction.moveTextStart:
                newPosition = new TextPosition(0);
                break;

            case TextInputAction.moveTextEnd:
                newPosition = new TextPosition(this._value.text.Length);
                break;

            case TextInputAction.moveParagraphForward:
                newPosition = this.renderEditable.getParagraphForward(startPos);
                break;

            case TextInputAction.moveParagraphBackward:
                newPosition = this.renderEditable.getParagraphBackward(startPos);
                break;

            case TextInputAction.moveGraphicalLineStart:
                newPosition = this.renderEditable.getLineStartPosition(startPos, TextAffinity.downstream);
                break;

            case TextInputAction.moveGraphicalLineEnd:
                newPosition = this.renderEditable.getLineEndPosition(startPos, TextAffinity.upstream);
                break;

            case TextInputAction.selectLeft:
                newValue = this._value.extendLeft();
                break;

            case TextInputAction.selectRight:
                newValue = this._value.extendRight();
                break;

            case TextInputAction.selectUp:
                newExtend = this.renderEditable.getPositionUp(this._value.selection.extendPos);
                break;

            case TextInputAction.selectDown:
                newExtend = this.renderEditable.getPositionDown(this._value.selection.extendPos);
                break;

            case TextInputAction.selectWordRight:
                newExtend = this.renderEditable.getWordRight(this._value.selection.extendPos);
                break;

            case TextInputAction.selectWordLeft:
                newExtend = this.renderEditable.getWordLeft(this._value.selection.extendPos);
                break;

//                case TextInputAction.SelectToEndOfPreviousWord:  SelectToEndOfPreviousWord(); break;
//                case TextInputAction.SelectToStartOfNextWord:    SelectToStartOfNextWord(); break;
//
            case TextInputAction.selectTextStart:
                newExtend = new TextPosition(0);
                break;

            case TextInputAction.selectTextEnd:
                newExtend = new TextPosition(this._value.text.Length);
                break;

            case TextInputAction.expandSelectGraphicalLineStart:
                if (this._value.selection.isCollapsed ||
                    !this.renderEditable.isLineEndOrStart(this._value.selection.start))
                {
                    newSelection = new TextSelection(this.renderEditable.getLineStartPosition(startPos).offset,
                                                     this._value.selection.end, this._value.selection.affinity);
                }

                break;

            case TextInputAction.expandSelectGraphicalLineEnd:
                if (this._value.selection.isCollapsed ||
                    !this.renderEditable.isLineEndOrStart(this._value.selection.end))
                {
                    newSelection = new TextSelection(this._value.selection.start,
                                                     this.renderEditable.getLineEndPosition(this._value.selection.endPos).offset,
                                                     this._value.selection.affinity);
                }

                break;

            case TextInputAction.selectParagraphForward:
                newExtend = this.renderEditable.getParagraphForward(this._value.selection.extendPos);
                break;

            case TextInputAction.selectParagraphBackward:
                newExtend = this.renderEditable.getParagraphBackward(this._value.selection.extendPos);
                break;

            case TextInputAction.selectGraphicalLineStart:
                newExtend = this.renderEditable.getLineStartPosition(this._value.selection.extendPos);
                break;

            case TextInputAction.selectGraphicalLineEnd:
                newExtend = this.renderEditable.getLineEndPosition(startPos);
                break;

            case TextInputAction.delete:
                newValue = this._value.deleteSelection(false);
                break;

            case TextInputAction.backspace:
                newValue = this._value.deleteSelection();
                break;

            case TextInputAction.selectAll:
                newSelection = this._value.selection.copyWith(baseOffset: 0, extentOffset: this._value.text.Length);
                break;
            }

            if (newPosition != null)
            {
                return(this._value.copyWith(selection: TextSelection.fromPosition(newPosition)));
            }
            else if (newExtend != null)
            {
                return(this._value.copyWith(selection: this._value.selection.copyWith(extentOffset: newExtend.offset)));
            }
            else if (newSelection != null)
            {
                return(this._value.copyWith(selection: newSelection));
            }
            else if (newValue != null)
            {
                return(newValue);
            }

            return(this._value);
        }