fix: NVDA hears a tree entry open, a list's new choice and the pictures and figures

Under the mouse a reader hears only what lies there. The row of a tree entry now
carries whether it is open, an arrow or icon in it its name; a picture takes the
name of the button or card it sits in, the scenery picture says what it shows; a
consist figure is one text, label and value together.

Avalonia tells Windows of a closed list's new value only for a list one types
into, so going through the weathers with the arrows was silent; the new choice
is now said aloud.
This commit is contained in:
maj00r
2026-11-15 19:39:09 +01:00
parent 95f1cd2398
commit 184629b544
11 changed files with 289 additions and 25 deletions

View File

@@ -49,4 +49,14 @@ public class ExpandableTreeViewItemTests
Assert.Null(item.ReadableParent);
Assert.Null(peer.GetParent());
}
[Fact]
public void The_row_under_the_mouse_is_told_open_or_closed_and_a_leaf_nothing()
{
static string Loc(string key) => key == "TreeExpanded" ? "rozwinięte" : "zwinięte";
Assert.Equal("rozwinięte", ExpandableTreeViewItem.WordFor(ExpandCollapseState.Expanded, Loc));
Assert.Equal("zwinięte", ExpandableTreeViewItem.WordFor(ExpandCollapseState.Collapsed, Loc));
Assert.Null(ExpandableTreeViewItem.WordFor(ExpandCollapseState.LeafNode, Loc));
}
}

View File

@@ -0,0 +1,89 @@
using Avalonia.Automation;
using Avalonia.Controls;
using StarterNG.Controls;
using StarterNG.Infrastructure;
namespace StarterNG.Tests;
public class MouseReadingTests
{
static MouseReadingTests() => ScreenReaderNames.Install();
[Fact]
public void A_picture_on_a_button_is_read_as_the_button()
{
var logo = new Image();
var button = new Button { Content = logo };
ScreenReaderNames.NamePicture(logo);
Assert.Same(button, AutomationProperties.GetLabeledBy(logo));
}
[Fact]
public void A_picture_in_a_card_is_read_as_the_card_tooltip()
{
var mini = new Image();
var card = new Border { Child = new Panel { Children = { mini } } };
ToolTip.SetTip(card, "EP09-018");
ScreenReaderNames.NamePicture(mini);
Assert.Equal("EP09-018", AutomationProperties.GetName(mini));
}
[Fact]
public void A_picture_named_on_purpose_or_far_from_any_name_is_left_alone()
{
var named = new Image();
AutomationProperties.SetName(named, "Obraz scenerii: baltyk");
var far = new Image();
var outer = new Border { Child = new Border { Child = new Border { Child = new Border { Child = far } } } };
ToolTip.SetTip(outer, "Opis");
_ = new Button { Content = named };
ScreenReaderNames.NamePicture(named);
ScreenReaderNames.NamePicture(far);
Assert.Equal("Obraz scenerii: baltyk", AutomationProperties.GetName(named));
Assert.Null(AutomationProperties.GetLabeledBy(named));
Assert.Null(AutomationProperties.GetName(far));
}
[Fact]
public void A_list_entry_is_read_as_its_text()
{
var named = new ComboBoxItem { Content = "Lato" };
AutomationProperties.SetName(named, "Pora: lato");
Assert.Equal("Pogodnie", ScreenReaderNames.SpokenItem(new ComboBoxItem { Content = "Pogodnie" }));
Assert.Equal("Pora: lato", ScreenReaderNames.SpokenItem(named));
Assert.Equal("12", ScreenReaderNames.SpokenItem(12));
Assert.Null(ScreenReaderNames.SpokenItem(null));
}
[Fact]
public void A_list_not_in_focus_says_nothing_of_its_new_choice()
{
var combo = new ComboBox();
var announcer = new Announcer();
announcer.Child = combo;
ScreenReaderNames.TellChoice(combo);
Assert.Null(announcer.Message);
}
[Fact]
public void A_figure_says_its_label_and_its_value_together()
{
var host = new StackPanel();
StatsBar.Fill(host, new[] { ("Masa [t]:", "440"), ("Tor:", "tor0325") });
var figure = Assert.IsType<TextBlock>(host.Children[0]);
Assert.Equal("Masa [t]: 440", figure.Inlines!.Text);
Assert.Equal(22, figure.Margin.Right);
Assert.Equal(0, ((TextBlock)host.Children[1]).Margin.Right);
}
}

