修复配方编辑器无未保存修改时的退出提示

This commit is contained in:
wanglei 2026-11-28 16:35:44 +08:00
parent 79037a0e40
commit 8c4ab4cc71
3 changed files with 45 additions and 7 deletions

View File

@ -29,6 +29,7 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware
private Action<string>? _onSaved;
private readonly HashSet<EditorNode> _observedStepNodes = [];
private string _saveStatus = "";
private string _closeBaselineYaml = "";
private bool _closingDialog;
private bool _closeApproved;
public Func<Task<RecipeCloseChoice>>? ConfirmCloseAsync { get; set; }
@ -108,7 +109,7 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware
/// <summary>保存</summary>
public DelegateCommand SaveCommand { get; private set; } = null!;
/// <summary>确认保存后关闭编辑器</summary>
/// <summary>有未保存修改时先确认,再关闭编辑器。</summary>
public DelegateCommand CloseCommand { get; private set; } = null!;
/// <summary>
@ -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
/// <inheritdoc />
public bool CanCloseDialog() => _closeApproved;
/// <summary>等待保存/放弃/取消选择;保存成功或明确放弃后先清理后台任务,再允许关闭。</summary>
/// <summary>仅在待写入的配方内容变更时询问是否保存,关闭前统一清理后台任务。</summary>
public async Task<bool> 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;

View File

@ -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();

View File

@ -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<Button>().Single(b => b.Name == "DiscardRecipeAndCloseButton")
.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Until(() => !window.IsVisible);
Check(before.SequenceEqual(File.ReadAllBytes(store.ConfigPath(config.Sop.Name))) && savedCalls == 1,
Check(before.SequenceEqual(File.ReadAllBytes(store.ConfigPath(config.Sop.Name))) && savedCalls == 2,
"选择不保存时关闭窗口且保留原配方");
model.OnDialogClosed();
@ -63,7 +90,7 @@ internal static class RecipeCloseChecks
}
var retry = model.PrepareCloseAsync(); Pump(retry);
Check(retry.Result && SopConfigLoader.Load(store.ConfigPath(config.Sop.Name)).Sop.Steps.Single().Description == "确认后应保存"
&& savedCalls == 2, "解除占用后可以重试保存关闭,当前步骤修改持久化");
&& savedCalls == 3, "解除占用后可以重试保存关闭,当前步骤修改持久化");
model.OnDialogClosed();
Console.WriteLine("PASS: 关闭保存确认、两按钮弹窗、新建与现有配方、放弃修改和保存失败重试");
}