This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
/
ObjectGraphControl.xaml.cs
222 lines (192 loc) · 7.26 KB
/
ObjectGraphControl.xaml.cs
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
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the BSD license (for details please see \src\AddIns\Debugger\Debugger.AddIn\license.txt)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using Debugger.AddIn.TreeModel;
using Debugger.AddIn.Visualizers.Graph.Layout;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.SharpDevelop.Debugging;
using ICSharpCode.SharpDevelop.Services;
using Log = ICSharpCode.Core.LoggingService;
namespace Debugger.AddIn.Visualizers.Graph
{
/// <summary>
/// Interaction logic for ObjectGraphControl.xaml
/// </summary>
public partial class ObjectGraphControl : UserControl
{
WindowsDebugger debuggerService;
EnumViewModel<LayoutDirection> layoutViewModel;
ObjectGraph objectGraph;
ObjectGraphBuilder objectGraphBuilder;
PositionedGraph oldPosGraph;
PositionedGraph currentPosGraph;
GraphDrawer graphDrawer;
static double mouseWheelZoomSpeed = 0.05;
/// <summary> Long-lived map telling which graph nodes and content nodes the user expanded. </summary>
static Expanded expanded = new Expanded();
public ObjectGraphControl()
{
InitializeComponent();
debuggerService = DebuggerService.CurrentDebugger as WindowsDebugger;
if (debuggerService == null) throw new ApplicationException("Only windows debugger is currently supported");
this.layoutViewModel = new EnumViewModel<LayoutDirection>();
this.layoutViewModel.PropertyChanged += new PropertyChangedEventHandler(layoutViewModel_PropertyChanged);
this.cmbLayoutDirection.DataContext = this.layoutViewModel;
this.graphDrawer = new GraphDrawer(this.canvas);
}
public void Clear()
{
txtExpression.Text = string.Empty;
}
public void Refresh()
{
// Almost all of the blocking is done ContentPropertyNode.Evaluate,
// which is being called by WPF for all the properties.
// It would be better if we were filling the node texts ourselves in a loop,
// so that we could cancel any time. UI would redraw gradually thanks to INotifyPropertyChanged.
try {
Debugger.AddIn.TreeModel.Utils.DoEvents(debuggerService.DebuggedProcess);
} catch(AbortedBecauseDebuggeeResumedException) {
Log.Warn("Object graph - debuggee resumed, cancelling refresh.");
this.graphDrawer.ClearCanvas();
return;
}
ClearErrorMessage();
if (string.IsNullOrEmpty(txtExpression.Text)) {
this.graphDrawer.ClearCanvas();
return;
}
if (debuggerService.IsProcessRunning) {
// "Process not paused" exception still occurs
ErrorMessage("Cannot inspect when the process is running.");
return;
}
bool isSuccess = true;
try {
this.objectGraph = RebuildGraph(txtExpression.Text);
} catch(DebuggerVisualizerException ex) {
isSuccess = false;
ErrorMessage(ex.Message);
} catch(Debugger.GetValueException ex) {
isSuccess = false;
ErrorMessage("Expression cannot be evaluated - " + ex.Message);
}
if (isSuccess) {
LayoutGraph(this.objectGraph);
} else {
this.graphDrawer.ClearCanvas();
}
}
private ICSharpCode.NRefactory.Ast.Expression shownExpression;
public ICSharpCode.NRefactory.Ast.Expression ShownExpression
{
get {
return shownExpression;
}
set {
if (value == null) {
shownExpression = null;
txtExpression.Text = null;
Refresh();
return;
}
if (shownExpression == null || value.PrettyPrint() != shownExpression.PrettyPrint()) {
txtExpression.Text = value.PrettyPrint();
Refresh();
}
}
}
private void Inspect_Button_Click(object sender, RoutedEventArgs e)
{
this.Refresh();
}
ObjectGraph RebuildGraph(string expression)
{
this.objectGraphBuilder = new ObjectGraphBuilder(debuggerService);
Log.Debug("Debugger visualizer: Building graph for expression: " + txtExpression.Text);
return this.objectGraphBuilder.BuildGraphForExpression(expression, expanded.Expressions);
}
void LayoutGraph(ObjectGraph graph)
{
this.oldPosGraph = this.currentPosGraph;
Log.Debug("Debugger visualizer: Calculating graph layout");
var layoutDirection = layoutViewModel.SelectedEnumValue;
this.currentPosGraph = new TreeLayout(layoutDirection).CalculateLayout(graph, expanded);
Log.Debug("Debugger visualizer: Graph layout done");
RegisterExpandCollapseEvents(this.currentPosGraph);
var graphDiff = new GraphMatcher().MatchGraphs(oldPosGraph, currentPosGraph);
Log.Debug("Debugger visualizer: starting graph animation");
this.graphDrawer.StartAnimation(oldPosGraph, currentPosGraph, graphDiff);
}
void layoutViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "SelectedEnumValue") {
if (this.objectGraph != null) {
LayoutGraph(this.objectGraph);
}
}
}
void ClearErrorMessage()
{
this.pnlError.Visibility = Visibility.Collapsed;
}
void ErrorMessage(string message)
{
this.txtError.Text = message;
this.pnlError.Visibility = Visibility.Visible;
//MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
}
void RegisterExpandCollapseEvents(PositionedGraph posGraph)
{
foreach (var node in posGraph.Nodes) {
node.PropertyExpanded += new EventHandler<PositionedPropertyEventArgs>(node_PropertyExpanded);
node.PropertyCollapsed += new EventHandler<PositionedPropertyEventArgs>(node_PropertyCollapsed);
node.ContentNodeExpanded += new EventHandler<ContentNodeEventArgs>(node_ContentNodeExpanded);
node.ContentNodeCollapsed += new EventHandler<ContentNodeEventArgs>(node_ContentNodeCollapsed);
}
}
void node_ContentNodeExpanded(object sender, ContentNodeEventArgs e)
{
expanded.ContentNodes.SetExpanded(e.Node);
LayoutGraph(this.objectGraph);
}
void node_ContentNodeCollapsed(object sender, ContentNodeEventArgs e)
{
expanded.ContentNodes.SetCollapsed(e.Node);
LayoutGraph(this.objectGraph);
}
void node_PropertyExpanded(object sender, PositionedPropertyEventArgs e)
{
// remember this property is expanded (for later graph rebuilds)
expanded.Expressions.SetExpanded(e.Property.Expression);
// add edge (+ possibly nodes) to underlying object graph (no need to fully rebuild)
// TODO can add more nodes if they are expanded - now this adds always one node
e.Property.ObjectGraphProperty.TargetNode = this.objectGraphBuilder.ObtainNodeForExpression(e.Property.Expression);
LayoutGraph(this.objectGraph);
}
void node_PropertyCollapsed(object sender, PositionedPropertyEventArgs e)
{
// remember this property is collapsed (for later graph rebuilds)
expanded.Expressions.SetCollapsed(e.Property.Expression);
// just remove edge from underlying object graph (no need to fully rebuild)
e.Property.ObjectGraphProperty.TargetNode = null;
LayoutGraph(this.objectGraph);
}
void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control)) {
zoomSlider.Value += e.Delta > 0 ? mouseWheelZoomSpeed : -mouseWheelZoomSpeed;
}
}
}
}