fix: Sonar's remarks on the screen reader work, and tests for it

The cab panel's drawing and the consist narration are split into smaller
steps, with plain switches and concrete return types where Sonar asked.

Tests drive the black box panels, both tape views, the key bindings, the
windows opened over another, the search count and the themed tree
entries, sliders, number boxes, pickers and settings sections headless,
as someone at the keyboard with a screen reader meets them.
This commit is contained in:
maj00r
2026-11-15 22:06:55 +01:00
parent ff5228555e
commit 3f4a20097e
18 changed files with 1273 additions and 134 deletions

View File

@@ -115,10 +115,12 @@ public class ExpandableTreeViewItem : TreeViewItem
/// <summary>What a screen reader is told: a leaf, or open or closed.</summary>
public ExpandCollapseState ExpandState => StateFor(ItemCount > 0, IsExpanded);
public static ExpandCollapseState StateFor(bool hasChildren, bool expanded) =>
!hasChildren ? ExpandCollapseState.LeafNode
: expanded ? ExpandCollapseState.Expanded
: ExpandCollapseState.Collapsed;
public static ExpandCollapseState StateFor(bool hasChildren, bool expanded) => (hasChildren, expanded) switch
{
(false, _) => ExpandCollapseState.LeafNode,
(true, true) => ExpandCollapseState.Expanded,
(true, false) => ExpandCollapseState.Collapsed
};
/// <summary>The entry this one hangs under, or null at the top of the tree.</summary>
public ExpandableTreeViewItem? ReadableParent => ItemsControlFromItemContainer(this) as ExpandableTreeViewItem;

View File

@@ -164,10 +164,12 @@ public static class ScreenReaderNames
}
/// <summary>The picker's label and what it holds, as the button inside it is read.</summary>
public static string? PickerText(string? label, string? value) =>
string.IsNullOrEmpty(label) ? value
: string.IsNullOrEmpty(value) ? label
: $"{label}: {value}";
public static string? PickerText(string? label, string? value)
{
if (string.IsNullOrEmpty(label))
return value;
return string.IsNullOrEmpty(value) ? label : $"{label}: {value}";
}
/// <summary>Day and month in words, the year too when the picker shows one.</summary>
public static string? DateText(DateTimeOffset? date, bool withYear) =>

View File

