delete console.writeline; add vertical drag; improve code style;

This commit is contained in:
LiWenhao
2024-04-07 10:57:57 +08:00
parent a03591516a
commit 436faccd9b
3 changed files with 61 additions and 35 deletions

View File

@@ -42,6 +42,15 @@
AllowDrag="{Binding #allowDrag.IsChecked}" AllowDrag="{Binding #allowDrag.IsChecked}"
IsTimeLoop="{Binding #isTimeLoop.IsChecked}"/> IsTimeLoop="{Binding #isTimeLoop.IsChecked}"/>
<TextBlock Classes="" Text="Drag Vertical" />
<u:TimeBox
Width="200"
InputMode="Fast"
ShowLeadingZero="{Binding #format.IsChecked}"
AllowDrag="{Binding #allowDrag.IsChecked}"
IsTimeLoop="{Binding #isTimeLoop.IsChecked}"
DragOrientation="Vertical"/>
<TextBlock Classes="" Text="Binding From Source" /> <TextBlock Classes="" Text="Binding From Source" />
<RepeatButton Command="{Binding ChangeRandomTimeCommand}" Content="Random" /> <RepeatButton Command="{Binding ChangeRandomTimeCommand}" Content="Random" />
<u:TimeBox <u:TimeBox

View File

@@ -46,8 +46,7 @@
<Panel Name="{x:Static u:TimeBox.PART_HourDragPanel}" <Panel Name="{x:Static u:TimeBox.PART_HourDragPanel}"
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" VerticalAlignment="Stretch"
Background="Transparent" Background="Transparent"/>
Cursor="SizeWestEast"/>
</Grid> </Grid>
</Border> </Border>
<TextBlock <TextBlock
@@ -74,8 +73,7 @@
<Panel Name="{x:Static u:TimeBox.PART_MinuteDragPanel}" <Panel Name="{x:Static u:TimeBox.PART_MinuteDragPanel}"
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" VerticalAlignment="Stretch"
Background="Transparent" Background="Transparent"/>
Cursor="SizeWestEast"/>
</Grid> </Grid>
</Border> </Border>
<TextBlock <TextBlock
@@ -102,8 +100,7 @@
<Panel Name="{x:Static u:TimeBox.PART_SecondDragPanel}" <Panel Name="{x:Static u:TimeBox.PART_SecondDragPanel}"
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" VerticalAlignment="Stretch"
Background="Transparent" Background="Transparent"/>
Cursor="SizeWestEast"/>
</Grid> </Grid>
</Border> </Border>
<TextBlock <TextBlock
@@ -130,8 +127,7 @@
<Panel Name="{x:Static u:TimeBox.PART_MilliSecondDragPanel}" <Panel Name="{x:Static u:TimeBox.PART_MilliSecondDragPanel}"
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" VerticalAlignment="Stretch"
Background="Transparent" Background="Transparent"/>
Cursor="SizeWestEast"/>
</Grid> </Grid>
</Border> </Border>
</Grid> </Grid>
@@ -145,5 +141,15 @@
<Setter Property="Background" Value="{DynamicResource TimeBoxDisabledBackground}" /> <Setter Property="Background" Value="{DynamicResource TimeBoxDisabledBackground}" />
<Setter Property="Foreground" Value="{DynamicResource SemiColorDisabledText}" /> <Setter Property="Foreground" Value="{DynamicResource SemiColorDisabledText}" />
</Style> </Style>
<Style Selector="^[DragOrientation=Horizontal]">
<Style Selector="^/template/ Panel">
<Setter Property="Cursor" Value="SizeWestEast"/>
</Style>
</Style>
<Style Selector="^[DragOrientation=Vertical]">
<Style Selector="^/template/ Panel">
<Setter Property="Cursor" Value="SizeNorthSouth"/>
</Style>
</Style>
</ControlTheme> </ControlTheme>
</ResourceDictionary> </ResourceDictionary>

View File

