From 7e249a7a9a5a4e7f7a2f2354b0196a02cd519d52 Mon Sep 17 00:00:00 2001 From: maj00r Date: Tue, 17 Nov 2026 22:14:45 +0100 Subject: [PATCH] fix(settings): a first start has nothing to save before the simulator starts With no eu07.ini of the player's own, starting the simulator asked to save changes nobody made. The UART tune box and the Vulkan device box went by TextChanged, which Avalonia raises later from the dispatcher: the text the page put there while loading came back as an edit once the window was shown. Both now go by their Text property, which changes at once and is passed over while the page loads; typing in them still counts. --- StarterNG.Tests/SettingsFirstStartTests.cs | 39 ++++++++++++++++++++++ StarterNG/Views/Settings.axaml.cs | 21 +++++++++--- 2 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 StarterNG.Tests/SettingsFirstStartTests.cs diff --git a/StarterNG.Tests/SettingsFirstStartTests.cs b/StarterNG.Tests/SettingsFirstStartTests.cs new file mode 100644 index 0000000..7af3962 --- /dev/null +++ b/StarterNG.Tests/SettingsFirstStartTests.cs @@ -0,0 +1,39 @@ +using StarterNG.Application; +using StarterNG.Tests.Fakes; + +namespace StarterNG.Tests; + +/// The settings page on a start with no eu07.ini of the player's own yet. +[Collection(AvaloniaControls.Name)] +public class SettingsFirstStartTests +{ + [Fact] + public void A_first_start_has_nothing_to_save_before_the_simulator_starts() + { + var files = new InMemoryFileSystem(); + files.WriteAllText(TestInstallation.At("eu07.ini"), "width 1920\nheight 1080\nendconfig\n"); + var installation = new TestInstallation(files); + installation.Environment.AsWindows(); + var services = AppServices.Initialize(installation.Paths, installation.Files, installation.Clock, + installation.Environment); + services.SettingsStore.Load(); + services.SettingsStore.StartAtHighWithoutOwnSettings(); + + HeadlessDrawing.Ensure(); + // The page reads the graphics cards away from the window and comes back to it, as in the application. + var context = SynchronizationContext.Current; + SynchronizationContext.SetSynchronizationContext(new Avalonia.Threading.AvaloniaSynchronizationContext()); + Avalonia.Controls.Window? window = null; + try + { + var page = new StarterNG.Views.Settings(); + window = Controls.Show(page, 1600, 1000, themed: true); + Assert.False(page.HasUnsavedChanges); + } + finally + { + window?.Close(); + SynchronizationContext.SetSynchronizationContext(context); + } + } +} diff --git a/StarterNG/Views/Settings.axaml.cs b/StarterNG/Views/Settings.axaml.cs index 6dce6ff..420918f 100644 --- a/StarterNG/Views/Settings.axaml.cs +++ b/StarterNG/Views/Settings.axaml.cs @@ -966,11 +966,11 @@ public partial class Settings : UserControl, ISettingsCapture OnControlChanged(); }; UartEnabledCb.IsCheckedChanged += (_, _) => ValidateUart(); - UartTuneBox.TextChanged += (_, _) => + OnTextEdited(UartTuneBox, () => { ValidateUart(); OnControlChanged(); - }; + }); App.Loc.LanguageChanged += UpdateUartTexts; UpdateUartTexts(); } @@ -1511,7 +1511,7 @@ public partial class Settings : UserControl, ISettingsCapture nud.ValueChanged += (_, _) => OnControlChanged(); break; case TextBox box when ReferenceEquals(box, VulkanDeviceTb): - box.TextChanged += (_, _) => OnControlChanged(); + OnTextEdited(box, OnControlChanged); break; } } @@ -1521,6 +1521,18 @@ public partial class Settings : UserControl, ISettingsCapture toggle.IsCheckedChanged += (_, _) => OnControlChanged(); } + /// + /// The text as it changes, not TextChanged: that one comes later, from the + /// dispatcher, so the text the page itself put there while loading came back + /// as an edit once the window was shown - the first start asked to save. + /// + private static void OnTextEdited(TextBox box, Action edited) => + box.PropertyChanged += (_, e) => + { + if (e.Property == TextBox.TextProperty) + edited(); + }; + private void OnControlChanged() { MarkDirty(); @@ -1533,8 +1545,7 @@ public partial class Settings : UserControl, ISettingsCapture if (_loading || _dirty) return; - _dirty = true; - UpdateSaveState(); + _dirty = true; UpdateSaveState(); } /// Edits on the page, or of the keys, not saved yet: what "Niezapisane zmiany" tells.