XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text.Json.Nodes;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using XFEWorkflow.Abstractions;

namespace XFEWorkflow.Controls;

public sealed partial class WorkflowCanvas
{
    private readonly Dictionary<(string Node, string Port), WorkflowValueEditor> literalEditors = [];
    private WorkflowValueEditor? lastValueEditor;
    private DateTimeOffset lastValueChange;
    private bool lastValueStructural;
    private static bool InsideValueEditor(DependencyObject? source)
    { for (var item = source; item is not null; item = VisualTreeHelper.GetParent(item)) if (item is WorkflowValueEditor) return true; return false; }
    public void SetNodeValue(NodeDefinition node, string port, JsonNode? value, WorkflowValueEditor? source = null, bool notifySelection = false)
    {
        if (IsReadOnly || !Graph.Nodes.Contains(node) || node.Parameters.TryGetPropertyValue(port, out var previous) && JsonNode.DeepEquals(previous, value)) return;
        if (source is null || source.LastChangeStructural || lastValueStructural || !ReferenceEquals(lastValueEditor, source) || DateTimeOffset.UtcNow - lastValueChange > TimeSpan.FromSeconds(1)) Checkpoint();
        lastValueEditor = source; lastValueChange = DateTimeOffset.UtcNow; lastValueStructural = source?.LastChangeStructural == true;
        node.Parameters[port] = value?.DeepClone();
        if (literalEditors.TryGetValue((node.Id, port), out var editor) && !ReferenceEquals(source, editor)) editor.SetValue(value);
        DrawEdges(); Changed?.Invoke(Graph);
        if (notifySelection) SelectionChanged?.Invoke(node);
    }
    private void AddLiteralEditors(NodeDefinition node, StackPanel body)
    {
        if (!Catalog.TryGetValue(node.CapabilityId, out var descriptor) || !(node.CapabilityId.StartsWith("core.value.", StringComparison.Ordinal)
            && node.CapabilityId is not ("core.value.type" or "core.value.is" or "core.value.isNull" or "core.value.coalesce" or "core.value.select")
            || node.CapabilityId == "core.set" || node.CapabilityId.StartsWith("core.variable.", StringComparison.Ordinal))) return;
        foreach (var port in descriptor.Inputs)
        {
            if (Graph.Edges.Any(e => e.Kind == PortKind.Data && e.ToNode == node.Id && e.ToPort == port.Name)) continue;
            var value = node.Parameters.TryGetPropertyValue(port.Name, out var saved) ? saved : port.DefaultValue;
            if (value is JsonObject binding && binding.Any(p => p.Key.StartsWith('$'))) continue;
            var panel = new StackPanel { Spacing = 4, Margin = new(10, 6, 10, 3), RequestedTheme = ElementTheme.Dark };
            panel.Children.Add(new TextBlock { Text = port.Label, FontSize = 10, Opacity = .7 });
            if (IsReadOnly)
                panel.Children.Add(new TextBlock { Text = value?.Display(true) ?? "null", FontSize = 11, TextWrapping = TextWrapping.Wrap, MaxLines = 8 });
            else
            {
                var editor = new WorkflowValueEditor(value, port.Type, true); literalEditors[(node.Id, port.Name)] = editor;
                Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(editor, "NodeValue-" + node.Id + "-" + port.Name);
                editor.ValueChanged += next => SetNodeValue(node, port.Name, next, editor, true);
                editor.AddHandler(PointerPressedEvent, new PointerEventHandler((_, e) => { selected.Clear(); selected.Add(node.Id); UpdateSelection(); e.Handled = true; }), true);
                panel.Children.Add(editor);
            }
            body.Children.Add(panel);
        }
    }
}