Ejemplo n.º 1
0
        public TextEditingValue deleteSelection(bool backDelete = true)
        {
            if (this.selection.isCollapsed)
            {
                if (backDelete)
                {
                    if (this.selection.start == 0)
                    {
                        return(this);
                    }

                    return(this.copyWith(
                               text: this.text.Substring(0, this.selection.start - 1) + this.selection.textAfter(this.text),
                               selection: TextSelection.collapsed(this.selection.start - 1)));
                }

                if (this.selection.start >= this.text.Length)
                {
                    return(this);
                }

                return(this.copyWith(text: this.text.Substring(0, this.selection.start) +
                                     this.text.Substring(this.selection.start + 1)));
            }
            else
            {
                var newText = this.selection.textBefore(this.text) + this.selection.textAfter(this.text);
                return(this.copyWith(text: newText, selection: TextSelection.collapsed(this.selection.start)));
            }
        }
Ejemplo n.º 2
0
        public TextEditingValue(string text = "", TextSelection selection = null, TextRange composing = null)
        {
            this.text      = text;
            this.composing = composing ?? TextRange.empty;

            if (selection != null && selection.start >= 0 && selection.end >= 0)
            {
                // handle surrogate pair emoji, which takes 2 utf16 chars
                // if selection cuts in the middle of the emoji, move it to the end
                int start = selection.start, end = selection.end;
                if (start < text.Length && char.IsLowSurrogate(text[start]))
                {
                    start++;
                }
                if (end < text.Length && char.IsLowSurrogate(text[end]))
                {
                    end++;
                }
                this.selection = selection.copyWith(start, end);
            }
            else
            {
                this.selection = TextSelection.collapsed(-1);
            }
        }
Ejemplo n.º 3
0
        //unity-specific
        public TextEditingValue insert(string text)
        {
            string        newText;
            TextSelection newSelection;

            if (string.IsNullOrEmpty(text))
            {
                return(this);
            }

            var selection = this.selection;

            if (selection.start < 0)
            {
                selection = TextSelection.collapsed(0, this.selection.affinity);
            }

            newText      = selection.textBefore(this.text) + text + selection.textAfter(this.text);
            newSelection = TextSelection.collapsed(selection.start + text.Length);
            return(new TextEditingValue(
                       text: newText,
                       selection: newSelection,
                       composing: TextRange.empty
                       ));
        }
Ejemplo n.º 4
0
        public TextEditingValue moveSelection(int move)
        {
            int offset = this.selection.baseOffset + move;

            offset = Mathf.Max(0, offset);
            offset = Mathf.Min(offset, this.text.Length);
            return(this.copyWith(selection: TextSelection.collapsed(offset, affinity: this.selection.affinity)));
        }
Ejemplo n.º 5
0
        public TextEditingValue deleteSelection(bool backDelete = true)
        {
            if (this.selection.isCollapsed)
            {
                if (backDelete)
                {
                    if (this.selection.start == 0)
                    {
                        return(this);
                    }

                    if (char.IsHighSurrogate(this.text[this.selection.start - 1]))
                    {
                        return(this.copyWith(
                                   text: this.text.Substring(0, this.selection.start - 1) +
                                   this.text.Substring(this.selection.start + 1),
                                   selection: TextSelection.collapsed(this.selection.start - 1),
                                   composing: TextRange.empty));
                    }

                    if (char.IsLowSurrogate(this.text[this.selection.start - 1]))
                    {
                        D.assert(this.selection.start > 1);
                        return(this.copyWith(
                                   text: this.text.Substring(0, this.selection.start - 2) +
                                   this.selection.textAfter(this.text),
                                   selection: TextSelection.collapsed(this.selection.start - 2),
                                   composing: TextRange.empty));
                    }

                    return(this.copyWith(
                               text: this.text.Substring(0, this.selection.start - 1) + this.selection.textAfter(this.text),
                               selection: TextSelection.collapsed(this.selection.start - 1),
                               composing: TextRange.empty));
                }

                if (this.selection.start >= this.text.Length)
                {
                    return(this);
                }

                return(this.copyWith(text: this.text.Substring(0, this.selection.start) +
                                     this.text.Substring(this.selection.start + 1),
                                     composing: TextRange.empty));
            }
            else
            {
                var newText = this.selection.textBefore(this.text) + this.selection.textAfter(this.text);
                return(this.copyWith(text: newText, selection: TextSelection.collapsed(this.selection.start),
                                     composing: TextRange.empty));
            }
        }
Ejemplo n.º 6
0
        public TextEditingValue clearCompose()
        {
            if (this.composing == TextRange.empty)
            {
                return(this);
            }

            return(new TextEditingValue(
                       text: this.text.Substring(0, this.composing.start) + this.text.Substring(this.composing.end),
                       selection: TextSelection.collapsed(this.composing.start),
                       composing: TextRange.empty
                       ));
        }
