Fixed the abnormal dispatching of DatePicker's Hover selection event

Fixed the abnormal dispatching of DatePicker's Hover selection event
This commit is contained in:
polarboy 2024-09-13 19:47:00 +08:00
parent a5a2020d06
commit e21f79e23c
5 changed files with 83 additions and 32 deletions

View File

@ -12,7 +12,7 @@
<desktop:ShowCaseItem
Title="Basic"
Description="Click DatePicker, and then we could select or input a date in panel.">
<atom:DatePicker Watermark="Select date" SelectedDateTime="2029-9-3" IsNeedConfirm="True"/>
<atom:DatePicker Watermark="Select date" DefaultDateTime="2029-9-3" IsNeedConfirm="True"/>
</desktop:ShowCaseItem>
<!-- -->
<!-- <desktop:ShowCaseItem -->

View File

@ -1,4 +1,5 @@
using AtomUI.Controls.Internal;
using AtomUI.Controls.CalendarView;
using AtomUI.Controls.Internal;
using AtomUI.Data;
using Avalonia;
using Avalonia.Controls;
@ -127,44 +128,67 @@ public class DatePicker : InfoPickerInput
{
return;
}
presenter.ChoosingStatueChanged += (sender, args) =>
{
_isChoosing = args.IsChoosing;
UpdatePseudoClasses();
if (!args.IsChoosing)
{
ClearHoverSelectedInfo();
}
};
presenter.HoverDateTimeChanged += (sender, args) =>
{
if (args.Value.HasValue)
{
Text = args.Value.Value.ToString(EffectiveFormat());
}
else
{
Text = null;
}
};
presenter.Confirmed += (sender, args) =>
{
SelectedDateTime = presenter.SelectedDateTime;
Dispatcher.UIThread.Post(() => ClosePickerFlyout());
};
BindUtils.RelayBind(this, SelectedDateTimeProperty, presenter, DatePickerPresenter.SelectedDateTimeProperty);
BindUtils.RelayBind(this, IsNeedConfirmProperty, presenter, DatePickerPresenter.IsNeedConfirmProperty);
BindUtils.RelayBind(this, IsShowNowProperty, presenter, DatePickerPresenter.IsShowNowProperty);
BindUtils.RelayBind(this, IsShowTimeProperty, presenter, DatePickerPresenter.IsShowTimeProperty);
}
protected override void NotifyFlyoutOpened()
{
base.NotifyFlyoutOpened();
if (_pickerPresenter is not null)
{
_pickerPresenter.ChoosingStatueChanged += HandleChoosingStatueChanged;
_pickerPresenter.HoverDateTimeChanged += HandleHoverDateTimeChanged;
_pickerPresenter.Confirmed += HandleConfirmed;
}
}
protected override void NotifyFlyoutAboutToClose(bool selectedIsValid)
{
base.NotifyFlyoutAboutToClose(selectedIsValid);
if (_pickerPresenter is not null)
{
_pickerPresenter.ChoosingStatueChanged -= HandleChoosingStatueChanged;
_pickerPresenter.HoverDateTimeChanged -= HandleHoverDateTimeChanged;
_pickerPresenter.Confirmed -= HandleConfirmed;
}
}
private void HandleChoosingStatueChanged(object? sender, ChoosingStatusEventArgs args)
{
_isChoosing = args.IsChoosing;
UpdatePseudoClasses();
if (!args.IsChoosing)
{
ClearHoverSelectedInfo();
}
}
private void HandleHoverDateTimeChanged(object? sender, DateSelectedEventArgs args)
{
if (args.Value.HasValue)
{
Text = args.Value.Value.ToString(EffectiveFormat());
}
else
{
Text = null;
}
}
private void HandleConfirmed(object? sender, EventArgs args)
{
SelectedDateTime = _pickerPresenter?.SelectedDateTime;
ClosePickerFlyout();
}
private void ClearHoverSelectedInfo()
{
DateTime? targetValue = default;
targetValue = SelectedDateTime ?? DefaultDateTime;
targetValue = SelectedDateTime;
Text = targetValue?.ToString(EffectiveFormat());
}

View File

@ -98,6 +98,7 @@ internal class DatePickerPresenter : PickerPresenterBase
private Button? _confirmButton;
private PickerCalendar? _calendarView;
private IDisposable? _choosingStateDisposable;
private bool _isConfirmed;
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
@ -111,7 +112,8 @@ internal class DatePickerPresenter : PickerPresenterBase
ChoosingStatueChanged?.Invoke(this, new ChoosingStatusEventArgs(args.GetNewValue<bool>()));
});
}
_isConfirmed = false;
}
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
@ -165,6 +167,18 @@ internal class DatePickerPresenter : PickerPresenterBase
{
_confirmButton.Click += HandleConfirmButtonClicked;
_confirmButton.IsEnabled = SelectedDateTime is not null;
_confirmButton.PointerEntered += (sender, args) =>
{
if (_calendarView?.SelectedDate is not null)
{
var hoverDateTime = CollectDateTime(_calendarView?.SelectedDate);
HoverDateTimeChanged?.Invoke(this, new DateSelectedEventArgs(hoverDateTime));
}
};
_confirmButton.PointerExited += (sender, args) =>
{
ChoosingStatueChanged?.Invoke(this, new ChoosingStatusEventArgs(false));
};
}
}

View File

@ -60,6 +60,7 @@ internal class FlyoutStateHelper : AvaloniaObject
public event EventHandler<EventArgs>? FlyoutAboutToClose;
public event EventHandler<EventArgs>? FlyoutClosed;
public event EventHandler<EventArgs>? FlyoutAboutToShow;
public event EventHandler<EventArgs>? FlyoutOpened;
public Func<Point, bool>? OpenFlyoutPredicate;
public Func<IPopupHostProvider, RawPointerEventArgs, bool>? ClickHideFlyoutPredicate;
@ -114,6 +115,7 @@ internal class FlyoutStateHelper : AvaloniaObject
}
}
}
FlyoutOpened?.Invoke(this, EventArgs.Empty);
}
private void HandleFlyoutClosed(object? sender, EventArgs e)

View File

@ -199,6 +199,7 @@ public abstract class InfoPickerInput : TemplatedControl
};
_flyoutStateHelper.FlyoutAboutToShow += HandleFlyoutAboutToShow;
_flyoutStateHelper.FlyoutAboutToClose += HandleFlyoutAboutToClose;
_flyoutStateHelper.FlyoutOpened += HandleFlyoutOpened;
_flyoutStateHelper.OpenFlyoutPredicate = FlyoutOpenPredicate;
_flyoutStateHelper.ClickHideFlyoutPredicate = ClickHideFlyoutPredicate;
}
@ -227,11 +228,21 @@ public abstract class InfoPickerInput : TemplatedControl
{
NotifyFlyoutAboutToClose(_currentValidSelected);
}
protected virtual void NotifyFlyoutAboutToClose(bool selectedIsValid)
{
}
private void HandleFlyoutOpened(object? sender, EventArgs args)
{
NotifyFlyoutOpened();
}
protected virtual void NotifyFlyoutOpened()
{
}
protected virtual bool ClickHideFlyoutPredicate(IPopupHostProvider hostProvider, RawPointerEventArgs args)
{
if (hostProvider.PopupHost != args.Root)