diff --git a/StarterNG.Tests/ExpandableTreeViewItemTests.cs b/StarterNG.Tests/ExpandableTreeViewItemTests.cs index ad875de..e36f7a3 100644 --- a/StarterNG.Tests/ExpandableTreeViewItemTests.cs +++ b/StarterNG.Tests/ExpandableTreeViewItemTests.cs @@ -41,14 +41,12 @@ public class ExpandableTreeViewItemTests } [Fact] - public void An_entry_on_its_own_has_nothing_under_it_and_nothing_over_it() + public void An_entry_on_its_own_has_nothing_over_it() { var item = new ExpandableTreeViewItem { Header = "Całkowo", IsExpanded = true }; var peer = ControlAutomationPeer.CreatePeerForElement(item); - Assert.Empty(item.ReadableChildren); Assert.Null(item.ReadableParent); - Assert.Empty(peer.GetChildren()); Assert.Null(peer.GetParent()); } } diff --git a/StarterNG.Tests/ReadableSliderTests.cs b/StarterNG.Tests/ReadableSliderTests.cs index 1c84b2c..5445206 100644 --- a/StarterNG.Tests/ReadableSliderTests.cs +++ b/StarterNG.Tests/ReadableSliderTests.cs @@ -1,4 +1,5 @@ using System.Globalization; +using Avalonia.Automation; using Avalonia.Automation.Peers; using Avalonia.Automation.Provider; using Avalonia.Controls; @@ -8,6 +9,16 @@ namespace StarterNG.Tests; public class ReadableSliderTests { + [Fact] + public void The_track_under_the_mouse_says_the_name_and_the_value() + { + var fog = new ReadableSlider { ValueText = "2,4 km" }; + Assert.Equal("2,4 km", fog.TrackText); + + AutomationProperties.SetName(fog, "Widoczność "); + Assert.Equal("Widoczność: 2,4 km", fog.TrackText); + } + [Fact] public void A_slider_is_read_as_the_value_shown_beside_it() { diff --git a/StarterNG.Tests/ScreenReaderNamesTests.cs b/StarterNG.Tests/ScreenReaderNamesTests.cs index b4aad90..8b1a74f 100644 --- a/StarterNG.Tests/ScreenReaderNamesTests.cs +++ b/StarterNG.Tests/ScreenReaderNamesTests.cs @@ -69,11 +69,20 @@ public class ScreenReaderNamesTests { var button = new Button { Content = new Image() }; ToolTip.SetTip(button, "Pomoc"); - button.Content = "?"; + button.Content = "Pomoc"; Assert.Null(AutomationProperties.GetName(button)); } + [Fact] + public void A_button_with_only_a_sign_on_it_is_named_by_its_tooltip() + { + var button = new Button { Content = "?" }; + ToolTip.SetTip(button, "Pomoc"); + + Assert.Equal("Pomoc", AutomationProperties.GetName(button)); + } + [Fact] public void A_tree_entry_is_named_by_the_visible_text_of_its_header() { diff --git a/StarterNG/Controls/ExpandableTreeViewItem.cs b/StarterNG/Controls/ExpandableTreeViewItem.cs index 50603d5..b50ba36 100644 --- a/StarterNG/Controls/ExpandableTreeViewItem.cs +++ b/StarterNG/Controls/ExpandableTreeViewItem.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using Avalonia.Automation; using Avalonia.Automation.Peers; using Avalonia.Automation.Provider; @@ -28,10 +26,6 @@ public class ExpandableTreeViewItem : TreeViewItem : expanded ? ExpandCollapseState.Expanded : ExpandCollapseState.Collapsed; - /// The entries a reader finds under this one: none while it is closed. - public IReadOnlyList ReadableChildren => - IsExpanded ? GetRealizedContainers().Where(c => c.IsVisible).ToList() : Array.Empty(); - /// The entry this one hangs under, or null at the top of the tree. public ExpandableTreeViewItem? ReadableParent => ItemsControlFromItemContainer(this) as ExpandableTreeViewItem; @@ -45,22 +39,14 @@ public class ExpandableTreeViewItem : TreeViewItem item.PropertyChanged += (_, e) => { if (e.Property == IsExpandedProperty && e.OldValue is bool was && e.NewValue is bool now) - { - InvalidateChildren(); RaisePropertyChangedEvent(ExpandCollapsePatternIdentifiers.ExpandCollapseStateProperty, StateFor(item.ItemCount > 0, was), StateFor(item.ItemCount > 0, now)); - } - else if (e.Property == ItemCountProperty) - InvalidateChildren(); }; - item.ContainerPrepared += (_, _) => InvalidateChildren(); - item.ContainerClearing += (_, _) => InvalidateChildren(); } - // The entries under it, straight: its name already says what its header shows. - protected override IReadOnlyList? GetChildrenCore() => - _item.ReadableChildren.Select(GetOrCreate).ToList(); - + // Only the way up changes: a reader counts the entries over this one to tell its + // level. The way down stays the controls on screen, so whatever the mouse points + // at still leads up through its entry to the window. protected override AutomationPeer? GetParentCore() => _item.ReadableParent is { } parent ? GetOrCreate(parent) : base.GetParentCore(); diff --git a/StarterNG/Controls/ReadableSlider.cs b/StarterNG/Controls/ReadableSlider.cs index 5728b29..472d325 100644 --- a/StarterNG/Controls/ReadableSlider.cs +++ b/StarterNG/Controls/ReadableSlider.cs @@ -1,9 +1,14 @@ using System; using System.Globalization; +using System.Linq; using Avalonia; +using Avalonia.Automation; using Avalonia.Automation.Peers; using Avalonia.Automation.Provider; using Avalonia.Controls; +using Avalonia.Controls.Primitives; +using Avalonia.Interactivity; +using Avalonia.VisualTree; namespace StarterNG.Controls; @@ -11,6 +16,7 @@ namespace StarterNG.Controls; /// A slider that tells a screen reader the value it shows beside it. Avalonia's own /// gives only the bare number, which NVDA reads as a share of the slider's length: /// -5 °C was read as 45, a fog of 2 km as wherever the knob stood on its scale. +/// The track under the mouse says the same, where its bare buttons and knob said nothing. /// public class ReadableSlider : Slider { @@ -36,6 +42,46 @@ public class ReadableSlider : Slider ? value.ToString("0.##", CultureInfo.CurrentCulture) : valueText.Trim(); + /// What the mouse finds over the track: the slider's name and its value. + public string TrackText => + AutomationProperties.GetName(this) is { } name && !string.IsNullOrWhiteSpace(name) + ? $"{name.Trim()}: {SpokenValue}" + : SpokenValue; + + private Track? _track; + + protected override void OnApplyTemplate(TemplateAppliedEventArgs e) + { + base.OnApplyTemplate(e); + _track = e.NameScope.Find("PART_Track"); + NameTrack(); + } + + // The knob gets its own template only when first laid out. + protected override void OnLoaded(RoutedEventArgs e) + { + base.OnLoaded(e); + NameTrack(); + } + + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + if (change.Property == ValueProperty || change.Property == ValueTextProperty + || change.Property == AutomationProperties.NameProperty) + NameTrack(); + } + + private void NameTrack() + { + if (_track is null) + return; + + string text = TrackText; + foreach (var part in _track.GetVisualDescendants().OfType().Prepend(_track)) + AutomationProperties.SetName(part, text); + } + private sealed class Peer : RangeBaseAutomationPeer, IValueProvider { private readonly ReadableSlider _slider; diff --git a/StarterNG/Controls/ScreenReaderNames.cs b/StarterNG/Controls/ScreenReaderNames.cs index 3d50681..c49294b 100644 --- a/StarterNG/Controls/ScreenReaderNames.cs +++ b/StarterNG/Controls/ScreenReaderNames.cs @@ -158,8 +158,11 @@ public static class ScreenReaderNames AutomationProperties.SetAccessibilityView(panel, name is null ? AccessibilityView.Default : AccessibilityView.Content); } + // A sign such as "?" says nothing read aloud; its tooltip says what it does. private static void NameButton(Button button) => - Give(button, button.Content is string ? null : TextOf(ToolTip.GetTip(button)) ?? TextOf(button.Content)); + Give(button, button.Content is string text && text.Any(char.IsLetterOrDigit) + ? null + : TextOf(ToolTip.GetTip(button)) ?? TextOf(button.Content)); // The descriptions given here, so a later one replaces it but nothing else. private static readonly ConditionalWeakTable Described = new();