mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-18 14:35:59 +01:00
fix: a field without a name is read by the label beside it
A list, box, slider or picker with no name of its own takes the text a sighted reader sees next to it: to its left in a grid row, above it in a grid column, or just before it in a panel. The name follows the label into another language.
This commit is contained in:
97
StarterNG.Tests/FieldLabelTests.cs
Normal file
97
StarterNG.Tests/FieldLabelTests.cs
Normal file
@@ -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>(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));
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,13 @@ public static class ScreenReaderNames
|
||||
ComboBox.PlaceholderTextProperty.Changed.AddClassHandler<ComboBox>((c, _) => Give(c, c.PlaceholderText));
|
||||
TextBox.PlaceholderTextProperty.Changed.AddClassHandler<TextBox>((t, _) => Give(t, t.PlaceholderText));
|
||||
|
||||
Control.LoadedEvent.AddClassHandler<ComboBox>((c, _) => NameByLabel(c));
|
||||
Control.LoadedEvent.AddClassHandler<TextBox>((c, _) => NameByLabel(c));
|
||||
Control.LoadedEvent.AddClassHandler<Slider>((c, _) => NameByLabel(c));
|
||||
Control.LoadedEvent.AddClassHandler<NumericUpDown>((c, _) => NameByLabel(c));
|
||||
Control.LoadedEvent.AddClassHandler<DatePicker>((c, _) => NameByLabel(c));
|
||||
Control.LoadedEvent.AddClassHandler<TimePicker>((c, _) => NameByLabel(c));
|
||||
|
||||
TemplatedControl.TemplateAppliedEvent.AddClassHandler<DatePicker>((p, e) => KeepPickerButton(p, e));
|
||||
TemplatedControl.TemplateAppliedEvent.AddClassHandler<TimePicker>((p, e) => KeepPickerButton(p, e));
|
||||
DatePicker.SelectedDateProperty.Changed.AddClassHandler<DatePicker>((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<TextBlock, Control> Followed = new();
|
||||
|
||||
/// <summary>Longer than this, a text beside a field is a description, not its label.</summary>
|
||||
private const int LabelLength = 60;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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));
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public static TextBlock? LabelOf(Control input)
|
||||
{
|
||||
if (input.Parent is not Panel panel)
|
||||
return null;
|
||||
|
||||
var labels = panel.Children.OfType<TextBlock>().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 };
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
|
||||
Reference in New Issue
Block a user