Ejemplo n.º 7
0
        public TextEditingValue compose(string composeText)
        {
            D.assert(!string.IsNullOrEmpty(composeText));
            var composeStart   = this.composing == TextRange.empty ? this.selection.start : this.composing.start;
            var lastComposeEnd = this.composing == TextRange.empty ? this.selection.end : this.composing.end;
            var newText        = this.text.Substring(0, composeStart) + composeText + this.text.Substring(lastComposeEnd);
            var componseEnd    = composeStart + composeText.Length;

            return(new TextEditingValue(
                       text: newText, selection: TextSelection.collapsed(componseEnd),
                       composing: new TextRange(composeStart, componseEnd)
                       ));
        }
Ejemplo n.º 8
0
        internal static TextEditingValue _selectionAwareTextManipulation(TextEditingValue value,
                                                                         Func <string, string> substringManipulation)
        {
            int           selectionStartIndex = value.selection.start;
            int           selectionEndIndex   = value.selection.end;
            string        manipulatedText;
            TextSelection manipulatedSelection = null;

            if (selectionStartIndex < 0 || selectionEndIndex < 0)
            {
                manipulatedText = substringManipulation(value.text);
            }
            else
            {
                var beforeSelection = substringManipulation(
                    value.text.Substring(0, selectionStartIndex)
                    );
                var inSelection = substringManipulation(
                    value.text.Substring(selectionStartIndex, selectionEndIndex - selectionStartIndex)
                    );
                var afterSelection = substringManipulation(
                    value.text.Substring(selectionEndIndex)
                    );
                manipulatedText = beforeSelection + inSelection + afterSelection;
                if (value.selection.baseOffset > value.selection.extentOffset)
                {
                    manipulatedSelection = value.selection.copyWith(
                        baseOffset: beforeSelection.Length + inSelection.Length,
                        extentOffset: beforeSelection.Length
                        );
                }
                else
                {
                    manipulatedSelection = value.selection.copyWith(
                        baseOffset: beforeSelection.Length,
                        extentOffset: beforeSelection.Length + inSelection.Length
                        );
                }
            }

            return(new TextEditingValue(
                       text: manipulatedText,
                       selection: manipulatedSelection ?? TextSelection.collapsed(offset: -1),
                       composing: manipulatedText == value.text ? value.composing : TextRange.empty
                       ));
        }
Ejemplo n.º 9
0
        public void Update()
        {
            if (this._client == 0 || this._keyboard == null)
            {
                return;
            }

            if (this._keyboard.canSetSelection && this._pendingSelection != null)
            {
                this._keyboard.selection = this._pendingSelection.Value;
                this._pendingSelection   = null;
            }

            if (this._keyboard.status == TouchScreenKeyboard.Status.Done)
            {
                if (!this._screenKeyboardDone)
                {
                    this._screenKeyboardDone = true;
                    Window.instance.run(() => {
                        TextInput._performAction(this._client,
                                                 TextInputAction.done);
                    });
                }
            }
            else if (this._keyboard.status == TouchScreenKeyboard.Status.Visible)
            {
                var keyboardSelection = this._keyboard.selection;
                var newValue          = new TextEditingValue(
                    this._keyboard.text,
                    this._keyboard.canGetSelection
                        ? new TextSelection(keyboardSelection.start, keyboardSelection.end)
                        : TextSelection.collapsed(0)
                    );
                var changed = this._value != newValue;

                this._value = newValue;
                if (changed)
                {
                    Window.instance.run(() => {
                        TextInput._updateEditingState(this._client,
                                                      this._value);
                    });
                }
            }
        }
Ejemplo n.º 10
0
        public void Update()
        {
            if (_client == 0 || _keyboard == null)
            {
                return;
            }

            if (_keyboard.canSetSelection && _pendingSelection != null)
            {
                _keyboard.selection = _pendingSelection.Value;
                _pendingSelection   = null;
            }

            if (_keyboard.status == TouchScreenKeyboard.Status.Done)
            {
                if (!_screenKeyboardDone)
                {
                    _screenKeyboardDone = true;
                    Timer.create(TimeSpan.Zero, () => {
                        TextInput._performAction(_client,
                                                 TextInputAction.done);
                    });
                }
            }
            else if (_keyboard.status == TouchScreenKeyboard.Status.Visible)
            {
                var keyboardSelection = _keyboard.selection;
                var newValue          = new TextEditingValue(
                    _keyboard.text,
                    _keyboard.canGetSelection
                        ? new TextSelection(keyboardSelection.start, keyboardSelection.end)
                        : TextSelection.collapsed(0)
                    );
                var changed = _value != newValue;

                _value = newValue;
                if (changed)
                {
                    Timer.create(TimeSpan.Zero, () => {
                        TextInput._updateEditingState(_client,
                                                      _value);
                    });
                }
            }
        }
Ejemplo n.º 11
0
 public TextEditingValue(string text = "", TextSelection selection = null, TextRange composing = null)
 {
     this.text      = text;
     this.selection = selection ?? TextSelection.collapsed(-1);
     this.composing = composing ?? TextRange.empty;
 }