From 184629b5440c17fabfba0367a96f73a315dd248e Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 15 Nov 2026 19:39:09 +0100 Subject: [PATCH] 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. --- .../ExpandableTreeViewItemTests.cs | 10 +++ StarterNG.Tests/MouseReadingTests.cs | 89 +++++++++++++++++++ StarterNG/Controls/ExpandableTreeViewItem.cs | 89 +++++++++++++++++++ StarterNG/Controls/ScreenReaderNames.cs | 53 +++++++++++ StarterNG/Infrastructure/StatsBar.cs | 55 ++++++------ StarterNG/Views/Scenarios.axaml.cs | 3 + StarterNG/startercfg/lang/cz.xml | 3 + StarterNG/startercfg/lang/en.xml | 3 + StarterNG/startercfg/lang/hu.xml | 3 + StarterNG/startercfg/lang/pl.xml | 3 + StarterNG/startercfg/lang/ru.xml | 3 + 11 files changed, 289 insertions(+), 25 deletions(-) create mode 100644 StarterNG.Tests/MouseReadingTests.cs diff --git a/StarterNG.Tests/ExpandableTreeViewItemTests.cs b/StarterNG.Tests/ExpandableTreeViewItemTests.cs index e36f7a3..41155a7 100644 --- a/StarterNG.Tests/ExpandableTreeViewItemTests.cs +++ b/StarterNG.Tests/ExpandableTreeViewItemTests.cs @@ -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)); + } } diff --git a/StarterNG.Tests/MouseReadingTests.cs b/StarterNG.Tests/MouseReadingTests.cs new file mode 100644 index 0000000..465e621 --- /dev/null +++ b/StarterNG.Tests/MouseReadingTests.cs @@ -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(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); + } +} diff --git a/StarterNG/Controls/ExpandableTreeViewItem.cs b/StarterNG/Controls/ExpandableTreeViewItem.cs index b50ba36..33ddca6 100644 --- a/StarterNG/Controls/ExpandableTreeViewItem.cs +++ b/StarterNG/Controls/ExpandableTreeViewItem.cs @@ -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. /// public class ExpandableTreeViewItem : TreeViewItem { @@ -18,6 +27,86 @@ public class ExpandableTreeViewItem : TreeViewItem protected override AutomationPeer OnCreateAutomationPeer() => new Peer(this); + /// The words for an open and a closed entry, in the starter's language. + public static Func 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 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("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); + + /// + /// 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. + /// + private void TellRow() + { + if (_row is null) + return; + + string? state = StateWords(ExpandState); + string? name = AutomationProperties.GetName(this); + foreach (var part in _row.GetVisualDescendants().OfType().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; + } + /// What a screen reader is told: a leaf, or open or closed. public ExpandCollapseState ExpandState => StateFor(ItemCount > 0, IsExpanded); diff --git a/StarterNG/Controls/ScreenReaderNames.cs b/StarterNG/Controls/ScreenReaderNames.cs index c49294b..d05cbd4 100644 --- a/StarterNG/Controls/ScreenReaderNames.cs +++ b/StarterNG/Controls/ScreenReaderNames.cs @@ -36,6 +36,7 @@ public static class ScreenReaderNames ComboBox.PlaceholderTextProperty.Changed.AddClassHandler((c, _) => Give(c, c.PlaceholderText)); TextBox.PlaceholderTextProperty.Changed.AddClassHandler((t, _) => Give(t, t.PlaceholderText)); + SelectingItemsControl.SelectionChangedEvent.AddClassHandler((c, _) => TellChoice(c)); Control.LoadedEvent.AddClassHandler((c, _) => NameByLabel(c)); Control.LoadedEvent.AddClassHandler((c, _) => NameByLabel(c)); @@ -43,6 +44,8 @@ public static class ScreenReaderNames Control.LoadedEvent.AddClassHandler((c, _) => NameByLabel(c)); Control.LoadedEvent.AddClassHandler((c, _) => NameByLabel(c)); Control.LoadedEvent.AddClassHandler((c, _) => NameByLabel(c)); + Control.LoadedEvent.AddClassHandler((i, _) => NamePicture(i)); + Control.LoadedEvent.AddClassHandler((i, _) => NamePicture(i)); TemplatedControl.TemplateAppliedEvent.AddClassHandler((p, e) => KeepPickerButton(p, e)); TemplatedControl.TemplateAppliedEvent.AddClassHandler((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; + + /// + /// 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. + /// + 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; + } + } + } + + /// + /// 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. + /// + public static void TellChoice(ComboBox combo) + { + if (combo.IsKeyboardFocusWithin && !combo.IsDropDownOpen && !combo.IsEditable) + Announcer.Say(combo, SpokenItem(combo.SelectedItem)); + } + + /// What a reader is told a list entry is, as Avalonia tells it. + 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) diff --git a/StarterNG/Infrastructure/StatsBar.cs b/StarterNG/Infrastructure/StatsBar.cs index cbc7048..083b0e3 100644 --- a/StarterNG/Infrastructure/StatsBar.cs +++ b/StarterNG/Infrastructure/StatsBar.cs @@ -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)); } } + + /// + /// 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. + /// + 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 }, + }, + }; + } } diff --git a/StarterNG/Views/Scenarios.axaml.cs b/StarterNG/Views/Scenarios.axaml.cs index fc0afba..236aa85 100644 --- a/StarterNG/Views/Scenarios.axaml.cs +++ b/StarterNG/Views/Scenarios.axaml.cs @@ -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 diff --git a/StarterNG/startercfg/lang/cz.xml b/StarterNG/startercfg/lang/cz.xml index 9e5f199..4b96d3f 100644 --- a/StarterNG/startercfg/lang/cz.xml +++ b/StarterNG/startercfg/lang/cz.xml @@ -565,6 +565,9 @@ Nastavení uloženo Výchozí hodnoty obnoveny Uložené nastavení obnoveno + rozbaleno + sbaleno + Obrázek scenérie Nastavení se nepodařilo uložit Neuložené změny diff --git a/StarterNG/startercfg/lang/en.xml b/StarterNG/startercfg/lang/en.xml index dcc3d58..4bae708 100644 --- a/StarterNG/startercfg/lang/en.xml +++ b/StarterNG/startercfg/lang/en.xml @@ -591,6 +591,9 @@ Settings saved Defaults restored Saved settings restored + expanded + collapsed + Scenery picture Could not save the settings Unsaved changes diff --git a/StarterNG/startercfg/lang/hu.xml b/StarterNG/startercfg/lang/hu.xml index 6423a79..f2cb9cb 100644 --- a/StarterNG/startercfg/lang/hu.xml +++ b/StarterNG/startercfg/lang/hu.xml @@ -565,6 +565,9 @@ A beállítások elmentve Alapértékek visszaállítva A mentett beállítások visszaállítva + kibontva + összecsukva + A pálya képe A beállításokat nem sikerült elmenteni Nem mentett módosítások diff --git a/StarterNG/startercfg/lang/pl.xml b/StarterNG/startercfg/lang/pl.xml index 0cf01f4..e291c6e 100644 --- a/StarterNG/startercfg/lang/pl.xml +++ b/StarterNG/startercfg/lang/pl.xml @@ -591,6 +591,9 @@ Zapisano ustawienia Przywrócono domyślne Przywrócono zapisane ustawienia + rozwinięte + zwinięte + Obraz scenerii Nie udało się zapisać ustawień Niezapisane zmiany diff --git a/StarterNG/startercfg/lang/ru.xml b/StarterNG/startercfg/lang/ru.xml index 15e5f2e..ca7a0a7 100644 --- a/StarterNG/startercfg/lang/ru.xml +++ b/StarterNG/startercfg/lang/ru.xml @@ -565,6 +565,9 @@ Настройки сохранены Восстановлены значения по умолчанию Восстановлены сохранённые настройки + развёрнуто + свёрнуто + Изображение сценерии Не удалось сохранить настройки Несохранённые изменения