fix: a screen reader hears which sceneries lie under a group

Avalonia hung the entries under a group on the panel inside it, so NVDA counted
every scenery at level one and the tree sounded flat. An open group now gives its
entries as its own, and each entry names its group as its parent: Całkowo is
level one, calkowo_lotos under it level two.
This commit is contained in:
maj00r
2026-11-15 18:36:23 +01:00
parent 099ff3603e
commit d2b8cdf3ba
2 changed files with 39 additions and 3 deletions

View File

@@ -39,4 +39,16 @@ public class ExpandableTreeViewItemTests
peer.Collapse();
Assert.False(item.IsExpanded);
}
[Fact]
public void An_entry_on_its_own_has_nothing_under_it_and_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,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Automation;
using Avalonia.Automation.Peers;
using Avalonia.Automation.Provider;
@@ -7,9 +9,10 @@ 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.
/// A tree entry that tells a screen reader whether it opens and what lies under it.
/// Avalonia's own says only that it is an entry, and hangs the entries under a group
/// on the panel inside the group, so a reader heard every scenery at level one: a
/// group such as Całkowo sounded like a scenery, and what was in it like its neighbours.
/// </summary>
public class ExpandableTreeViewItem : TreeViewItem
{
@@ -25,6 +28,13 @@ 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;
private sealed class Peer : TreeViewItemAutomationPeer, IExpandCollapseProvider
{
private readonly ExpandableTreeViewItem _item;
@@ -35,11 +45,25 @@ 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();
protected override AutomationPeer? GetParentCore() =>
_item.ReadableParent is { } parent ? GetOrCreate(parent) : base.GetParentCore();
public ExpandCollapseState ExpandCollapseState => _item.ExpandState;
public bool ShowsMenu => false;