diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs index 37bea85..add569b 100644 --- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs +++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs @@ -29,6 +29,7 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware private Action? _onSaved; private readonly HashSet _observedStepNodes = []; private string _saveStatus = ""; + private string _closeBaselineYaml = ""; private bool _closingDialog; private bool _closeApproved; public Func>? ConfirmCloseAsync { get; set; } @@ -108,7 +109,7 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware /// 保存 public DelegateCommand SaveCommand { get; private set; } = null!; - /// 确认保存后关闭编辑器 + /// 有未保存修改时先确认,再关闭编辑器。 public DelegateCommand CloseCommand { get; private set; } = null!; /// @@ -161,6 +162,8 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware var fileName = string.IsNullOrEmpty(_path) ? config.Sop.Name : Path.GetFileNameWithoutExtension(_path); var recipe = RecipeNode.From(config, fileName, _isNew); + // 以编辑器映射后的内容为基准,避免旧配方迁移和新建默认值被当成修改。 + _closeBaselineYaml = SopConfigLoader.ToYaml(recipe.ToConfig()); var selectedStep = recipe.SelectedStep; // 先发布配方的下拉选项,再绑定步骤,避免区域选中值先于 ItemsSource 到达而被清空。 recipe.SelectedStep = null; @@ -176,7 +179,7 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware /// public bool CanCloseDialog() => _closeApproved; - /// 等待保存/放弃/取消选择;保存成功或明确放弃后先清理后台任务,再允许关闭。 + /// 仅在待写入的配方内容变更时询问是否保存,关闭前统一清理后台任务。 public async Task PrepareCloseAsync() { if (_closeApproved) return true; @@ -185,9 +188,12 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware CloseCommand.RaiseCanExecuteChanged(); try { - var choice = ConfirmCloseAsync is { } confirm ? await confirm() : RecipeCloseChoice.Cancel; - if (choice == RecipeCloseChoice.Cancel) return false; - if (choice == RecipeCloseChoice.Save && !TrySave()) return false; + if (SopConfigLoader.ToYaml(Recipe.ToConfig()) != _closeBaselineYaml) + { + var choice = ConfirmCloseAsync is { } confirm ? await confirm() : RecipeCloseChoice.Cancel; + if (choice == RecipeCloseChoice.Cancel) return false; + if (choice == RecipeCloseChoice.Save && !TrySave()) return false; + } await ShutdownAsync(); _closeApproved = true; return true; @@ -340,7 +346,10 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware if (_isNew && File.Exists(path)) throw new IOException("同名配方已存在,请使用其他配方名"); Directory.CreateDirectory(Path.GetDirectoryName(path)!); var config = Recipe.ToConfig(); + var baselineYaml = SopConfigLoader.ToYaml(config); var saved = SopConfigLoader.Save(config, path); + // 只有落盘并回读成功才更新基准,保存失败时仍保留关闭确认。 + _closeBaselineYaml = baselineYaml; _path = path; _isNew = false; diff --git a/tests/VideoFlowTest.Smoke/Program.cs b/tests/VideoFlowTest.Smoke/Program.cs index e551aa5..744d231 100644 --- a/tests/VideoFlowTest.Smoke/Program.cs +++ b/tests/VideoFlowTest.Smoke/Program.cs @@ -34,6 +34,8 @@ internal static class Program if (args.Contains("--live")) { LiveVideoFlowChecks.Run(args); return; } if (args.Contains("--agent-generation-only")) { AgentDraftGenerationChecks.Run(root); return; } if (args.Contains("--optimization-only")) { OptimizeStepsChecks.Run(); return; } + // 独立验证配方关闭流程,避免受标准视频编解码环境影响。 + if (args.Contains("--recipe-close-only")) { RecipeCloseChecks.Run(root); return; } // 单独验证步骤交换和 Excel 导出,无需先执行视频判定用例。 if (args.Contains("--step-transfer-only")) { StepTransferChecks.Run(root); return; } CheckLegacyRecipeRoundTrip(); diff --git a/tests/VideoFlowTest.Smoke/RecipeCloseChecks.cs b/tests/VideoFlowTest.Smoke/RecipeCloseChecks.cs index e472ef1..765e55b 100644 --- a/tests/VideoFlowTest.Smoke/RecipeCloseChecks.cs +++ b/tests/VideoFlowTest.Smoke/RecipeCloseChecks.cs @@ -22,7 +22,22 @@ internal static class RecipeCloseChecks return model; } + // 新建默认值、回退到原值及手动保存后的内容,都应直接关闭。 + void CloseWithoutPrompt(RecipeEditorViewModel editor) + { + var cleanWindow = new Window { Content = new RecipeEditor { DataContext = editor } }; + cleanWindow.Show(); + cleanWindow.Close(); + Until(() => !cleanWindow.IsVisible || cleanWindow.OwnedWindows.Any()); + Check(!cleanWindow.IsVisible && !cleanWindow.OwnedWindows.Any(), "没有未保存修改时直接关闭"); + editor.OnDialogClosed(); + } + + CloseWithoutPrompt(Create(true)); + Check(savedCalls == 0 && !File.Exists(store.ConfigPath(config.Sop.Name)), "未编辑的新配方直接关闭且不落盘"); + var model = Create(true); + model.Recipe.Description = "新配方内容"; var view = new RecipeEditor { DataContext = model }; var window = new Window { Content = view, Width = 1040, Height = 800 }; window.Show(); @@ -39,6 +54,18 @@ internal static class RecipeCloseChecks "确认保存后新配方落盘、回调一次并关闭窗口"); model.OnDialogClosed(); + model = Create(false); + var originalDescription = model.Recipe.Description; + model.Recipe.Description = "临时修改"; + model.Recipe.Description = originalDescription; + CloseWithoutPrompt(model); + + model = Create(false); + model.Recipe.Description = "手动保存"; + model.SaveCommand.Execute(); + CloseWithoutPrompt(model); + Check(savedCalls == 2, "手动保存后关闭不会重复写盘"); + var before = File.ReadAllBytes(store.ConfigPath(config.Sop.Name)); model = Create(false); model.Recipe.AddStepCommand.Execute(); model.Recipe.SelectedStep!.Description = "不应落盘"; @@ -48,7 +75,7 @@ internal static class RecipeCloseChecks window.OwnedWindows.Single().GetVisualDescendants().OfType