View File

@@ -1,8 +1,15 @@
using System;
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.Threading;
using Avalonia.VisualTree;
using StarterNG.Application;
namespace StarterNG.Controls;
@@ -11,6 +18,8 @@ namespace StarterNG.Controls;
/// 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.
/// Under the mouse a reader hears only what lies there, the name in the row; the row
/// is told the state as well, so the mouse also hears that an entry opens.
/// </summary>
public class ExpandableTreeViewItem : TreeViewItem
{
@@ -18,6 +27,86 @@ public class ExpandableTreeViewItem : TreeViewItem
protected override AutomationPeer OnCreateAutomationPeer() => new Peer(this);
/// <summary>The words for an open and a closed entry, in the starter's language.</summary>
public static Func<ExpandCollapseState, string?> StateWords { get; set; } = DefaultStateWords;
private static string? DefaultStateWords(ExpandCollapseState state) =>
AppServices.Current?.Localization is { } loc
? WordFor(state, key => loc[key])
: null;
public static string? WordFor(ExpandCollapseState state, Func<string, string> loc) => state switch
{
ExpandCollapseState.Expanded => loc("TreeExpanded"),
ExpandCollapseState.Collapsed => loc("TreeCollapsed"),
_ => null,
};
private Control? _row;
private string? _rowName;
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
_row = e.NameScope.Find<Control>("PART_LayoutRoot");
TellRowLater();
}
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
if (AppServices.Current?.Localization is { } loc)
loc.LanguageChanged += TellRowLater;
TellRow();
}
protected override void OnUnloaded(RoutedEventArgs e)
{
base.OnUnloaded(e);
if (AppServices.Current?.Localization is { } loc)
loc.LanguageChanged -= TellRowLater;
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == IsExpandedProperty || change.Property == ItemCountProperty
|| change.Property == AutomationProperties.NameProperty)
TellRow();
else if (change.Property == HeaderProperty)
TellRowLater();
}
// A new header is laid out only after it is set.
private void TellRowLater() => Dispatcher.UIThread.Post(TellRow, DispatcherPriority.Loaded);
/// <summary>
/// Gives whatever the mouse may point at in the row the entry's state, and its name
/// where it shows no text of its own, such as the arrow or an icon.
/// </summary>
private void TellRow()
{
if (_row is null)
return;
string? state = StateWords(ExpandState);
string? name = AutomationProperties.GetName(this);
foreach (var part in _row.GetVisualDescendants().OfType<Control>().Prepend(_row))
{
if (string.IsNullOrEmpty(state))
part.ClearValue(AutomationProperties.HelpTextProperty);
else
AutomationProperties.SetHelpText(part, state);
if (part is TextBlock)
continue;
string? had = AutomationProperties.GetName(part);
if (string.IsNullOrEmpty(had) || had == _rowName)
AutomationProperties.SetName(part, name);
}
_rowName = name;
}
/// <summary>What a screen reader is told: a leaf, or open or closed.</summary>
public ExpandCollapseState ExpandState => StateFor(ItemCount > 0, IsExpanded);

View File

