diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs index 0b1cfd6..ad7e9d6 100644 --- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs +++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs @@ -158,7 +158,12 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware // 新建时文件名还没定,用配方名兜底;已存在的文件名不可改(SOP 页按文件名选配方) var fileName = string.IsNullOrEmpty(_path) ? config.Sop.Name : Path.GetFileNameWithoutExtension(_path); - Recipe = RecipeNode.From(config, fileName, _isNew); + var recipe = RecipeNode.From(config, fileName, _isNew); + var selectedStep = recipe.SelectedStep; + // 先发布配方的下拉选项,再绑定步骤,避免区域选中值先于 ItemsSource 到达而被清空。 + recipe.SelectedStep = null; + Recipe = recipe; + recipe.SelectedStep = selectedStep; Recipe.Changed += RefreshWarnings; InitializeRecipeTools(); LoadAgentSettings(); diff --git a/tests/Avalonia.Smoke/Program.cs b/tests/Avalonia.Smoke/Program.cs index 0840571..0300693 100644 --- a/tests/Avalonia.Smoke/Program.cs +++ b/tests/Avalonia.Smoke/Program.cs @@ -173,7 +173,7 @@ internal static class Smoke if(Environment.GetCommandLineArgs().Contains("--recipe-close-only")) { await CheckRecipeClose(app,lifetime);_exit=0;return; } if(Environment.GetCommandLineArgs().Contains("--step-trigger-tabs-only")) - { await CheckStepTriggerTabs();_exit=0;return; } + { await CheckStepTriggerTabs(app,lifetime);_exit=0;return; } if(Environment.GetCommandLineArgs().Contains("--runtime-settings-only")) { var model=app.Container.Resolve(); @@ -1425,24 +1425,34 @@ internal static class Smoke Check(owner.IsVisible&&buttons.Length==2,"关闭保存提示仅有保存并关闭和不保存两个按钮"); buttons.Single(b=>b.Name==buttonName).RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent)); } - private static async Task CheckStepTriggerTabs() + private static async Task CheckStepTriggerTabs(App app,IClassicDesktopStyleApplicationLifetime lifetime) { - var model=new RecipeEditorViewModel(); - model.Recipe.AddRegion("work");model.Recipe.AddRegion("finish"); - var first=model.Recipe.AddStep();first.Detection.Method="hand_in_region"; - first.Detection.Region="work";first.Detection.FromRegion="work";first.Detection.ToRegion="finish"; - first.Detection.Hand="left";first.AiImageMode="fixed_region";first.AiImageRegion="work"; - var second=model.Recipe.AddStep();second.Detection.Method="time_elapsed"; - model.Recipe.SelectedStep=first; - var editor=new RecipeEditor{DataContext=model}; - var dialog=new Window{Content=editor,Width=1040,Height=900};dialog.Show();await Task.Delay(60); + var recipe=new RecipeNode(); + recipe.AddRegion("work");recipe.AddRegion("finish"); + var step=recipe.AddStep();step.Detection.Method="hand_in_region"; + step.Detection.Region="work";step.Detection.FromRegion="work";step.Detection.ToRegion="finish"; + step.Detection.Hand="left";step.AiImageMode="fixed_region";step.AiImageRegion="work"; + recipe.AddStep().Detection.Method="time_elapsed"; + var config=recipe.ToConfig();config.Sop.Name="区域初始选择"; + app.Container.Resolve().ShowDialog("RecipeEditorDialog",new DialogParameters + {{"directory",new RecipeWorkspace(Environment.GetEnvironmentVariable("SOPHELPER_WORKSPACE_ROOT")!).RecipesDirectory}, + {"config",config},{"new",true}},_=>{}); + await Task.Delay(60); + var dialog=lifetime.Windows.OfType().Single(); + var editor=dialog.GetVisualDescendants().OfType().Single(); + var model=(RecipeEditorViewModel)editor.DataContext!; + var first=model.Recipe.Steps[0];var second=model.Recipe.Steps[1]; try { var tabs=editor.GetVisualDescendants().OfType().First(); var picker=editor.FindControl("StepTriggerMethodPicker")!; var selections=editor.FindControl("StepParameters")!.GetVisualDescendants() .OfType().Select(c=>(Picker:c,Selected:c.SelectedItem)).ToArray(); - Check(selections.Length==7&&selections.All(s=>s.Selected is not null),"Step selectors initially display all configured values"); + Check(selections.Select(s=>s.Selected?.ToString()).SequenceEqual( + new[]{"hand_in_region","work","work","finish","left","fixed_region","work"}), + "Step selectors initially display all configured values"); + Check(editor.FindControl("TriggerRegionRow")!.GetVisualDescendants().OfType().Single().SelectedItem?.ToString()=="work", + "Opening a recipe through Prism immediately displays the configured trigger region"); void CheckSelection(string expected) { Check(picker.SelectedItem?.ToString()==expected,"Trigger selection survives tab changes: "+expected);