From 436faccd9b4a0265949dd55c0a9ec019271e66da Mon Sep 17 00:00:00 2001 From: LiWenhao Date: Sun, 7 Apr 2024 10:57:57 +0800 Subject: [PATCH] delete console.writeline; add vertical drag; improve code style; --- demo/Ursa.Demo/Pages/TimeBoxDemo.axaml | 9 +++ src/Ursa.Themes.Semi/Controls/TimeBox.axaml | 22 ++++--- src/Ursa/Controls/TimeBox.cs | 65 ++++++++++++--------- 3 files changed, 61 insertions(+), 35 deletions(-) diff --git a/demo/Ursa.Demo/Pages/TimeBoxDemo.axaml b/demo/Ursa.Demo/Pages/TimeBoxDemo.axaml index ac20b40..f691986 100644 --- a/demo/Ursa.Demo/Pages/TimeBoxDemo.axaml +++ b/demo/Ursa.Demo/Pages/TimeBoxDemo.axaml @@ -42,6 +42,15 @@ AllowDrag="{Binding #allowDrag.IsChecked}" IsTimeLoop="{Binding #isTimeLoop.IsChecked}"/> + + + + Background="Transparent"/> + Background="Transparent"/> + Background="Transparent"/> + Background="Transparent"/> @@ -145,5 +141,15 @@ + + + + \ No newline at end of file diff --git a/src/Ursa/Controls/TimeBox.cs b/src/Ursa/Controls/TimeBox.cs index f238936..92c24b2 100644 --- a/src/Ursa/Controls/TimeBox.cs +++ b/src/Ursa/Controls/TimeBox.cs @@ -22,6 +22,12 @@ public enum TimeBoxInputMode Fast, } +public enum TimeBoxDragOrientation +{ + Horizontal, + Vertical, +} + [TemplatePart(PART_HoursTextPresenter, typeof(TextPresenter))] [TemplatePart(PART_MinuteTextPresenter, typeof(TextPresenter))] [TemplatePart(PART_SecondTextPresenter, typeof(TextPresenter))] @@ -143,15 +149,15 @@ public class TimeBox : TemplatedControl set => SetValue(AllowDragProperty, value); } - public static readonly StyledProperty IsReadOnlyProperty = AvaloniaProperty.Register( - nameof(IsReadOnly), defaultValue: false, defaultBindingMode: BindingMode.TwoWay); + public static readonly StyledProperty DragOrientationProperty + = AvaloniaProperty.Register(nameof(DragOrientation), defaultValue: TimeBoxDragOrientation.Horizontal); - public bool IsReadOnly + public TimeBoxDragOrientation DragOrientation { - get => GetValue(IsReadOnlyProperty); - set => SetValue(IsReadOnlyProperty, value); + get => GetValue(DragOrientationProperty); + set => SetValue(DragOrientationProperty, value); } - + public static readonly StyledProperty IsTimeLoopProperty = AvaloniaProperty.Register( nameof(IsTimeLoop), defaultBindingMode: BindingMode.TwoWay); @@ -168,11 +174,6 @@ public class TimeBox : TemplatedControl AllowDragProperty.Changed.AddClassHandler((o, e) => o.OnAllowDragChange(e)); } - private void OnAllowDragChange(AvaloniaPropertyChangedEventArgs args) - { - IsVisibleProperty.SetValue(args.NewValue.Value, _dragPanels); - } - #region Overrides protected override void OnApplyTemplate(TemplateAppliedEventArgs e) @@ -288,8 +289,6 @@ public class TimeBox : TemplatedControl } _presenters[_currentActiveSectionIndex.Value].Text = newText; - Console.WriteLine( - $"OnTextInput @ _secondText HashCode: {_presenters[_currentActiveSectionIndex.Value]?.GetHashCode()}"); _presenters[_currentActiveSectionIndex.Value].MoveCaretHorizontal(); if (_presenters[_currentActiveSectionIndex.Value].CaretIndex == 2 && InputMode == TimeBoxInputMode.Fast) { @@ -341,10 +340,6 @@ public class TimeBox : TemplatedControl SetTimeSpanInternal(); } - protected override void OnGotFocus(GotFocusEventArgs e) - { - } - #endregion private void OnFormatChange(AvaloniaPropertyChangedEventArgs arg) @@ -353,6 +348,11 @@ public class TimeBox : TemplatedControl ParseTimeSpan(showLeadingZero); } + private void OnAllowDragChange(AvaloniaPropertyChangedEventArgs args) + { + IsVisibleProperty.SetValue(args.NewValue.Value, _dragPanels); + } + private void OnTimeChanged(AvaloniaPropertyChangedEventArgs arg) { TimeSpan? timeSpan = arg.GetNewValue(); @@ -369,7 +369,7 @@ public class TimeBox : TemplatedControl if (_hourText != null) _hourText.Text = timeSpan.Value.Hours.ToString(); if (_minuteText != null) _minuteText.Text = timeSpan.Value.Minutes.ToString(); if (_secondText != null) _secondText.Text = timeSpan.Value.Seconds.ToString(); - if (_milliSecondText != null) _milliSecondText.Text = (timeSpan.Value.Milliseconds / 10).ToString(); + if (_milliSecondText != null) _milliSecondText.Text = ClampMilliSecond(timeSpan.Value.Milliseconds).ToString(); ParseTimeSpan(ShowLeadingZero); } } @@ -377,7 +377,6 @@ public class TimeBox : TemplatedControl private void ParseTimeSpan(bool showLeadingZero, bool skipParseFromText = false) { string format = showLeadingZero ? "D2" : ""; - Console.WriteLine($"ParseTimeSpan @ _secondText HashCode: {_secondText?.GetHashCode()}"); if (_hourText is null || _minuteText is null || _secondText is null || _milliSecondText is null) { _values[0] = 0; @@ -404,7 +403,7 @@ public class TimeBox : TemplatedControl } private void OnDragPanelPointerMoved(object sender, PointerEventArgs e) { - if (!AllowDrag || IsReadOnly) return; + if (!AllowDrag) return; if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return; var point = e.GetPosition(this); var delta = point - _lastDragPoint; @@ -430,12 +429,24 @@ public class TimeBox : TemplatedControl private int GetDelta(Point point) { - return point.X switch + switch (DragOrientation) { - > 0 => 1, - < 0 => -1, - _ => 0 - }; + case TimeBoxDragOrientation.Horizontal: + return point.X switch + { + > 0 => 1, + < 0 => -1, + _ => 0 + }; + case TimeBoxDragOrientation.Vertical: + return point.Y switch + { + > 0 => -1, + < 0 => 1, + _ => 0 + }; + } + return 0; } private void EnterSection(int index) @@ -627,7 +638,7 @@ public class TimeBox : TemplatedControl else if(_currentActiveSectionIndex.Value == 3) _values[3] += 1; ParseTimeSpan(ShowLeadingZero, true); - SetTimeSpanInternal(); + //SetTimeSpanInternal(); } private void Decrease() @@ -642,7 +653,7 @@ public class TimeBox : TemplatedControl else if(_currentActiveSectionIndex.Value == 3) _values[3] -= 1; ParseTimeSpan(ShowLeadingZero, true); - SetTimeSpanInternal(); + //SetTimeSpanInternal(); } private int ClampMilliSecond(int milliSecond)