@@ -36,6 +36,7 @@ public static class ScreenReaderNames
ComboBox.PlaceholderTextProperty.Changed.AddClassHandler<ComboBox>((c, _) => Give(c, c.PlaceholderText));
TextBox.PlaceholderTextProperty.Changed.AddClassHandler<TextBox>((t, _) => Give(t, t.PlaceholderText));
SelectingItemsControl.SelectionChangedEvent.AddClassHandler<ComboBox>((c, _) => TellChoice(c));
Control.LoadedEvent.AddClassHandler<ComboBox>((c, _) => NameByLabel(c));
Control.LoadedEvent.AddClassHandler<TextBox>((c, _) => NameByLabel(c));
@@ -43,6 +44,8 @@ public static class ScreenReaderNames
Control.LoadedEvent.AddClassHandler<NumericUpDown>((c, _) => NameByLabel(c));
Control.LoadedEvent.AddClassHandler<DatePicker>((c, _) => NameByLabel(c));
Control.LoadedEvent.AddClassHandler<TimePicker>((c, _) => NameByLabel(c));
Control.LoadedEvent.AddClassHandler<Image>((i, _) => NamePicture(i));
Control.LoadedEvent.AddClassHandler<PathIcon>((i, _) => NamePicture(i));
TemplatedControl.TemplateAppliedEvent.AddClassHandler<DatePicker>((p, e) => KeepPickerButton(p, e));
TemplatedControl.TemplateAppliedEvent.AddClassHandler<TimePicker>((p, e) => KeepPickerButton(p, e));
@@ -158,6 +161,56 @@ public static class ScreenReaderNames
AutomationProperties.SetAccessibilityView(panel, name is null ? AccessibilityView.Default : AccessibilityView.Content);
}
// How far up a picture looks for what it shows: the button or card it sits in,
// not the panel around them.
private const int PictureReach = 3;
/// <summary>
/// Names a picture after the button or card it lies in. Under the mouse a reader
/// hears what lies there, and a bare picture said nothing: a vehicle in the consist,
/// the logo, an icon on a button.
/// </summary>
public static void NamePicture(Control picture)
{
if ((!string.IsNullOrEmpty(AutomationProperties.GetName(picture)) && !Given.TryGetValue(picture, out _))
|| AutomationProperties.GetLabeledBy(picture) is not null)
return;
var holder = picture.Parent as Control;
for (int i = 0; i < PictureReach && holder is not null; i++, holder = holder.Parent as Control)
{
if (!string.IsNullOrEmpty(AutomationProperties.GetName(holder)) || holder is Button)
{
AutomationProperties.SetLabeledBy(picture, holder);
return;
}
if (TextOf(ToolTip.GetTip(holder)) is { } tip)
{
Give(picture, tip);
return;
}
}
}
/// <summary>
/// Says what a closed list now holds when the arrow keys change it. Avalonia tells
/// Windows of a new value only for a list one types into, so NVDA said nothing.
/// </summary>
public static void TellChoice(ComboBox combo)
{
if (combo.IsKeyboardFocusWithin && !combo.IsDropDownOpen && !combo.IsEditable)
Announcer.Say(combo, SpokenItem(combo.SelectedItem));
}
/// <summary>What a reader is told a list entry is, as Avalonia tells it.</summary>
public static string? SpokenItem(object? item) => item switch
{
null => null,
Control c when AutomationProperties.GetName(c) is { Length: > 0 } name => name,
ContentControl c => TextOf(c.Content) ?? c.Content?.ToString(),
_ => TextOf(item) ?? item.ToString(),
};
// 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 text && text.Any(char.IsLetterOrDigit)

View File

@@ -1,6 +1,8 @@
using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Layout;
using Avalonia.Markup.Xaml.MarkupExtensions;
using Avalonia.Media;
namespace StarterNG.Infrastructure;
@@ -13,31 +15,34 @@ public static class StatsBar
for (int i = 0; i < fields.Count; i++)
{
var (label, value) = fields[i];
var pair = new StackPanel
{
Orientation = Orientation.Horizontal,
Spacing = 5,
// no trailing gap on the last pair - these sit in a panel title,
// flush with its right edge
Margin = new Avalonia.Thickness(0, 0, i == fields.Count - 1 ? 0 : 22, 0),
VerticalAlignment = VerticalAlignment.Center
};
pair.Children.Add(new TextBlock
{
Text = label,
FontSize = 12,
Opacity = 0.65,
VerticalAlignment = VerticalAlignment.Center
});
pair.Children.Add(new TextBlock
{
Text = value,
FontSize = 12,
FontWeight = FontWeight.SemiBold,
VerticalAlignment = VerticalAlignment.Center,
TextTrimming = TextTrimming.CharacterEllipsis
});
host.Children.Add(pair);
host.Children.Add(Figure(label, value, last: i == fields.Count - 1));
}
}
/// <summary>
/// One text holding the label and the value: under the mouse a reader hears the
/// text that lies there, and with two it heard "Masa [t]:" without the mass.
/// </summary>
public static TextBlock Figure(string label, string value, bool last)
{
var name = new Run(label);
// The label dimmed, as the panel titles' other labels.
name.Bind(TextElement.ForegroundProperty, new DynamicResourceExtension("CarbonFgDim"));
return new TextBlock
{
FontSize = 12,
// no trailing gap on the last pair - these sit in a panel title,
// flush with its right edge
Margin = new Avalonia.Thickness(0, 0, last ? 0 : 22, 0),
VerticalAlignment = VerticalAlignment.Center,
TextTrimming = TextTrimming.CharacterEllipsis,
Inlines =
{
name,
new Run(" "),
new Run(value) { FontWeight = FontWeight.SemiBold },
},
};
}
}