@@ -37,7 +37,7 @@ public static class StatsBar
Margin = new Avalonia.Thickness(0, 0, last ? 0 : 22, 0),
VerticalAlignment = VerticalAlignment.Center,
TextTrimming = TextTrimming.CharacterEllipsis,
Inlines =
Inlines = new InlineCollection
{
name,
new Run(" "),

View File

@@ -43,6 +43,10 @@
<ProjectReference Include="..\Eu07.BlackBox.Views\Eu07.BlackBox.Views.csproj" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="StarterNG.Tests" />
</ItemGroup>
<ItemGroup>
<Folder Include="Assets\Logo\" />
</ItemGroup>

View File

@@ -28,14 +28,7 @@ public static class ConsistNarration
if (after.Count == 0)
return before.Count == 0 ? null : loc("ConsistSaidEmpty");
int head = 0;
while (head < before.Count && head < after.Count && before[head] == after[head])
head++;
int tail = 0;
while (tail < before.Count - head && tail < after.Count - head
&& before[before.Count - 1 - tail] == after[after.Count - 1 - tail])
tail++;
var (head, tail) = Common(before, after);
var gone = before.Skip(head).Take(before.Count - head - tail).ToList();
var come = after.Skip(head).Take(after.Count - head - tail).ToList();
@@ -48,34 +41,54 @@ public static class ConsistNarration
if (come.Count == 0)
return string.Format(loc("ConsistSaidRemoved"), Labels(gone), vehicles);
if (gone.Count == come.Count)
{
// The same vehicles in the same places: some of them turned round.
if (gone.Select(u => u.Label).SequenceEqual(come.Select(u => u.Label)))
{
var turned = come.Where((u, i) => u.Turned != gone[i].Turned).ToList();
return turned.Count == 1
? string.Format(loc(turned[0].Turned ? "ConsistSaidTurned" : "ConsistSaidUnturned"), turned[0].Label)
: string.Format(loc("ConsistSaidTurnedMany"), turned.Count);
}
// One vehicle taken out and put back elsewhere: the rest keep their order.
if (MovedOne(gone, come) is { } moved)
return string.Format(loc("ConsistSaidMoved"), moved.Unit.Label, head + moved.To + 1, after.Count);
if (gone.OrderBy(u => u.Label, StringComparer.Ordinal).SequenceEqual(
come.OrderBy(u => u.Label, StringComparer.Ordinal)))
return loc("ConsistSaidReordered");
if (gone.Count == 1)
return string.Format(loc("ConsistSaidReplaced"), gone[0].Label, come[0].Label);
}
if (gone.Count == come.Count && SameCount(gone, come, head, after.Count, loc) is { } said)
return said;
return string.Format(loc("ConsistSaidChangedMany"), Math.Max(gone.Count, come.Count), vehicles);
}
/// <summary>How many units at the front and at the back stayed as they were.</summary>
private static (int Head, int Tail) Common(IReadOnlyList<Unit> before, IReadOnlyList<Unit> after)
{
int head = 0;
while (head < before.Count && head < after.Count && before[head] == after[head])
head++;
int tail = 0;
while (tail < before.Count - head && tail < after.Count - head
&& before[before.Count - 1 - tail] == after[after.Count - 1 - tail])
tail++;
return (head, tail);
}
/// <summary>A change that left as many units as there were: turned, moved, reordered or one replaced; or null.</summary>
private static string? SameCount(List<Unit> gone, List<Unit> come, int head, int count, Func<string, string> loc)
{
// The same vehicles in the same places: some of them turned round.
if (gone.Select(u => u.Label).SequenceEqual(come.Select(u => u.Label)))
return Turned(gone, come, loc);
// One vehicle taken out and put back elsewhere: the rest keep their order.
if (MovedOne(gone, come) is { } moved)
return string.Format(loc("ConsistSaidMoved"), moved.Unit.Label, head + moved.To + 1, count);
if (gone.OrderBy(u => u.Label, StringComparer.Ordinal).SequenceEqual(
come.OrderBy(u => u.Label, StringComparer.Ordinal)))
return loc("ConsistSaidReordered");
return gone.Count == 1 ? string.Format(loc("ConsistSaidReplaced"), gone[0].Label, come[0].Label) : null;
}
private static string Turned(List<Unit> gone, List<Unit> come, Func<string, string> loc)
{
var turned = come.Where((u, i) => u.Turned != gone[i].Turned).ToList();
if (turned.Count != 1)
return string.Format(loc("ConsistSaidTurnedMany"), turned.Count);
string key = turned[0].Turned ? "ConsistSaidTurned" : "ConsistSaidUnturned";
return string.Format(loc(key), turned[0].Label);
}
/// <summary>The unit and the place it went to when one alone moved, or null.</summary>
private static (Unit Unit, int To)? MovedOne(IReadOnlyList<Unit> gone, IReadOnlyList<Unit> come)
private static (Unit Unit, int To)? MovedOne(List<Unit> gone, List<Unit> come)
{
// The run that differs starts and ends with a change, so the mover is at one end of it.
if (come.Count >= 2 && come.Take(come.Count - 1).SequenceEqual(gone.Skip(1)) && come[^1] == gone[0])

View File

@@ -436,7 +436,7 @@ internal sealed class KeyBindingsTab(Grid host, TextBox search)
}
}
private Control BuildKeyBlock(KeyCap[][] rows, Dictionary<string, KeyState> states, int blockIndex)
private StackPanel BuildKeyBlock(KeyCap[][] rows, Dictionary<string, KeyState> states, int blockIndex)
{
var block = new StackPanel { Spacing = KeyGap, VerticalAlignment = VerticalAlignment.Top };
for (int r = 0; r < rows.Length; r++)
@@ -449,7 +449,7 @@ internal sealed class KeyBindingsTab(Grid host, TextBox search)
return block;
}
private Control BuildKeyCap(KeyCap cap, Dictionary<string, KeyState> states, int blockIndex, int rowIndex)
private Border BuildKeyCap(KeyCap cap, Dictionary<string, KeyState> states, int blockIndex, int rowIndex)
{
double width = cap.Width * KeyUnit + (cap.Width - 1) * KeyGap;
var content = new Grid { Width = width, Height = KeyHeight };

View File

@@ -101,7 +101,7 @@ public partial class Scenarios : UserControl
sceneryList.Items.Add(ToTreeItem(node, expandGroups));
}
private TreeViewItem ToTreeItem(SceneryTreeNode node, bool expandGroups)
private ExpandableTreeViewItem ToTreeItem(SceneryTreeNode node, bool expandGroups)
{
var item = new ExpandableTreeViewItem
{

View File

@@ -399,7 +399,7 @@ public sealed class HaslerTapeWindow : Window
_list.SelectedItem = chosen.Tape;
}
private TreeViewItem SessionItem(TapeSession session, bool open)
private ExpandableTreeViewItem SessionItem(TapeSession session, bool open)
{
string title = session.Launch is { } launch
? string.Join(" · ", new[] { string.IsNullOrWhiteSpace(launch.SceneryName) ? launch.Scenery : launch.SceneryName, launch.Train }
@@ -441,7 +441,7 @@ public sealed class HaslerTapeWindow : Window
return item;
}
private TreeViewItem BlackBoxItem(BlackBoxRecording box)
private ExpandableTreeViewItem BlackBoxItem(BlackBoxRecording box)
{
var text = new StackPanel { Spacing = 1 };
text.Children.Add(new TextBlock { Text = App.Loc["HaslerBlackBox"], FontWeight = FontWeight.SemiBold });
@@ -585,7 +585,7 @@ public sealed class HaslerTapeWindow : Window
}
}
private static TreeViewItem TapeItem(TapeFile file)
private static ExpandableTreeViewItem TapeItem(TapeFile file)
{
var text = new StackPanel { Spacing = 1 };
text.Children.Add(new TextBlock