Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic date/time picker automation peers. #17426

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading