feat: add button interaction.
This commit is contained in:
@@ -37,14 +37,9 @@ public class TimeRangePicker : TimePickerBase, IClearControl
|
||||
AvaloniaProperty.Register<TimeRangePicker, string?>(
|
||||
nameof(StartWatermark));
|
||||
|
||||
public static readonly StyledProperty<string?> EndWatermarkProperty = AvaloniaProperty.Register<TimeRangePicker, string?>(
|
||||
nameof(EndWatermark));
|
||||
|
||||
public string? EndWatermark
|
||||
{
|
||||
get => GetValue(EndWatermarkProperty);
|
||||
set => SetValue(EndWatermarkProperty, value);
|
||||
}
|
||||
public static readonly StyledProperty<string?> EndWatermarkProperty =
|
||||
AvaloniaProperty.Register<TimeRangePicker, string?>(
|
||||
nameof(EndWatermark));
|
||||
|
||||
private Button? _button;
|
||||
private TimePickerPresenter? _endPresenter;
|
||||
@@ -58,24 +53,15 @@ public class TimeRangePicker : TimePickerBase, IClearControl
|
||||
static TimeRangePicker()
|
||||
{
|
||||
StartTimeProperty.Changed.AddClassHandler<TimeRangePicker, TimeSpan?>((picker, args) =>
|
||||
picker.OnSelectionChanged(args, true));
|
||||
picker.OnSelectionChanged(args));
|
||||
EndTimeProperty.Changed.AddClassHandler<TimeRangePicker, TimeSpan?>((picker, args) =>
|
||||
picker.OnSelectionChanged(args, false));
|
||||
}
|
||||
|
||||
private void OnSelectionChanged(AvaloniaPropertyChangedEventArgs<TimeSpan?> args, bool start = true)
|
||||
public string? EndWatermark
|
||||
{
|
||||
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;
|
||||
get => GetValue(EndWatermarkProperty);
|
||||
set => SetValue(EndWatermarkProperty, value);
|
||||
}
|
||||
|
||||
public string? StartWatermark
|
||||
@@ -103,22 +89,47 @@ public class TimeRangePicker : TimePickerBase, IClearControl
|
||||
_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)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
|
||||
|
||||
GotFocusEvent.RemoveHandler(OnTextBoxGetFocus, _startTextBox, _endTextBox);
|
||||
PointerPressedEvent.RemoveHandler(OnTextBoxPointerPressed, _startTextBox, _endTextBox);
|
||||
|
||||
Button.ClickEvent.RemoveHandler(OnButtonClick, _button);
|
||||
|
||||
_popup = e.NameScope.Find<Popup>(PartNames.PART_Popup);
|
||||
_startTextBox = e.NameScope.Find<TextBox>(PART_StartTextBox);
|
||||
_endTextBox = e.NameScope.Find<TextBox>(PART_EndTextBox);
|
||||
_startPresenter = e.NameScope.Find<TimePickerPresenter>(PART_StartPresenter);
|
||||
_endPresenter = e.NameScope.Find<TimePickerPresenter>(PART_EndPresenter);
|
||||
_button = e.NameScope.Find<Button>(PART_Button);
|
||||
|
||||
|
||||
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)
|
||||
@@ -139,20 +150,20 @@ public class TimeRangePicker : TimePickerBase, IClearControl
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.Key == Key.Down)
|
||||
{
|
||||
SetCurrentValue(IsDropdownOpenProperty, true);
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.Key == Key.Tab)
|
||||
{
|
||||
if (e.Source == _endTextBox)
|
||||
{
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
}
|
||||
if (e.Source == _endTextBox) SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
return;
|
||||
}
|
||||
|
||||
base.OnKeyDown(e);
|
||||
}
|
||||
|
||||
@@ -163,4 +174,10 @@ public class TimeRangePicker : TimePickerBase, IClearControl
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
Focus();
|
||||
}
|
||||
|
||||
public void Dismiss()
|
||||
{
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
Focus();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user