diff --git a/Perspex.Controls/TextBox.cs b/Perspex.Controls/TextBox.cs index 58800ba3e6..74f27c128a 100644 --- a/Perspex.Controls/TextBox.cs +++ b/Perspex.Controls/TextBox.cs @@ -108,14 +108,23 @@ namespace Perspex.Controls private void MoveHorizontal(int count, ModifierKeys modifiers) { - if (modifiers == ModifierKeys.None) + var text = this.Text ?? string.Empty; + var caretIndex = this.CaretIndex; + + if ((modifiers & ModifierKeys.Control) != 0) + { + count = this.NextWord(text, count, caretIndex); + } + + this.CaretIndex = caretIndex += count; + + if ((modifiers & ModifierKeys.Shift) == 0) { - this.CaretIndex += count; this.SelectionStart = this.SelectionEnd = this.CaretIndex; } - else if ((modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) + else { - this.SelectionEnd = (this.CaretIndex += count); + this.SelectionEnd = this.CaretIndex; } } @@ -143,6 +152,14 @@ namespace Perspex.Controls break; + case Key.Home: + this.CaretIndex = 0; + break; + + case Key.End: + this.CaretIndex = text.Length; + break; + case Key.Delete: if (caretIndex < text.Length) { @@ -180,6 +197,54 @@ namespace Perspex.Controls e.Handled = true; } + private int NextWord(string text, int direction, int caretIndex) + { + int pos = caretIndex; + bool foundNonWhiteSpace = false; + + for (; ;) + { + pos += direction; + + if (direction < 0 && pos <= 0) + { + pos = 0; + break; + } + else if (direction > 0 && pos >= text.Length) + { + pos = text.Length; + break; + } + else if (char.IsWhiteSpace(text[pos])) + { + if (foundNonWhiteSpace) + { + if (direction < 0) + { + ++pos; + break; + } + else + { + while (pos < text.Length && char.IsWhiteSpace(text[pos])) + { + ++pos; + } + + break; + } + } + } + else + { + foundNonWhiteSpace = true; + } + } + + return pos - caretIndex; + } + private void OnPointerPressed(object sender, PointerEventArgs e) { var point = e.GetPosition(this.textBoxView); diff --git a/TestApplication/Program.cs b/TestApplication/Program.cs index aa667c1aad..0cf8b695d6 100644 --- a/TestApplication/Program.cs +++ b/TestApplication/Program.cs @@ -228,7 +228,7 @@ namespace TestApplication }, new TextBox { - Text = "Text Box", + Text = "Some example text", }, } },