View File

@@ -8,6 +8,7 @@ using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Automation;
using Avalonia.Animation;
using Avalonia.Controls;
using Avalonia.Input;
@@ -410,6 +411,8 @@ public partial class Scenarios : UserControl
sceneryDescription.Text = scenery.LocalizedDescription(AppServices.Current.SceneryTexts);
string? imagePath = AppServices.Current.SceneryImages.Resolve(scenery);
// Under the mouse a reader hears what the picture is of.
AutomationProperties.SetName(sceneryImage, $"{App.Loc["SceneryImage"]}: {scenery.Name}");
if (imagePath is not null)
{
try

View File

@@ -565,6 +565,9 @@
<String key="SettingsSaved">Nastavení uloženo</String>
<String key="DefaultsRestored">Výchozí hodnoty obnoveny</String>
<String key="SettingsRestored">Uložené nastavení obnoveno</String>
<String key="TreeExpanded">rozbaleno</String>
<String key="TreeCollapsed">sbaleno</String>
<String key="SceneryImage">Obrázek scenérie</String>
<String key="SettingsNotSaved">Nastavení se nepodařilo uložit</String>
<String key="UnsavedChanges">Neuložené změny</String>

View File

@@ -591,6 +591,9 @@
<String key="SettingsSaved">Settings saved</String>
<String key="DefaultsRestored">Defaults restored</String>
<String key="SettingsRestored">Saved settings restored</String>
<String key="TreeExpanded">expanded</String>
<String key="TreeCollapsed">collapsed</String>
<String key="SceneryImage">Scenery picture</String>
<String key="SettingsNotSaved">Could not save the settings</String>
<String key="UnsavedChanges">Unsaved changes</String>

View File

@@ -565,6 +565,9 @@
<String key="SettingsSaved">A beállítások elmentve</String>
<String key="DefaultsRestored">Alapértékek visszaállítva</String>
<String key="SettingsRestored">A mentett beállítások visszaállítva</String>
<String key="TreeExpanded">kibontva</String>
<String key="TreeCollapsed">összecsukva</String>
<String key="SceneryImage">A pálya képe</String>
<String key="SettingsNotSaved">A beállításokat nem sikerült elmenteni</String>
<String key="UnsavedChanges">Nem mentett módosítások</String>

View File

@@ -591,6 +591,9 @@
<String key="SettingsSaved">Zapisano ustawienia</String>
<String key="DefaultsRestored">Przywrócono domyślne</String>
<String key="SettingsRestored">Przywrócono zapisane ustawienia</String>
<String key="TreeExpanded">rozwinięte</String>
<String key="TreeCollapsed">zwinięte</String>
<String key="SceneryImage">Obraz scenerii</String>
<String key="SettingsNotSaved">Nie udało się zapisać ustawień</String>
<String key="UnsavedChanges">Niezapisane zmiany</String>

View File

@@ -565,6 +565,9 @@
<String key="SettingsSaved">Настройки сохранены</String>
<String key="DefaultsRestored">Восстановлены значения по умолчанию</String>
<String key="SettingsRestored">Восстановлены сохранённые настройки</String>
<String key="TreeExpanded">развёрнуто</String>
<String key="TreeCollapsed">свёрнуто</String>
<String key="SceneryImage">Изображение сценерии</String>
<String key="SettingsNotSaved">Не удалось сохранить настройки</String>
<String key="UnsavedChanges">Несохранённые изменения</String>