Browse Source

Fix text edit interactions

pull/7693/head
Benedikt Stebner 5 years ago
parent
commit
5cc496f612
  1. 70
      src/Avalonia.Controls/TextBox.cs

70
src/Avalonia.Controls/TextBox.cs

@ -1237,6 +1237,7 @@ namespace Avalonia.Controls
private void MoveHorizontal(int direction, bool wholeWord, bool isSelecting) private void MoveHorizontal(int direction, bool wholeWord, bool isSelecting)
{ {
var text = Text ?? string.Empty; var text = Text ?? string.Empty;
var selectionStart = SelectionStart;
var selectionEnd = SelectionEnd; var selectionEnd = SelectionEnd;
if (!wholeWord) if (!wholeWord)
@ -1246,15 +1247,30 @@ namespace Avalonia.Controls
return; return;
} }
_presenter.MoveCaretHorizontal(direction > 0 ? LogicalDirection.Forward : LogicalDirection.Backward);
if (isSelecting) if (isSelecting)
{ {
_presenter.MoveCaretHorizontal(direction > 0 ?
LogicalDirection.Forward :
LogicalDirection.Backward);
SelectionEnd = _presenter.CaretIndex; SelectionEnd = _presenter.CaretIndex;
} }
else else
{ {
SelectionStart = SelectionEnd = _presenter.CaretIndex; if (selectionStart != selectionEnd)
{
_presenter.MoveCaretToTextPosition(direction > 0 ?
Math.Max(selectionStart, selectionEnd) :
Math.Min(selectionStart, selectionEnd));
}
else
{
_presenter.MoveCaretHorizontal(direction > 0 ?
LogicalDirection.Forward :
LogicalDirection.Backward);
}
CaretIndex = _presenter.CaretIndex;
} }
} }
else else
@ -1345,46 +1361,52 @@ namespace Avalonia.Controls
private bool DeleteSelection(bool raiseTextChanged = true) private bool DeleteSelection(bool raiseTextChanged = true)
{ {
if (!IsReadOnly) if (IsReadOnly) return true;
{
var selectionStart = SelectionStart; var selectionStart = SelectionStart;
var selectionEnd = SelectionEnd; var selectionEnd = SelectionEnd;
if (selectionStart != 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), raiseTextChanged);
CaretIndex= start;
ClearSelection();
return true;
}
else
{
CaretIndex = SelectionStart;
return false;
}
}
else
{ {
var start = Math.Min(selectionStart, selectionEnd);
var end = Math.Max(selectionStart, selectionEnd);
var text = Text!;
SetTextInternal(text.Substring(0, start) + text.Substring(end), raiseTextChanged);
_presenter?.MoveCaretToTextPosition(start);
CaretIndex= start;
ClearSelection();
return true; return true;
} }
CaretIndex = SelectionStart;
return false;
} }
private string GetSelection() private string GetSelection()
{ {
var text = Text; var text = Text;
if (string.IsNullOrEmpty(text)) if (string.IsNullOrEmpty(text))
{
return ""; return "";
}
var selectionStart = SelectionStart; var selectionStart = SelectionStart;
var selectionEnd = SelectionEnd; var selectionEnd = SelectionEnd;
var start = Math.Min(selectionStart, selectionEnd); var start = Math.Min(selectionStart, selectionEnd);
var end = Math.Max(selectionStart, selectionEnd); var end = Math.Max(selectionStart, selectionEnd);
if (start == end || (Text?.Length ?? 0) < end) if (start == end || (Text?.Length ?? 0) < end)
{ {
return ""; return "";
} }
return text.Substring(start, end - start); return text.Substring(start, end - start);
} }

Loading…
Cancel
Save