forked from KirillOsenkov/MSBuildStructuredLog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphControl.cs
More file actions
697 lines (587 loc) · 20.7 KB
/
GraphControl.cs
File metadata and controls
697 lines (587 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Build.Logging.StructuredLogger;
namespace StructuredLogViewer.Controls;
public class GraphControl
{
private ScrollViewer scrollViewer;
private Grid grid;
private StackPanel layersControl;
private Canvas canvas;
private Dictionary<Vertex, FrameworkElement> controlFromVertex = new();
private HashSet<FrameworkElement> selectedControls = new();
private HashSet<Vertex> selectedVertices = new();
private Color outgoingColor, incomingColor, border;
private Brush outgoingBrush, incomingBrush;
public UIElement CanvasElement => grid;
public event Action SelectionChanged;
public GraphControl()
{
DarkTheme = SettingsService.UseDarkTheme;
layersControl = new StackPanel { Orientation = Orientation.Vertical };
canvas = new Canvas();
canvas.SetResourceReference(Panel.BackgroundProperty, "Theme_WhiteBackground");
grid = new Grid();
grid.Children.Add(canvas);
grid.Children.Add(layersControl);
scrollViewer = new ScrollViewer()
{
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
VerticalScrollBarVisibility = ScrollBarVisibility.Auto
};
scrollViewer.Content = grid;
}
private bool darkTheme;
public bool DarkTheme
{
get => darkTheme;
set
{
darkTheme = value;
if (DarkTheme)
{
outgoingColor = Colors.MediumOrchid;
border = Colors.DeepSkyBlue;
incomingColor = Colors.PaleGreen;
}
else
{
outgoingColor = Colors.MediumOrchid;
border = Colors.DarkCyan;
incomingColor = Colors.Green;
}
outgoingBrush = new LinearGradientBrush(outgoingColor, border, 90.0);
incomingBrush = new LinearGradientBrush(border, incomingColor, 90.0);
}
}
private Digraph graph;
public Digraph Digraph
{
get => graph;
set
{
if (graph != null)
{
Clear();
}
graph = value;
if (graph != null)
{
Populate();
}
}
}
public void Redraw()
{
Clear(clearSelection: false);
Populate();
}
private bool hideTransitiveEdges;
public bool HideTransitiveEdges
{
get => hideTransitiveEdges;
set
{
if (hideTransitiveEdges == value)
{
return;
}
hideTransitiveEdges = value;
SelectVertices(selectedVertices.ToArray());
}
}
private bool directReferencesOnly;
private Vertex focusVertex;
public bool DirectReferencesOnly
{
get => directReferencesOnly;
set
{
if (directReferencesOnly == value)
{
return;
}
directReferencesOnly = value;
// If enabling direct references only mode, set focus to first selected vertex
if (value && selectedVertices.Any())
{
focusVertex = selectedVertices.First();
}
else if (!value)
{
focusVertex = null;
}
Redraw();
}
}
private bool layerByDepth;
public bool LayerByDepth
{
get => layerByDepth;
set
{
if (layerByDepth == value)
{
return;
}
layerByDepth = value;
Redraw();
}
}
private bool horizontal;
public bool Horizontal
{
get => horizontal;
set
{
if (horizontal == value)
{
return;
}
horizontal = value;
Redraw();
}
}
private bool inverted;
public bool Inverted
{
get => inverted;
set
{
if (inverted == value)
{
return;
}
inverted = value;
Redraw();
}
}
private void Populate()
{
if (graph.IsEmpty)
{
return;
}
// Get vertices to display based on filtering mode
var verticesToDisplay = GetVerticesToDisplay();
if (!verticesToDisplay.Any())
{
return;
}
var maxHeight = verticesToDisplay.Max(g => g.Height);
var maxDepth = verticesToDisplay.Max(g => g.Depth);
var primaryOrientation = Orientation.Vertical;
var secondaryOrientation = Orientation.Horizontal;
if (Horizontal)
{
primaryOrientation = Orientation.Horizontal;
secondaryOrientation = Orientation.Vertical;
}
layersControl.Orientation = primaryOrientation;
Func<Vertex, int> groupBy = v => v.Height;
if (LayerByDepth)
{
groupBy = v => maxDepth - v.Depth;
}
var groups = verticesToDisplay.GroupBy(groupBy).OrderBy(g => g.Key).ToArray();
if (Inverted)
{
Array.Reverse(groups);
}
foreach (var vertexGroup in groups)
{
var layerPanel = new StackPanel { Orientation = secondaryOrientation };
foreach (var vertex in vertexGroup.OrderByDescending(s => s.InDegree).ThenBy(s => s.Title))
{
var depthOrHeight = vertex.Depth;
if (LayerByDepth)
{
depthOrHeight = maxHeight - vertex.Height;
}
var background = ComputeBackground(depthOrHeight);
var paddingHeight = Math.Pow(vertex.InDegree, 0.6);
var opacity = vertex.InDegree > 1 ? 0.9 : 0.5;
// Highlight the focus vertex if in direct references mode
if (DirectReferencesOnly && vertex == focusVertex)
{
opacity = 1.0;
background = DarkTheme ? Color.FromRgb(255, 140, 0) : Color.FromRgb(255, 255, 0); // Orange/Yellow highlight
}
var vertexControl = new TextBlock()
{
Text = vertex.Title.TrimQuotes(),
Margin = new Thickness(4, 2, 4, 2),
Padding = new Thickness(2, paddingHeight, 2, paddingHeight),
Background = new SolidColorBrush(background),
VerticalAlignment = VerticalAlignment.Center,
Opacity = opacity,
Tag = vertex
};
controlFromVertex[vertex] = vertexControl;
vertexControl.MouseDown += (s, args) =>
{
var vertex = GetVertex(vertexControl);
if (selectedVertices.Contains(vertex))
{
SelectVertices(selectedVertices.Where(c => c != vertex).ToArray());
}
else
{
if (Keyboard.Modifiers is ModifierKeys.Shift or ModifierKeys.Control)
{
SelectVertices(selectedVertices.Take(1).Append(vertex).ToArray());
}
else
{
SelectVertices([vertex]);
}
}
// In direct references mode, clicking a vertex recenters the graph on that vertex
if (DirectReferencesOnly && vertex != focusVertex)
{
focusVertex = vertex;
Redraw();
}
};
layerPanel.Children.Add(vertexControl);
}
layersControl.Children.Add(layerPanel);
}
SelectVertices(selectedVertices.ToArray());
}
private IEnumerable<Vertex> GetVerticesToDisplay()
{
if (!DirectReferencesOnly)
{
return graph.Vertices;
}
// If no focus vertex is set, use the first vertex with the highest in-degree (most connected)
// or fall back to the first vertex
if (focusVertex == null)
{
focusVertex = graph.Vertices
.OrderByDescending(v => v.InDegree + v.Outgoing.Count())
.ThenBy(v => v.Title)
.FirstOrDefault();
if (focusVertex == null)
{
return Enumerable.Empty<Vertex>();
}
}
// In direct references mode, show only the focus vertex and its directly connected vertices
var directlyConnected = new HashSet<Vertex>();
// Add the focus vertex itself
directlyConnected.Add(focusVertex);
// Add vertices that the focus vertex depends on (outgoing connections)
foreach (var outgoing in focusVertex.Outgoing)
{
directlyConnected.Add(outgoing);
}
// Add vertices that depend on the focus vertex (incoming connections)
foreach (var incoming in focusVertex.Incoming)
{
directlyConnected.Add(incoming);
}
return directlyConnected;
}
private Color ComputeBackground(int depth)
{
byte ratio, halfratio;
Color background;
if (DarkTheme)
{
ratio = (byte)Math.Min(150, depth * 6);
halfratio = (byte)Math.Min(100, depth * 4);
background = Color.FromRgb(40, halfratio, ratio);
}
else
{
ratio = (byte)Math.Max(200, 255 - depth * 2);
halfratio = (byte)Math.Max(224, 255 - depth);
background = Color.FromRgb(ratio, halfratio, 255);
}
return background;
}
Rect GetRectOnCanvas(FrameworkElement control)
{
return new Rect(
control.TranslatePoint(new Point(0, 0), canvas),
control.TranslatePoint(new Point(control.ActualWidth, control.ActualHeight), canvas)
);
}
void AddLine(Point sourcePoint, Point destinationPoint, Brush stroke)
{
var line = new System.Windows.Shapes.Line
{
X1 = sourcePoint.X,
Y1 = sourcePoint.Y,
X2 = destinationPoint.X,
Y2 = destinationPoint.Y,
Stroke = stroke,
};
canvas.Children.Add(line);
}
void AddLine(Rect sourceRect, Rect destinationRect, Brush stroke)
{
var sourceCenter = Center(sourceRect);
var destinationCenter = Center(destinationRect);
var sourcePoint = GetPointOnBoundary(sourceRect, destinationCenter);
var destinationPoint = GetPointOnBoundary(destinationRect, sourceCenter);
AddLine(sourcePoint, destinationPoint, stroke);
}
void AddLine(FrameworkElement fromControl, FrameworkElement toControl, Brush stroke)
{
var sourceRect = GetRectOnCanvas(fromControl);
var destinationRect = GetRectOnCanvas(toControl);
AddLine(sourceRect, destinationRect, stroke);
}
private Point Center(Rect rect) => new Point(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2);
private Point GetPointOnBoundary(Rect rect, Point outsidePoint)
{
var center = Center(rect);
var horizontalRatio = Math.Abs((outsidePoint.X - center.X) / (rect.Width / 2));
var verticalRatio = Math.Abs((outsidePoint.Y - center.Y) / (rect.Height / 2));
var horizontal = center.X + (outsidePoint.X - center.X) / verticalRatio;
if (Math.Abs(horizontal - center.X) <= rect.Width / 2)
{
if (outsidePoint.Y > center.Y)
{
return new Point(horizontal, rect.Bottom);
}
else
{
return new Point(horizontal, rect.Top);
}
}
var vertical = center.Y + (outsidePoint.Y - center.Y) / horizontalRatio;
if (outsidePoint.X > center.X)
{
return new Point(rect.Right, vertical);
}
else
{
return new Point(rect.Left, vertical);
}
}
void AddRectangle(FrameworkElement element, Brush stroke, Brush fill = null)
{
var rect = GetRectOnCanvas(element);
AddRectangle(rect, stroke, fill);
}
void AddRectangle(Rect rect, Brush stroke, Brush fill = null)
{
var rectangleShape = new System.Windows.Shapes.Rectangle
{
Width = rect.Width + 2,
Height = rect.Height + 2,
Stroke = stroke,
Fill = fill
};
Canvas.SetLeft(rectangleShape, rect.Left - 1);
Canvas.SetTop(rectangleShape, rect.Top - 1);
canvas.Children.Add(rectangleShape);
}
private void RaiseSelectionChanged()
{
SelectionChanged?.Invoke();
}
void SelectVertices(IEnumerable<Vertex> vertices)
{
selectedVertices.Clear();
if (vertices == null)
{
SelectControls(null);
return;
}
selectedVertices.UnionWith(vertices);
var controls = vertices.Select(GetControl).Where(c => c != null).ToArray();
SelectControls(controls);
}
void SelectControls(IEnumerable<FrameworkElement> controls)
{
canvas.Children.Clear();
selectedControls.Clear();
if (controls != null)
{
var toSelect = controls.Take(2).ToArray();
selectedControls.UnionWith(controls);
foreach (var control in toSelect)
{
control.UpdateLayout();
}
if (toSelect.Length == 1)
{
SelectControl(toSelect[0]);
}
else if (toSelect.Length == 2)
{
SelectControls(toSelect[0], toSelect[1]);
}
}
RaiseSelectionChanged();
}
private FrameworkElement GetControl(Vertex vertex)
{
controlFromVertex.TryGetValue(vertex, out var result);
return result;
}
private Vertex GetVertex(FrameworkElement control)
{
return (Vertex)control.Tag;
}
private void SelectControls(FrameworkElement fromControl, FrameworkElement toControl)
{
Vertex from = GetVertex(fromControl);
Vertex to = GetVertex(toControl);
if (from.Height < to.Height)
{
(from, to) = (to, from);
(fromControl, toControl) = (toControl, fromControl);
}
var highlighted = new HashSet<FrameworkElement>();
var edges = new HashSet<(FrameworkElement start, FrameworkElement end)>();
Digraph.FindAllPaths(from, to, path =>
{
for (int i = 0; i < path.Count; i++)
{
var control = GetControl(path[i]);
if (control != null)
{
highlighted.Add(control);
var target = i < path.Count - 1 ? path[i + 1] : to;
if (GetControl(target) is { } targetControl)
{
edges.Add((control, targetControl));
}
}
}
});
highlighted.Remove(fromControl);
highlighted.Remove(toControl);
foreach (var highlight in highlighted)
{
AddRectangle(highlight, new SolidColorBrush(Colors.Blue), Brushes.Azure);
}
AddRectangle(fromControl, Brushes.Red, Brushes.Pink);
AddRectangle(toControl, Brushes.Red, Brushes.Pink);
foreach (var edge in edges)
{
AddLine(edge.start, edge.end, outgoingBrush);
}
}
void SelectControl(FrameworkElement vertexControl)
{
var sourceRect = GetRectOnCanvas(vertexControl);
var vertex = GetVertex(vertexControl);
AddOutgoingEdges(sourceRect, vertex);
AddIncomingEdges(sourceRect, vertex);
AddRectangle(sourceRect, new SolidColorBrush(border), Brushes.PaleGreen);
}
private void AddIncomingEdges(Rect destinationRect, Vertex destinationVertex)
{
var visibleVertices = DirectReferencesOnly ? GetVerticesToDisplay().ToHashSet() : null;
foreach (var incoming in destinationVertex.Incoming)
{
if (HideTransitiveEdges &&
incoming.TransitiveOutgoing != null &&
incoming.TransitiveOutgoing.Contains(destinationVertex))
{
continue;
}
// In direct references mode, only show edges to visible vertices
if (DirectReferencesOnly && visibleVertices != null && !visibleVertices.Contains(incoming))
{
continue;
}
if (GetControl(incoming) is { } sourceControl)
{
var sourceRect = GetRectOnCanvas(sourceControl);
AddLine(sourceRect, destinationRect, incomingBrush);
AddRectangle(sourceRect, new SolidColorBrush(incomingColor));
}
}
}
private void AddOutgoingEdges(Rect sourceRect, Vertex node)
{
IEnumerable<Vertex> list = HideTransitiveEdges ? node.NonRedundantOutgoing : node.Outgoing;
var visibleVertices = DirectReferencesOnly ? GetVerticesToDisplay().ToHashSet() : null;
foreach (var outgoing in list)
{
// In direct references mode, only show edges to visible vertices
if (DirectReferencesOnly && visibleVertices != null && !visibleVertices.Contains(outgoing))
{
continue;
}
if (GetControl(outgoing) is { } destinationControl)
{
var destinationRect = GetRectOnCanvas(destinationControl);
AddLine(sourceRect, destinationRect, outgoingBrush);
AddRectangle(destinationRect, new SolidColorBrush(outgoingColor));
}
}
}
IEnumerable<TextBlock> AllTextBlocks()
{
foreach (var layer in layersControl.Children.OfType<Panel>())
{
foreach (var textBlock in layer.Children.OfType<TextBlock>())
{
yield return textBlock;
}
}
}
private void Clear(bool clearSelection = true)
{
if (clearSelection)
{
selectedVertices.Clear();
}
selectedControls.Clear();
controlFromVertex.Clear();
canvas.Children.Clear();
layersControl.Children.Clear();
}
public void Locate(string text)
{
if (string.IsNullOrWhiteSpace(text))
{
SelectVertices(null);
return;
}
var parts = text.Split([';'], StringSplitOptions.RemoveEmptyEntries);
var found = parts.Select(p => FindControlByText(p)).Where(c => c != null).Take(2).ToArray();
foreach (var foundControl in found)
{
foundControl.BringIntoView();
}
var foundVertices = found.Select(GetVertex).ToArray();
SelectVertices(foundVertices);
// In direct references mode, set focus to the first found vertex
if (DirectReferencesOnly && foundVertices.Any())
{
focusVertex = foundVertices.First();
Redraw();
}
}
private FrameworkElement FindControlByText(string text)
{
if (string.IsNullOrWhiteSpace(text))
{
return null;
}
text = text.Trim();
return AllTextBlocks()
.OrderBy(t => selectedControls.Contains(t))
.OrderBy(t => t.Text.Length)
.ThenBy(t => t.Text)
.FirstOrDefault(t => t.Text.ContainsIgnoreCase(text));
}
public Vertex SelectedVertex => SelectedVertices.FirstOrDefault();
public IReadOnlyList<Vertex> SelectedVertices => selectedVertices.ToArray();
public UIElement Content => scrollViewer;
}