From f29ce10f5c18f1d66035831071d1a395f8716604 Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 15 Nov 2026 21:13:53 +0100 Subject: [PATCH] fix: a simulator copied in while the starter runs is on the list The list of simulators in the settings was filled when they opened, so a new eu07*.exe from CI or one's own build stayed missing until the starter was reopened, and the older file was easy to start by mistake. The folder is now looked at again when the settings page is shown, when the list takes focus and when the window comes back to the front. Looking again never changes the choice: a file picked from elsewhere, or one since removed, stays chosen rather than falling back to automatic, and nothing waits for Save after it. A list that did not change is left alone. Closes #48 --- StarterNG.Tests/ExecutableListTests.cs | 53 ++++++++++++++++++++++++++ StarterNG/MainWindow.axaml.cs | 8 +++- StarterNG/Views/ExecutableList.cs | 31 +++++++++++++++ StarterNG/Views/Settings.axaml.cs | 38 ++++++++++++++---- 4 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 StarterNG.Tests/ExecutableListTests.cs create mode 100644 StarterNG/Views/ExecutableList.cs diff --git a/StarterNG.Tests/ExecutableListTests.cs b/StarterNG.Tests/ExecutableListTests.cs new file mode 100644 index 0000000..9e3cc38 --- /dev/null +++ b/StarterNG.Tests/ExecutableListTests.cs @@ -0,0 +1,53 @@ +using StarterNG.Views; + +namespace StarterNG.Tests; + +public class ExecutableListTests +{ + [Fact] + public void A_build_copied_in_joins_the_list_and_the_choice_stays() + { + var (entries, chosen) = ExecutableList.Refreshed(["eu07_new.exe", "eu07.exe"], "eu07.exe"); + + Assert.Equal(new[] { "eu07_new.exe", "eu07.exe" }, entries); + Assert.Equal(1, chosen); + } + + [Fact] + public void Automatic_stays_automatic() + { + var (entries, chosen) = ExecutableList.Refreshed(["eu07_new.exe", "eu07.exe"], null); + + Assert.Equal(new[] { "eu07_new.exe", "eu07.exe" }, entries); + Assert.Equal(-1, chosen); + } + + [Fact] + public void A_simulator_picked_from_elsewhere_stays_on_the_list_and_chosen() + { + const string picked = @"D:\build\eu07.exe"; + + var (entries, chosen) = ExecutableList.Refreshed(["eu07.exe"], picked); + + Assert.Equal(new[] { "eu07.exe", picked }, entries); + Assert.Equal(1, chosen); + } + + [Fact] + public void A_chosen_file_since_removed_stays_chosen_rather_than_switch_silently() + { + var (entries, chosen) = ExecutableList.Refreshed(["eu07.exe"], "eu07_old.exe"); + + Assert.Equal(new[] { "eu07.exe", "eu07_old.exe" }, entries); + Assert.Equal(1, chosen); + } + + [Fact] + public void The_choice_is_found_whatever_the_case_of_its_name() + { + var (entries, chosen) = ExecutableList.Refreshed(["EU07.exe"], "eu07.exe"); + + Assert.Equal(new[] { "EU07.exe" }, entries); + Assert.Equal(0, chosen); + } +} diff --git a/StarterNG/MainWindow.axaml.cs b/StarterNG/MainWindow.axaml.cs index 2a203ad..02304bd 100644 --- a/StarterNG/MainWindow.axaml.cs +++ b/StarterNG/MainWindow.axaml.cs @@ -108,7 +108,13 @@ public partial class MainWindow : Window _sceneryFilesTimer.Start(); FollowLastRun(); }; - Activated += (_, _) => CheckExternalSettings(); + Activated += (_, _) => + { + CheckExternalSettings(); + // Back from a build or a download: a new simulator is on the list already. + if (SettingsView.IsVisible) + SettingsView.RefreshExecutables(); + }; _sceneryFilesTimer.Tick += async (_, _) => await LookAtSceneryFilesAsync(); Closed += (_, _) => _sceneryFilesTimer.Stop(); } diff --git a/StarterNG/Views/ExecutableList.cs b/StarterNG/Views/ExecutableList.cs new file mode 100644 index 0000000..3174ed7 --- /dev/null +++ b/StarterNG/Views/ExecutableList.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace StarterNG.Views; + +/// +/// The simulators offered in the settings after the folder was looked at again: a build +/// copied in while the starter runs joins the list, and what was chosen stays chosen, even +/// a file picked from elsewhere or one since removed - looking again never changes the choice. +/// +public static class ExecutableList +{ + /// The entries that follow "automatic", and the place of among them. + /// The simulators in the folder now, newest first. + /// The entry chosen, or null for "automatic" or nothing chosen yet. + /// Entries, and the index of the chosen one in them, -1 when is null. + public static (IReadOnlyList Entries, int Chosen) Refreshed(IReadOnlyList found, string? chosen) + { + var entries = found.ToList(); + if (chosen is null) + return (entries, -1); + + int at = entries.FindIndex(entry => string.Equals(entry, chosen, StringComparison.OrdinalIgnoreCase)); + if (at >= 0) + return (entries, at); + + entries.Add(chosen); + return (entries, entries.Count - 1); + } +} diff --git a/StarterNG/Views/Settings.axaml.cs b/StarterNG/Views/Settings.axaml.cs index aecbad4..617dff4 100644 --- a/StarterNG/Views/Settings.axaml.cs +++ b/StarterNG/Views/Settings.axaml.cs @@ -105,6 +105,13 @@ public partial class Settings : UserControl, ISettingsCapture FeedbackCb.SelectionChanged += FeedbackCb_OnSelectionChanged; SelectExeCb.SelectionChanged += (_, _) => UpdateIdleNotes(); + // Coming to the page or to the list is when a new build is looked for. + SelectExeCb.GotFocus += (_, _) => RefreshExecutables(); + PropertyChanged += (_, e) => + { + if (e.Property == IsVisibleProperty && IsVisible) + RefreshExecutables(); + }; FullscreenWindowedCb.IsCheckedChanged += (_, _) => UpdateIdleNotes(); App.Loc.LanguageChanged += UpdateIdleNotes; App.Loc.LanguageChanged += UpdateGpuNote; @@ -246,16 +253,33 @@ public partial class Settings : UserControl, ISettingsCapture private void PopulateExeCombo() { - string? current = (SelectExeCb.SelectedItem as ComboBoxItem)?.Content?.ToString(); + int index = SelectExeCb.SelectedIndex; + string? chosen = index > 0 ? (SelectExeCb.SelectedItem as ComboBoxItem)?.Content?.ToString() : null; + var (entries, at) = ExecutableList.Refreshed(AppServices.Current.Executables.ListCandidates(), chosen); + + // The same files as shown: left alone, so a reader on the list hears nothing change. + var shown = SelectExeCb.Items.Skip(1).Select(item => (item as ComboBoxItem)?.Content?.ToString() ?? ""); + if (SelectExeCb.Items.Count > 0 && shown.SequenceEqual(entries, StringComparer.Ordinal)) + return; + SelectExeCb.Items.Clear(); SelectExeCb.Items.Add(new ComboBoxItem { Content = App.Loc["SelectEXEAuto"] }); - foreach (string exe in AppServices.Current.Executables.ListCandidates()) + foreach (string exe in entries) SelectExeCb.Items.Add(new ComboBoxItem { Content = exe }); - if (current is not null) - { - int idx = FindComboIndexByContent(SelectExeCb, current); - SelectExeCb.SelectedIndex = idx >= 0 ? idx : 0; - } + if (index >= 0) + SelectExeCb.SelectedIndex = at >= 0 ? at + 1 : 0; + } + + /// + /// Looks at the simulator folder again, so a build copied in while the starter runs is + /// on the list without reopening anything. The choice stays as it was and is no edit: + /// nothing waits for Save after it. Not while the list is open under the user's hand. + /// + public void RefreshExecutables() + { + if (SelectExeCb.IsDropDownOpen) + return; + WhileLoading(PopulateExeCombo); } private async void BrowseExeButton_OnClick(object? sender, RoutedEventArgs e) =>