| 1 | #nullable enable |
| 2 | using CascadeIDE.Cockpit.Graph.Layout; |
| 3 | using CascadeIDE.Models; |
| 4 | |
| 5 | namespace CascadeIDE.ViewModels; |
| 6 | |
| 7 | /// <summary>Мост <see cref="GraphLayoutScene"/> ↔ binding VM (ADR 0055 layout/render split).</summary> |
| 8 | public static class CodeNavigationMapGraphSceneProjection |
| 9 | { |
| 10 | public static CodeNavigationMapGraphSceneVm ToViewModel( |
| 11 | GraphLayoutScene scene, |
| 12 | double layoutViewportWidth = 0, |
| 13 | double layoutViewportHeight = 0, |
| 14 | CodeNavigationMapSettings? mapSettings = null, |
| 15 | string? solutionPath = null) |
| 16 | { |
| 17 | var branchLabels = CodeNavigationMapConditionBranchLabels.Resolve(mapSettings, solutionPath); |
| 18 | return new CodeNavigationMapGraphSceneVm |
| 19 | { |
| 20 | Nodes = scene.Nodes.Select(MapNode).ToList(), |
| 21 | Edges = scene.Edges.Select(e => MapEdge(e, branchLabels)).ToList(), |
| 22 | Presentation = MapPresentation(scene.Presentation), |
| 23 | Legend = scene.Legend.Select(e => new CodeNavigationMapLegendEntry { Index = e.Index, Text = e.Text }).ToList(), |
| 24 | UseLegendColumn = scene.UseLegendColumn, |
| 25 | ShowLegendConditionKey = scene.ShowLegendConditionKey, |
| 26 | ShowLegendReturnKey = scene.ShowLegendReturnKey, |
| 27 | ShowLegendExceptionFlowKey = scene.ShowLegendExceptionFlowKey, |
| 28 | ShowLegendEdgeStyleKey = scene.ShowLegendEdgeStyleKey, |
| 29 | LegendColumnLeft = scene.LegendColumnLeft, |
| 30 | LegendPlacement = MapLegendPlacement(scene.LegendPlacement), |
| 31 | LegendBlockTopY = scene.LegendBlockTopY, |
| 32 | HighlightedNodeIds = scene.HighlightedNodeIds, |
| 33 | HighlightedEdgeKeys = scene.HighlightedEdgeKeys, |
| 34 | SideLabelFontSizePx = scene.SideLabelFontSizePx, |
| 35 | ShowNodeLegendGlyphs = scene.ShowNodeLegendGlyphs, |
| 36 | RelatedFilesLayout = scene.RelatedFilesLayout, |
| 37 | ControlFlowMainAxis = scene.ControlFlowMainAxis, |
| 38 | LayoutViewportWidth = layoutViewportWidth, |
| 39 | LayoutViewportHeight = layoutViewportHeight |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | public static GraphLayoutScene FromViewModel(CodeNavigationMapGraphSceneVm scene) => |
| 44 | new() |
| 45 | { |
| 46 | Nodes = scene.Nodes.Select(ToLayoutNode).ToList(), |
| 47 | Edges = scene.Edges.Select(ToLayoutEdge).ToList(), |
| 48 | Presentation = MapPresentationToLayout(scene.Presentation), |
| 49 | Legend = scene.Legend.Select(e => new GraphLegendEntry { Index = e.Index, Text = e.Text }).ToList(), |
| 50 | UseLegendColumn = scene.UseLegendColumn, |
| 51 | ShowLegendConditionKey = scene.ShowLegendConditionKey, |
| 52 | ShowLegendReturnKey = scene.ShowLegendReturnKey, |
| 53 | ShowLegendExceptionFlowKey = scene.ShowLegendExceptionFlowKey, |
| 54 | ShowLegendEdgeStyleKey = scene.ShowLegendEdgeStyleKey, |
| 55 | LegendColumnLeft = scene.LegendColumnLeft, |
| 56 | LegendPlacement = MapLegendPlacementToLayout(scene.LegendPlacement), |
| 57 | LegendBlockTopY = scene.LegendBlockTopY, |
| 58 | HighlightedNodeIds = scene.HighlightedNodeIds, |
| 59 | HighlightedEdgeKeys = scene.HighlightedEdgeKeys, |
| 60 | SideLabelFontSizePx = scene.SideLabelFontSizePx, |
| 61 | ShowNodeLegendGlyphs = scene.ShowNodeLegendGlyphs, |
| 62 | RelatedFilesLayout = scene.RelatedFilesLayout, |
| 63 | ControlFlowMainAxis = scene.ControlFlowMainAxis |
| 64 | }; |
| 65 | |
| 66 | public static CodeNavigationMapGraphNodeLayout MapNode(GraphLayoutNode n) => |
| 67 | new() |
| 68 | { |
| 69 | Id = n.Id, |
| 70 | Kind = n.Kind, |
| 71 | FullPath = n.FullPath, |
| 72 | Label = n.Label, |
| 73 | Center = n.Center, |
| 74 | Radius = n.Radius, |
| 75 | IsAnchor = n.IsAnchor, |
| 76 | Shape = MapNodeShape(n.Shape), |
| 77 | LegendIndex = n.LegendIndex, |
| 78 | LegendLine = n.LegendLine, |
| 79 | LineStart = n.LineStart, |
| 80 | LineEnd = n.LineEnd, |
| 81 | LoopGroupId = n.LoopGroupId |
| 82 | }; |
| 83 | |
| 84 | public static CodeNavigationMapGraphEdgeLayout MapEdge( |
| 85 | GraphLayoutEdge e, |
| 86 | CodeNavigationMapConditionBranchLabels.Pair? branchLabels = null) => |
| 87 | new() |
| 88 | { |
| 89 | FromNodeId = e.FromNodeId, |
| 90 | ToNodeId = e.ToNodeId, |
| 91 | From = e.From, |
| 92 | To = e.To, |
| 93 | ToRadius = e.ToRadius, |
| 94 | Kind = e.Kind, |
| 95 | RelatedKind = e.RelationKind, |
| 96 | BranchLabel = branchLabels is null |
| 97 | ? e.BranchLabel |
| 98 | : CodeNavigationMapConditionBranchLabels.ResolveDisplayLabel(e.EdgeProvenance, branchLabels) |
| 99 | ?? e.BranchLabel |
| 100 | }; |
| 101 | |
| 102 | public static GraphLayoutNode ToLayoutNode(CodeNavigationMapGraphNodeLayout n) => |
| 103 | new() |
| 104 | { |
| 105 | Id = n.Id, |
| 106 | Kind = n.Kind, |
| 107 | FullPath = n.FullPath, |
| 108 | Label = n.Label, |
| 109 | Center = n.Center, |
| 110 | Radius = n.Radius, |
| 111 | IsAnchor = n.IsAnchor, |
| 112 | Shape = MapNodeShapeToLayout(n.Shape), |
| 113 | LegendIndex = n.LegendIndex, |
| 114 | LegendLine = n.LegendLine, |
| 115 | LineStart = n.LineStart, |
| 116 | LineEnd = n.LineEnd, |
| 117 | LoopGroupId = n.LoopGroupId |
| 118 | }; |
| 119 | |
| 120 | public static GraphLayoutEdge ToLayoutEdge(CodeNavigationMapGraphEdgeLayout e) => |
| 121 | new() |
| 122 | { |
| 123 | FromNodeId = e.FromNodeId, |
| 124 | ToNodeId = e.ToNodeId, |
| 125 | From = e.From, |
| 126 | To = e.To, |
| 127 | ToRadius = e.ToRadius, |
| 128 | Kind = e.Kind, |
| 129 | RelationKind = e.RelatedKind, |
| 130 | BranchLabel = e.BranchLabel |
| 131 | }; |
| 132 | |
| 133 | private static CodeNavigationMapGraphPresentationKind MapPresentation(GraphLayoutPresentation presentation) => |
| 134 | presentation == GraphLayoutPresentation.WorkspaceRelatedFiles |
| 135 | ? CodeNavigationMapGraphPresentationKind.WorkspaceRelatedFiles |
| 136 | : CodeNavigationMapGraphPresentationKind.CodeControlFlow; |
| 137 | |
| 138 | private static GraphLayoutPresentation MapPresentationToLayout(CodeNavigationMapGraphPresentationKind kind) => |
| 139 | kind == CodeNavigationMapGraphPresentationKind.WorkspaceRelatedFiles |
| 140 | ? GraphLayoutPresentation.WorkspaceRelatedFiles |
| 141 | : GraphLayoutPresentation.CodeControlFlow; |
| 142 | |
| 143 | private static CodeNavigationMapLegendBlockPlacement MapLegendPlacement(GraphLegendBlockPlacement placement) => |
| 144 | placement == GraphLegendBlockPlacement.BelowGraph |
| 145 | ? CodeNavigationMapLegendBlockPlacement.BelowGraph |
| 146 | : CodeNavigationMapLegendBlockPlacement.BesideGraph; |
| 147 | |
| 148 | private static GraphLegendBlockPlacement MapLegendPlacementToLayout(CodeNavigationMapLegendBlockPlacement placement) => |
| 149 | placement == CodeNavigationMapLegendBlockPlacement.BelowGraph |
| 150 | ? GraphLegendBlockPlacement.BelowGraph |
| 151 | : GraphLegendBlockPlacement.BesideGraph; |
| 152 | |
| 153 | private static CodeNavigationMapNodeShape MapNodeShape(GraphNodeShape shape) => |
| 154 | shape switch |
| 155 | { |
| 156 | GraphNodeShape.Condition => CodeNavigationMapNodeShape.Condition, |
| 157 | GraphNodeShape.Rectangle => CodeNavigationMapNodeShape.Rectangle, |
| 158 | _ => CodeNavigationMapNodeShape.Circle |
| 159 | }; |
| 160 | |
| 161 | private static GraphNodeShape MapNodeShapeToLayout(CodeNavigationMapNodeShape shape) => |
| 162 | shape switch |
| 163 | { |
| 164 | CodeNavigationMapNodeShape.Condition => GraphNodeShape.Condition, |
| 165 | CodeNavigationMapNodeShape.Rectangle => GraphNodeShape.Rectangle, |
| 166 | _ => GraphNodeShape.Circle |
| 167 | }; |
| 168 | } |
| 169 | |