| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115 |
- using Avalonia.Input.Platform;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reactive.Linq;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Primitives;
- using Avalonia.Controls.Utils;
- using Avalonia.Input;
- using Avalonia.Interactivity;
- using Avalonia.Media;
- using Avalonia.Metadata;
- using Avalonia.Data;
- using Avalonia.Layout;
- using Avalonia.Utilities;
- namespace Avalonia.Controls
- {
- public class TextBox : TemplatedControl, UndoRedoHelper<TextBox.UndoRedoState>.IUndoRedoHost
- {
- public static readonly StyledProperty<bool> AcceptsReturnProperty =
- AvaloniaProperty.Register<TextBox, bool>(nameof(AcceptsReturn));
- public static readonly StyledProperty<bool> AcceptsTabProperty =
- AvaloniaProperty.Register<TextBox, bool>(nameof(AcceptsTab));
- public static readonly DirectProperty<TextBox, int> CaretIndexProperty =
- AvaloniaProperty.RegisterDirect<TextBox, int>(
- nameof(CaretIndex),
- o => o.CaretIndex,
- (o, v) => o.CaretIndex = v);
- public static readonly StyledProperty<bool> IsReadOnlyProperty =
- AvaloniaProperty.Register<TextBox, bool>(nameof(IsReadOnly));
- public static readonly StyledProperty<char> PasswordCharProperty =
- AvaloniaProperty.Register<TextBox, char>(nameof(PasswordChar));
- public static readonly StyledProperty<IBrush> SelectionBrushProperty =
- AvaloniaProperty.Register<TextBox, IBrush>(nameof(SelectionBrushProperty));
- public static readonly StyledProperty<IBrush> SelectionForegroundBrushProperty =
- AvaloniaProperty.Register<TextBox, IBrush>(nameof(SelectionForegroundBrushProperty));
- public static readonly StyledProperty<IBrush> CaretBrushProperty =
- AvaloniaProperty.Register<TextBox, IBrush>(nameof(CaretBrushProperty));
- public static readonly DirectProperty<TextBox, int> SelectionStartProperty =
- AvaloniaProperty.RegisterDirect<TextBox, int>(
- nameof(SelectionStart),
- o => o.SelectionStart,
- (o, v) => o.SelectionStart = v);
- public static readonly DirectProperty<TextBox, int> SelectionEndProperty =
- AvaloniaProperty.RegisterDirect<TextBox, int>(
- nameof(SelectionEnd),
- o => o.SelectionEnd,
- (o, v) => o.SelectionEnd = v);
- public static readonly StyledProperty<int> MaxLengthProperty =
- AvaloniaProperty.Register<TextBox, int>(nameof(MaxLength), defaultValue: 0);
- public static readonly DirectProperty<TextBox, string> TextProperty =
- TextBlock.TextProperty.AddOwnerWithDataValidation<TextBox>(
- o => o.Text,
- (o, v) => o.Text = v,
- defaultBindingMode: BindingMode.TwoWay,
- enableDataValidation: true);
- public static readonly StyledProperty<TextAlignment> TextAlignmentProperty =
- TextBlock.TextAlignmentProperty.AddOwner<TextBox>();
- /// <summary>
- /// Defines the <see cref="HorizontalAlignment"/> property.
- /// </summary>
- public static readonly StyledProperty<HorizontalAlignment> HorizontalContentAlignmentProperty =
- ContentControl.HorizontalContentAlignmentProperty.AddOwner<TextBox>();
- /// <summary>
- /// Defines the <see cref="VerticalAlignment"/> property.
- /// </summary>
- public static readonly StyledProperty<VerticalAlignment> VerticalContentAlignmentProperty =
- ContentControl.VerticalContentAlignmentProperty.AddOwner<TextBox>();
- public static readonly StyledProperty<TextWrapping> TextWrappingProperty =
- TextBlock.TextWrappingProperty.AddOwner<TextBox>();
- public static readonly StyledProperty<string> WatermarkProperty =
- AvaloniaProperty.Register<TextBox, string>(nameof(Watermark));
- public static readonly StyledProperty<bool> UseFloatingWatermarkProperty =
- AvaloniaProperty.Register<TextBox, bool>(nameof(UseFloatingWatermark));
- public static readonly DirectProperty<TextBox, string> NewLineProperty =
- AvaloniaProperty.RegisterDirect<TextBox, string>(nameof(NewLine),
- textbox => textbox.NewLine, (textbox, newline) => textbox.NewLine = newline);
- public static readonly StyledProperty<object> InnerLeftContentProperty =
- AvaloniaProperty.Register<TextBox, object>(nameof(InnerLeftContent));
- public static readonly StyledProperty<object> InnerRightContentProperty =
- AvaloniaProperty.Register<TextBox, object>(nameof(InnerRightContent));
- public static readonly StyledProperty<bool> ShowPasswordCharProperty =
- AvaloniaProperty.Register<TextBox, bool>(nameof(ShowPasswordChar));
- struct UndoRedoState : IEquatable<UndoRedoState>
- {
- public string Text { get; }
- public int CaretPosition { get; }
- public UndoRedoState(string text, int caretPosition)
- {
- Text = text;
- CaretPosition = caretPosition;
- }
- public bool Equals(UndoRedoState other) => ReferenceEquals(Text, other.Text) || Equals(Text, other.Text);
- }
- private string _text;
- private int _caretIndex;
- private int _selectionStart;
- private int _selectionEnd;
- private TextPresenter _presenter;
- private UndoRedoHelper<UndoRedoState> _undoRedoHelper;
- private bool _isUndoingRedoing;
- private bool _ignoreTextChanges;
- private string _newLine = Environment.NewLine;
- private static readonly string[] invalidCharacters = new String[1] { "\u007f" };
- static TextBox()
- {
- FocusableProperty.OverrideDefaultValue(typeof(TextBox), true);
- }
- public TextBox()
- {
- var horizontalScrollBarVisibility = Observable.CombineLatest(
- this.GetObservable(AcceptsReturnProperty),
- this.GetObservable(TextWrappingProperty),
- (acceptsReturn, wrapping) =>
- {
- if (acceptsReturn)
- {
- return wrapping != TextWrapping.Wrap ?
- ScrollBarVisibility.Auto :
- ScrollBarVisibility.Disabled;
- }
- else
- {
- return ScrollBarVisibility.Hidden;
- }
- });
- this.Bind(
- ScrollViewer.HorizontalScrollBarVisibilityProperty,
- horizontalScrollBarVisibility,
- BindingPriority.Style);
- _undoRedoHelper = new UndoRedoHelper<UndoRedoState>(this);
- }
- public bool AcceptsReturn
- {
- get { return GetValue(AcceptsReturnProperty); }
- set { SetValue(AcceptsReturnProperty, value); }
- }
- public bool AcceptsTab
- {
- get { return GetValue(AcceptsTabProperty); }
- set { SetValue(AcceptsTabProperty, value); }
- }
- public int CaretIndex
- {
- get
- {
- return _caretIndex;
- }
- set
- {
- value = CoerceCaretIndex(value);
- SetAndRaise(CaretIndexProperty, ref _caretIndex, value);
- UndoRedoState state;
- if (_undoRedoHelper.TryGetLastState(out state) && state.Text == Text)
- _undoRedoHelper.UpdateLastState();
- }
- }
- public bool IsReadOnly
- {
- get { return GetValue(IsReadOnlyProperty); }
- set { SetValue(IsReadOnlyProperty, value); }
- }
- public char PasswordChar
- {
- get => GetValue(PasswordCharProperty);
- set => SetValue(PasswordCharProperty, value);
- }
- public IBrush SelectionBrush
- {
- get => GetValue(SelectionBrushProperty);
- set => SetValue(SelectionBrushProperty, value);
- }
- public IBrush SelectionForegroundBrush
- {
- get => GetValue(SelectionForegroundBrushProperty);
- set => SetValue(SelectionForegroundBrushProperty, value);
- }
- public IBrush CaretBrush
- {
- get => GetValue(CaretBrushProperty);
- set => SetValue(CaretBrushProperty, value);
- }
- public int SelectionStart
- {
- get
- {
- return _selectionStart;
- }
- set
- {
- value = CoerceCaretIndex(value);
- SetAndRaise(SelectionStartProperty, ref _selectionStart, value);
- if (SelectionStart == SelectionEnd)
- {
- CaretIndex = SelectionStart;
- }
- }
- }
- public int SelectionEnd
- {
- get
- {
- return _selectionEnd;
- }
- set
- {
- value = CoerceCaretIndex(value);
- SetAndRaise(SelectionEndProperty, ref _selectionEnd, value);
- if (SelectionStart == SelectionEnd)
- {
- CaretIndex = SelectionEnd;
- }
- }
- }
- public int MaxLength
- {
- get { return GetValue(MaxLengthProperty); }
- set { SetValue(MaxLengthProperty, value); }
- }
- [Content]
- public string Text
- {
- get { return _text; }
- set
- {
- if (!_ignoreTextChanges)
- {
- var caretIndex = CaretIndex;
- SelectionStart = CoerceCaretIndex(SelectionStart, value);
- SelectionEnd = CoerceCaretIndex(SelectionEnd, value);
- CaretIndex = CoerceCaretIndex(caretIndex, value);
- if (SetAndRaise(TextProperty, ref _text, value) && !_isUndoingRedoing)
- {
- _undoRedoHelper.Clear();
- }
- }
- }
- }
- public string SelectedText
- {
- get { return GetSelection(); }
- set
- {
- _undoRedoHelper.Snapshot();
- if (string.IsNullOrEmpty(value))
- {
- DeleteSelection();
- }
- else
- {
- HandleTextInput(value);
- }
- _undoRedoHelper.Snapshot();
- }
- }
- /// <summary>
- /// Gets or sets the horizontal alignment of the content within the control.
- /// </summary>
- public HorizontalAlignment HorizontalContentAlignment
- {
- get { return GetValue(HorizontalContentAlignmentProperty); }
- set { SetValue(HorizontalContentAlignmentProperty, value); }
- }
- /// <summary>
- /// Gets or sets the vertical alignment of the content within the control.
- /// </summary>
- public VerticalAlignment VerticalContentAlignment
- {
- get { return GetValue(VerticalContentAlignmentProperty); }
- set { SetValue(VerticalContentAlignmentProperty, value); }
- }
- public TextAlignment TextAlignment
- {
- get { return GetValue(TextAlignmentProperty); }
- set { SetValue(TextAlignmentProperty, value); }
- }
- public string Watermark
- {
- get { return GetValue(WatermarkProperty); }
- set { SetValue(WatermarkProperty, value); }
- }
- public bool UseFloatingWatermark
- {
- get { return GetValue(UseFloatingWatermarkProperty); }
- set { SetValue(UseFloatingWatermarkProperty, value); }
- }
- public object InnerLeftContent
- {
- get { return GetValue(InnerLeftContentProperty); }
- set { SetValue(InnerLeftContentProperty, value); }
- }
- public object InnerRightContent
- {
- get { return GetValue(InnerRightContentProperty); }
- set { SetValue(InnerRightContentProperty, value); }
- }
- public bool ShowPasswordChar
- {
- get { return GetValue(ShowPasswordCharProperty); }
- set { SetValue(ShowPasswordCharProperty, value); }
- }
- public TextWrapping TextWrapping
- {
- get { return GetValue(TextWrappingProperty); }
- set { SetValue(TextWrappingProperty, value); }
- }
- /// <summary>
- /// Gets or sets which characters are inserted when Enter is pressed. Default: <see cref="Environment.NewLine"/>
- /// </summary>
- public string NewLine
- {
- get { return _newLine; }
- set { SetAndRaise(NewLineProperty, ref _newLine, value); }
- }
- protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
- {
- _presenter = e.NameScope.Get<TextPresenter>("PART_TextPresenter");
- if (IsFocused)
- {
- _presenter?.ShowCaret();
- }
- }
- protected override void OnGotFocus(GotFocusEventArgs e)
- {
- base.OnGotFocus(e);
- // when navigating to a textbox via the tab key, select all text if
- // 1) this textbox is *not* a multiline textbox
- // 2) this textbox has any text to select
- if (e.NavigationMethod == NavigationMethod.Tab &&
- !AcceptsReturn &&
- Text?.Length > 0)
- {
- SelectAll();
- }
- _presenter?.ShowCaret();
- }
- protected override void OnLostFocus(RoutedEventArgs e)
- {
- base.OnLostFocus(e);
- SelectionStart = 0;
- SelectionEnd = 0;
- _presenter?.HideCaret();
- }
- protected override void OnTextInput(TextInputEventArgs e)
- {
- if (!e.Handled)
- {
- HandleTextInput(e.Text);
- e.Handled = true;
- }
- }
- private void HandleTextInput(string input)
- {
- if (!IsReadOnly)
- {
- input = RemoveInvalidCharacters(input);
- string text = Text ?? string.Empty;
- int caretIndex = CaretIndex;
- if (!string.IsNullOrEmpty(input) && (MaxLength == 0 || input.Length + text.Length - (Math.Abs(SelectionStart - SelectionEnd)) <= MaxLength))
- {
- DeleteSelection();
- caretIndex = CaretIndex;
- text = Text ?? string.Empty;
- SetTextInternal(text.Substring(0, caretIndex) + input + text.Substring(caretIndex));
- CaretIndex += input.Length;
- SelectionStart = SelectionEnd = CaretIndex;
- _undoRedoHelper.DiscardRedo();
- }
- }
- }
- public string RemoveInvalidCharacters(string text)
- {
- for (var i = 0; i < invalidCharacters.Length; i++)
- {
- text = text.Replace(invalidCharacters[i], string.Empty);
- }
- return text;
- }
- private async void Copy()
- {
- await ((IClipboard)AvaloniaLocator.Current.GetService(typeof(IClipboard)))
- .SetTextAsync(GetSelection());
- }
- private async void Paste()
- {
- var text = await ((IClipboard)AvaloniaLocator.Current.GetService(typeof(IClipboard))).GetTextAsync();
- if (text == null)
- {
- return;
- }
- _undoRedoHelper.Snapshot();
- HandleTextInput(text);
- _undoRedoHelper.Snapshot();
- }
- protected override void OnKeyDown(KeyEventArgs e)
- {
- string text = Text ?? string.Empty;
- int caretIndex = CaretIndex;
- bool movement = false;
- bool selection = false;
- bool handled = false;
- var modifiers = e.KeyModifiers;
- var keymap = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>();
- bool Match(List<KeyGesture> gestures) => gestures.Any(g => g.Matches(e));
- bool DetectSelection() => e.KeyModifiers.HasFlag(keymap.SelectionModifiers);
- if (Match(keymap.SelectAll))
- {
- SelectAll();
- handled = true;
- }
- else if (Match(keymap.Copy))
- {
- if (!IsPasswordBox)
- {
- Copy();
- }
- handled = true;
- }
- else if (Match(keymap.Cut))
- {
- if (!IsPasswordBox)
- {
- _undoRedoHelper.Snapshot();
- Copy();
- DeleteSelection();
- _undoRedoHelper.Snapshot();
- }
- handled = true;
- }
- else if (Match(keymap.Paste))
- {
- Paste();
- handled = true;
- }
- else if (Match(keymap.Undo))
- {
- try
- {
- _isUndoingRedoing = true;
- _undoRedoHelper.Undo();
- }
- finally
- {
- _isUndoingRedoing = false;
- }
- handled = true;
- }
- else if (Match(keymap.Redo))
- {
- try
- {
- _isUndoingRedoing = true;
- _undoRedoHelper.Redo();
- }
- finally
- {
- _isUndoingRedoing = false;
- }
- handled = true;
- }
- else if (Match(keymap.MoveCursorToTheStartOfDocument))
- {
- MoveHome(true);
- movement = true;
- selection = false;
- handled = true;
- }
- else if (Match(keymap.MoveCursorToTheEndOfDocument))
- {
- MoveEnd(true);
- movement = true;
- selection = false;
- handled = true;
- }
- else if (Match(keymap.MoveCursorToTheStartOfLine))
- {
- MoveHome(false);
- movement = true;
- selection = false;
- handled = true;
- }
- else if (Match(keymap.MoveCursorToTheEndOfLine))
- {
- MoveEnd(false);
- movement = true;
- selection = false;
- handled = true;
- }
- else if (Match(keymap.MoveCursorToTheStartOfDocumentWithSelection))
- {
- MoveHome(true);
- movement = true;
- selection = true;
- handled = true;
- }
- else if (Match(keymap.MoveCursorToTheEndOfDocumentWithSelection))
- {
- MoveEnd(true);
- movement = true;
- selection = true;
- handled = true;
- }
- else if (Match(keymap.MoveCursorToTheStartOfLineWithSelection))
- {
- MoveHome(false);
- movement = true;
- selection = true;
- handled = true;
- }
- else if (Match(keymap.MoveCursorToTheEndOfLineWithSelection))
- {
- MoveEnd(false);
- movement = true;
- selection = true;
- handled = true;
- }
- else
- {
- bool hasWholeWordModifiers = modifiers.HasFlag(keymap.WholeWordTextActionModifiers);
- switch (e.Key)
- {
- case Key.Left:
- selection = DetectSelection();
- MoveHorizontal(-1, hasWholeWordModifiers, selection);
- movement = true;
- break;
- case Key.Right:
- selection = DetectSelection();
- MoveHorizontal(1, hasWholeWordModifiers, selection);
- movement = true;
- break;
- case Key.Up:
- movement = MoveVertical(-1);
- selection = DetectSelection();
- break;
- case Key.Down:
- movement = MoveVertical(1);
- selection = DetectSelection();
- break;
- case Key.Back:
- _undoRedoHelper.Snapshot();
- if (hasWholeWordModifiers && SelectionStart == SelectionEnd)
- {
- SetSelectionForControlBackspace();
- }
- if (!DeleteSelection() && CaretIndex > 0)
- {
- var removedCharacters = 1;
- // handle deleting /r/n
- // you don't ever want to leave a dangling /r around. So, if deleting /n, check to see if
- // a /r should also be deleted.
- if (CaretIndex > 1 &&
- text[CaretIndex - 1] == '\n' &&
- text[CaretIndex - 2] == '\r')
- {
- removedCharacters = 2;
- }
- SetTextInternal(text.Substring(0, caretIndex - removedCharacters) +
- text.Substring(caretIndex));
- CaretIndex -= removedCharacters;
- SelectionStart = SelectionEnd = CaretIndex;
- }
- _undoRedoHelper.Snapshot();
- handled = true;
- break;
- case Key.Delete:
- _undoRedoHelper.Snapshot();
- if (hasWholeWordModifiers && SelectionStart == SelectionEnd)
- {
- SetSelectionForControlDelete();
- }
- if (!DeleteSelection() && caretIndex < text.Length)
- {
- var removedCharacters = 1;
- // handle deleting /r/n
- // you don't ever want to leave a dangling /r around. So, if deleting /n, check to see if
- // a /r should also be deleted.
- if (CaretIndex < text.Length - 1 &&
- text[caretIndex + 1] == '\n' &&
- text[caretIndex] == '\r')
- {
- removedCharacters = 2;
- }
- SetTextInternal(text.Substring(0, caretIndex) +
- text.Substring(caretIndex + removedCharacters));
- }
- _undoRedoHelper.Snapshot();
- handled = true;
- break;
- case Key.Enter:
- if (AcceptsReturn)
- {
- _undoRedoHelper.Snapshot();
- HandleTextInput(NewLine);
- _undoRedoHelper.Snapshot();
- handled = true;
- }
- break;
- case Key.Tab:
- if (AcceptsTab)
- {
- _undoRedoHelper.Snapshot();
- HandleTextInput("\t");
- _undoRedoHelper.Snapshot();
- handled = true;
- }
- else
- {
- base.OnKeyDown(e);
- }
- break;
- default:
- handled = false;
- break;
- }
- }
- if (movement && selection)
- {
- SelectionEnd = CaretIndex;
- }
- else if (movement)
- {
- SelectionStart = SelectionEnd = CaretIndex;
- }
- if (handled || movement)
- {
- e.Handled = true;
- }
- }
- protected override void OnPointerPressed(PointerPressedEventArgs e)
- {
- var text = Text;
- var clickInfo = e.GetCurrentPoint(this);
- if (text != null && clickInfo.Properties.IsLeftButtonPressed && !(clickInfo.Pointer?.Captured is Border))
- {
- var point = e.GetPosition(_presenter);
- var index = CaretIndex = _presenter.GetCaretIndex(point);
- switch (e.ClickCount)
- {
- case 1:
- SelectionStart = SelectionEnd = index;
- break;
- case 2:
- if (!StringUtils.IsStartOfWord(text, index))
- {
- SelectionStart = StringUtils.PreviousWord(text, index);
- }
- SelectionEnd = StringUtils.NextWord(text, index);
- break;
- case 3:
- SelectAll();
- break;
- }
- }
- e.Pointer.Capture(_presenter);
- e.Handled = true;
- }
- protected override void OnPointerMoved(PointerEventArgs e)
- {
- // selection should not change during pointer move if the user right clicks
- if (_presenter != null && e.Pointer.Captured == _presenter && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
- {
- var point = e.GetPosition(_presenter);
- point = new Point(MathUtilities.Clamp(point.X, 0, _presenter.Bounds.Width - 1), MathUtilities.Clamp(point.Y, 0, _presenter.Bounds.Height - 1));
- CaretIndex = SelectionEnd = _presenter.GetCaretIndex(point);
- }
- }
- protected override void OnPointerReleased(PointerReleasedEventArgs e)
- {
- if (_presenter != null && e.Pointer.Captured == _presenter)
- {
- if (e.InitialPressMouseButton == MouseButton.Right)
- {
- var point = e.GetPosition(_presenter);
- var caretIndex = _presenter.GetCaretIndex(point);
- // see if mouse clicked inside current selection
- // if it did not, we change the selection to where the user clicked
- var firstSelection = Math.Min(SelectionStart, SelectionEnd);
- var lastSelection = Math.Max(SelectionStart, SelectionEnd);
- var didClickInSelection = SelectionStart != SelectionEnd &&
- caretIndex >= firstSelection && caretIndex <= lastSelection;
- if (!didClickInSelection)
- {
- CaretIndex = SelectionEnd = SelectionStart = caretIndex;
- }
- }
- e.Pointer.Capture(null);
- }
- }
- protected override void UpdateDataValidation<T>(AvaloniaProperty<T> property, BindingValue<T> value)
- {
- if (property == TextProperty)
- {
- DataValidationErrors.SetError(this, value.Error);
- }
- }
- private int CoerceCaretIndex(int value) => CoerceCaretIndex(value, Text);
- private int CoerceCaretIndex(int value, string text)
- {
- if (text == null)
- {
- return 0;
- }
- var length = text.Length;
- if (value < 0)
- {
- return 0;
- }
- else if (value > length)
- {
- return length;
- }
- else if (value > 0 && text[value - 1] == '\r' && value < length && text[value] == '\n')
- {
- return value + 1;
- }
- else
- {
- return value;
- }
- }
- public void Clear()
- {
- Text = string.Empty;
- }
- private int DeleteCharacter(int index)
- {
- var start = index + 1;
- var text = Text;
- var c = text[index];
- var result = 1;
- if (c == '\n' && index > 0 && text[index - 1] == '\r')
- {
- --index;
- ++result;
- }
- else if (c == '\r' && index < text.Length - 1 && text[index + 1] == '\n')
- {
- ++start;
- ++result;
- }
- Text = text.Substring(0, index) + text.Substring(start);
- return result;
- }
- private void MoveHorizontal(int direction, bool wholeWord, bool isSelecting)
- {
- var text = Text ?? string.Empty;
- var caretIndex = CaretIndex;
- if (!wholeWord)
- {
- if (SelectionStart != SelectionEnd && !isSelecting)
- {
- var start = Math.Min(SelectionStart, SelectionEnd);
- var end = Math.Max(SelectionStart, SelectionEnd);
- CaretIndex = direction < 0 ? start : end;
- return;
- }
- var index = caretIndex + direction;
- if (index < 0 || index > text.Length)
- {
- return;
- }
- else if (index == text.Length)
- {
- CaretIndex = index;
- return;
- }
- var c = text[index];
- if (direction > 0)
- {
- CaretIndex += (c == '\r' && index < text.Length - 1 && text[index + 1] == '\n') ? 2 : 1;
- }
- else
- {
- CaretIndex -= (c == '\n' && index > 0 && text[index - 1] == '\r') ? 2 : 1;
- }
- }
- else
- {
- if (direction > 0)
- {
- CaretIndex += StringUtils.NextWord(text, caretIndex) - caretIndex;
- }
- else
- {
- CaretIndex += StringUtils.PreviousWord(text, caretIndex) - caretIndex;
- }
- }
- }
- private bool MoveVertical(int count)
- {
- var formattedText = _presenter.FormattedText;
- var lines = formattedText.GetLines().ToList();
- var caretIndex = CaretIndex;
- var lineIndex = GetLine(caretIndex, lines) + count;
- if (lineIndex >= 0 && lineIndex < lines.Count)
- {
- var line = lines[lineIndex];
- var rect = formattedText.HitTestTextPosition(caretIndex);
- var y = count < 0 ? rect.Y : rect.Bottom;
- var point = new Point(rect.X, y + (count * (line.Height / 2)));
- var hit = formattedText.HitTestPoint(point);
- CaretIndex = hit.TextPosition + (hit.IsTrailing ? 1 : 0);
- return true;
- }
- else
- {
- return false;
- }
- }
- private void MoveHome(bool document)
- {
- var text = Text ?? string.Empty;
- var caretIndex = CaretIndex;
- if (document)
- {
- caretIndex = 0;
- }
- else
- {
- var lines = _presenter.FormattedText.GetLines();
- var pos = 0;
- foreach (var line in lines)
- {
- if (pos + line.Length > caretIndex || pos + line.Length == text.Length)
- {
- break;
- }
- pos += line.Length;
- }
- caretIndex = pos;
- }
- CaretIndex = caretIndex;
- }
- private void MoveEnd(bool document)
- {
- var text = Text ?? string.Empty;
- var caretIndex = CaretIndex;
- if (document)
- {
- caretIndex = text.Length;
- }
- else
- {
- var lines = _presenter.FormattedText.GetLines();
- var pos = 0;
- foreach (var line in lines)
- {
- pos += line.Length;
- if (pos > caretIndex)
- {
- if (pos < text.Length)
- {
- --pos;
- if (pos > 0 && text[pos - 1] == '\r' && text[pos] == '\n')
- {
- --pos;
- }
- }
- break;
- }
- }
- caretIndex = pos;
- }
- CaretIndex = caretIndex;
- }
- /// <summary>
- /// Select all text in the TextBox
- /// </summary>
- public void SelectAll()
- {
- SelectionStart = 0;
- SelectionEnd = Text?.Length ?? 0;
- CaretIndex = SelectionEnd;
- }
- private bool DeleteSelection()
- {
- if (!IsReadOnly)
- {
- var selectionStart = SelectionStart;
- var selectionEnd = SelectionEnd;
- if (selectionStart != selectionEnd)
- {
- var start = Math.Min(selectionStart, selectionEnd);
- var end = Math.Max(selectionStart, selectionEnd);
- var text = Text;
- SetTextInternal(text.Substring(0, start) + text.Substring(end));
- SelectionStart = SelectionEnd = CaretIndex = start;
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return true;
- }
- }
- private string GetSelection()
- {
- var text = Text;
- if (string.IsNullOrEmpty(text))
- return "";
- var selectionStart = SelectionStart;
- var selectionEnd = SelectionEnd;
- var start = Math.Min(selectionStart, selectionEnd);
- var end = Math.Max(selectionStart, selectionEnd);
- if (start == end || (Text?.Length ?? 0) < end)
- {
- return "";
- }
- return text.Substring(start, end - start);
- }
- private int GetLine(int caretIndex, IList<FormattedTextLine> lines)
- {
- int pos = 0;
- int i;
- for (i = 0; i < lines.Count - 1; ++i)
- {
- var line = lines[i];
- pos += line.Length;
- if (pos > caretIndex)
- {
- break;
- }
- }
- return i;
- }
- private void SetTextInternal(string value)
- {
- try
- {
- _ignoreTextChanges = true;
- SetAndRaise(TextProperty, ref _text, value);
- }
- finally
- {
- _ignoreTextChanges = false;
- }
- }
- private void SetSelectionForControlBackspace()
- {
- SelectionStart = CaretIndex;
- MoveHorizontal(-1, true, false);
- SelectionEnd = CaretIndex;
- }
- private void SetSelectionForControlDelete()
- {
- SelectionStart = CaretIndex;
- MoveHorizontal(1, true, false);
- SelectionEnd = CaretIndex;
- }
- private bool IsPasswordBox => PasswordChar != default(char);
- UndoRedoState UndoRedoHelper<UndoRedoState>.IUndoRedoHost.UndoRedoState
- {
- get { return new UndoRedoState(Text, CaretIndex); }
- set
- {
- Text = value.Text;
- SelectionStart = SelectionEnd = CaretIndex = value.CaretPosition;
- }
- }
- }
- }
|