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;