Browse Source

Make Home, End and Ctrl keys work in TextBox.

pull/10/head
Steven Kirk 12 years ago
parent
commit
d0c8146681
  1. 73
      Perspex.Controls/TextBox.cs
  2. 2
      TestApplication/Program.cs

73
Perspex.Controls/TextBox.cs

@ -108,14 +108,23 @@ namespace Perspex.Controls
private void MoveHorizontal(int count, ModifierKeys modifiers) 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; 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; break;
case Key.Home:
this.CaretIndex = 0;
break;
case Key.End:
this.CaretIndex = text.Length;
break;
case Key.Delete: case Key.Delete:
if (caretIndex < text.Length) if (caretIndex < text.Length)
{ {
@ -180,6 +197,54 @@ namespace Perspex.Controls
e.Handled = true; 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) private void OnPointerPressed(object sender, PointerEventArgs e)
{ {
var point = e.GetPosition(this.textBoxView); var point = e.GetPosition(this.textBoxView);

2
TestApplication/Program.cs

@ -228,7 +228,7 @@ namespace TestApplication
}, },
new TextBox new TextBox
{ {
Text = "Text Box", Text = "Some example text",
}, },
} }
}, },

Loading…
Cancel
Save