fix: a screen reader says what the date and time hold and that a tree group opens

The date and time pickers were read as a grid: the button inside them now carries
the field's label and its value. A scenery group in the tree says whether it is
open or closed, and the weather fields are named by their labels.
This commit is contained in:
maj00r
2026-11-15 17:49:44 +01:00
parent a83bb4a7eb
commit 5883376762
7 changed files with 192 additions and 10 deletions

View File

@@ -0,0 +1,42 @@
using Avalonia.Automation;
using Avalonia.Automation.Peers;
using Avalonia.Automation.Provider;
using StarterNG.Controls;
namespace StarterNG.Tests;
public class ExpandableTreeViewItemTests
{
private static IExpandCollapseProvider Peer(ExpandableTreeViewItem item) =>
Assert.IsAssignableFrom<IExpandCollapseProvider>(ControlAutomationPeer.CreatePeerForElement(item));
[Fact]
public void A_scenery_with_nothing_under_it_is_a_leaf()
{
var peer = Peer(new ExpandableTreeViewItem { Header = "test32" });
Assert.Equal(ExpandCollapseState.LeafNode, peer.ExpandCollapseState);
Assert.False(peer.ShowsMenu);
}
[Fact]
public void A_group_says_whether_it_is_open()
{
Assert.Equal(ExpandCollapseState.Collapsed, ExpandableTreeViewItem.StateFor(hasChildren: true, expanded: false));
Assert.Equal(ExpandCollapseState.Expanded, ExpandableTreeViewItem.StateFor(hasChildren: true, expanded: true));
Assert.Equal(ExpandCollapseState.LeafNode, ExpandableTreeViewItem.StateFor(hasChildren: false, expanded: true));
}
[Fact]
public void A_screen_reader_can_open_and_close_an_entry()
{
var item = new ExpandableTreeViewItem { Header = "Całkowo" };
var peer = Peer(item);
peer.Expand();
Assert.True(item.IsExpanded);
peer.Collapse();
Assert.False(item.IsExpanded);
}
}

View File

@@ -1,3 +1,4 @@
using System.Globalization;
using Avalonia.Automation;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
@@ -149,3 +150,41 @@ public class ScreenReaderNamesTests
Assert.Null(AutomationProperties.GetName(box));
}
}
public class PickerNameTests
{
[Fact]
public void A_picker_is_read_as_its_label_and_its_value()
{
Assert.Equal("Godzina: 07:05", ScreenReaderNames.PickerText("Godzina", "07:05"));
Assert.Equal("Data", ScreenReaderNames.PickerText("Data", null));
Assert.Equal("07:05", ScreenReaderNames.PickerText(null, "07:05"));
Assert.Null(ScreenReaderNames.PickerText("", null));
}
[Fact]
public void A_date_is_read_in_words_with_the_year_only_when_shown()
{
var before = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("pl-PL");
try
{
var date = new DateTimeOffset(2026, 10, 27, 0, 0, 0, TimeSpan.FromHours(1));
Assert.Equal("27 października", ScreenReaderNames.DateText(date, withYear: false));
Assert.Equal("wtorek, 27 października 2026", ScreenReaderNames.DateText(date, withYear: true));
Assert.Null(ScreenReaderNames.DateText(null, withYear: false));
}
finally
{
CultureInfo.CurrentCulture = before;
}
}
[Fact]
public void A_time_is_read_as_hours_and_minutes()
{
Assert.Equal("07:05", ScreenReaderNames.TimeText(new TimeSpan(7, 5, 30)));
Assert.Null(ScreenReaderNames.TimeText(null));
}
}

View File

