using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media.Imaging; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using XFEWorkflow.Abstractions; using XFEWorkflow.Core.Runtime; using XFEWorkflow.Services; using Windows.System; namespace XFEWorkflow.Controls; public sealed partial class WorkflowCanvas { internal void VerifyTriggerPortLayout(string nodeId) { var ports = portElements.Where(x => x.Node == nodeId).ToArray(); if (ports.Any(x => !x.Output) || !ports.Any(x => x.Port == "next" && x.Kind == PortKind.Execution) || !ports.Any(x => x.Port == "event" && x.Kind == PortKind.Data) || !ports.Any(x => x.Port == "inputs")) throw new InvalidOperationException("触发节点应只有执行与事件数据输出,没有输入端口"); } internal async Task SmokeWirePresentationAsync(string output) { var start = new NodeDefinition { CapabilityId = "core.start", Name = "开始", X = 0, Y = 0 }; var end = new NodeDefinition { CapabilityId = "core.end", Name = "结束", X = 650, Y = 0 }; var cover = new NodeDefinition { CapabilityId = "core.delay", Name = "覆盖", X = 300, Y = 0 }; SetGraph(new() { Nodes = [start, end, cover], Edges = [new() { FromNode = start.Id, ToNode = end.Id }] }); await Task.Delay(200); CenterView(true); await Task.Delay(180); DrawEdges(); var from = portElements.Single(x => x.Node == start.Id && x.Port == "next"); var to = portElements.Single(x => x.Node == end.Id && x.Port == "in"); var a = PortCenter(from); var b = PortCenter(to); var wireColor = ((SolidColorBrush)from.Ring.Stroke).Color; async Task AssertPixel(Point world, Windows.UI.Color expected, string message, int tolerance = 8) { var bitmap = new RenderTargetBitmap(); await bitmap.RenderAsync(this); var pixels = (await bitmap.GetPixelsAsync()).ToArray(); var point = surface.TransformToVisual(this).TransformPoint(world); var x = (int)Math.Round(point.X * bitmap.PixelWidth / ActualWidth); var y = (int)Math.Round(point.Y * bitmap.PixelHeight / ActualHeight); if (x < 0 || y < 0 || x >= bitmap.PixelWidth || y >= bitmap.PixelHeight) throw new InvalidOperationException("连线像素检查点不在视口内"); var offset = (y * bitmap.PixelWidth + x) * 4; if (Math.Abs(pixels[offset] - expected.B) > tolerance || Math.Abs(pixels[offset + 1] - expected.G) > tolerance || Math.Abs(pixels[offset + 2] - expected.R) > tolerance) throw new InvalidOperationException($"{message}:RGB={pixels[offset + 2]},{pixels[offset + 1]},{pixels[offset]}"); } void MoveCover(double x, double y) { cover.X = x; cover.Y = y; Canvas.SetLeft(cardVisuals[cover.Id], x + OriginX); Canvas.SetTop(cardVisuals[cover.Id], y + OriginY); DrawEdges(); } if (lines.Any(x => x.StrokeStartLineCap != PenLineCap.Round || x.StrokeEndLineCap != PenLineCap.Round)) throw new InvalidOperationException("连线端头没有使用圆形线帽"); var endpoints = cardWires.Values.SelectMany(x => x.Children.OfType()).SelectMany(x => x.Children.OfType()).ToArray(); if (endpoints.Length != 2 || endpoints.Any(x => x.Width != x.Height || x.Width <= lines[0].StrokeThickness || x.Width >= from.Ring.Width - 4)) throw new InvalidOperationException("连线端点应为稍大于线宽、位于端口圆环内部的正圆"); // The endpoint's round cap extends into the ring instead of stopping // flat at its center; card-local terminals keep it visible there. await AssertPixel(new(a.X - .5, a.Y), wireColor, "圆环内部没有圆头连线", 75); var coveredPoint = new Point(OriginX + cover.X + 150, a.Y); MoveCover(300, a.Y - OriginY - 20); await Task.Delay(100); var header = Microsoft.UI.ColorHelper.FromArgb(255, 44, 53, 70); await AssertPixel(coveredPoint, header, "连线悬浮在遮挡卡片上方"); wireDrag = from; DrawWirePreview(b); if (wirePreview!.StrokeDashCap != PenLineCap.Round) throw new InvalidOperationException("预览虚线断点没有使用圆头"); await AssertPixel(coveredPoint, header, "拖线预览悬浮在遮挡卡片上方"); CancelWire(); await UiSmokeTest.CaptureWindowAsync(System.IO.Path.Combine(output, "wire-below-overlapping-card.png")); MoveCover(a.X - OriginX - 100, a.Y - OriginY - 20); await Task.Delay(100); await AssertPixel(a, header, "被卡片覆盖的端口连接仍然悬浮显示"); await UiSmokeTest.CaptureWindowAsync(System.IO.Path.Combine(output, "wire-covered-port.png")); MoveCover(300, 260); await Task.Delay(100); await AssertPixel(coveredPoint, wireColor, "移开卡片后连线没有恢复显示", 30); await UiSmokeTest.CaptureWindowAsync(System.IO.Path.Combine(output, "wire-rounded-terminals.png")); } internal async Task SmokeWiresAsync(string output) { var start = new NodeDefinition { CapabilityId = "core.start", Name = "开始", X = 0, Y = 0 }; var end = new NodeDefinition { CapabilityId = "core.end", Name = "结束", X = 400, Y = 100 }; SetGraph(new() { Nodes = [start, end] }); await Task.Delay(150); CenterView(true); await Task.Delay(180); PortAnchor Port(string node, string name) => portElements.Single(x => x.Node == node && x.Port == name); var from = Port(start.Id, "next"); var to = Port(end.Id, "in"); if (portElements.Any(x => Math.Abs(x.Ring.ActualWidth - x.Ring.ActualHeight) > .1 || x.Ring.StrokeThickness < 1 || x.Ring.Fill == x.Ring.Stroke)) throw new InvalidOperationException("端口不是空心圆环"); await UiSmokeTest.DragFromToAsync(from.Ring, to.Ring, async () => { if (wirePreview is null || Graph.Edges.Count != 0) throw new InvalidOperationException("拖线预览未出现或松开前已经连接"); await UiSmokeTest.CaptureWindowAsync(System.IO.Path.Combine(output, "wire-drag-preview.png")); }); if (Graph.Edges.Count != 1 || wirePreview is not null) throw new InvalidOperationException("鼠标松开后未连接端口或没有清理预览"); void AssertCenters() { var geometry = (PathGeometry)lines.Single().Data; var a = geometry.Figures[0].StartPoint; var b = ((BezierSegment)geometry.Figures[0].Segments[0]).Point3; var expectedA = PortCenter(from); var expectedB = PortCenter(to); if (Math.Abs(a.X - expectedA.X) + Math.Abs(a.Y - expectedA.Y) + Math.Abs(b.X - expectedB.X) + Math.Abs(b.Y - expectedB.Y) > .01) throw new InvalidOperationException("连线端点未位于端口圆心"); } AssertCenters(); Zoom(1.2f); await Task.Delay(160); DrawEdges(); AssertCenters(); await UiSmokeTest.CaptureWindowAsync(System.IO.Path.Combine(output, "wire-ring-centers.png")); Undo(); await Task.Delay(100); from = Port(start.Id, "next"); to = Port(end.Id, "in"); await UiSmokeTest.DragFromToAsync(from.Ring, to.Ring, () => { UiSmokeTest.PressKey(VirtualKey.Escape); return Task.Delay(120); }); if (Graph.Edges.Count != 0 || wirePreview is not null) throw new InvalidOperationException("Esc 没有取消拖线"); var dataTarget = Port(end.Id, "result"); await UiSmokeTest.DragFromToAsync(from.Ring, dataTarget.Ring); if (Graph.Edges.Count != 0) throw new InvalidOperationException("执行端口错误地连接到数据端口"); var dataFrom = Port(start.Id, "inputs"); await UiSmokeTest.DragFromToAsync(dataFrom.Ring, dataTarget.Ring); if (Graph.Edges.Count != 1 || Graph.Edges[0].Kind != PortKind.Data) throw new InvalidOperationException("数据端口无法拖线连接"); await UiSmokeTest.DragFromToAsync(to.Ring, from.Ring); if (Graph.Edges.Count != 2 || Graph.Edges.All(x => x.Kind != PortKind.Execution)) throw new InvalidOperationException("从输入端反向拖线无法连接"); } }