@@ -22,6 +22,12 @@ public enum TimeBoxInputMode
Fast, Fast,
} }
public enum TimeBoxDragOrientation
{
Horizontal,
Vertical,
}
[TemplatePart(PART_HoursTextPresenter, typeof(TextPresenter))] [TemplatePart(PART_HoursTextPresenter, typeof(TextPresenter))]
[TemplatePart(PART_MinuteTextPresenter, typeof(TextPresenter))] [TemplatePart(PART_MinuteTextPresenter, typeof(TextPresenter))]
[TemplatePart(PART_SecondTextPresenter, typeof(TextPresenter))] [TemplatePart(PART_SecondTextPresenter, typeof(TextPresenter))]
@@ -143,15 +149,15 @@ public class TimeBox : TemplatedControl
set => SetValue(AllowDragProperty, value); set => SetValue(AllowDragProperty, value);
} }
public static readonly StyledProperty<bool> IsReadOnlyProperty = AvaloniaProperty.Register<TimeBox, bool>( public static readonly StyledProperty<TimeBoxDragOrientation> DragOrientationProperty
nameof(IsReadOnly), defaultValue: false, defaultBindingMode: BindingMode.TwoWay); = AvaloniaProperty.Register<TimeBox, TimeBoxDragOrientation>(nameof(DragOrientation), defaultValue: TimeBoxDragOrientation.Horizontal);
public bool IsReadOnly public TimeBoxDragOrientation DragOrientation
{ {
get => GetValue(IsReadOnlyProperty); get => GetValue(DragOrientationProperty);
set => SetValue(IsReadOnlyProperty, value); set => SetValue(DragOrientationProperty, value);
} }
public static readonly StyledProperty<bool> IsTimeLoopProperty = AvaloniaProperty.Register<TimeBox, bool>( public static readonly StyledProperty<bool> IsTimeLoopProperty = AvaloniaProperty.Register<TimeBox, bool>(
nameof(IsTimeLoop), defaultBindingMode: BindingMode.TwoWay); nameof(IsTimeLoop), defaultBindingMode: BindingMode.TwoWay);
@@ -168,11 +174,6 @@ public class TimeBox : TemplatedControl
AllowDragProperty.Changed.AddClassHandler<TimeBox, bool>((o, e) => o.OnAllowDragChange(e)); AllowDragProperty.Changed.AddClassHandler<TimeBox, bool>((o, e) => o.OnAllowDragChange(e));
} }
private void OnAllowDragChange(AvaloniaPropertyChangedEventArgs<bool> args)
{
IsVisibleProperty.SetValue(args.NewValue.Value, _dragPanels);
}
#region Overrides #region Overrides
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
@@ -288,8 +289,6 @@ public class TimeBox : TemplatedControl
} }
_presenters[_currentActiveSectionIndex.Value].Text = newText; _presenters[_currentActiveSectionIndex.Value].Text = newText;
Console.WriteLine(
$"OnTextInput @ _secondText HashCode: {_presenters[_currentActiveSectionIndex.Value]?.GetHashCode()}");
_presenters[_currentActiveSectionIndex.Value].MoveCaretHorizontal(); _presenters[_currentActiveSectionIndex.Value].MoveCaretHorizontal();
if (_presenters[_currentActiveSectionIndex.Value].CaretIndex == 2 && InputMode == TimeBoxInputMode.Fast) if (_presenters[_currentActiveSectionIndex.Value].CaretIndex == 2 && InputMode == TimeBoxInputMode.Fast)
{ {
@@ -341,10 +340,6 @@ public class TimeBox : TemplatedControl
SetTimeSpanInternal(); SetTimeSpanInternal();
} }
protected override void OnGotFocus(GotFocusEventArgs e)
{
}
#endregion #endregion
private void OnFormatChange(AvaloniaPropertyChangedEventArgs arg) private void OnFormatChange(AvaloniaPropertyChangedEventArgs arg)
@@ -353,6 +348,11 @@ public class TimeBox : TemplatedControl
ParseTimeSpan(showLeadingZero); ParseTimeSpan(showLeadingZero);
} }
private void OnAllowDragChange(AvaloniaPropertyChangedEventArgs<bool> args)
{
IsVisibleProperty.SetValue(args.NewValue.Value, _dragPanels);
}
private void OnTimeChanged(AvaloniaPropertyChangedEventArgs arg) private void OnTimeChanged(AvaloniaPropertyChangedEventArgs arg)
{ {
TimeSpan? timeSpan = arg.GetNewValue<TimeSpan?>(); TimeSpan? timeSpan = arg.GetNewValue<TimeSpan?>();
@@ -369,7 +369,7 @@ public class TimeBox : TemplatedControl
if (_hourText != null) _hourText.Text = timeSpan.Value.Hours.ToString(); if (_hourText != null) _hourText.Text = timeSpan.Value.Hours.ToString();
if (_minuteText != null) _minuteText.Text = timeSpan.Value.Minutes.ToString(); if (_minuteText != null) _minuteText.Text = timeSpan.Value.Minutes.ToString();
if (_secondText != null) _secondText.Text = timeSpan.Value.Seconds.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); ParseTimeSpan(ShowLeadingZero);
} }
} }
@@ -377,7 +377,6 @@ public class TimeBox : TemplatedControl
private void ParseTimeSpan(bool showLeadingZero, bool skipParseFromText = false) private void ParseTimeSpan(bool showLeadingZero, bool skipParseFromText = false)
{ {
string format = showLeadingZero ? "D2" : ""; string format = showLeadingZero ? "D2" : "";
Console.WriteLine($"ParseTimeSpan @ _secondText HashCode: {_secondText?.GetHashCode()}");
if (_hourText is null || _minuteText is null || _secondText is null || _milliSecondText is null) if (_hourText is null || _minuteText is null || _secondText is null || _milliSecondText is null)
{ {
_values[0] = 0; _values[0] = 0;
@@ -404,7 +403,7 @@ public class TimeBox : TemplatedControl
} }
private void OnDragPanelPointerMoved(object sender, PointerEventArgs e) private void OnDragPanelPointerMoved(object sender, PointerEventArgs e)
{ {
if (!AllowDrag || IsReadOnly) return; if (!AllowDrag) return;
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return; if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return;
var point = e.GetPosition(this); var point = e.GetPosition(this);
var delta = point - _lastDragPoint; var delta = point - _lastDragPoint;
@@ -430,12 +429,24 @@ public class TimeBox : TemplatedControl
private int GetDelta(Point point) private int GetDelta(Point point)
{ {
return point.X switch switch (DragOrientation)
{ {
> 0 => 1, case TimeBoxDragOrientation.Horizontal:
< 0 => -1, return point.X switch
_ => 0 {
}; > 0 => 1,
< 0 => -1,
_ => 0
};
case TimeBoxDragOrientation.Vertical:
return point.Y switch
{
> 0 => -1,
< 0 => 1,
_ => 0
};
}
return 0;
} }
private void EnterSection(int index) private void EnterSection(int index)
@@ -627,7 +638,7 @@ public class TimeBox : TemplatedControl
else if(_currentActiveSectionIndex.Value == 3) else if(_currentActiveSectionIndex.Value == 3)
_values[3] += 1; _values[3] += 1;
ParseTimeSpan(ShowLeadingZero, true); ParseTimeSpan(ShowLeadingZero, true);
SetTimeSpanInternal(); //SetTimeSpanInternal();
} }
private void Decrease() private void Decrease()
@@ -642,7 +653,7 @@ public class TimeBox : TemplatedControl
else if(_currentActiveSectionIndex.Value == 3) else if(_currentActiveSectionIndex.Value == 3)
_values[3] -= 1; _values[3] -= 1;
ParseTimeSpan(ShowLeadingZero, true); ParseTimeSpan(ShowLeadingZero, true);
SetTimeSpanInternal(); //SetTimeSpanInternal();
} }
private int ClampMilliSecond(int milliSecond) private int ClampMilliSecond(int milliSecond)