@@ -0,0 +1,51 @@
using System;
using Avalonia.Automation;
using Avalonia.Automation.Peers;
using Avalonia.Automation.Provider;
using Avalonia.Controls;
namespace StarterNG.Controls;
/// <summary>
/// A tree entry that tells a screen reader whether it opens. Avalonia's own says only
/// that it is an entry, so a scenery group such as Całkowo sounded like a scenery and
/// nobody without the arrow on screen knew there was more under it.
/// </summary>
public class ExpandableTreeViewItem : TreeViewItem
{
protected override Type StyleKeyOverride => typeof(TreeViewItem);
protected override AutomationPeer OnCreateAutomationPeer() => new Peer(this);
/// <summary>What a screen reader is told: a leaf, or open or closed.</summary>
public ExpandCollapseState ExpandState => StateFor(ItemCount > 0, IsExpanded);
public static ExpandCollapseState StateFor(bool hasChildren, bool expanded) =>
!hasChildren ? ExpandCollapseState.LeafNode
: expanded ? ExpandCollapseState.Expanded
: ExpandCollapseState.Collapsed;
private sealed class Peer : TreeViewItemAutomationPeer, IExpandCollapseProvider
{
private readonly ExpandableTreeViewItem _item;
public Peer(ExpandableTreeViewItem item) : base(item)
{
_item = item;
item.PropertyChanged += (_, e) =>
{
if (e.Property == IsExpandedProperty && e.OldValue is bool was && e.NewValue is bool now)
RaisePropertyChangedEvent(ExpandCollapsePatternIdentifiers.ExpandCollapseStateProperty,
StateFor(item.ItemCount > 0, was), StateFor(item.ItemCount > 0, now));
};
}
public ExpandCollapseState ExpandCollapseState => _item.ExpandState;
public bool ShowsMenu => false;
public void Expand() => _item.IsExpanded = true;
public void Collapse() => _item.IsExpanded = false;
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using Avalonia;
@@ -32,8 +33,55 @@ public static class ScreenReaderNames
ComboBox.PlaceholderTextProperty.Changed.AddClassHandler<ComboBox>((c, _) => Give(c, c.PlaceholderText));
TextBox.PlaceholderTextProperty.Changed.AddClassHandler<TextBox>((t, _) => Give(t, t.PlaceholderText));
TemplatedControl.TemplateAppliedEvent.AddClassHandler<DatePicker>((p, e) => KeepPickerButton(p, e));
TemplatedControl.TemplateAppliedEvent.AddClassHandler<TimePicker>((p, e) => KeepPickerButton(p, e));
DatePicker.SelectedDateProperty.Changed.AddClassHandler<DatePicker>((p, _) => NamePicker(p));
TimePicker.SelectedTimeProperty.Changed.AddClassHandler<TimePicker>((p, _) => NamePicker(p));
AutomationProperties.NameProperty.Changed.AddClassHandler<DatePicker>((p, _) => NamePicker(p));
AutomationProperties.NameProperty.Changed.AddClassHandler<TimePicker>((p, _) => NamePicker(p));
}
// A date or time picker is reached through the button inside it, which was read as
// "Avalonia.Controls.Grid": its texts are filled in only after the name is looked for.
private static readonly ConditionalWeakTable<TemplatedControl, Button> PickerButtons = new();
private static void KeepPickerButton(TemplatedControl picker, TemplateAppliedEventArgs e)
{
if (e.NameScope.Find<Button>("PART_FlyoutButton") is not { } button)
return;
PickerButtons.AddOrUpdate(picker, button);
NamePicker(picker);
}
private static void NamePicker(TemplatedControl picker)
{
if (!PickerButtons.TryGetValue(picker, out var button))
return;
string? value = picker switch
{
DatePicker date => DateText(date.SelectedDate, date.YearVisible),
TimePicker time => TimeText(time.SelectedTime),
_ => null
};
AutomationProperties.SetName(button, PickerText(AutomationProperties.GetName(picker), value));
}
/// <summary>The picker's label and what it holds, as the button inside it is read.</summary>
public static string? PickerText(string? label, string? value) =>
string.IsNullOrEmpty(label) ? value
: string.IsNullOrEmpty(value) ? label
: $"{label}: {value}";
/// <summary>Day and month in words, the year too when the picker shows one.</summary>
public static string? DateText(DateTimeOffset? date, bool withYear) =>
date?.ToString(withYear ? "D" : "M", CultureInfo.CurrentCulture);
public static string? TimeText(TimeSpan? time) =>
time?.ToString(@"hh\:mm", CultureInfo.InvariantCulture);
private static void NameButton(Button button) =>
Give(button, button.Content is string ? null : TextOf(ToolTip.GetTip(button)) ?? TextOf(button.Content));

View File

@@ -110,11 +110,11 @@
Foreground="{StaticResource CarbonFgDim}"
Text="{Binding [Date], Source={x:Static starterNg:App.Loc}}" />
<Grid Grid.Row="0" Grid.Column="2" ColumnDefinitions="150,10,150,10,*">
<DatePicker Grid.Column="0" Name="weatherDate"
<DatePicker Grid.Column="0" Name="weatherDate" AutomationProperties.Name="{Binding [Date], Source={x:Static starterNg:App.Loc}}"
YearVisible="False" MinWidth="0"
VerticalAlignment="Center" HorizontalAlignment="Stretch"
SelectedDateChanged="WeatherDate_OnChanged" />
<ComboBox Grid.Column="2" Name="weatherSeason" Classes="Localized"
<ComboBox Grid.Column="2" Name="weatherSeason" AutomationProperties.Name="{Binding [Season], Source={x:Static starterNg:App.Loc}}" Classes="Localized"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
SelectionChanged="WeatherSeason_OnChanged">
<ComboBoxItem Tag="0" Content="{Binding [SeasonNow], Source={x:Static starterNg:App.Loc}}" />
@@ -132,7 +132,7 @@
Foreground="{StaticResource CarbonFgDim}"
Text="{Binding [Time], Source={x:Static starterNg:App.Loc}}" />
<Grid Grid.Row="2" Grid.Column="2" ColumnDefinitions="150,8,*">
<TimePicker Grid.Column="0" Name="weatherTime"
<TimePicker Grid.Column="0" Name="weatherTime" AutomationProperties.Name="{Binding [Time], Source={x:Static starterNg:App.Loc}}"
ClockIdentifier="24HourClock" MinuteIncrement="1"
MinWidth="0" VerticalAlignment="Center"
HorizontalAlignment="Stretch"
@@ -147,7 +147,7 @@
<TextBlock Grid.Row="4" Grid.Column="0" VerticalAlignment="Center"
Foreground="{StaticResource CarbonFgDim}"
Text="{Binding [Temperature], Source={x:Static starterNg:App.Loc}}" />
<Slider Grid.Row="4" Grid.Column="2" Name="weatherTemp"
<Slider Grid.Row="4" Grid.Column="2" Name="weatherTemp" AutomationProperties.Name="{Binding [Temperature], Source={x:Static starterNg:App.Loc}}"
Minimum="-50" Maximum="50"
TickFrequency="1" IsSnapToTickEnabled="True"
ValueChanged="Weather_OnSliderChanged" />
@@ -164,7 +164,7 @@
<TextBlock Grid.Row="7" Grid.Column="0" VerticalAlignment="Center"
Foreground="{StaticResource CarbonFgDim}"
Text="{Binding [Visibility], Source={x:Static starterNg:App.Loc}}" />
<Slider Grid.Row="7" Grid.Column="2" Name="weatherFog"
<Slider Grid.Row="7" Grid.Column="2" Name="weatherFog" AutomationProperties.Name="{Binding [Visibility], Source={x:Static starterNg:App.Loc}}"
Minimum="0" Maximum="1000"
TickFrequency="1" IsSnapToTickEnabled="True"
ValueChanged="Weather_OnSliderChanged" />
@@ -182,7 +182,7 @@
<TextBlock Grid.Row="10" Grid.Column="0" VerticalAlignment="Center"
Foreground="{StaticResource CarbonFgDim}"
Text="{Binding [Overcast], Source={x:Static starterNg:App.Loc}}" />
<ComboBox Grid.Row="10" Grid.Column="2" Name="weatherOvercast" Classes="Localized"
<ComboBox Grid.Row="10" Grid.Column="2" Name="weatherOvercast" AutomationProperties.Name="{Binding [Overcast], Source={x:Static starterNg:App.Loc}}" Classes="Localized"
HorizontalAlignment="Stretch"
SelectionChanged="Weather_OnSelectionChanged">
<ComboBoxItem Tag="random" Content="{Binding [OvercastRandom], Source={x:Static starterNg:App.Loc}}" />

View File

@@ -17,6 +17,7 @@ using Avalonia.Media.Imaging;
using Avalonia.Threading;
using Avalonia.VisualTree;
using StarterNG.Classes;
using StarterNG.Controls;
using StarterNG.Domain;
using StarterNG.Application;
using StarterNG.Domain.Sceneries;
@@ -101,7 +102,7 @@ public partial class Scenarios : UserControl
private TreeViewItem ToTreeItem(SceneryTreeNode node, bool expandGroups)
{
var item = new TreeViewItem
var item = new ExpandableTreeViewItem
{
Header = node.IsGroup ? node.Label : SceneryHeader(node.Label, Sceneries[node.SceneryIndex]),
Tag = node.IsGroup ? null : node.SceneryIndex,

View File

@@ -14,6 +14,7 @@ using Avalonia.Platform.Storage;
using Avalonia.Threading;
using Eu07.BlackBox.Views;
using StarterNG.Application;
using StarterNG.Controls;
using StarterNG.Domain.Settings;
using StarterNG.Domain.Tapes;
@@ -418,7 +419,7 @@ public sealed class HaslerTapeWindow : Window
var name = new TextBlock { Text = title, FontWeight = FontWeight.SemiBold, TextTrimming = TextTrimming.CharacterEllipsis };
ToolTip.SetTip(name, title);
var item = new TreeViewItem
var item = new ExpandableTreeViewItem
{
Header = new StackPanel { Spacing = 1, Children = { name, details } },
Tag = session,
@@ -442,7 +443,7 @@ public sealed class HaslerTapeWindow : Window
Opacity = 0.7
});
ToolTip.SetTip(text, App.Loc["HaslerBlackBoxTip"]);
var item = new TreeViewItem { Header = text, Tag = box };
var item = new ExpandableTreeViewItem { Header = text, Tag = box };
// Also when it is the one picked already, and its window was closed since.
item.Tapped += async (_, _) => await OpenBlackBoxAsync(box);
return item;
@@ -592,7 +593,7 @@ public sealed class HaslerTapeWindow : Window
};
if (role is not null)
text.Children.Add(new TextBlock { Text = role, FontSize = 11, Opacity = 0.7 });
return new TreeViewItem { Header = text, Tag = file };
return new ExpandableTreeViewItem { Header = text, Tag = file };
}
/// <summary>