From d2b8cdf3baed9155c686c776fc58cc8229b4e26e Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 15 Nov 2026 18:36:23 +0100 Subject: [PATCH] fix: a screen reader hears which sceneries lie under a group MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../ExpandableTreeViewItemTests.cs | 12 ++++++++ StarterNG/Controls/ExpandableTreeViewItem.cs | 30 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/StarterNG.Tests/ExpandableTreeViewItemTests.cs b/StarterNG.Tests/ExpandableTreeViewItemTests.cs index 3765220..ad875de 100644 --- a/StarterNG.Tests/ExpandableTreeViewItemTests.cs +++ b/StarterNG.Tests/ExpandableTreeViewItemTests.cs @@ -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()); + } } diff --git a/StarterNG/Controls/ExpandableTreeViewItem.cs b/StarterNG/Controls/ExpandableTreeViewItem.cs index 4964be0..50603d5 100644 --- a/StarterNG/Controls/ExpandableTreeViewItem.cs +++ b/StarterNG/Controls/ExpandableTreeViewItem.cs @@ -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; /// -/// 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. /// public class ExpandableTreeViewItem : TreeViewItem { @@ -25,6 +28,13 @@ 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; + 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? 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;