forked from microsoft/vs-threading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDgml.cs
More file actions
294 lines (240 loc) · 10.3 KB
/
Copy pathDgml.cs
File metadata and controls
294 lines (240 loc) · 10.3 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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.Threading
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
internal static class Dgml
{
/// <summary>
/// The namespace that all DGML nodes appear in.
/// </summary>
internal const string Namespace = "http://schemas.microsoft.com/vs/2009/dgml";
private static readonly XName NodeName = XName.Get("Node", Namespace);
private static readonly XName NodesName = XName.Get("Nodes", Namespace);
private static readonly XName LinkName = XName.Get("Link", Namespace);
private static readonly XName LinksName = XName.Get("Links", Namespace);
private static readonly XName StylesName = XName.Get("Styles", Namespace);
private static readonly XName StyleName = XName.Get("Style", Namespace);
internal static XDocument Create(out XElement nodes, out XElement links, string layout = "Sugiyama", string? direction = null)
{
var dgml = new XDocument();
dgml.Add(
new XElement(
XName.Get("DirectedGraph", Namespace),
new XAttribute("Layout", layout)));
if (direction is object)
{
dgml.Root.Add(new XAttribute("GraphDirection", direction));
}
nodes = new XElement(XName.Get("Nodes", Namespace));
links = new XElement(XName.Get("Links", Namespace));
dgml.Root.Add(nodes);
dgml.Root.Add(links);
dgml.WithCategories(Category("Contains", isContainment: true));
return dgml;
}
internal static XDocument WithCategories(this XDocument document, params string[] categories)
{
Requires.NotNull(document, nameof(document));
Requires.NotNull(categories, nameof(categories));
GetRootElement(document, "Categories").Add(categories.Select(c => Category(c)));
return document;
}
internal static XDocument WithCategories(this XDocument document, params XElement[] categories)
{
Requires.NotNull(document, nameof(document));
Requires.NotNull(categories, nameof(categories));
GetRootElement(document, "Categories").Add(categories);
return document;
}
internal static XElement Node(string? id = null, string? label = null, string? group = null)
{
var element = new XElement(NodeName);
if (!string.IsNullOrEmpty(id))
{
element.SetAttributeValue("Id", id);
}
if (!string.IsNullOrEmpty(label))
{
element.SetAttributeValue("Label", label);
}
if (!string.IsNullOrEmpty(group))
{
element.SetAttributeValue("Group", group);
}
return element;
}
internal static XDocument WithNode(this XDocument document, XElement node)
{
Requires.NotNull(document, nameof(document));
Requires.NotNull(node, nameof(node));
XElement? nodes = document.GetRootElement(NodesName);
nodes.Add(node);
return document;
}
internal static XElement Link(string source, string target)
{
Requires.NotNullOrEmpty(source, nameof(source));
Requires.NotNullOrEmpty(target, nameof(target));
return new XElement(
LinkName,
new XAttribute("Source", source),
new XAttribute("Target", target));
}
internal static XElement Link(XElement source, XElement target)
{
return Link(source.Attribute("Id").Value, target.Attribute("Id").Value);
}
internal static XDocument WithLink(this XDocument document, XElement link)
{
Requires.NotNull(document, nameof(document));
Requires.NotNull(link, nameof(link));
XElement? links = document.GetRootElement(LinksName);
links.Add(link);
return document;
}
internal static XElement Category(string id, string? label = null, string? background = null, string? foreground = null, string? icon = null, bool isTag = false, bool isContainment = false)
{
Requires.NotNullOrEmpty(id, nameof(id));
var category = new XElement(XName.Get("Category", Namespace), new XAttribute("Id", id));
if (!string.IsNullOrEmpty(label))
{
category.SetAttributeValue("Label", label);
}
if (!string.IsNullOrEmpty(background))
{
category.SetAttributeValue("Background", background);
}
if (!string.IsNullOrEmpty(foreground))
{
category.SetAttributeValue("Foreground", foreground);
}
if (!string.IsNullOrEmpty(icon))
{
category.SetAttributeValue("Icon", icon);
}
if (isTag)
{
category.SetAttributeValue("IsTag", "True");
}
if (isContainment)
{
category.SetAttributeValue("IsContainment", "True");
}
return category;
}
internal static XElement Comment(string label)
{
return Node(label: label).WithCategories("Comment");
}
internal static XElement Container(string id, string? label = null)
{
return Node(id, label, group: "Expanded");
}
internal static XDocument WithContainers(this XDocument document, IEnumerable<XElement> containers)
{
foreach (XElement? container in containers)
{
WithNode(document, container);
}
return document;
}
internal static XElement ContainedBy(this XElement node, XElement container)
{
Requires.NotNull(node, nameof(node));
Requires.NotNull(container, nameof(container));
Link(container, node).WithCategories("Contains");
return node;
}
internal static XElement ContainedBy(this XElement node, string containerId, XDocument document)
{
Requires.NotNull(node, nameof(node));
Requires.NotNullOrEmpty(containerId, nameof(containerId));
document.WithLink(Link(containerId, node.Attribute("Id").Value).WithCategories("Contains"));
return node;
}
/// <summary>
/// Adds categories to a DGML node or link.
/// </summary>
/// <param name="element">The node or link to add categories to.</param>
/// <param name="categories">The categories to add.</param>
/// <returns>The same node that was passed in. To enable "fluent" syntax.</returns>
internal static XElement WithCategories(this XElement element, params string[] categories)
{
Requires.NotNull(element, nameof(element));
foreach (var category in categories)
{
if (element.Attribute("Category") is null)
{
element.SetAttributeValue("Category", category);
}
else
{
element.Add(new XElement(
XName.Get("Category", Namespace),
new XAttribute("Ref", category)));
}
}
return element;
}
internal static XDocument WithStyle(this XDocument document, string categoryId, IEnumerable<KeyValuePair<string, string?>> properties, string targetType = "Node")
{
Requires.NotNull(document, nameof(document));
Requires.NotNullOrEmpty(categoryId, nameof(categoryId));
Requires.NotNull(properties, nameof(properties));
Requires.NotNullOrEmpty(targetType, nameof(targetType));
XElement? container = document.Root.Element(StylesName);
if (container is null)
{
document.Root.Add(container = new XElement(StylesName));
}
var style = new XElement(
StyleName,
new XAttribute("TargetType", targetType),
new XAttribute("GroupLabel", categoryId),
new XElement(XName.Get("Condition", Namespace), new XAttribute("Expression", "HasCategory('" + categoryId + "')")));
style.Add(properties.Select(p => new XElement(XName.Get("Setter", Namespace), new XAttribute("Property", p.Key), new XAttribute("Value", p.Value))));
container.Add(style);
return document;
}
internal static XDocument WithStyle(this XDocument document, string categoryId, string targetType = "Node", string? foreground = null, string? background = null, string? icon = null)
{
var properties = new Dictionary<string, string?>();
if (!string.IsNullOrEmpty(foreground))
{
properties.Add("Foreground", foreground);
}
if (!string.IsNullOrEmpty(background))
{
properties.Add("Background", background);
}
if (!string.IsNullOrEmpty(icon))
{
properties.Add("Icon", icon);
}
return WithStyle(document, categoryId, properties, targetType);
}
private static XElement GetRootElement(this XDocument document, XName name)
{
Requires.NotNull(document, nameof(document));
Requires.NotNull(name, nameof(name));
XElement? container = document.Root.Element(name);
if (container is null)
{
document.Root.Add(container = new XElement(name));
}
return container;
}
private static XElement GetRootElement(XDocument document, string elementName)
{
Requires.NotNull(document, nameof(document));
Requires.NotNullOrEmpty(elementName, nameof(elementName));
return GetRootElement(document, XName.Get(elementName, Namespace));
}
}
}