From 5883376762a0bea6574807dca9ab57efce650c9c Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 15 Nov 2026 17:49:44 +0100 Subject: [PATCH] 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. --- .../ExpandableTreeViewItemTests.cs | 42 +++++++++++++++ StarterNG.Tests/ScreenReaderNamesTests.cs | 39 ++++++++++++++ StarterNG/Controls/ExpandableTreeViewItem.cs | 51 +++++++++++++++++++ StarterNG/Controls/ScreenReaderNames.cs | 48 +++++++++++++++++ StarterNG/Views/Scenarios.axaml | 12 ++--- StarterNG/Views/Scenarios.axaml.cs | 3 +- StarterNG/Views/Tapes/HaslerTapeWindow.cs | 7 +-- 7 files changed, 192 insertions(+), 10 deletions(-) create mode 100644 StarterNG.Tests/ExpandableTreeViewItemTests.cs create mode 100644 StarterNG/Controls/ExpandableTreeViewItem.cs diff --git a/StarterNG.Tests/ExpandableTreeViewItemTests.cs b/StarterNG.Tests/ExpandableTreeViewItemTests.cs new file mode 100644 index 0000000..3765220 --- /dev/null +++ b/StarterNG.Tests/ExpandableTreeViewItemTests.cs @@ -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(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); + } +} diff --git a/StarterNG.Tests/ScreenReaderNamesTests.cs b/StarterNG.Tests/ScreenReaderNamesTests.cs index 8e45293..8473be7 100644 --- a/StarterNG.Tests/ScreenReaderNamesTests.cs +++ b/StarterNG.Tests/ScreenReaderNamesTests.cs @@ -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)); + } +} diff --git a/StarterNG/Controls/ExpandableTreeViewItem.cs b/StarterNG/Controls/ExpandableTreeViewItem.cs new file mode 100644 index 0000000..4964be0 --- /dev/null +++ b/StarterNG/Controls/ExpandableTreeViewItem.cs @@ -0,0 +1,51 @@ +using System; +using Avalonia.Automation; +using Avalonia.Automation.Peers; +using Avalonia.Automation.Provider; +using Avalonia.Controls; + +namespace StarterNG.Controls; + +/// +/// 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. +/// +public class ExpandableTreeViewItem : TreeViewItem +{ + protected override Type StyleKeyOverride => typeof(TreeViewItem); + + protected override AutomationPeer OnCreateAutomationPeer() => new Peer(this); + + /// What a screen reader is told: a leaf, or open or closed. + 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; + } +} diff --git a/StarterNG/Controls/ScreenReaderNames.cs b/StarterNG/Controls/ScreenReaderNames.cs index 1ce9499..9df379b 100644 --- a/StarterNG/Controls/ScreenReaderNames.cs +++ b/StarterNG/Controls/ScreenReaderNames.cs @@ -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((c, _) => Give(c, c.PlaceholderText)); TextBox.PlaceholderTextProperty.Changed.AddClassHandler((t, _) => Give(t, t.PlaceholderText)); + + TemplatedControl.TemplateAppliedEvent.AddClassHandler((p, e) => KeepPickerButton(p, e)); + TemplatedControl.TemplateAppliedEvent.AddClassHandler((p, e) => KeepPickerButton(p, e)); + DatePicker.SelectedDateProperty.Changed.AddClassHandler((p, _) => NamePicker(p)); + TimePicker.SelectedTimeProperty.Changed.AddClassHandler((p, _) => NamePicker(p)); + AutomationProperties.NameProperty.Changed.AddClassHandler((p, _) => NamePicker(p)); + AutomationProperties.NameProperty.Changed.AddClassHandler((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 PickerButtons = new(); + + private static void KeepPickerButton(TemplatedControl picker, TemplateAppliedEventArgs e) + { + if (e.NameScope.Find