diff --git a/StarterNG.Tests/ReadableSliderTests.cs b/StarterNG.Tests/ReadableSliderTests.cs new file mode 100644 index 0000000..1c84b2c --- /dev/null +++ b/StarterNG.Tests/ReadableSliderTests.cs @@ -0,0 +1,64 @@ +using System.Globalization; +using Avalonia.Automation.Peers; +using Avalonia.Automation.Provider; +using Avalonia.Controls; +using StarterNG.Controls; + +namespace StarterNG.Tests; + +public class ReadableSliderTests +{ + [Fact] + public void A_slider_is_read_as_the_value_shown_beside_it() + { + var fog = new ReadableSlider { Minimum = 0, Maximum = 1000, Value = 523, ValueText = " 2,4 km " }; + + Assert.Equal("2,4 km", fog.SpokenValue); + } + + [Fact] + public void Without_a_shown_value_the_number_itself_is_read() + { + var before = CultureInfo.CurrentCulture; + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("pl-PL"); + try + { + Assert.Equal("-5", ReadableSlider.SpokenText(null, -5)); + Assert.Equal("1,25", ReadableSlider.SpokenText(" ", 1.25)); + } + finally + { + CultureInfo.CurrentCulture = before; + } + } + + [Fact] + public void A_setting_row_gives_its_value_to_the_slider_in_it() + { + var slider = new ReadableSlider { Minimum = 0, Maximum = 100, Value = 40 }; + var row = new SettingRow { Content = new Border { Child = slider } }; + + row.ValueText = "40%"; + Assert.Equal("40%", slider.SpokenValue); + + row.ValueText = null; + Assert.Equal("40", slider.SpokenValue); + } + + [Fact] + public void A_screen_reader_is_given_the_text_and_may_set_a_number() + { + var slider = new ReadableSlider { Minimum = 0, Maximum = 1000, Value = 523, ValueText = "2 km" }; + var peer = ControlAutomationPeer.CreatePeerForElement(slider); + var value = Assert.IsAssignableFrom(peer); + + Assert.Equal(AutomationControlType.Slider, peer.GetAutomationControlType()); + Assert.Equal("Slider", peer.GetClassName()); + Assert.Equal("2 km", value.Value); + Assert.False(value.IsReadOnly); + + value.SetValue("300"); + Assert.Equal(300, slider.Value); + Assert.Throws(() => value.SetValue("dużo")); + } +} diff --git a/StarterNG/Controls/ReadableSlider.cs b/StarterNG/Controls/ReadableSlider.cs new file mode 100644 index 0000000..5728b29 --- /dev/null +++ b/StarterNG/Controls/ReadableSlider.cs @@ -0,0 +1,62 @@ +using System; +using System.Globalization; +using Avalonia; +using Avalonia.Automation.Peers; +using Avalonia.Automation.Provider; +using Avalonia.Controls; + +namespace StarterNG.Controls; + +/// +/// A slider that tells a screen reader the value it shows beside it. Avalonia's own +/// gives only the bare number, which NVDA reads as a share of the slider's length: +/// -5 °C was read as 45, a fog of 2 km as wherever the knob stood on its scale. +/// +public class ReadableSlider : Slider +{ + public static readonly StyledProperty ValueTextProperty = + AvaloniaProperty.Register(nameof(ValueText)); + + /// The value as written for the reader, such as "2,4 km"; the number when unset. + public string? ValueText + { + get => GetValue(ValueTextProperty); + set => SetValue(ValueTextProperty, value); + } + + protected override Type StyleKeyOverride => typeof(Slider); + + protected override AutomationPeer OnCreateAutomationPeer() => new Peer(this); + + /// What a screen reader is told the slider holds. + public string SpokenValue => SpokenText(ValueText, Value); + + public static string SpokenText(string? valueText, double value) => + string.IsNullOrWhiteSpace(valueText) + ? value.ToString("0.##", CultureInfo.CurrentCulture) + : valueText.Trim(); + + private sealed class Peer : RangeBaseAutomationPeer, IValueProvider + { + private readonly ReadableSlider _slider; + + // A moved knob is told by the range peer already; the reader then asks for this + // text, set by then. Telling the text's change too would have it spoken twice. + public Peer(ReadableSlider slider) : base(slider) => _slider = slider; + + protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.Slider; + + protected override string GetClassNameCore() => "Slider"; + + string? IValueProvider.Value => _slider.SpokenValue; + + bool IValueProvider.IsReadOnly => !_slider.IsEnabled; + + void IValueProvider.SetValue(string? value) + { + if (!double.TryParse(value, NumberStyles.Float, CultureInfo.CurrentCulture, out double number)) + throw new ArgumentException($"Not a number: {value}", nameof(value)); + _slider.Value = number; + } + } +} diff --git a/StarterNG/Controls/ScreenReaderNames.cs b/StarterNG/Controls/ScreenReaderNames.cs index 9df379b..1d66994 100644 --- a/StarterNG/Controls/ScreenReaderNames.cs +++ b/StarterNG/Controls/ScreenReaderNames.cs @@ -95,6 +95,16 @@ public static class ScreenReaderNames Give(input, label); } + /// + /// The value a setting row shows, given to the slider in it, so the reader hears + /// "100%" or "Wyłączone" and not where the knob stands. + /// + public static void TellValue(object? content, string? valueText) + { + foreach (var slider in Inputs(content).OfType()) + slider.ValueText = valueText; + } + private static IEnumerable Inputs(object? content) => content switch { ComboBox or Slider or TextBox or NumericUpDown => new[] { (Control)content }, diff --git a/StarterNG/Controls/SettingRow.cs b/StarterNG/Controls/SettingRow.cs index 9ed9a12..5db77fe 100644 --- a/StarterNG/Controls/SettingRow.cs +++ b/StarterNG/Controls/SettingRow.cs @@ -77,6 +77,9 @@ public class SettingRow : ContentControl if (change.Property == LabelProperty || change.Property == ContentProperty) ScreenReaderNames.NameInputs(Content, Label); + + if (change.Property == ValueTextProperty || change.Property == ContentProperty) + ScreenReaderNames.TellValue(Content, ValueText); } // Again once in the window: a panel given as content may be filled after it was set. @@ -84,6 +87,7 @@ public class SettingRow : ContentControl { base.OnLoaded(e); ScreenReaderNames.NameInputs(Content, Label); + ScreenReaderNames.TellValue(Content, ValueText); } private void UpdatePseudoClasses() diff --git a/StarterNG/Views/Scenarios.axaml b/StarterNG/Views/Scenarios.axaml index 7f229ae..9cd52ab 100644 --- a/StarterNG/Views/Scenarios.axaml +++ b/StarterNG/Views/Scenarios.axaml @@ -4,6 +4,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:starterNg="clr-namespace:StarterNG" xmlns:infra="clr-namespace:StarterNG.Infrastructure" + xmlns:ctl="clr-namespace:StarterNG.Controls" xmlns:materialIcons="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="StarterNG.Views.Scenarios" @@ -147,7 +148,7 @@ - @@ -164,7 +165,7 @@ - diff --git a/StarterNG/Views/Scenarios.axaml.cs b/StarterNG/Views/Scenarios.axaml.cs index 0519ecd..9514941 100644 --- a/StarterNG/Views/Scenarios.axaml.cs +++ b/StarterNG/Views/Scenarios.axaml.cs @@ -762,6 +762,8 @@ public partial class Scenarios : UserControl { weatherTempLabel.Text = $"{(int)weatherTemp.Value} °C"; weatherFogLabel.Text = FogScale2(FogScale.ToMetres(weatherFog.Value)); + weatherTemp.ValueText = weatherTempLabel.Text; + weatherFog.ValueText = weatherFogLabel.Text; weatherDayDate.Text = string.Format(LanguageCulture, App.Loc["DayOfYear"], SelectedDayOfYear()); weatherRestore.IsEnabled = _currentScenery?.Weather.Changed ?? false; diff --git a/StarterNG/Views/Settings.axaml b/StarterNG/Views/Settings.axaml index 1e32662..d3a8431 100644 --- a/StarterNG/Views/Settings.axaml +++ b/StarterNG/Views/Settings.axaml @@ -142,7 +142,7 @@ - @@ -259,13 +259,13 @@ Label="{Binding [BufforResolution], Source={x:Static starterNg:App.Loc}}" Description="{Binding [BufforResolutionDesc], Source={x:Static starterNg:App.Loc}}" ValueText="{Binding #bufferScale.Value, StringFormat='{}{0:0}%'}"> - - - + @@ -313,14 +313,14 @@ Label="{Binding [RenderRange], Source={x:Static starterNg:App.Loc}}" Description="{Binding [RenderRangeDesc], Source={x:Static starterNg:App.Loc}}"> - - @@ -331,13 +331,13 @@ - - @@ -345,7 +345,7 @@ Label="{Binding [TexFilteringQuality], Source={x:Static starterNg:App.Loc}}" Description="{Binding [TexFilteringQualityDesc], Source={x:Static starterNg:App.Loc}}"> - @@ -398,7 +398,7 @@ - @@ -406,14 +406,14 @@ Label="{Binding [ShaderRange], Source={x:Static starterNg:App.Loc}}" Description="{Binding [ShaderRangeDesc], Source={x:Static starterNg:App.Loc}}"> - - @@ -421,7 +421,7 @@ Label="{Binding [ShadowAngleLimit], Source={x:Static starterNg:App.Loc}}" Description="{Binding [ShadowAngleLimitDesc], Source={x:Static starterNg:App.Loc}}"> - @@ -439,7 +439,7 @@ - @@ -463,7 +463,7 @@ Label="{Binding [ReflectionsFramerate], Source={x:Static starterNg:App.Loc}}" Description="{Binding [ReflectionsFramerateDesc], Source={x:Static starterNg:App.Loc}}"> - @@ -492,7 +492,7 @@ - @@ -512,27 +512,27 @@ - - - - - @@ -575,17 +575,17 @@ - - - @@ -700,7 +700,7 @@ - @@ -775,7 +775,7 @@ - + @@ -852,7 +852,7 @@ Label="{Binding [CabProbeBounce], Source={x:Static starterNg:App.Loc}}" Description="{Binding [CabProbeBounceDesc], Source={x:Static starterNg:App.Loc}}" ValueText="{Binding #CabProbeBounceSlider.Value, StringFormat='{}{0:0.0}'}"> - + @@ -893,12 +893,12 @@ - + - + diff --git a/StarterNG/Views/SimulatorKeyRows.cs b/StarterNG/Views/SimulatorKeyRows.cs index a59fea5..f90773d 100644 --- a/StarterNG/Views/SimulatorKeyRows.cs +++ b/StarterNG/Views/SimulatorKeyRows.cs @@ -70,7 +70,7 @@ internal sealed class SimulatorKeyRows Control input = key.Kind switch { SimulatorKeyKind.Toggle => new CheckBox(), - SimulatorKeyKind.Number => new Slider + SimulatorKeyKind.Number => new ReadableSlider { Minimum = key.Minimum, Maximum = key.Maximum,