feat: add button interaction.

This commit is contained in:
rabbitism
2024-04-28 18:13:20 +08:00
parent 2b2259376a
commit bfd7f0c44e

View File

@@ -37,15 +37,10 @@ public class TimeRangePicker : TimePickerBase, IClearControl
AvaloniaProperty.Register<TimeRangePicker, string?>( AvaloniaProperty.Register<TimeRangePicker, string?>(
nameof(StartWatermark)); nameof(StartWatermark));
public static readonly StyledProperty<string?> EndWatermarkProperty = AvaloniaProperty.Register<TimeRangePicker, string?>( public static readonly StyledProperty<string?> EndWatermarkProperty =
AvaloniaProperty.Register<TimeRangePicker, string?>(
nameof(EndWatermark)); nameof(EndWatermark));
public string? EndWatermark
{
get => GetValue(EndWatermarkProperty);
set => SetValue(EndWatermarkProperty, value);
}
private Button? _button; private Button? _button;
private TimePickerPresenter? _endPresenter; private TimePickerPresenter? _endPresenter;
private TextBox? _endTextBox; private TextBox? _endTextBox;
@@ -58,24 +53,15 @@ public class TimeRangePicker : TimePickerBase, IClearControl
static TimeRangePicker() static TimeRangePicker()
{ {
StartTimeProperty.Changed.AddClassHandler<TimeRangePicker, TimeSpan?>((picker, args) => StartTimeProperty.Changed.AddClassHandler<TimeRangePicker, TimeSpan?>((picker, args) =>
picker.OnSelectionChanged(args, true)); picker.OnSelectionChanged(args));
EndTimeProperty.Changed.AddClassHandler<TimeRangePicker, TimeSpan?>((picker, args) => EndTimeProperty.Changed.AddClassHandler<TimeRangePicker, TimeSpan?>((picker, args) =>
picker.OnSelectionChanged(args, false)); picker.OnSelectionChanged(args, false));
} }
private void OnSelectionChanged(AvaloniaPropertyChangedEventArgs<TimeSpan?> args, bool start = true) public string? EndWatermark
{ {
var textBox = start ? _startTextBox : _endTextBox; get => GetValue(EndWatermarkProperty);
if (textBox is null) return; set => SetValue(EndWatermarkProperty, value);
var time = args.NewValue.Value;
if (time is null)
{
textBox.Text = null;
return;
}
var date = new DateTime(1, 1, 1, time.Value.Hours, time.Value.Minutes, time.Value.Seconds);
var text = date.ToString(DisplayFormat);
textBox.Text = text;
} }
public string? StartWatermark public string? StartWatermark
@@ -103,12 +89,29 @@ public class TimeRangePicker : TimePickerBase, IClearControl
_endPresenter?.SetValue(TimePickerPresenter.TimeProperty, null); _endPresenter?.SetValue(TimePickerPresenter.TimeProperty, null);
} }
private void OnSelectionChanged(AvaloniaPropertyChangedEventArgs<TimeSpan?> args, bool start = true)
{
var textBox = start ? _startTextBox : _endTextBox;
if (textBox is null) return;
var time = args.NewValue.Value;
if (time is null)
{
textBox.Text = null;
return;
}
var date = new DateTime(1, 1, 1, time.Value.Hours, time.Value.Minutes, time.Value.Seconds);
var text = date.ToString(DisplayFormat);
textBox.Text = text;
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
GotFocusEvent.RemoveHandler(OnTextBoxGetFocus, _startTextBox, _endTextBox); GotFocusEvent.RemoveHandler(OnTextBoxGetFocus, _startTextBox, _endTextBox);
PointerPressedEvent.RemoveHandler(OnTextBoxPointerPressed, _startTextBox, _endTextBox); PointerPressedEvent.RemoveHandler(OnTextBoxPointerPressed, _startTextBox, _endTextBox);
Button.ClickEvent.RemoveHandler(OnButtonClick, _button);
_popup = e.NameScope.Find<Popup>(PartNames.PART_Popup); _popup = e.NameScope.Find<Popup>(PartNames.PART_Popup);
_startTextBox = e.NameScope.Find<TextBox>(PART_StartTextBox); _startTextBox = e.NameScope.Find<TextBox>(PART_StartTextBox);
@@ -118,7 +121,15 @@ public class TimeRangePicker : TimePickerBase, IClearControl
_button = e.NameScope.Find<Button>(PART_Button); _button = e.NameScope.Find<Button>(PART_Button);
GotFocusEvent.AddHandler(OnTextBoxGetFocus, _startTextBox, _endTextBox); GotFocusEvent.AddHandler(OnTextBoxGetFocus, _startTextBox, _endTextBox);
PointerPressedEvent.AddHandler(OnTextBoxPointerPressed, RoutingStrategies.Tunnel, false, _startTextBox, _endTextBox); PointerPressedEvent.AddHandler(OnTextBoxPointerPressed, RoutingStrategies.Tunnel, false, _startTextBox,
_endTextBox);
Button.ClickEvent.AddHandler(OnButtonClick, _button);
}
private void OnButtonClick(object sender, RoutedEventArgs e)
{
Focus(NavigationMethod.Pointer);
SetCurrentValue(IsDropdownOpenProperty, !IsDropdownOpen);
} }
private void OnTextBoxPointerPressed(object sender, PointerPressedEventArgs e) private void OnTextBoxPointerPressed(object sender, PointerPressedEventArgs e)
@@ -139,20 +150,20 @@ public class TimeRangePicker : 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)
{ {
if (e.Source == _endTextBox) if (e.Source == _endTextBox) SetCurrentValue(IsDropdownOpenProperty, false);
{
SetCurrentValue(IsDropdownOpenProperty, false);
}
return; return;
} }
base.OnKeyDown(e); base.OnKeyDown(e);
} }
@@ -163,4 +174,10 @@ public class TimeRangePicker : TimePickerBase, IClearControl
SetCurrentValue(IsDropdownOpenProperty, false); SetCurrentValue(IsDropdownOpenProperty, false);
Focus(); Focus();
} }
public void Dismiss()
{
SetCurrentValue(IsDropdownOpenProperty, false);
Focus();
}
} }