Optimization: Optimize TimePicker clearing logic

This commit is contained in:
polarboy 2024-09-11 21:23:43 +08:00
parent a07f15df88
commit 0e29a7e236
6 changed files with 58 additions and 27 deletions

View File

@ -135,7 +135,9 @@
<atom:RangeTimePicker Width="320"
Status="Default"
Watermark="Start time"
SecondaryWatermark="End time" />
SecondaryWatermark="End time"
RangeStartDefaultTime="10:09:20"
RangeEndDefaultTime="12:12:20"/>
</StackPanel>
</desktop:ShowCaseItem>
</desktop:ShowCasePanel>

View File

@ -1,6 +1,7 @@
using AtomUI.Controls.Internal;
using AtomUI.Controls.Utils;
using Avalonia;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.LogicalTree;
@ -81,10 +82,22 @@ public class RangeTimePicker : RangeInfoPickerInput
#endregion
/// <summary>
/// 清除时间选择器的值,不考虑默认值
/// </summary>
public override void Clear()
{
base.Clear();
RangeStartSelectedTime = null;
RangeEndSelectedTime = null;
}
/// <summary>
/// 重置时间选择器的值,当有默认值设置的时候,会将当前的值设置成默认值
/// </summary>
public void Reset()
{
RangeStartSelectedTime = RangeStartDefaultTime;
RangeEndSelectedTime = RangeEndDefaultTime;
}
@ -155,7 +168,7 @@ public class RangeTimePicker : RangeInfoPickerInput
{
if (RangeStartSelectedTime.HasValue)
{
_infoInputBox!.Text = DateTimeUtils.FormatTimeSpan(RangeStartSelectedTime.Value,
Text = DateTimeUtils.FormatTimeSpan(RangeStartSelectedTime.Value,
ClockIdentifier == ClockIdentifierType.HourClock12);
}
else
@ -167,7 +180,7 @@ public class RangeTimePicker : RangeInfoPickerInput
{
if (RangeEndSelectedTime.HasValue)
{
_secondaryInfoInputBox!.Text = DateTimeUtils.FormatTimeSpan(RangeEndSelectedTime.Value,
SecondaryText = DateTimeUtils.FormatTimeSpan(RangeEndSelectedTime.Value,
ClockIdentifier == ClockIdentifierType.HourClock12);
}
else
@ -217,14 +230,14 @@ public class RangeTimePicker : RangeInfoPickerInput
{
if (RangeEndSelectedTime is null)
{
ResetRangeEndTimeValue();
ResetRangeStartTimeValue();
}
}
else if (RangeActivatedPart == RangeActivatedPart.End)
{
if (RangeStartSelectedTime is null)
{
ResetRangeStartTimeValue();
ResetRangeEndTimeValue();
}
}
else
@ -240,21 +253,21 @@ public class RangeTimePicker : RangeInfoPickerInput
}
}
}
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnAttachedToLogicalTree(e);
if (RangeStartDefaultTime is not null)
base.OnApplyTemplate(e);
if (RangeStartDefaultTime is not null && RangeStartSelectedTime is null)
{
RangeStartSelectedTime = RangeStartDefaultTime;
}
if (RangeEndDefaultTime is not null)
if (RangeEndDefaultTime is not null && RangeEndSelectedTime is null)
{
RangeEndDefaultTime = RangeEndDefaultTime;
RangeEndSelectedTime = RangeEndDefaultTime;
}
}
protected override bool ShowClearButtonPredicate()
{
return RangeStartSelectedTime is not null || RangeEndSelectedTime is not null;
@ -284,4 +297,5 @@ public class RangeTimePicker : RangeInfoPickerInput
SecondaryText = DateTimeUtils.FormatTimeSpan(value, ClockIdentifier == ClockIdentifierType.HourClock12);
}
}
}

View File

