test: add datepicker test, make DatePicker focusable, change many behaviors.

This commit is contained in:
Dong Bin
2025-02-18 01:33:04 +08:00
parent 8cd3d1e031
commit e062efead0
4 changed files with 105 additions and 18 deletions

View File

@@ -8,6 +8,10 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Ursa.Demo.Pages.DatePickerDemo">
<StackPanel Margin="20" HorizontalAlignment="Left">
<u:Form>
<u:DatePicker Width="200" u:FormItem.Label="_Test"/>
<TextBox Width="200" u:FormItem.Label="_West"/>
</u:Form>
<u:CalendarView DateSelected="CalendarView_OnOnDateSelected" DatePreviewed="CalendarView_OnOnDatePreviewed"/>
<TextBlock Text="{Binding #singlePicker.SelectedDate}" ></TextBlock>
<u:DatePicker Name="singlePicker" Width="200" Classes="ClearButton" />

View File

@@ -7,8 +7,6 @@ using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.VisualTree;
using Irihi.Avalonia.Shared.Common;
using Irihi.Avalonia.Shared.Contracts;
using Irihi.Avalonia.Shared.Helpers;
@@ -49,6 +47,7 @@ public class DatePicker: DatePickerBase, IClearControl
static DatePicker()
{
FocusableProperty.OverrideDefaultValue<DatePicker>(true);
SelectedDateProperty.Changed.AddClassHandler<DatePicker, DateTime?>((picker, args) =>
picker.OnSelectionChanged(args));
}
@@ -58,13 +57,28 @@ public class DatePicker: DatePickerBase, IClearControl
SyncSelectedDateToText(args.NewValue.Value);
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == IsDropdownOpenProperty)
{
if (change.GetNewValue<bool>() == false)
{
}
else
{
}
}
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
GotFocusEvent.RemoveHandler(OnTextBoxGetFocus, _textBox);
TextBox.TextChangedEvent.RemoveHandler(OnTextChanged, _textBox);
PointerPressedEvent.RemoveHandler(OnTextBoxPointerPressed, _textBox);
Button.ClickEvent.RemoveHandler(OnButtonClick, _button);
CalendarView.DateSelectedEvent.RemoveHandler(OnDateSelected, _calendar);
@@ -73,10 +87,9 @@ public class DatePicker: DatePickerBase, IClearControl
_textBox = e.NameScope.Find<TextBox>(PART_TextBox);
_calendar = e.NameScope.Find<CalendarView>(PART_Calendar);
Button.ClickEvent.AddHandler(OnButtonClick, RoutingStrategies.Bubble, true, _button);
Button.ClickEvent.AddHandler(OnButtonClick, RoutingStrategies.Bubble, false, _button);
GotFocusEvent.AddHandler(OnTextBoxGetFocus, _textBox);
TextBox.TextChangedEvent.AddHandler(OnTextChanged, _textBox);
PointerPressedEvent.AddHandler(OnTextBoxPointerPressed, RoutingStrategies.Tunnel, false, _textBox);
CalendarView.DateSelectedEvent.AddHandler(OnDateSelected, RoutingStrategies.Bubble, true, _calendar);
SyncSelectedDateToText(SelectedDate);
}
@@ -89,19 +102,10 @@ public class DatePicker: DatePickerBase, IClearControl
private void OnButtonClick(object? sender, RoutedEventArgs e)
{
Focus(NavigationMethod.Pointer);
SetCurrentValue(IsDropdownOpenProperty, !IsDropdownOpen);
}
private void OnTextBoxPointerPressed(object? sender, PointerPressedEventArgs e)
{
if (_calendar is not null)
if(IsFocused)
{
var date = SelectedDate ?? DateTime.Today;
_calendar.ContextDate = new CalendarContext(date.Year, date.Month);
_calendar.UpdateDayButtons();
SetCurrentValue(IsDropdownOpenProperty, !IsDropdownOpen);
}
SetCurrentValue(IsDropdownOpenProperty, true);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -186,6 +190,7 @@ public class DatePicker: DatePickerBase, IClearControl
e.Handled = true;
}
}
}
protected override void OnKeyDown(KeyEventArgs e)
@@ -213,4 +218,40 @@ public class DatePicker: DatePickerBase, IClearControl
{
SetCurrentValue(SelectedDateProperty, null);
}
protected override void OnGotFocus(GotFocusEventArgs e)
{
base.OnGotFocus(e);
FocusChanged(IsKeyboardFocusWithin);
}
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
FocusChanged(IsKeyboardFocusWithin);
var top = TopLevel.GetTopLevel(this);
var element = top?.FocusManager?.GetFocusedElement();
if (element is Visual v && _popup?.IsInsidePopup(v)==true)
{
return;
}
SetCurrentValue(IsDropdownOpenProperty, false);
}
private bool _isFocused;
private void FocusChanged(bool hasFocus)
{
bool wasFocused = _isFocused;
_isFocused = hasFocus;
if (hasFocus)
{
if (!wasFocused && _textBox != null)
{
_textBox.Focus();
_textBox.SelectAll();
}
}
}
}

View File

@@ -7,6 +7,7 @@ using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.LogicalTree;
using Avalonia.Threading;
using Avalonia.VisualTree;
using HeadlessTest.Ursa.TestHelpers;
using Ursa.Controls;
using DatePicker = Ursa.Controls.DatePicker;
@@ -309,5 +310,46 @@ public class DatePickerTests
Dispatcher.UIThread.RunJobs();
Assert.Equal(new DateTime(2025, 2, 19), picker.SelectedDate);
}
[AvaloniaFact]
public void Ensure_Focusable()
{
var picker = new DatePicker();
Assert.True(picker.Focusable);
}
[AvaloniaFact]
public void Click_On_Popup_Will_Not_Close_Popup()
{
var window = new Window()
{
Width = 800, Height = 800
};
var picker = new DatePicker()
{
Width = 300,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top
};
window.Content = picker;
window.Show();
Assert.False(picker.IsDropdownOpen);
Dispatcher.UIThread.RunJobs();
window.MouseDown(new Point(10, 10), MouseButton.Left);
Dispatcher.UIThread.RunJobs();
Assert.True(picker.IsDropdownOpen);
var popup = picker.GetTemplateChildOfType<Popup>(DatePicker.PART_Popup);
var calendar = popup?.GetLogicalDescendants().OfType<CalendarView>().FirstOrDefault();
Assert.NotNull(calendar);
var nextButton = calendar.GetTemplateChildOfType<Button>(CalendarView.PART_NextButton);
Assert.NotNull(nextButton);
var position = nextButton.TranslatePoint(new Point(5, 5), window);
Assert.NotNull(position);
window.MouseDown(new Point(10, 10), MouseButton.Left);
var renderRoot = popup.GetVisualRoot();
Dispatcher.UIThread.RunJobs();
Assert.True(picker.IsDropdownOpen);
}
}

View File

@@ -9,7 +9,7 @@ namespace HeadlessTest.Ursa.Controls.DateTimePicker;
public class TimePickerPresenterTests
{
[Fact]
[AvaloniaFact]
public void TimePickerPresenter_DefaultValues_ShouldBeCorrect()
{
var presenter = new TimePickerPresenter();
@@ -21,7 +21,7 @@ public class TimePickerPresenterTests
Assert.Equal("HH mm ss t", presenter.PanelFormat);
}
[Fact]
[AvaloniaFact]
public void TimePickerPresenter_SetTime_ShouldUpdateTimeProperty()
{
var presenter = new TimePickerPresenter();