| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- // Copyright (c) The Perspex Project. All rights reserved.
- // Licensed under the MIT license. See licence.md file in the project root for full license information.
- using Perspex.Input.Platform;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reactive.Linq;
- using Perspex.Controls.Presenters;
- using Perspex.Controls.Primitives;
- using Perspex.Controls.Templates;
- using Perspex.Controls.Utils;
- using Perspex.Input;
- using Perspex.Interactivity;
- using Perspex.Media;
- using Perspex.Metadata;
- namespace Perspex.Controls
- {
- public class TextBox : TemplatedControl
- {
- public static readonly PerspexProperty<bool> AcceptsReturnProperty =
- PerspexProperty.Register<TextBox, bool>("AcceptsReturn");
- public static readonly PerspexProperty<bool> AcceptsTabProperty =
- PerspexProperty.Register<TextBox, bool>("AcceptsTab");
- public static readonly PerspexProperty<int> CaretIndexProperty =
- PerspexProperty.Register<TextBox, int>("CaretIndex", validate: ValidateCaretIndex);
- public static readonly PerspexProperty<int> SelectionStartProperty =
- PerspexProperty.Register<TextBox, int>("SelectionStart", validate: ValidateCaretIndex);
- public static readonly PerspexProperty<int> SelectionEndProperty =
- PerspexProperty.Register<TextBox, int>("SelectionEnd", validate: ValidateCaretIndex);
- public static readonly PerspexProperty<string> TextProperty =
- TextBlock.TextProperty.AddOwner<TextBox>();
- public static readonly PerspexProperty<TextAlignment> TextAlignmentProperty =
- TextBlock.TextAlignmentProperty.AddOwner<TextBox>();
- public static readonly PerspexProperty<TextWrapping> TextWrappingProperty =
- TextBlock.TextWrappingProperty.AddOwner<TextBox>();
- public static readonly PerspexProperty<string> WatermarkProperty =
- PerspexProperty.Register<TextBox, string>("Watermark");
- public static readonly PerspexProperty<bool> UseFloatingWatermarkProperty =
- PerspexProperty.Register<TextBox, bool>("UseFloatingWatermark");
- private TextPresenter _presenter;
- static TextBox()
- {
- FocusableProperty.OverrideDefaultValue(typeof(TextBox), true);
- }
- public TextBox()
- {
- var canScrollHorizontally = GetObservable(AcceptsReturnProperty)
- .Select(x => !x);
- Bind(
- ScrollViewer.CanScrollHorizontallyProperty,
- canScrollHorizontally,
- BindingPriority.Style);
- var horizontalScrollBarVisibility = GetObservable(AcceptsReturnProperty)
- .Select(x => x ? ScrollBarVisibility.Auto : ScrollBarVisibility.Hidden);
- Bind(
- ScrollViewer.HorizontalScrollBarVisibilityProperty,
- horizontalScrollBarVisibility,
- BindingPriority.Style);
- }
- 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 GetValue(CaretIndexProperty); }
- set { SetValue(CaretIndexProperty, value); }
- }
- public int SelectionStart
- {
- get { return GetValue(SelectionStartProperty); }
- set { SetValue(SelectionStartProperty, value); }
- }
- public int SelectionEnd
- {
- get { return GetValue(SelectionEndProperty); }
- set { SetValue(SelectionEndProperty, value); }
- }
- [Content]
- public string Text
- {
- get { return GetValue(TextProperty); }
- set { SetValue(TextProperty, 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 TextWrapping TextWrapping
- {
- get { return GetValue(TextWrappingProperty); }
- set { SetValue(TextWrappingProperty, value); }
- }
- protected override void OnTemplateApplied(INameScope nameScope)
- {
- _presenter = nameScope.Get<TextPresenter>("PART_TextPresenter");
- _presenter.Cursor = new Cursor(StandardCursorType.Ibeam);
- }
- protected override void OnGotFocus(GotFocusEventArgs e)
- {
- base.OnGotFocus(e);
- _presenter.ShowCaret();
- }
- protected override void OnLostFocus(RoutedEventArgs e)
- {
- base.OnLostFocus(e);
- SelectionStart = 0;
- SelectionEnd = 0;
- _presenter.HideCaret();
- }
- protected override void OnTextInput(TextInputEventArgs e)
- {
- HandleTextInput(e.Text);
- }
- private void HandleTextInput(string input)
- {
- string text = Text ?? string.Empty;
- int caretIndex = CaretIndex;
- if (!string.IsNullOrEmpty(input))
- {
- DeleteSelection();
- caretIndex = CaretIndex;
- text = Text ?? string.Empty;
- Text = text.Substring(0, caretIndex) + input + text.Substring(caretIndex);
- CaretIndex += input.Length;
- SelectionStart = SelectionEnd = CaretIndex;
- }
- }
- private async void Copy()
- {
- await ((IClipboard)PerspexLocator.Current.GetService(typeof(IClipboard)))
- .SetTextAsync(GetSelection());
- }
- private async void Paste()
- {
- var text = await ((IClipboard)PerspexLocator.Current.GetService(typeof(IClipboard))).GetTextAsync();
- if (text == null)
- {
- return;
- }
- HandleTextInput(text);
- }
- protected override void OnKeyDown(KeyEventArgs e)
- {
- string text = Text ?? string.Empty;
- int caretIndex = CaretIndex;
- bool movement = false;
- bool handled = true;
- var modifiers = e.Modifiers;
- switch (e.Key)
- {
- case Key.A:
- if (modifiers == InputModifiers.Control)
- {
- SelectAll();
- }
- break;
- case Key.C:
- if (modifiers == InputModifiers.Control)
- {
- Copy();
- }
- break;
- case Key.V:
- if (modifiers == InputModifiers.Control)
- {
- Paste();
- }
- break;
- case Key.Left:
- MoveHorizontal(-1, modifiers);
- movement = true;
- break;
- case Key.Right:
- MoveHorizontal(1, modifiers);
- movement = true;
- break;
- case Key.Up:
- MoveVertical(-1, modifiers);
- movement = true;
- break;
- case Key.Down:
- MoveVertical(1, modifiers);
- movement = true;
- break;
- case Key.Home:
- MoveHome(modifiers);
- movement = true;
- break;
- case Key.End:
- MoveEnd(modifiers);
- movement = true;
- break;
- case Key.Back:
- if (!DeleteSelection() && CaretIndex > 0)
- {
- Text = text.Substring(0, caretIndex - 1) + text.Substring(caretIndex);
- --CaretIndex;
- }
- break;
- case Key.Delete:
- if (!DeleteSelection() && caretIndex < text.Length)
- {
- Text = text.Substring(0, caretIndex) + text.Substring(caretIndex + 1);
- }
- break;
- case Key.Enter:
- if (AcceptsReturn)
- {
- HandleTextInput("\r\n");
- }
- break;
- case Key.Tab:
- if (AcceptsTab)
- {
- HandleTextInput("\t");
- }
- else
- {
- base.OnKeyDown(e);
- handled = false;
- }
- break;
- }
- if (movement && ((modifiers & InputModifiers.Shift) != 0))
- {
- SelectionEnd = CaretIndex;
- }
- else if (movement)
- {
- SelectionStart = SelectionEnd = CaretIndex;
- }
- if (handled)
- {
- e.Handled = true;
- }
- }
- protected override void OnPointerPressed(PointerPressEventArgs e)
- {
- if (e.Source == _presenter)
- {
- var point = e.GetPosition(_presenter);
- var index = CaretIndex = _presenter.GetCaretIndex(point);
- var text = Text;
- switch (e.ClickCount)
- {
- case 1:
- SelectionStart = SelectionEnd = index;
- break;
- case 2:
- if (!StringUtils.IsStartOfWord(text, index))
- {
- SelectionStart = StringUtils.PreviousWord(text, index, false);
- }
- SelectionEnd = StringUtils.NextWord(text, index, false);
- break;
- case 3:
- SelectionStart = 0;
- SelectionEnd = text.Length;
- break;
- }
- e.Device.Capture(_presenter);
- e.Handled = true;
- }
- }
- protected override void OnPointerMoved(PointerEventArgs e)
- {
- if (_presenter != null && e.Device.Captured == _presenter)
- {
- var point = e.GetPosition(_presenter);
- CaretIndex = SelectionEnd = _presenter.GetCaretIndex(point);
- }
- }
- protected override void OnPointerReleased(PointerEventArgs e)
- {
- if (_presenter != null && e.Device.Captured == _presenter)
- {
- e.Device.Capture(null);
- }
- }
- private static int ValidateCaretIndex(PerspexObject o, int value)
- {
- var text = o.GetValue(TextProperty);
- var length = (text != null) ? text.Length : 0;
- return Math.Max(0, Math.Min(length, value));
- }
- private void MoveHorizontal(int count, InputModifiers modifiers)
- {
- var text = Text ?? string.Empty;
- var caretIndex = CaretIndex;
- if ((modifiers & InputModifiers.Control) != 0)
- {
- if (count > 0)
- {
- count = StringUtils.NextWord(text, caretIndex, false) - caretIndex;
- }
- else
- {
- count = StringUtils.PreviousWord(text, caretIndex, false) - caretIndex;
- }
- }
- CaretIndex = caretIndex += count;
- }
- private void MoveVertical(int count, InputModifiers modifiers)
- {
- 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);
- }
- }
- private void MoveHome(InputModifiers modifiers)
- {
- var text = Text ?? string.Empty;
- var caretIndex = CaretIndex;
- if ((modifiers & InputModifiers.Control) != 0)
- {
- 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(InputModifiers modifiers)
- {
- var text = Text ?? string.Empty;
- var caretIndex = CaretIndex;
- if ((modifiers & InputModifiers.Control) != 0)
- {
- 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;
- }
- break;
- }
- }
- caretIndex = pos;
- }
- CaretIndex = caretIndex;
- }
- private void SelectAll()
- {
- SelectionStart = 0;
- SelectionEnd = Text.Length;
- }
- private bool DeleteSelection()
- {
- var selectionStart = SelectionStart;
- var selectionEnd = SelectionEnd;
- if (selectionStart != selectionEnd)
- {
- var start = Math.Min(selectionStart, selectionEnd);
- var end = Math.Max(selectionStart, selectionEnd);
- var text = Text;
- Text = text.Substring(0, start) + text.Substring(end);
- SelectionStart = SelectionEnd = CaretIndex = start;
- return true;
- }
- else
- {
- return false;
- }
- }
- private string GetSelection()
- {
- 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; ++i)
- {
- var line = lines[i];
- pos += line.Length;
- if (pos > caretIndex)
- {
- break;
- }
- }
- return i;
- }
- }
- }
|