Merge pull request #220 from AlvinRey/TimeBox
[feat]: Time box support double click to select all
This commit is contained in:
@@ -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)
|
||||||
@@ -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,26 +470,33 @@ 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;
|
||||||
|
|||||||
Reference in New Issue
Block a user