feat: support double click to select all

This commit is contained in:
LiWenhao
2024-04-16 22:43:43 +08:00
parent 36b01902c7
commit 1f51efaf68

View File

@@ -72,7 +72,7 @@ public class TimeBox : TemplatedControl
private readonly int[] _sectionLength = new[] { 2, 2, 2, 3 }; private readonly int[] _sectionLength = new[] { 2, 2, 2, 3 };
private readonly bool[] _isShowedCaret = new[] { false, false, false, false }; private readonly bool[] _isShowedCaret = new[] { false, false, false, false };
private int? _currentActiveSectionIndex; private int? _currentActiveSectionIndex;
private bool _isAlreadyDrag; private bool _isDragging;
private Point _pressedPosition; private Point _pressedPosition;
private Point? _lastDragPoint; private Point? _lastDragPoint;
@@ -213,7 +213,8 @@ public class TimeBox : TemplatedControl
_milliSecondText.Text = Time != null ? Time.Value.Milliseconds.ToString() : "0"; _milliSecondText.Text = Time != null ? Time.Value.Milliseconds.ToString() : "0";
ParseTimeSpan(ShowLeadingZero); ParseTimeSpan(ShowLeadingZero);
PointerMovedEvent.AddHandler(OnDragPanelPointerMoved, _dragPanels[0], _dragPanels[1], _dragPanels[2], _dragPanels[3]); PointerMovedEvent.AddHandler(OnDragPanelPointerMoved, _dragPanels[0], _dragPanels[1], _dragPanels[2],
_dragPanels[3]);
} }
protected override void OnKeyDown(KeyEventArgs e) protected override void OnKeyDown(KeyEventArgs e)
@@ -224,7 +225,8 @@ public class TimeBox : TemplatedControl
if (keymap is not null && Match(keymap.SelectAll)) if (keymap is not null && Match(keymap.SelectAll))
{ {
_presenters[_currentActiveSectionIndex.Value].SelectionStart = 0; _presenters[_currentActiveSectionIndex.Value].SelectionStart = 0;
_presenters[_currentActiveSectionIndex.Value].SelectionEnd = _presenters[_currentActiveSectionIndex.Value].Text?.Length ?? 0; _presenters[_currentActiveSectionIndex.Value].SelectionEnd =
_presenters[_currentActiveSectionIndex.Value].Text?.Length ?? 0;
return; return;
} }
@@ -311,13 +313,23 @@ public class TimeBox : TemplatedControl
_lastDragPoint = _pressedPosition; _lastDragPoint = _pressedPosition;
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
{ {
if (_borders[i].Bounds.Contains(_pressedPosition)) if (!_borders[i].Bounds.Contains(_pressedPosition))
{
_currentActiveSectionIndex = i;
}
else
{ {
LeaveSection(i); LeaveSection(i);
continue;
}
_currentActiveSectionIndex = i;
if (e.ClickCount == 2)
{
EnterSection(_currentActiveSectionIndex.Value);
continue;
}
if (!_dragPanels[_currentActiveSectionIndex.Value].IsVisible)
{
MoveCaret(_currentActiveSectionIndex.Value);
} }
} }
} }
@@ -325,16 +337,16 @@ public class TimeBox : TemplatedControl
protected override void OnPointerReleased(PointerReleasedEventArgs e) protected override void OnPointerReleased(PointerReleasedEventArgs e)
{ {
if (_currentActiveSectionIndex is null) return; if (_currentActiveSectionIndex is null) return;
if (_isAlreadyDrag) if (_isDragging)
{ {
_isAlreadyDrag = false; _isDragging = false;
_lastDragPoint = null;
return;
} }
else if(_dragPanels[_currentActiveSectionIndex.Value].IsVisible)
{ {
EnterSection(_currentActiveSectionIndex.Value); EnterSection(_currentActiveSectionIndex.Value);
} }
_lastDragPoint = null;
} }
protected override void OnLostFocus(RoutedEventArgs e) protected override void OnLostFocus(RoutedEventArgs e)
@@ -407,10 +419,10 @@ public class TimeBox : TemplatedControl
VerifyTimeValue(); VerifyTimeValue();
_hourText?.SetValue(TextPresenter.TextProperty,_values[0].ToString(format)); _hourText?.SetValue(TextPresenter.TextProperty, _values[0].ToString(format));
_minuteText?.SetValue(TextPresenter.TextProperty,_values[1].ToString(format)); _minuteText?.SetValue(TextPresenter.TextProperty, _values[1].ToString(format));
_secondText?.SetValue(TextPresenter.TextProperty,_values[2].ToString(format)); _secondText?.SetValue(TextPresenter.TextProperty, _values[2].ToString(format));
_milliSecondText?.SetValue(TextPresenter.TextProperty,_values[3].ToString(millisecondformat)); _milliSecondText?.SetValue(TextPresenter.TextProperty, _values[3].ToString(millisecondformat));
} }
private void OnDragPanelPointerMoved(object sender, PointerEventArgs e) private void OnDragPanelPointerMoved(object sender, PointerEventArgs e)
@@ -424,12 +436,12 @@ public class TimeBox : TemplatedControl
if (d > 0) if (d > 0)
{ {
Increase(); Increase();
_isAlreadyDrag = true; _isDragging = true;
} }
else if (d < 0) else if (d < 0)
{ {
Decrease(); Decrease();
_isAlreadyDrag = true; _isDragging = true;
} }
_lastDragPoint = point; _lastDragPoint = point;
@@ -458,29 +470,36 @@ public class TimeBox : TemplatedControl
return 0; return 0;
} }
/// <summary>
/// Set dragPanel IsVisible to false if AllowDrag is true, and select all text in the section
/// </summary>
/// <param name="index">The index of section that will be enter</param>
private void EnterSection(int index) private void EnterSection(int index)
{ {
if(index < 0 || index > 3) return; if (index < 0 || index > 3) return;
if (!_isShowedCaret[index])
{
if (AllowDrag) if (AllowDrag)
_dragPanels[index].IsVisible = false; _dragPanels[index].IsVisible = false;
_presenters[index].ShowCaret(); _presenters[index].ShowCaret();
_isShowedCaret[index] = true; _isShowedCaret[index] = true;
_presenters[index].SelectAll(); _presenters[index].SelectAll();
} }
else
private void MoveCaret(int index)
{ {
_presenters[index].ClearSelection(); _presenters[index].ClearSelection();
var caretPosition = var caretPosition =
_pressedPosition.WithX(_pressedPosition.X - _borders[index].Bounds.X); _pressedPosition.WithX(_pressedPosition.X - _borders[index].Bounds.X);
_presenters[index].MoveCaretToPoint(caretPosition); _presenters[index].MoveCaretToPoint(caretPosition);
} }
}
/// <summary>
/// Set dragPanel IsVisible to true if AllowDrag is true, and clear selection in the section
/// </summary>
/// <param name="index">The index of section that will be leave</param>
private void LeaveSection(int index) private void LeaveSection(int index)
{ {
if(index < 0 || index > 3) return; if (index < 0 || index > 3) return;
_presenters[index].ClearSelection(); _presenters[index].ClearSelection();
if (_isShowedCaret[index]) if (_isShowedCaret[index])
{ {
@@ -494,7 +513,7 @@ public class TimeBox : TemplatedControl
private bool MoveToNextSection(int index) private bool MoveToNextSection(int index)
{ {
if(index < 0 || index >= 3) return false; if (index < 0 || index >= 3) return false;
LeaveSection(index); LeaveSection(index);
_currentActiveSectionIndex = index + 1; _currentActiveSectionIndex = index + 1;
EnterSection(_currentActiveSectionIndex.Value); EnterSection(_currentActiveSectionIndex.Value);
@@ -503,7 +522,7 @@ public class TimeBox : TemplatedControl
private bool MoveToPreviousSection(int index) private bool MoveToPreviousSection(int index)
{ {
if(index <= 0 || index > 3) return false; if (index <= 0 || index > 3) return false;
LeaveSection(index); LeaveSection(index);
_currentActiveSectionIndex = index - 1; _currentActiveSectionIndex = index - 1;
EnterSection(_currentActiveSectionIndex.Value); EnterSection(_currentActiveSectionIndex.Value);
@@ -571,7 +590,7 @@ public class TimeBox : TemplatedControl
private void DeleteImplementation(int index) private void DeleteImplementation(int index)
{ {
if(index < 0 || index > 3) return; if (index < 0 || index > 3) return;
var oldText = _presenters[index].Text; var oldText = _presenters[index].Text;
if (_presenters[index].SelectionStart != _presenters[index].SelectionEnd) if (_presenters[index].SelectionStart != _presenters[index].SelectionEnd)
{ {