From 942f645995e7be2fe2e77d30d51623512659353e Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 4 Nov 2024 11:52:12 +0100 Subject: [PATCH] Added basic date/time picker automation peers. --- .../Peers/DatePickerAutomationPeer.cs | 25 +++++++++++++++++++ .../Peers/TimePickerAutomationPeer.cs | 25 +++++++++++++++++++ .../DateTimePickers/DatePicker.cs | 5 +++- .../DateTimePickers/TimePicker.cs | 3 +++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/Avalonia.Controls/Automation/Peers/DatePickerAutomationPeer.cs create mode 100644 src/Avalonia.Controls/Automation/Peers/TimePickerAutomationPeer.cs diff --git a/src/Avalonia.Controls/Automation/Peers/DatePickerAutomationPeer.cs b/src/Avalonia.Controls/Automation/Peers/DatePickerAutomationPeer.cs new file mode 100644 index 00000000000..9492f5cf743 --- /dev/null +++ b/src/Avalonia.Controls/Automation/Peers/DatePickerAutomationPeer.cs @@ -0,0 +1,25 @@ +using System; +using Avalonia.Automation.Provider; +using Avalonia.Controls; + +namespace Avalonia.Automation.Peers; + +public class DatePickerAutomationPeer : ControlAutomationPeer, IValueProvider +{ + public DatePickerAutomationPeer(DatePicker owner) + : base(owner) + { + } + + public bool IsReadOnly => false; + public new DatePicker Owner => (DatePicker)base.Owner; + public string? Value => Owner.SelectedDate?.ToString(); + + public void SetValue(string? value) + { + if (DateTimeOffset.TryParse(value, out var result)) + Owner.SelectedDate = result; + } + + protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.Custom; +} diff --git a/src/Avalonia.Controls/Automation/Peers/TimePickerAutomationPeer.cs b/src/Avalonia.Controls/Automation/Peers/TimePickerAutomationPeer.cs new file mode 100644 index 00000000000..e9045cc208b --- /dev/null +++ b/src/Avalonia.Controls/Automation/Peers/TimePickerAutomationPeer.cs @@ -0,0 +1,25 @@ +using System; +using Avalonia.Automation.Provider; +using Avalonia.Controls; + +namespace Avalonia.Automation.Peers; + +public class TimePickerAutomationPeer : ControlAutomationPeer, IValueProvider +{ + public TimePickerAutomationPeer(TimePicker owner) + : base(owner) + { + } + + public bool IsReadOnly => false; + public new TimePicker Owner => (TimePicker)base.Owner; + public string? Value => Owner.SelectedTime?.ToString(); + + public void SetValue(string? value) + { + if (TimeSpan.TryParse(value, out var result)) + Owner.SelectedTime = result; + } + + protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.Custom; +} diff --git a/src/Avalonia.Controls/DateTimePickers/DatePicker.cs b/src/Avalonia.Controls/DateTimePickers/DatePicker.cs index 212cb64b13d..837d8ec9a30 100644 --- a/src/Avalonia.Controls/DateTimePickers/DatePicker.cs +++ b/src/Avalonia.Controls/DateTimePickers/DatePicker.cs @@ -1,4 +1,5 @@ -using Avalonia.Controls.Metadata; +using Avalonia.Automation.Peers; +using Avalonia.Controls.Metadata; using Avalonia.Controls.Primitives; using Avalonia.Controls.Shapes; using Avalonia.Data; @@ -267,6 +268,8 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e) } } + protected override AutomationPeer OnCreateAutomationPeer() => new DatePickerAutomationPeer(this); + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { base.OnPropertyChanged(change); diff --git a/src/Avalonia.Controls/DateTimePickers/TimePicker.cs b/src/Avalonia.Controls/DateTimePickers/TimePicker.cs index d1452985635..5ad110eb693 100644 --- a/src/Avalonia.Controls/DateTimePickers/TimePicker.cs +++ b/src/Avalonia.Controls/DateTimePickers/TimePicker.cs @@ -6,6 +6,7 @@ using System; using System.Globalization; using Avalonia.Controls.Utils; +using Avalonia.Automation.Peers; namespace Avalonia.Controls { @@ -330,6 +331,8 @@ private void SetSelectedTimeText() } } + protected override AutomationPeer OnCreateAutomationPeer() => new TimePickerAutomationPeer(this); + protected virtual void OnSelectedTimeChanged(TimeSpan? oldTime, TimeSpan? newTime) { SelectedTimeChanged?.Invoke(this, new TimePickerSelectedValueChangedEventArgs(oldTime, newTime));