diff --git a/StarterNG.Tests/FieldLabelTests.cs b/StarterNG.Tests/FieldLabelTests.cs new file mode 100644 index 0000000..809a2e2 --- /dev/null +++ b/StarterNG.Tests/FieldLabelTests.cs @@ -0,0 +1,97 @@ +using Avalonia.Automation; +using Avalonia.Controls; +using StarterNG.Controls; + +namespace StarterNG.Tests; + +public class FieldLabelTests +{ + static FieldLabelTests() => ScreenReaderNames.Install(); + + private static T At(T control, int row, int column) where T : Control + { + Grid.SetRow(control, row); + Grid.SetColumn(control, column); + return control; + } + + [Fact] + public void A_field_is_named_by_the_nearest_label_left_of_it_in_its_row() + { + var far = At(new TextBlock { Text = "Pogoda" }, 0, 0); + var near = At(new TextBlock { Text = "Temperatura" }, 0, 1); + var value = At(new TextBlock { Text = "-5 °C" }, 0, 3); + var other = At(new TextBlock { Text = "Widoczność" }, 1, 1); + var slider = At(new Slider(), 0, 2); + _ = new Grid { Children = { far, near, value, other, slider } }; + + ScreenReaderNames.NameByLabel(slider); + + Assert.Equal("Temperatura", AutomationProperties.GetName(slider)); + } + + [Fact] + public void A_field_with_nothing_to_its_left_is_named_by_the_label_above() + { + var label = At(new TextBlock { Text = "Klasa" }, 0, 1); + var combo = At(new ComboBox(), 1, 1); + _ = new Grid { Children = { label, combo } }; + + Assert.Same(label, ScreenReaderNames.LabelOf(combo)); + } + + [Fact] + public void In_a_panel_the_label_is_the_text_just_before_the_field() + { + var label = new TextBlock { Text = "Profil" }; + var box = new TextBox(); + var after = new TextBox(); + _ = new StackPanel { Children = { label, box, new Button(), after } }; + + ScreenReaderNames.NameByLabel(box); + + Assert.Equal("Profil", AutomationProperties.GetName(box)); + Assert.Null(ScreenReaderNames.LabelOf(after)); + } + + [Fact] + public void A_hidden_text_or_a_long_description_is_no_label() + { + var hidden = new TextBlock { Text = "Ukryte", IsVisible = false }; + var first = new ComboBox(); + var description = new TextBlock { Text = new string('x', 61) }; + var second = new ComboBox(); + _ = new StackPanel { Children = { hidden, first, description, second } }; + + Assert.Null(ScreenReaderNames.LabelOf(first)); + Assert.Null(ScreenReaderNames.LabelOf(second)); + Assert.Null(ScreenReaderNames.LabelOf(new ComboBox())); + } + + [Fact] + public void The_name_follows_the_label_into_another_language() + { + var label = new TextBlock { Text = "Data" }; + var picker = new DatePicker(); + _ = new DockPanel { Children = { label, picker } }; + + ScreenReaderNames.NameByLabel(picker); + ScreenReaderNames.NameByLabel(picker); + label.Text = "Date"; + + Assert.Equal("Date", AutomationProperties.GetName(picker)); + } + + [Fact] + public void A_name_given_on_purpose_beats_the_label() + { + var label = new TextBlock { Text = "Szukaj" }; + var box = new TextBox(); + AutomationProperties.SetName(box, "Szukaj pojazdów"); + _ = new StackPanel { Children = { label, box } }; + + ScreenReaderNames.NameByLabel(box); + + Assert.Equal("Szukaj pojazdów", AutomationProperties.GetName(box)); + } +} diff --git a/StarterNG/Controls/ScreenReaderNames.cs b/StarterNG/Controls/ScreenReaderNames.cs index 344a944..c77bb76 100644 --- a/StarterNG/Controls/ScreenReaderNames.cs +++ b/StarterNG/Controls/ScreenReaderNames.cs @@ -37,6 +37,13 @@ public static class ScreenReaderNames ComboBox.PlaceholderTextProperty.Changed.AddClassHandler((c, _) => Give(c, c.PlaceholderText)); TextBox.PlaceholderTextProperty.Changed.AddClassHandler((t, _) => Give(t, t.PlaceholderText)); + Control.LoadedEvent.AddClassHandler((c, _) => NameByLabel(c)); + Control.LoadedEvent.AddClassHandler((c, _) => NameByLabel(c)); + Control.LoadedEvent.AddClassHandler((c, _) => NameByLabel(c)); + Control.LoadedEvent.AddClassHandler((c, _) => NameByLabel(c)); + Control.LoadedEvent.AddClassHandler((c, _) => NameByLabel(c)); + Control.LoadedEvent.AddClassHandler((c, _) => NameByLabel(c)); + TemplatedControl.TemplateAppliedEvent.AddClassHandler((p, e) => KeepPickerButton(p, e)); TemplatedControl.TemplateAppliedEvent.AddClassHandler((p, e) => KeepPickerButton(p, e)); DatePicker.SelectedDateProperty.Changed.AddClassHandler((p, _) => NamePicker(p)); @@ -85,6 +92,55 @@ public static class ScreenReaderNames public static string? TimeText(TimeSpan? time) => time?.ToString(@"hh\:mm", CultureInfo.InvariantCulture); + // The labels already followed, so a label read again is not followed twice. + private static readonly ConditionalWeakTable Followed = new(); + + /// Longer than this, a text beside a field is a description, not its label. + private const int LabelLength = 60; + + /// + /// A field named by the label a sighted reader sees beside it, and named again when + /// the label changes with the language. A name given otherwise is left alone. + /// + public static void NameByLabel(Control input) + { + if (LabelOf(input) is not { } label) + return; + + Give(input, TextOf(label)); + if (Followed.TryAdd(label, input)) + label.PropertyChanged += (_, e) => + { + if (e.Property == TextBlock.TextProperty) + Give(input, TextOf(label)); + }; + } + + /// + /// The text that labels a field: to its left in the same grid row, above it in the + /// same grid column, or just before it in any other panel. Null when there is none. + /// + public static TextBlock? LabelOf(Control input) + { + if (input.Parent is not Panel panel) + return null; + + var labels = panel.Children.OfType().Where(IsLabel).ToList(); + if (panel is Grid) + { + int row = Grid.GetRow(input), column = Grid.GetColumn(input); + return labels.Where(t => Grid.GetRow(t) == row && Grid.GetColumn(t) < column) + .OrderByDescending(Grid.GetColumn).FirstOrDefault() + ?? labels.FirstOrDefault(t => Grid.GetColumn(t) == column && Grid.GetRow(t) == row - 1); + } + + int at = panel.Children.IndexOf(input); + return at > 0 && panel.Children[at - 1] is TextBlock before && labels.Contains(before) ? before : null; + } + + private static bool IsLabel(TextBlock block) => + block.IsVisible && TextOf(block) is { Length: <= LabelLength }; + /// /// A panel with a title, such as Scenerie or Skład, made a named group: the reader /// says where focus went in, where before the panel was not there for it at all.