Browse Source

Handle empty text when double-clicking TextBox.

Fixes #458
pull/478/head
Steven Kirk 11 years ago
parent
commit
1d6813b918
  1. 35
      src/Perspex.Controls/TextBox.cs
  2. 5
      src/Perspex.Controls/Utils/StringUtils.cs

35
src/Perspex.Controls/TextBox.cs

@ -373,23 +373,26 @@ namespace Perspex.Controls
var index = CaretIndex = _presenter.GetCaretIndex(point); var index = CaretIndex = _presenter.GetCaretIndex(point);
var text = Text; var text = Text;
switch (e.ClickCount) if (text != null)
{ {
case 1: switch (e.ClickCount)
SelectionStart = SelectionEnd = index; {
break; case 1:
case 2: SelectionStart = SelectionEnd = index;
if (!StringUtils.IsStartOfWord(text, index)) break;
{ case 2:
SelectionStart = StringUtils.PreviousWord(text, index, false); if (!StringUtils.IsStartOfWord(text, index))
} {
SelectionStart = StringUtils.PreviousWord(text, index, false);
SelectionEnd = StringUtils.NextWord(text, index, false); }
break;
case 3: SelectionEnd = StringUtils.NextWord(text, index, false);
SelectionStart = 0; break;
SelectionEnd = text.Length; case 3:
break; SelectionStart = 0;
SelectionEnd = text.Length;
break;
}
} }
e.Device.Capture(_presenter); e.Device.Capture(_presenter);

5
src/Perspex.Controls/Utils/StringUtils.cs

@ -21,6 +21,11 @@ namespace Perspex.Controls.Utils
public static bool IsStartOfWord(string text, int index) public static bool IsStartOfWord(string text, int index)
{ {
if (index >= text.Length)
{
return false;
}
// A 'word' starts with an AlphaNumeric or some punctuation symbols immediately // A 'word' starts with an AlphaNumeric or some punctuation symbols immediately
// preceeded by lwsp. // preceeded by lwsp.
if (index > 0 && !char.IsWhiteSpace(text[index - 1])) if (index > 0 && !char.IsWhiteSpace(text[index - 1]))

Loading…
Cancel
Save