Fix saving detection methods for steps without conditions

This commit is contained in:
sophelper 2026-11-05 23:47:10 +08:00
parent e739dcf35c
commit 15472311ff
5 changed files with 65 additions and 3 deletions

View File

@ -779,7 +779,8 @@ public class StepNode : EditorNode
private string _description = "";
private int _timeout;
private int _standardDurationMs;
private ConditionNode? _detection;
// 即使 YAML 没有 detection,也保留可编辑对象,供界面写入新选择的方法。
private ConditionNode? _detection = new();
/// <summary>步骤号(执行顺序按它升序)</summary>
public int Id

View File

@ -230,7 +230,7 @@
<TextBlock Text="判定条件" Style="{StaticResource Title}" FontSize="13" Margin="0,10,0,6" />
<StackPanel>
<TextBlock Text="本步无判定条件(只能靠超时推进)">
<TextBlock Text="请先选择判定方法;等待后完成请选择 time_elapsed">
<TextBlock.Style>
<Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="Visibility" Value="Collapsed" />

View File

@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Media;
@ -23,6 +24,7 @@ public class RecipeEditorViewModel : BindableBase, IDialogAware
private string _path = "";
private bool _isNew;
private Action<string>? _onSaved;
private readonly HashSet<EditorNode> _observedStepNodes = [];
private string _saveStatus = "";
public string SaveStatus { get => _saveStatus; private set => SetProperty(ref _saveStatus, value); }
@ -127,6 +129,8 @@ public class RecipeEditorViewModel : BindableBase, IDialogAware
public void OnDialogClosed()
{
Recipe.Changed -= RefreshWarnings;
foreach (var node in _observedStepNodes) node.PropertyChanged -= StepChanged;
_observedStepNodes.Clear();
_onSaved = null;
}
@ -243,6 +247,20 @@ public class RecipeEditorViewModel : BindableBase, IDialogAware
private void RefreshWarnings()
{
SaveStatus = "";
// 步骤和条件字段变化时同步校验,避免方法已选择却仍显示旧的缺失提示。
var current = new HashSet<EditorNode>(Recipe.Steps);
foreach (var step in Recipe.Steps)
if (step.Detection is not null) current.Add(step.Detection);
foreach (var removed in _observedStepNodes.Except(current).ToArray())
{
removed.PropertyChanged -= StepChanged;
_observedStepNodes.Remove(removed);
}
foreach (var added in current.Except(_observedStepNodes))
{
added.PropertyChanged += StepChanged;
_observedStepNodes.Add(added);
}
Warnings.Clear();
var config = Recipe.ToConfig();
@ -250,4 +268,6 @@ public class RecipeEditorViewModel : BindableBase, IDialogAware
foreach (var warning in config.Warnings) Warnings.Add(warning);
}
private void StepChanged(object? sender, PropertyChangedEventArgs e) => RefreshWarnings();
}

View File

@ -108,7 +108,7 @@ public static partial class SopConfigLoader
var condition = step.Detection;
if (condition is null)
{
config.Warnings.Add($"步骤 {step.Id}({step.Name})没有 detection 节点,只能靠超时推进");
config.Warnings.Add($"步骤 {step.Id}({step.Name})未设置完成条件,请选择判定方法;等待后完成请选择 time_elapsed");
continue;
}

View File

@ -74,6 +74,39 @@ internal static class Program
SopConfigLoader.Load(savePath).Sop.Name != "继续修改并保存" || Directory.GetFiles(saveDirectory).Length != 1)
throw new Exception("连续保存未更新原配方文件");
Console.WriteLine("PASS: 保存不请求关闭、编辑状态保留、新建转为编辑、连续保存更新同一文件并通知主页");
saveVm.Recipe.AddStepCommand.Execute();
if (!saveVm.Warnings.Any(w => w.Contains("未设置完成条件")))
throw new Exception("新增空步骤未提示设置完成条件");
var stepView = new RecipeEditor { DataContext = saveVm };
stepView.Measure(new Size(1020, 720));
stepView.Arrange(new Rect(0, 0, 1020, 720));
stepView.UpdateLayout();
SelectMethod(stepView, ConditionMethod.TimeElapsed);
if (saveVm.Warnings.Any(w => w.Contains("未设置完成条件")))
throw new Exception("选择方法后缺少判定的提示未实时更新");
saveVm.SaveCommand.Execute();
if (SopConfigLoader.Load(savePath).Sop.Steps.Single().Detection?.Method != ConditionMethod.TimeElapsed)
throw new Exception("新增步骤在界面选择方法后未保存");
SelectMethod(stepView, "");
if (!saveVm.Warnings.Any(w => w.Contains("未设置完成条件")))
throw new Exception("清空方法后未重新提示设置完成条件");
saveVm.SaveCommand.Execute();
var reopenedVm = new RecipeEditorViewModel();
reopenedVm.OnDialogOpened(new DialogParameters
{
{ "path", savePath }, { "config", SopConfigLoader.Load(savePath) },
});
stepView.DataContext = reopenedVm;
stepView.UpdateLayout();
SelectMethod(stepView, ConditionMethod.TimeElapsed);
reopenedVm.SaveCommand.Execute();
if (SopConfigLoader.Load(savePath).Sop.Steps.Single().Detection?.Method != ConditionMethod.TimeElapsed)
throw new Exception("未配置判定的步骤重新打开后选择方法无法保存");
var reopenedAgain = RecipeNode.From(SopConfigLoader.Load(savePath), "repeat-save", false);
if (reopenedAgain.SelectedStep?.Detection?.Method != ConditionMethod.TimeElapsed)
throw new Exception("保存后再次打开丢失判定方法");
reopenedVm.OnDialogClosed();
Console.WriteLine("PASS: 新增步骤和重新打开的空判定步骤,通过界面选择方法后均可保存");
}
finally
{
@ -172,6 +205,14 @@ internal static class Program
Console.WriteLine("PASS: 区域页布局、预览与所选区域绑定");
}
private static void SelectMethod(RecipeEditor view, string method)
{
var combo = Descendants(view).OfType<ComboBox>().Single(c =>
System.Windows.Data.BindingOperations.GetBinding(c, ComboBox.SelectedItemProperty)?.Path.Path == "Detection.Method");
combo.SetCurrentValue(ComboBox.SelectedItemProperty, method);
view.UpdateLayout();
}
private static IEnumerable<DependencyObject> Descendants(DependencyObject parent)
{
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)