From a83bb4a7ebcdf4d95162641b5e9ec52afad68552 Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 15 Nov 2026 17:40:51 +0100 Subject: [PATCH] fix: a screen reader names every control and Tab survives adding a vehicle NVDA read icon buttons as "Material.Icons.Avalonia.MaterialIcon" and the sceneries in the tree as nothing. Icon buttons are now named by their tooltip, list and tree entries by the text they show, fields by the hint in them, and the inputs of a setting row by its label. The lists, the tree and the language and profile pickers have names, and a consist card says its place and vehicles. Adding a vehicle rebuilt the browser list and switched the add button off for a moment, and focus fell out of the window: Tab did nothing until something was clicked. Focus goes back to the button or the list, and Tab with nothing focused starts from the first control. The splitters are no longer Tab stops. --- StarterNG.Tests/KeyboardFocusTests.cs | 39 ++++++ StarterNG.Tests/ScreenReaderNamesTests.cs | 151 ++++++++++++++++++++++ StarterNG/App.axaml.cs | 4 + StarterNG/Controls/KeyboardFocus.cs | 24 ++++ StarterNG/Controls/ScreenReaderNames.cs | 103 +++++++++++++++ StarterNG/Controls/SettingRow.cs | 11 ++ StarterNG/MainWindow.axaml | 1 + StarterNG/MainWindow.axaml.cs | 13 ++ StarterNG/Views/Depot.axaml | 5 +- StarterNG/Views/Depot/VehicleBrowser.cs | 23 +++- StarterNG/Views/Depot/VehicleCards.cs | 10 ++ StarterNG/Views/Scenarios.axaml | 6 +- StarterNG/Views/Settings.axaml | 3 +- StarterNG/Views/VehicleBaseWindow.axaml | 4 +- 14 files changed, 388 insertions(+), 9 deletions(-) create mode 100644 StarterNG.Tests/KeyboardFocusTests.cs create mode 100644 StarterNG.Tests/ScreenReaderNamesTests.cs create mode 100644 StarterNG/Controls/KeyboardFocus.cs create mode 100644 StarterNG/Controls/ScreenReaderNames.cs diff --git a/StarterNG.Tests/KeyboardFocusTests.cs b/StarterNG.Tests/KeyboardFocusTests.cs new file mode 100644 index 0000000..f531627 --- /dev/null +++ b/StarterNG.Tests/KeyboardFocusTests.cs @@ -0,0 +1,39 @@ +using Avalonia.Controls; +using Avalonia.Input; +using StarterNG.Controls; + +namespace StarterNG.Tests; + +public class KeyboardFocusTests +{ + private readonly Button _window = new(); + private readonly Button _first = new(); + private readonly Button _last = new(); + + private IInputElement? Tab(Key key, KeyModifiers modifiers, IInputElement? focused) => + KeyboardFocus.TabFromNowhere(key, modifiers, focused, _window, () => _first, () => _last); + + [Fact] + public void Tab_with_nothing_focused_starts_from_the_first_control() + { + Assert.Same(_first, Tab(Key.Tab, KeyModifiers.None, null)); + } + + [Fact] + public void Shift_tab_with_only_the_window_focused_starts_from_the_last_control() + { + Assert.Same(_last, Tab(Key.Tab, KeyModifiers.Shift, _window)); + } + + [Fact] + public void Tab_from_a_focused_control_is_left_to_the_usual_order() + { + Assert.Null(Tab(Key.Tab, KeyModifiers.None, new TextBox())); + } + + [Fact] + public void Other_keys_are_left_alone() + { + Assert.Null(Tab(Key.Enter, KeyModifiers.None, null)); + } +} diff --git a/StarterNG.Tests/ScreenReaderNamesTests.cs b/StarterNG.Tests/ScreenReaderNamesTests.cs new file mode 100644 index 0000000..8e45293 --- /dev/null +++ b/StarterNG.Tests/ScreenReaderNamesTests.cs @@ -0,0 +1,151 @@ +using Avalonia.Automation; +using Avalonia.Controls; +using Avalonia.Controls.Documents; +using StarterNG.Controls; + +namespace StarterNG.Tests; + +public class ScreenReaderNamesTests +{ + static ScreenReaderNamesTests() => ScreenReaderNames.Install(); + + private static StackPanel Row(params Control[] children) + { + var panel = new StackPanel(); + panel.Children.AddRange(children); + return panel; + } + + [Fact] + public void An_icon_button_is_named_by_its_tooltip() + { + var button = new Button { Content = new Image() }; + ToolTip.SetTip(button, "Otwórz ostatni log symulatora"); + + Assert.Equal("Otwórz ostatni log symulatora", AutomationProperties.GetName(button)); + } + + [Fact] + public void A_button_without_a_tooltip_is_named_by_the_text_it_shows() + { + var button = new Button { Content = Row(new Image(), new TextBlock { Text = "Dodaj" }) }; + + Assert.Equal("Dodaj", AutomationProperties.GetName(button)); + } + + [Fact] + public void A_button_with_text_of_its_own_keeps_it() + { + var button = new Button { Content = "Zapisz" }; + ToolTip.SetTip(button, "Zapisz ustawienia"); + + Assert.Null(AutomationProperties.GetName(button)); + } + + [Fact] + public void A_name_given_on_purpose_is_left_alone() + { + var button = new Button(); + AutomationProperties.SetName(button, "Discord"); + button.Content = new Image(); + ToolTip.SetTip(button, "Dołącz do nas"); + + Assert.Equal("Discord", AutomationProperties.GetName(button)); + } + + [Fact] + public void A_new_tooltip_replaces_the_name_it_gave() + { + var button = new Button { Content = new Image() }; + ToolTip.SetTip(button, "Undo"); + ToolTip.SetTip(button, "Cofnij"); + + Assert.Equal("Cofnij", AutomationProperties.GetName(button)); + } + + [Fact] + public void Text_put_on_a_button_later_takes_back_the_name_it_was_given() + { + var button = new Button { Content = new Image() }; + ToolTip.SetTip(button, "Pomoc"); + button.Content = "?"; + + Assert.Null(AutomationProperties.GetName(button)); + } + + [Fact] + public void A_tree_entry_is_named_by_the_visible_text_of_its_header() + { + var hidden = new TextBlock { Text = "ukryte", IsVisible = false }; + var item = new TreeViewItem + { + Header = Row(new Image(), new TextBlock { Text = "baltyk_cargo" }, hidden, new TextBlock { Text = " " }) + }; + + Assert.Equal("baltyk_cargo", AutomationProperties.GetName(item)); + } + + [Fact] + public void A_list_entry_joins_the_texts_it_shows() + { + var item = new ListBoxItem + { + Content = new Border { Child = Row(new TextBlock { Text = "PKPIC" }, new TextBlock { Text = "303E-EP-426" }) } + }; + + Assert.Equal("PKPIC, 303E-EP-426", AutomationProperties.GetName(item)); + } + + [Fact] + public void Text_made_of_runs_is_read_whole() + { + var block = new TextBlock(); + block.Inlines!.Add(new Run("EP07")); + block.Inlines.Add(new LineBreak()); + block.Inlines.Add(new Span { Inlines = { new Run("544") } }); + + Assert.Equal("EP07 544", ScreenReaderNames.TextOf(block)); + } + + [Fact] + public void Nothing_to_read_gives_no_name() + { + Assert.Null(ScreenReaderNames.TextOf(null)); + Assert.Null(ScreenReaderNames.TextOf(new Image())); + Assert.Null(ScreenReaderNames.TextOf(" ")); + Assert.Equal("Opis", ScreenReaderNames.TextOf(new ContentControl { Content = " Opis " })); + } + + [Fact] + public void A_field_is_named_by_the_hint_written_in_it() + { + var search = new TextBox { PlaceholderText = "Szukaj…" }; + var category = new ComboBox { PlaceholderText = "Kategoria" }; + + Assert.Equal("Szukaj…", AutomationProperties.GetName(search)); + Assert.Equal("Kategoria", AutomationProperties.GetName(category)); + } + + [Fact] + public void A_setting_row_names_its_inputs_by_its_label() + { + var combo = new ComboBox(); + var slider = new Slider(); + var browse = new Button { Content = "Wybierz plik…" }; + var row = new SettingRow { Content = new Border { Child = Row(combo, slider, browse) } }; + row.Label = "Wybór exe"; + + Assert.Equal("Wybór exe", AutomationProperties.GetName(combo)); + Assert.Equal("Wybór exe", AutomationProperties.GetName(slider)); + Assert.Null(AutomationProperties.GetName(browse)); + } + + [Fact] + public void A_setting_row_without_a_label_leaves_its_input_unnamed() + { + var box = new TextBox(); + _ = new SettingRow { Content = box }; + + Assert.Null(AutomationProperties.GetName(box)); + } +} diff --git a/StarterNG/App.axaml.cs b/StarterNG/App.axaml.cs index 76778b8..7e23076 100644 --- a/StarterNG/App.axaml.cs +++ b/StarterNG/App.axaml.cs @@ -7,6 +7,7 @@ using Avalonia.Markup.Xaml; using Avalonia.Threading; using StarterNG.Application; using StarterNG.Classes; +using StarterNG.Controls; using StarterNG.Infrastructure; using StarterNG.Services; @@ -24,6 +25,9 @@ public partial class App : Avalonia.Application public override void Initialize() { + // Before any control exists: the names are given as controls are filled in. + ScreenReaderNames.Install(); + AvaloniaXamlLoader.Load(this); AppServices.Current.SettingsStore.Load(); diff --git a/StarterNG/Controls/KeyboardFocus.cs b/StarterNG/Controls/KeyboardFocus.cs new file mode 100644 index 0000000..ae68585 --- /dev/null +++ b/StarterNG/Controls/KeyboardFocus.cs @@ -0,0 +1,24 @@ +using System; +using Avalonia.Input; + +namespace StarterNG.Controls; + +public static class KeyboardFocus +{ + /// + /// Focus that fell out with a control taken away or switched off leaves the window + /// itself focused, and Tab then went nowhere until something was clicked - for + /// someone without a mouse, nowhere at all. Tab starts again from the first + /// control, Shift+Tab from the last. Null when focus is somewhere and Tab moves it + /// as usual. + /// + public static IInputElement? TabFromNowhere(Key key, KeyModifiers modifiers, IInputElement? focused, + IInputElement window, Func first, + Func last) + { + if (key != Key.Tab || focused is not null && !ReferenceEquals(focused, window)) + return null; + + return modifiers.HasFlag(KeyModifiers.Shift) ? last() : first(); + } +} diff --git a/StarterNG/Controls/ScreenReaderNames.cs b/StarterNG/Controls/ScreenReaderNames.cs new file mode 100644 index 0000000..1ce9499 --- /dev/null +++ b/StarterNG/Controls/ScreenReaderNames.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using Avalonia; +using Avalonia.Automation; +using Avalonia.Controls; +using Avalonia.Controls.Documents; +using Avalonia.Controls.Primitives; + +namespace StarterNG.Controls; + +/// +/// The names a screen reader says for controls that show no plain text of their own. +/// Without one it said the type of whatever was inside: an icon button was read as +/// "Material.Icons.Avalonia.MaterialIcon", a scenery in the tree as nothing at all. +/// An icon button is named by its tooltip, a list or tree entry by the text it shows, +/// a field by the hint written in it. A name given on purpose is left alone. +/// +public static class ScreenReaderNames +{ + // What was given here last, so a later change replaces it but nothing else. + private static readonly ConditionalWeakTable Given = new(); + + public static void Install() + { + ContentControl.ContentProperty.Changed.AddClassHandler