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 4412144..70cc80b 100644 --- a/StarterNG/MainWindow.axaml.cs +++ b/StarterNG/MainWindow.axaml.cs @@ -110,7 +110,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 e3a7422..6ed7afb 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) =>