Skip to content

Commit

Permalink
Added basic date/time picker automation peers. (#17426)
Browse files Browse the repository at this point in the history
  • Loading branch information
grokys authored and maxkatz6 committed Nov 14, 2024
1 parent 1c1d1f3 commit 5e4a3f9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/Avalonia.Controls/Automation/Peers/DatePickerAutomationPeer.cs
Original file line number Diff line number Diff line change
@@ -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;
}
25 changes: 25 additions & 0 deletions src/Avalonia.Controls/Automation/Peers/TimePickerAutomationPeer.cs
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 4 additions & 1 deletion src/Avalonia.Controls/DateTimePickers/DatePicker.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/Avalonia.Controls/DateTimePickers/TimePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Globalization;
using Avalonia.Controls.Utils;
using Avalonia.Automation.Peers;

namespace Avalonia.Controls
{
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 5e4a3f9

Please sign in to comment.