@ -59,7 +59,6 @@ internal class RangeTimePickerFlyoutPresenter : FlyoutPresenter
{
TimePickerRef = timePicker;
HorizontalAlignment = HorizontalAlignment.Left;
SetCurrentValue(TimeProperty, DateTime.Now.TimeOfDay);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
@ -74,8 +73,6 @@ internal class RangeTimePickerFlyoutPresenter : FlyoutPresenter
{
TimePickerRef.NotifyConfirmed(_timePickerPresenter.Time);
};
SetupTime();
}
if (_confirmButton is not null)
@ -95,14 +92,14 @@ internal class RangeTimePickerFlyoutPresenter : FlyoutPresenter
{
if (TimePickerRef.RangeStartSelectedTime is not null)
{
_timePickerPresenter!.Time = TimePickerRef.RangeStartSelectedTime.Value;
Time = TimePickerRef.RangeStartSelectedTime.Value;
}
}
else if (TimePickerRef.RangeActivatedPart == RangeActivatedPart.End)
{
if (TimePickerRef.RangeEndSelectedTime is not null)
{
_timePickerPresenter!.Time = TimePickerRef.RangeEndSelectedTime.Value;
Time = TimePickerRef.RangeEndSelectedTime.Value;
}
}
}
@ -148,8 +145,7 @@ internal class RangeTimePickerFlyoutPresenter : FlyoutPresenter
}));
}
_compositeDisposable.Add(
RangeTimePicker.RangeActivatedPartProperty.Changed.Subscribe(HandleRangeActivatedPartChanged));
_compositeDisposable.Add(RangeTimePicker.RangeActivatedPartProperty.Changed.Subscribe(HandleRangeActivatedPartChanged));
SetupTime();
}
@ -162,6 +158,7 @@ internal class RangeTimePickerFlyoutPresenter : FlyoutPresenter
{
base.OnDetachedFromVisualTree(e);
_compositeDisposable?.Dispose();
_compositeDisposable = null;
_compositeDisposable = null;
_timePickerPresenter!.Time = TimeSpan.Zero;
}
}

View File

@ -1,6 +1,7 @@
using AtomUI.Controls.Internal;
using AtomUI.Controls.Utils;
using Avalonia;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
namespace AtomUI.Controls;
@ -98,9 +99,20 @@ public class TimePicker : InfoPickerInput
}
}
/// <summary>
/// 清除时间选择器的值,不考虑默认值
/// </summary>
public override void Clear()
{
base.Clear();
SelectedTime = null;
}
/// <summary>
/// 重置时间选择器的值,当有默认值设置的时候,会将当前的值设置成默认值
/// </summary>
public void Reset()
{
SelectedTime = DefaultTime;
}
@ -148,4 +160,13 @@ public class TimePicker : InfoPickerInput
return value;
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
if (DefaultTime is not null && SelectedTime is null)
{
SelectedTime = DefaultTime;
}
}
}

View File

@ -24,6 +24,8 @@ internal class TimePickerFlyout : Flyout
TimePickerFlyoutPresenter.SecondIncrementProperty);
BindUtils.RelayBind(TimePickerRef, TimePicker.ClockIdentifierProperty, presenter,
TimePickerFlyoutPresenter.ClockIdentifierProperty);
BindUtils.RelayBind(TimePickerRef, TimePicker.SelectedTimeProperty, presenter,
TimePickerFlyoutPresenter.TimeProperty);
CalculateShowArrowEffective();
SetupArrowPosition(Popup, presenter);

View File

@ -15,7 +15,7 @@ internal class TimePickerFlyoutPresenter : FlyoutPresenter
TimePicker.SecondIncrementProperty.AddOwner<TimePickerFlyoutPresenter>();
public static readonly StyledProperty<TimeSpan> TimeProperty =
AvaloniaProperty.Register<TimePickerPresenter, TimeSpan>(nameof(Time));
AvaloniaProperty.Register<TimePickerFlyoutPresenter, TimeSpan>(nameof(Time));
public static readonly StyledProperty<ClockIdentifierType> ClockIdentifierProperty =
TimePicker.ClockIdentifierProperty.AddOwner<TimePickerFlyoutPresenter>();
@ -57,7 +57,6 @@ internal class TimePickerFlyoutPresenter : FlyoutPresenter
{
TimePickerRef = timePicker;
HorizontalAlignment = HorizontalAlignment.Left;
SetCurrentValue(TimeProperty, DateTime.Now.TimeOfDay);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
@ -72,10 +71,6 @@ internal class TimePickerFlyoutPresenter : FlyoutPresenter
{
TimePickerRef.NotifyConfirmed(_timePickerPresenter.Time);
};
if (TimePickerRef.DefaultTime is not null)
{
_timePickerPresenter.Time = TimePickerRef.DefaultTime.Value;
}
}
if (_confirmButton is not null)