Files
StarterNG/StarterNG/App.axaml.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

219 lines
7.8 KiB
C#

using System;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using StarterNG.Application;
using StarterNG.Classes;
using StarterNG.Controls;
using StarterNG.Infrastructure;
using StarterNG.Services;
namespace StarterNG;
public partial class App : Avalonia.Application
{
/// <summary>
/// The translation table, as a static source for the XAML bindings. Code
/// should take <see cref="Application.Abstractions.ILocalizedStrings"/>
/// through its constructor rather than reading this.
/// </summary>
public static LocalizationService Loc => AppServices.Current.Localization;
public override void Initialize()
{
// Before any control exists: the names are given as controls are filled in.
ScreenReaderNames.Install();
DialogKeyboard.Install();
AvaloniaXamlLoader.Load(this);
AppServices.Current.SettingsStore.Load();
AppServices.Current.SettingsStore.StartAtHighWithoutOwnSettings();
KeyboardConfig.Instance.Load();
CultureInfo ci = CultureInfo.InstalledUICulture ;
var langName = AppServices.Current.Settings.LanguageWasSet
? AppServices.Current.Settings.Language
: ci.Name switch
{
"pl-PL" => "Polski",
_ => "English"
};
AppServices.Current.Settings.Language = langName;
// What eu07.ini asked for, before the starter picks a language it can
// actually display.
string requestedCode = AppServices.Current.Settings.LanguageCode;
// Subscribed before any view, so the views rebuild in the new culture.
Loc.LanguageChanged += () => ApplyCulture(Loc.CurrentLangCode);
Loc.Keep(requestedCode);
ApplyLanguage(langName);
// The starter ships no translation for this code, so its own interface
// falls back - but the simulator has one and keeps it. Overwriting the key
// here would silently move eu07.exe to English on the next save.
if (!string.IsNullOrWhiteSpace(requestedCode) &&
!string.Equals(requestedCode, Loc.CurrentLangCode, StringComparison.OrdinalIgnoreCase))
AppServices.Current.Settings.LanguageCode = requestedCode;
}
public static void ApplyLanguage(string langNameOrCode)
{
var resolvedName = Loc.Load(langNameOrCode);
// The service is the authority on which language actually loaded: the code
// goes to eu07.ini for the simulator, the name to the picker.
AppServices.Current.Settings.Language = resolvedName;
AppServices.Current.Settings.LanguageCode = Loc.CurrentLangCode;
}
/// <summary>
/// A language picked by the user, written to eu07.ini straight away as the old
/// starter did - not left waiting for the Save button of a different tab.
/// Only what is already in the settings goes with it, not other unsaved edits.
/// </summary>
public static void ChangeLanguage(string code)
{
if (string.Equals(code, Loc.CurrentLangCode, StringComparison.OrdinalIgnoreCase))
return;
ApplyLanguage(code);
AppServices.Current.SettingsStore.Save();
}
private static void ApplyCulture(string langCode)
{
CultureInfo culture;
try
{
culture = langCode switch
{
"" or "en" => CultureInfo.InvariantCulture,
"cz" => CultureInfo.GetCultureInfo("cs"),
_ => CultureInfo.GetCultureInfo(langCode)
};
}
catch (CultureNotFoundException)
{
culture = CultureInfo.InvariantCulture;
}
// Only the defaults: a culture set on the thread is an async local, so one set
// from the picker's handler was undone as soon as the handler returned, and
// the UI kept the first language's decimal comma and month names.
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
desktop.ShutdownRequested += (_, _) =>
{
try { AppServices.Current.SettingsStore.CaptureAndSave(); }
catch (Exception ex) { Diagnostics.Log("Settings save", ex); }
AppServices.Current.MissingVehicleLog.Dump();
try
{
if (KeyboardConfig.Instance.Dirty)
KeyboardConfig.Instance.Save();
}
catch (Exception ex) { Diagnostics.Log("Keyboard config save", ex); }
Diagnostics.Flush();
};
SplashWindow? splash = null;
bool mainReady = false;
var sw = System.Diagnostics.Stopwatch.StartNew();
Task.Run(async () =>
{
var progress = new Progress<LoadStatus>(status =>
{
Dispatcher.UIThread.Post(() =>
{
if (mainReady)
return;
if (sw.ElapsedMilliseconds > 300 && splash == null)
{
splash = new SplashWindow();
desktop.MainWindow = splash;
splash.Show();
}
splash?.Report(status);
});
});
AppServices.Current.Library.Load(progress);
}).ContinueWith(load =>
{
Dispatcher.UIThread.Post(async () =>
{
MainWindow main;
try
{
main = new MainWindow();
}
catch (Exception ex)
{
Diagnostics.Log("Main window", ex);
Diagnostics.Flush();
Console.Error.WriteLine(ex);
mainReady = true;
splash?.Close();
desktop.Shutdown(1);
return;
}
mainReady = true;
desktop.MainWindow = main;
main.Show();
splash?.Close();
splash = null;
if (load.IsFaulted && load.Exception is { } error)
{
var inner = error.GetBaseException();
Diagnostics.Log("Game data load", inner);
await Diagnostics.ReportAsync(
$"{Loc["FaultLoadData"]}{Environment.NewLine}{Environment.NewLine}{Loc["FaultDetail"]} {inner.Message}");
}
var check = Diagnostics.InstallationCheck();
var faults = check.Run();
if (faults.Count > 0)
{
await Diagnostics.ReportAsync(
string.Join(Environment.NewLine, faults) + Environment.NewLine + Environment.NewLine +
Loc["FaultBadInstall"]);
}
if (check.ExecutableMissing())
await main.OfferExecutablePickAsync();
if (CpuFeatures.LacksAvx2 && AppServices.Current.Settings.CpuNotice)
await main.ShowCpuNoticeAsync();
});
});
}
base.OnFrameworkInitializationCompleted();
}
}