diff --git a/src/SopHelper.Avalonia/VVMs/SopPage/SopPageViewModel.cs b/src/SopHelper.Avalonia/VVMs/SopPage/SopPageViewModel.cs index c8d33e8..4881d27 100644 --- a/src/SopHelper.Avalonia/VVMs/SopPage/SopPageViewModel.cs +++ b/src/SopHelper.Avalonia/VVMs/SopPage/SopPageViewModel.cs @@ -430,11 +430,20 @@ public class SopPageViewModel : ViewModelBase catch (Exception error) when (error is IOException or UnauthorizedAccessException or JsonException) { Log("warn", "读取上次配方选择失败:" + error.Message); } } + var names = DesktopPlatform.Recipes.Names().Select(name => name + ".yaml").ToList(); _refreshingRecipes = true; try { - Recipes.Clear(); - foreach (var name in DesktopPlatform.Recipes.Names()) Recipes.Add(name + ".yaml"); + // Keep surviving items in place: clearing a hidden ComboBox's source loses + // its selection even when the view model still holds the same recipe. + for (var index = Recipes.Count - 1; index >= 0; index--) + if (!names.Contains(Recipes[index])) Recipes.RemoveAt(index); + for (var index = 0; index < names.Count; index++) + { + var existing = Recipes.IndexOf(names[index]); + if (existing < 0) Recipes.Insert(index, names[index]); + else if (existing != index) Recipes.Move(existing, index); + } SelectedRecipe = preferred is not null && Recipes.Contains(preferred) ? preferred : Recipes.FirstOrDefault(); RaisePropertyChanged(nameof(SelectedRecipe)); } diff --git a/tests/Avalonia.Smoke/Program.cs b/tests/Avalonia.Smoke/Program.cs index e4b964c..4b59f64 100644 --- a/tests/Avalonia.Smoke/Program.cs +++ b/tests/Avalonia.Smoke/Program.cs @@ -25,11 +25,13 @@ internal static class Smoke { private static int _exit=1; private static bool _processImagesOnly; + private static bool _recipeSelectionOnly; private static readonly FakeCamera Camera=new(); [STAThread] public static int Main(string[] args) { _processImagesOnly=args.Contains("--process-images-only"); + _recipeSelectionOnly=args.Contains("--recipe-selection-only"); var previousRoot=Environment.GetEnvironmentVariable("SOPHELPER_WORKSPACE_ROOT"); var dataRoot=Path.Combine(Path.GetTempPath(),"sophelper-layout-smoke-"+Guid.NewGuid().ToString("N")); Directory.CreateDirectory(dataRoot); @@ -83,6 +85,8 @@ internal static class Smoke var settings=window.GetVisualDescendants().OfType().Single(); Check(ReferenceEquals(settings.DataContext,sop),"Settings and operator page share runtime state"); Check(settings.GetVisualDescendants().OfType().Count()==2&&settings.GetVisualDescendants().OfType().Count()==2,"Camera, recipe, threshold and stride editors are on settings page"); + await CheckRecipeSelection(window,navigation,sop); + if(_recipeSelectionOnly){_exit=0;return;} sop.ApplyRecipeCommand.Execute(); Check(sop.ActiveRecipe==sop.SelectedRecipe,"Applied recipe is shown as current"); var applied=sop.ActiveRecipe;var selected=sop.SelectedRecipe; @@ -451,6 +455,46 @@ internal static class Smoke sop.SelectedRecipe=selected; } + private static async Task CheckRecipeSelection(Window window,NavigationModel navigation,SopPageViewModel sop) + { + ComboBox RecipeBox()=>window.GetVisualDescendants().OfType().Single(c=>ReferenceEquals(c.ItemsSource,sop.Recipes)); + void Reload(string? preferred)=>typeof(SopPageViewModel).GetMethod("LoadRecipes",System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.NonPublic)!.Invoke(sop,[preferred]); + var selected=sop.SelectedRecipe; + Check(selected is not null&&Equals(RecipeBox().SelectedItem,selected),"Runtime settings initially displays its selected recipe"); + for(var i=0;i<3;i++) + { + navigation.SelectedIndex=0;await Task.Delay(30); + Reload(selected); + navigation.SelectedIndex=1;await Task.Delay(30); + Check(sop.SelectedRecipe==selected&&Equals(RecipeBox().SelectedItem,selected),"Recipe selection survives list refresh while runtime settings is hidden"); + Reload(selected); + await Task.Delay(30); + Check(sop.SelectedRecipe==selected&&Equals(RecipeBox().SelectedItem,selected),"Recipe selection survives list refresh while runtime settings is visible"); + } + var store=new RecipeWorkspace(Environment.GetEnvironmentVariable("SOPHELPER_WORKSPACE_ROOT")!); + var name="000-selection-"+Guid.NewGuid().ToString("N"); + Directory.CreateDirectory(store.DirectoryFor(name)); + var config=new SopHelper.Engine.Sop.SopConfig();config.Sop.Name=name; + SopHelper.Engine.Sop.SopConfigLoader.Save(config,store.ConfigPath(name)); + try + { + navigation.SelectedIndex=0;await Task.Delay(30);Reload(selected); + navigation.SelectedIndex=1;await Task.Delay(30); + Check(sop.Recipes.Contains(name+".yaml")&&Equals(RecipeBox().SelectedItem,selected),"Adding a recipe before the selected item preserves the visible selection"); + navigation.SelectedIndex=0;await Task.Delay(30);Reload(name+".yaml"); + navigation.SelectedIndex=1;await Task.Delay(30); + Check(sop.SelectedRecipe==name+".yaml"&&Equals(RecipeBox().SelectedItem,sop.SelectedRecipe),"Saving a new recipe while settings is hidden selects it on return"); + navigation.SelectedIndex=0;await Task.Delay(30);File.Delete(store.ConfigPath(name));Reload(null); + navigation.SelectedIndex=1;await Task.Delay(30); + Check(sop.SelectedRecipe is not null&&sop.SelectedRecipe!=name+".yaml"&&Equals(RecipeBox().SelectedItem,sop.SelectedRecipe),"Removing the selected recipe while settings is hidden displays the fallback recipe"); + } + finally + { + if(File.Exists(store.ConfigPath(name)))File.Delete(store.ConfigPath(name)); + Directory.Delete(store.DirectoryFor(name));Reload(selected); + } + } + private static void CheckRecipePreferences(App app) { var store=new RecipeWorkspace(Environment.GetEnvironmentVariable("SOPHELPER_WORKSPACE_ROOT")!);