Files
StarterNG/StarterNG/Views/MessageBox.cs
maj00r ff5228555e fix: a blind player can bind keys, build a consist and read the tapes
A review against WCAG 2.2 AA left places a screen reader user could not work:

- Key bindings: a key's button says the command and the key, the clear button
  which command it clears; a new key is said aloud and focus comes back to its
  row. The on-screen keyboard is reached with Tab and walked with the arrows.
- Number fields read their label; the spin buttons are named and left out of Tab.
- Dialogs: Enter presses the main button, Esc closes, focus starts on the first
  button and the message is read with it.
- Consist: every change is said - added, removed, turned, moved, replaced,
  undone - and the strip is a named list; a turned card says so.
- Searches say how many entries are left once typing pauses.
- Hasler tapes, the black box timeline, timetable and cab panel take focus, work
  from the keyboard, read their value as it changes and draw a focus frame.
- Section reset and level controls, the crew button and the tabs carry names;
  English names from Avalonia's templates are read in the starter's language;
  sort and crew menus are radio items.
- Input borders reach 3:1 against the panels; Tab starts at the top bar; the
  splash screen has a title and its progress reads the loading step.
2026-11-15 20:46:55 +01:00

136 lines
4.6 KiB
C#

using System;
using System.Threading.Tasks;
using Avalonia.Automation;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Media;
namespace StarterNG.Views;
public enum MessageBoxButtons
{
YesNo,
Ok
}
public static class MessageBox
{
/// <param name="yesText">A label that says what the answer does, in place of "Yes".</param>
/// <param name="noText">Likewise in place of "No".</param>
public static async Task<bool> Show(Window owner, string message, string title, MessageBoxButtons buttons,
string? yesText = null, string? noText = null)
{
var win = FixedDialog(title, 420);
bool yes = false;
var yesBtn = DialogButton(buttons == MessageBoxButtons.Ok ? App.Loc["Ok"] : yesText ?? App.Loc["Yes"],
"Accent", 80, () => { yes = true; win.Close(); });
var noBtn = DialogButton(noText ?? App.Loc["No"], "Flat", 80, () => win.Close());
noBtn.IsVisible = buttons != MessageBoxButtons.Ok;
// Enter answers, Esc declines - or only closes, where OK is all there is.
yesBtn.IsDefault = true;
if (buttons == MessageBoxButtons.Ok)
yesBtn.IsCancel = true;
else
noBtn.IsCancel = true;
ReadMessage(win, yesBtn, message);
win.Content = new StackPanel
{
Margin = new Avalonia.Thickness(16),
Spacing = 16,
Children =
{
new TextBlock { Text = message, TextWrapping = TextWrapping.Wrap },
ButtonRow(yesBtn, noBtn)
}
};
await win.ShowDialog(owner);
return yes;
}
/// <summary>
/// A notice with one action besides closing it, and a box to stop it coming
/// back, which counts however the window is closed.
/// </summary>
public static async Task<(bool Act, bool Silence)> ShowNotice(Window owner, string message, string title,
string actionText, string silenceText)
{
var win = FixedDialog(title, 460);
bool act = false;
var actBtn = DialogButton(actionText, "Accent", null, () => { act = true; win.Close(); });
var okBtn = DialogButton(App.Loc["Ok"], "Flat", 80, () => win.Close());
var silence = new CheckBox { Content = silenceText, VerticalAlignment = VerticalAlignment.Center };
actBtn.IsDefault = true;
okBtn.IsCancel = true;
ReadMessage(win, actBtn, message);
win.Content = new StackPanel
{
Margin = new Avalonia.Thickness(16),
Spacing = 16,
Children =
{
new SelectableTextBlock { Text = message, TextWrapping = TextWrapping.Wrap },
new DockPanel
{
Children =
{
silence,
ButtonRow(actBtn, okBtn)
}
}
}
};
await win.ShowDialog(owner);
return (act, silence.IsChecked == true);
}
/// <summary>
/// The message is told with the window and with the button focus opens on: a
/// screen reader said only the title, and the question it asked went unheard.
/// </summary>
internal static void ReadMessage(Window win, Control first, string message)
{
AutomationProperties.SetHelpText(win, message);
AutomationProperties.SetHelpText(first, message);
}
/// <summary>A dialog over its owner, as tall as what it holds, not to be resized.</summary>
internal static Window FixedDialog(string title, double width) => new()
{
Title = title,
Width = width,
SizeToContent = SizeToContent.Height,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
CanResize = false
};
/// <summary>A dialog's buttons, at its right edge.</summary>
internal static StackPanel ButtonRow(params Button[] buttons)
{
var row = new StackPanel
{
Orientation = Orientation.Horizontal,
HorizontalAlignment = HorizontalAlignment.Right,
Spacing = 8
};
row.Children.AddRange(buttons);
return row;
}
private static Button DialogButton(string content, string cssClass, double? minWidth, Action onClick)
{
var button = new Button { Content = content, Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand) };
if (minWidth is { } width)
button.MinWidth = width;
button.Classes.Add(cssClass);
button.Click += (_, _) => onClick();
return button;
}
}