fix: NVDA reads the tree entry, slider and help button under the mouse

The tree entry listed the entries under it as its own children, so the controls
inside it lost their way up and Windows failed every lookup there. It now only
points up to the entry it hangs under, which is all the level needs.

The buttons and knob of a slider's track had no name; they now say the slider's
name and value. A button showing only a sign such as "?" takes its tooltip.
This commit is contained in:
maj00r
2026-11-15 19:07:20 +01:00
parent edd530cb4b
commit 95f1cd2398
6 changed files with 75 additions and 22 deletions

View File

@@ -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());
}
}

View File

@@ -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()
{

View File

@@ -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()
{

View File

@@ -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;
/// <summary>The entries a reader finds under this one: none while it is closed.</summary>
public IReadOnlyList<Control> ReadableChildren =>
IsExpanded ? GetRealizedContainers().Where(c => c.IsVisible).ToList() : Array.Empty<Control>();
/// <summary>The entry this one hangs under, or null at the top of the tree.</summary>
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<AutomationPeer>? 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();

View File

@@ -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.
/// </summary>
public class ReadableSlider : Slider
{
@@ -36,6 +42,46 @@ public class ReadableSlider : Slider
? value.ToString("0.##", CultureInfo.CurrentCulture)
: valueText.Trim();
/// <summary>What the mouse finds over the track: the slider's name and its value.</summary>
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<Track>("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<Control>().Prepend(_track))
AutomationProperties.SetName(part, text);
}
private sealed class Peer : RangeBaseAutomationPeer, IValueProvider
{
private readonly ReadableSlider _slider;

View File

@@ -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<StyledElement, string> Described = new();