From 3d6169a400fa335053b4631349cdf70501db12dc Mon Sep 17 00:00:00 2001
From: tfarcraw <123211521@163.com>
Date: Thu, 19 Nov 2026 10:34:36 +0800
Subject: [PATCH] =?UTF-8?q?=E6=AD=A5=E9=AA=A4=E7=95=8C=E9=9D=A2=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0=E9=85=8D=E7=BD=AE=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA?=
=?UTF-8?q?=E5=B9=B6=E4=BF=9D=E6=8A=A4=E5=8C=BA=E5=9F=9F=E5=BC=95=E7=94=A8?=
=?UTF-8?q?=E5=8F=8A=E7=8E=B0=E6=9C=89=E7=BC=96=E8=BE=91=E5=86=85=E5=AE=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../VVMs/RecipeEditor/RecipeEditor.axaml | 7 +
.../RecipeEditor/RecipeEditorViewModel.cs | 5 +
.../VVMs/RecipeEditor/StepTransferView.cs | 86 ++++++++++++
.../RecipeEditor/StepTransferViewModel.cs | 36 +++++
.../Sop/SopStepTransfer.cs | 128 ++++++++++++++++++
tests/VideoFlowTest.Smoke/Program.cs | 1 +
.../VideoFlowTest.Smoke/StepTransferChecks.cs | 79 +++++++++++
7 files changed, 342 insertions(+)
create mode 100644 src/SopHelper.Avalonia/VVMs/RecipeEditor/StepTransferView.cs
create mode 100644 src/SopHelper.Avalonia/VVMs/RecipeEditor/StepTransferViewModel.cs
create mode 100644 src/SopHelper.CrossPlatform.Engine/Sop/SopStepTransfer.cs
create mode 100644 tests/VideoFlowTest.Smoke/StepTransferChecks.cs
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
index 2eb4f55..80a3334 100644
--- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
@@ -111,6 +111,13 @@
+
+
+
+
+
+
+
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs
index 3875826..36a00f2 100644
--- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs
@@ -111,6 +111,10 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware
OpenImageCommand = new DelegateCommand(OpenImage, () => !IsCapturing);
CaptureStandardVideoImageCommand = new DelegateCommand(async () => await CaptureStandardVideoImageAsync(), () => !IsCapturing);
InitializeAgentCommands();
+ PropertyChanged += (_, e) =>
+ {
+ if (e.PropertyName is nameof(IsAgentBusy) or nameof(IsCapturing)) RaisePropertyChanged(nameof(CanTransferSteps));
+ };
}
///
@@ -286,6 +290,7 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware
foreach (var warning in saved.Warnings) Warnings.Add(warning);
SaveStatus = $"已保存 {Path.GetFileName(_path)}";
+ StepTransferStatus = "";
_onSaved?.Invoke(Path.GetFileName(_path));
}
catch (Exception ex)
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/StepTransferView.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/StepTransferView.cs
new file mode 100644
index 0000000..e48c9f4
--- /dev/null
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/StepTransferView.cs
@@ -0,0 +1,86 @@
+using Avalonia.Controls;
+using Avalonia.Interactivity;
+using Avalonia.Platform.Storage;
+using SopHelper.Engine.Sop;
+using System.IO;
+using System.Text;
+
+namespace SopHelper.VVMs.RecipeEditor;
+
+public partial class RecipeEditor
+{
+ private bool _stepTransferInProgress;
+ private async void ImportStepsFile(object? sender, RoutedEventArgs args)
+ {
+ if (_stepTransferInProgress || _model is not { CanTransferSteps: true } model || TopLevel.GetTopLevel(this) is not Window owner) return;
+ _stepTransferInProgress = true;
+ try
+ {
+ var files = await owner.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
+ {
+ Title = "导入步骤或配方文件", AllowMultiple = false,
+ FileTypeFilter = [new("SOP步骤 / 配方") { Patterns = ["*.yaml", "*.yml", "*.json"] }]
+ });
+ if (files.Count == 0 || _closing || !ReferenceEquals(model, _model)) return;
+ var path = files[0].TryGetLocalPath() ?? throw new IOException("请选择本地步骤文件");
+ if (new FileInfo(path).Length > 8 * 1024 * 1024) throw new IOException("步骤文件超过8MB");
+ var incoming = SopStepTransfer.Read(await File.ReadAllTextAsync(path));
+ var mode = model.Recipe.Steps.Count == 0 ? "replace" : await ChooseStepImportMode(incoming.Sop.Steps.Count, model.Recipe.Steps.Count);
+ if (mode is null || _closing || !ReferenceEquals(model, _model)) return;
+ model.ImportSteps(incoming, mode == "append");
+ }
+ catch (Exception error) { await ShowNotice("导入步骤失败", error.Message, false); }
+ finally { _stepTransferInProgress = false; }
+ }
+
+ private async void ExportStepsFile(object? sender, RoutedEventArgs args)
+ {
+ if (_stepTransferInProgress || _model is not { CanTransferSteps: true } model || TopLevel.GetTopLevel(this) is not Window owner) return;
+ _stepTransferInProgress = true;
+ string? temporary = null;
+ try
+ {
+ var text = model.ExportStepsText();
+ var count = model.Recipe.Steps.Count;
+ var name = string.Concat(model.Recipe.Name.Select(c => Path.GetInvalidFileNameChars().Contains(c) ? '_' : c));
+ var file = await owner.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
+ {
+ Title = "导出全部步骤", SuggestedFileName = name + ".steps.yaml", DefaultExtension = "yaml", ShowOverwritePrompt = true,
+ FileTypeChoices = [new("SOP步骤文件") { Patterns = ["*.yaml"] }]
+ });
+ if (file is null || _closing || !ReferenceEquals(model, _model)) return;
+ var path = file.TryGetLocalPath() ?? throw new IOException("请选择本地保存位置");
+ if (model.IsCurrentRecipeFile(path)) throw new IOException("步骤导出不能覆盖当前配方文件,请选择其他文件名");
+ temporary = Path.Combine(Path.GetDirectoryName(path)!, ".steps-export-" + Guid.NewGuid().ToString("N") + ".tmp");
+ await File.WriteAllTextAsync(temporary, text, new UTF8Encoding(false));
+ File.Move(temporary, path, overwrite: true);
+ model.StepTransferStatus = $"已导出 {count} 个步骤:{Path.GetFileName(path)}";
+ }
+ catch (Exception error) { await ShowNotice("导出步骤失败", error.Message, false); }
+ finally
+ {
+ try { if (temporary is not null && File.Exists(temporary)) File.Delete(temporary); }
+ catch (Exception cleanup) when (cleanup is IOException or UnauthorizedAccessException) { }
+ _stepTransferInProgress = false;
+ }
+ }
+
+ private async Task ChooseStepImportMode(int incoming, int current)
+ {
+ if (_closing || _dialog is not null || TopLevel.GetTopLevel(this) is not Window owner) return null;
+ var actions = new StackPanel { Orientation = Avalonia.Layout.Orientation.Horizontal, HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Right, Spacing = 8 };
+ var dialog = new Window { Title = "导入步骤", Width = 500, SizeToContent = SizeToContent.Height, CanResize = false, WindowStartupLocation = WindowStartupLocation.CenterOwner,
+ Content = new StackPanel { Margin = new Avalonia.Thickness(20), Spacing = 18, Children =
+ {
+ new TextBlock { Text = $"文件含 {incoming} 个步骤,当前有 {current} 个步骤。\n请选择追加或替换。关联区域将一并导入,同名但坐标不同的区域会自动改名。导入后可继续编辑,点击“保存”才写入配方。", TextWrapping = Avalonia.Media.TextWrapping.Wrap }, actions
+ } } };
+ foreach (var (label, mode) in new[] { ("取消", ""), ("追加步骤", "append"), ("替换步骤", "replace") })
+ {
+ var button = new Button { Content = label, IsCancel = mode.Length == 0, IsDefault = mode.Length == 0 };
+ button.Click += (_, _) => dialog.Close(mode); actions.Children.Add(button);
+ }
+ _dialog = dialog;
+ try { var result = await dialog.ShowDialog