fix: fix clear, fix panel am-pm panel initialization issue.

This commit is contained in:
Dong Bin
2025-02-18 21:48:31 +08:00
parent 27375f722e
commit 53b65f4eb7
2 changed files with 18 additions and 22 deletions

View File

@@ -61,8 +61,8 @@ public class TimePicker : TimePickerBase, IClearControl
public void Clear() public void Clear()
{ {
SetCurrentValue(SelectedTimeProperty, null);
Focus(NavigationMethod.Pointer); Focus(NavigationMethod.Pointer);
_presenter?.SyncTime(null);
} }
private void OnDisplayFormatChanged(AvaloniaPropertyChangedEventArgs<string?> _) private void OnDisplayFormatChanged(AvaloniaPropertyChangedEventArgs<string?> _)
@@ -71,6 +71,16 @@ public class TimePicker : TimePickerBase, IClearControl
SyncTimeToText(SelectedTime); SyncTimeToText(SelectedTime);
} }
/// <inheritdoc />
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
if (!e.Handled && e.Source is Visual source)
if (_popup?.IsInsidePopup(source) == true)
e.Handled = true;
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
@@ -89,9 +99,7 @@ public class TimePicker : TimePickerBase, IClearControl
TextBox.TextChangedEvent.AddHandler(OnTextChanged, _textBox); TextBox.TextChangedEvent.AddHandler(OnTextChanged, _textBox);
Button.ClickEvent.AddHandler(OnButtonClick, _button); Button.ClickEvent.AddHandler(OnButtonClick, _button);
TimePickerPresenter.SelectedTimeChangedEvent.AddHandler(OnPresenterTimeChanged, _presenter); TimePickerPresenter.SelectedTimeChangedEvent.AddHandler(OnPresenterTimeChanged, _presenter);
// SetCurrentValue(SelectedTimeProperty, DateTime.Now.TimeOfDay);
// _presenter?.SetValue(TimePickerPresenter.TimeProperty, SelectedTime);
_presenter?.SyncTime(SelectedTime); _presenter?.SyncTime(SelectedTime);
SyncTimeToText(SelectedTime); SyncTimeToText(SelectedTime);
} }
@@ -105,17 +113,14 @@ public class TimePicker : TimePickerBase, IClearControl
private void OnButtonClick(object? sender, RoutedEventArgs e) private void OnButtonClick(object? sender, RoutedEventArgs e)
{ {
if(IsFocused) if (IsFocused) SetCurrentValue(IsDropdownOpenProperty, !IsDropdownOpen);
{
SetCurrentValue(IsDropdownOpenProperty, !IsDropdownOpen);
}
} }
private void OnTextBoxGetFocus(object? sender, GotFocusEventArgs e) private void OnTextBoxGetFocus(object? sender, GotFocusEventArgs e)
{ {
SetCurrentValue(IsDropdownOpenProperty, true); SetCurrentValue(IsDropdownOpenProperty, true);
} }
protected override void OnKeyDown(KeyEventArgs e) protected override void OnKeyDown(KeyEventArgs e)
{ {
if (e.Key == Key.Escape) if (e.Key == Key.Escape)
@@ -124,20 +129,17 @@ public class TimePicker : TimePickerBase, IClearControl
e.Handled = true; e.Handled = true;
return; return;
} }
if (e.Key == Key.Down) if (e.Key == Key.Down)
{ {
SetCurrentValue(IsDropdownOpenProperty, true); SetCurrentValue(IsDropdownOpenProperty, true);
e.Handled = true; e.Handled = true;
return; return;
} }
if (e.Key == Key.Tab) if (e.Key == Key.Tab)
{ {
SetCurrentValue(IsDropdownOpenProperty, false); SetCurrentValue(IsDropdownOpenProperty, false);
return; return;
} }
base.OnKeyDown(e); base.OnKeyDown(e);
} }
@@ -150,18 +152,13 @@ public class TimePicker : TimePickerBase, IClearControl
} }
else if (DisplayFormat is null || DisplayFormat.Length == 0) else if (DisplayFormat is null || DisplayFormat.Length == 0)
{ {
if (TimeSpan.TryParse(_textBox?.Text, out var defaultTime)) if (TimeSpan.TryParse(_textBox?.Text, out var defaultTime)) _presenter?.SyncTime(defaultTime);
{
_presenter?.SyncTime(defaultTime);
}
} }
else else
{ {
if (DateTime.TryParseExact(_textBox?.Text, DisplayFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None, if (DateTime.TryParseExact(_textBox?.Text, DisplayFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None,
out var time)) out var time))
{
_presenter?.SyncTime(time.TimeOfDay); _presenter?.SyncTime(time.TimeOfDay);
}
} }
} }
@@ -210,6 +207,7 @@ public class TimePicker : TimePickerBase, IClearControl
protected override void OnGotFocus(GotFocusEventArgs e) protected override void OnGotFocus(GotFocusEventArgs e)
{ {
base.OnGotFocus(e); base.OnGotFocus(e);
// SetCurrentValue(IsDropdownOpenProperty, true);
FocusChanged(IsKeyboardFocusWithin); FocusChanged(IsKeyboardFocusWithin);
} }
@@ -220,6 +218,7 @@ public class TimePicker : TimePickerBase, IClearControl
var top = TopLevel.GetTopLevel(this); var top = TopLevel.GetTopLevel(this);
var element = top?.FocusManager?.GetFocusedElement(); var element = top?.FocusManager?.GetFocusedElement();
if (element is Visual v && _popup?.IsInsidePopup(v) == true) return; if (element is Visual v && _popup?.IsInsidePopup(v) == true) return;
if (element == _textBox) return;
SetCurrentValue(IsDropdownOpenProperty, false); SetCurrentValue(IsDropdownOpenProperty, false);
} }
@@ -227,12 +226,8 @@ public class TimePicker : TimePickerBase, IClearControl
{ {
var wasFocused = _isFocused; var wasFocused = _isFocused;
_isFocused = hasFocus; _isFocused = hasFocus;
if (hasFocus) if (hasFocus)
if (!wasFocused && _textBox != null) if (!wasFocused && _textBox != null)
{
_textBox.Focus(); _textBox.Focus();
_textBox.SelectAll();
}
} }
} }

View File

@@ -154,6 +154,7 @@ public class TimePickerPresenter : TemplatedControl
{ {
panels.Add(_ampmScrollPanel); panels.Add(_ampmScrollPanel);
_ampmSelector?.SetValue(DateTimePickerPanel.ItemFormatProperty, part); _ampmSelector?.SetValue(DateTimePickerPanel.ItemFormatProperty, part);
if (_ampmSelector is not null) _ampmSelector.IsEnabled = _use12Clock;
} }
} }
catch catch