using System.Text.Json;
using System.Text.Json.Nodes;
using XFEWorkflow.Abstractions;
using XFEWorkflow.Controls;
namespace XFEWorkflow.Views;
public sealed partial class MainPage
{
private void ValidateValueEditors()
{ if (WorkflowValueEditor.HasInvalidValues(this)) throw new ArgumentException("请修正节点中无效的数字或字段名称"); }
private async Task ShowWorkflowSettingsAsync()
{
if (ViewModel.SelectedWorkflow is not { } workflow) return;
var description = new TextBox { Header = "工作流介绍", Text = workflow.Description, AcceptsReturn = true, TextWrapping = TextWrapping.Wrap, MaxHeight = 140 };
var variables = new WorkflowValueEditor(workflow.Variables, WorkflowValueType.Object);
var defaults = new WorkflowValueEditor(workflow.InputDefaults, WorkflowValueType.Object);
var declarations = new TextBox { Text = new JsonObject { ["inputs"] = JsonSerializer.SerializeToNode(workflow.Inputs, WorkflowJson.Options), ["outputs"] = JsonSerializer.SerializeToNode(workflow.Outputs, WorkflowJson.Options), ["pluginVersions"] = JsonSerializer.SerializeToNode(workflow.PluginVersions, WorkflowJson.Options) }.Display(true),
AcceptsReturn = true, TextWrapping = TextWrapping.NoWrap, FontFamily = new Microsoft.UI.Xaml.Media.FontFamily("Consolas"), MaxHeight = 420 };
var tabs = new Pivot { MinWidth = Math.Min(540, Math.Max(260, XamlRoot.Size.Width - 130)) };
tabs.Items.Add(new PivotItem { Header = "常规", Content = description });
tabs.Items.Add(new PivotItem { Header = "变量", Content = new ScrollViewer { Content = variables, MaxHeight = 420, HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled } });
tabs.Items.Add(new PivotItem { Header = "输入默认值", Content = new ScrollViewer { Content = defaults, MaxHeight = 420, HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled } });
tabs.Items.Add(new PivotItem { Header = "端口与插件", Content = declarations });
var error = new InfoBar { Severity = InfoBarSeverity.Error };
var panel = new StackPanel { Spacing = 8, Children = { tabs, error } };
var dialog = new ContentDialog { Title = "工作流设置", Content = panel, PrimaryButtonText = "应用", CloseButtonText = "取消", XamlRoot = XamlRoot };
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(dialog, "WorkflowTypedSettings");
dialog.PrimaryButtonClick += (_, e) =>
{
try
{
if (!variables.IsValid || !defaults.IsValid) throw new ArgumentException("请修正变量或默认输入中的无效值");
var value = JsonNode.Parse(declarations.Text)?.AsObject() ?? throw new JsonException("端口与插件声明不能为空");
var inputs = value["inputs"]?.Deserialize<List<PortDefinition>>(WorkflowJson.Options) ?? [];
var outputs = value["outputs"]?.Deserialize<List<PortDefinition>>(WorkflowJson.Options) ?? [];
var plugins = value["pluginVersions"]?.Deserialize<Dictionary<string, string>>(WorkflowJson.Options) ?? [];
workflow.Description = description.Text; workflow.Variables = variables.Value!.AsObject(); workflow.InputDefaults = defaults.Value!.AsObject();
workflow.Inputs = inputs; workflow.Outputs = outputs; workflow.PluginVersions = plugins;
RefreshNodeCatalog(); ViewModel.Status = "工作流设置已修改,请保存";
}
catch (Exception ex) when (ex is ArgumentException or JsonException or InvalidOperationException) { e.Cancel = true; error.Message = ex.Message; error.IsOpen = true; }
};
await dialog.ShowAsync();
}
}
using System.Text.Json;
using System.Text.Json.Nodes;
using XFEWorkflow.Abstractions;
using XFEWorkflow.Controls;
namespace XFEWorkflow.Views;
public sealed partial class MainPage
{
private void ValidateValueEditors()
{ if (WorkflowValueEditor.HasInvalidValues(this)) throw new ArgumentException("请修正节点中无效的数字或字段名称"); }
private async Task ShowWorkflowSettingsAsync()
{
if (ViewModel.SelectedWorkflow is not { } workflow) return;
var description = new TextBox { Header = "工作流介绍", Text = workflow.Description, AcceptsReturn = true, TextWrapping = TextWrapping.Wrap, MaxHeight = 140 };
var variables = new WorkflowValueEditor(workflow.Variables, WorkflowValueType.Object);
var defaults = new WorkflowValueEditor(workflow.InputDefaults, WorkflowValueType.Object);
var declarations = new TextBox { Text = new JsonObject { ["inputs"] = JsonSerializer.SerializeToNode(workflow.Inputs, WorkflowJson.Options), ["outputs"] = JsonSerializer.SerializeToNode(workflow.Outputs, WorkflowJson.Options), ["pluginVersions"] = JsonSerializer.SerializeToNode(workflow.PluginVersions, WorkflowJson.Options) }.Display(true),
AcceptsReturn = true, TextWrapping = TextWrapping.NoWrap, FontFamily = new Microsoft.UI.Xaml.Media.FontFamily("Consolas"), MaxHeight = 420 };
var tabs = new Pivot { MinWidth = Math.Min(540, Math.Max(260, XamlRoot.Size.Width - 130)) };
tabs.Items.Add(new PivotItem { Header = "常规", Content = description });
tabs.Items.Add(new PivotItem { Header = "变量", Content = new ScrollViewer { Content = variables, MaxHeight = 420, HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled } });
tabs.Items.Add(new PivotItem { Header = "输入默认值", Content = new ScrollViewer { Content = defaults, MaxHeight = 420, HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled } });
tabs.Items.Add(new PivotItem { Header = "端口与插件", Content = declarations });
var error = new InfoBar { Severity = InfoBarSeverity.Error };
var panel = new StackPanel { Spacing = 8, Children = { tabs, error } };
var dialog = new ContentDialog { Title = "工作流设置", Content = panel, PrimaryButtonText = "应用", CloseButtonText = "取消", XamlRoot = XamlRoot };
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(dialog, "WorkflowTypedSettings");
dialog.PrimaryButtonClick += (_, e) =>
{
try
{
if (!variables.IsValid || !defaults.IsValid) throw new ArgumentException("请修正变量或默认输入中的无效值");
var value = JsonNode.Parse(declarations.Text)?.AsObject() ?? throw new JsonException("端口与插件声明不能为空");
var inputs = value["inputs"]?.Deserialize<List<PortDefinition>>(WorkflowJson.Options) ?? [];
var outputs = value["outputs"]?.Deserialize<List<PortDefinition>>(WorkflowJson.Options) ?? [];
var plugins = value["pluginVersions"]?.Deserialize<Dictionary<string, string>>(WorkflowJson.Options) ?? [];
workflow.Description = description.Text; workflow.Variables = variables.Value!.AsObject(); workflow.InputDefaults = defaults.Value!.AsObject();
workflow.Inputs = inputs; workflow.Outputs = outputs; workflow.PluginVersions = plugins;
RefreshNodeCatalog(); ViewModel.Status = "工作流设置已修改,请保存";
}
catch (Exception ex) when (ex is ArgumentException or JsonException or InvalidOperationException) { e.Cancel = true; error.Message = ex.Message; error.IsOpen = true; }
};
await dialog.ShowAsync();
}
}