-
Notifications
You must be signed in to change notification settings - Fork 2
/
PropertyTree.cs
248 lines (220 loc) · 9.21 KB
/
PropertyTree.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
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
// Copyright (c) 2020, Dijji, and released under Ms-PL. This, with other relevant licenses, can be found in the root of this distribution.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
namespace CustomWindowsProperties
{
static class PropertyTree
{
public static Dictionary<string, TreeItem> PopulatePropertyTree(
IEnumerable<PropertyConfig> properties, ObservableCollection<TreeItem> treeItems,
bool isInstalled, Action<TreeItem, PropertyConfig> OnCreate = null)
{
Dictionary<string, TreeItem> dict = new Dictionary<string, TreeItem>();
// Build tree based on property names
foreach (var p in properties)
{
var treeItem = AddTreeItem(dict, treeItems, p);
OnCreate?.Invoke(treeItem, p);
}
// Populating installed tree - do some surgery on the system properties
if (isInstalled)
{
List<TreeItem> roots = new List<TreeItem>(treeItems);
treeItems.Clear();
List<TreeItem> lastRoots = new List<TreeItem>();
// Wire trees to tree controls, tweaking the structure as we go
TreeItem propGroup = null;
foreach (TreeItem root in
roots)
{
if (root.Name == "System")
{
lastRoots.Insert(0, root);
// Move property groups from root to their own subtree
propGroup = root.Children.Where(x => x.Name == "PropGroup").FirstOrDefault();
if (propGroup != null)
{
root.RemoveChild(propGroup);
lastRoots.Add(propGroup);
}
// Move properties with names of the form System.* to their own subtree
var systemProps = new TreeItem("System.*");
lastRoots.Insert(0, systemProps);
foreach (var ti in root.Children.Where(x => x.Children.Count == 0).ToList())
{
root.RemoveChild(ti);
systemProps.AddChild(ti);
}
}
else if (root.Name == "Microsoft")
lastRoots.Insert(0, root);
else
treeItems.Add(root);
}
foreach (var ti in lastRoots)
treeItems.Add(ti);
}
else
// Fully expand the saved tree
ExpandTree(treeItems);
return dict;
}
public static TreeItem FindTreeItem(string canonicalName, Dictionary<string, TreeItem> dictTree)
{
TreeItem result = null;
var parentName = FirstPartsOf(canonicalName);
if (parentName != null && dictTree.TryGetValue(parentName, out TreeItem parent))
{
result = parent.Children
.Where(t => (t.Item as PropertyConfig)?.CanonicalName == canonicalName)
.FirstOrDefault();
}
return result;
}
public static bool NameContainsExistingName(string name, ObservableCollection<TreeItem> roots, bool isEditor)
{
return NameContainsExistingNameInner(name, roots, isEditor);
}
private static bool NameContainsExistingNameInner(string name, ICollection<TreeItem> treeItems, bool isEditor)
{
var part = FirstPartOf(name, out string remainder);
var item = treeItems.Where(t => t.Name == part).FirstOrDefault();
if (item != null)
{
if (isEditor)
{
if ((item.Children.Count != 0 && remainder.Length == 0) || // Making a parent into a leaf
(item.Children.Count == 0 && remainder.Length > 0)) // Making a leap into a parent
return true;
}
else
{
// Everything is a clash
return true;
}
if (item.Children.Count > 0)
return NameContainsExistingNameInner(remainder, item.Children, isEditor);
}
return false;
}
// Top level entry point for the algorithm that builds the property name tree from an unordered sequence
// of property names
public static TreeItem AddTreeItem(Dictionary<string, TreeItem> dict,
ObservableCollection<TreeItem> roots, PropertyConfig pc, bool addRootTop = false)
{
Debug.Assert(pc.CanonicalName.Contains('.')); // Because the algorithm assumes that this is the case
TreeItem ti = AddTreeItemInner(dict, roots, pc.CanonicalName, addRootTop, pc.DisplayName);
ti.Item = pc;
return ti;
}
// Recurse backwards through each term in the property name, adding tree items as we go,
// until we join onto an existing part of the tree
private static TreeItem AddTreeItemInner(Dictionary<string, TreeItem> dict,
ObservableCollection<TreeItem> roots,
string name, bool addRootTop, string displayName = null)
{
TreeItem ti;
string parentName = FirstPartsOf(name);
if (parentName != null)
{
if (!dict.TryGetValue(parentName, out TreeItem parent))
{
parent = AddTreeItemInner(dict, roots, parentName, addRootTop);
dict.Add(parentName, parent);
}
if (displayName != null && displayName.Length > 0)
ti = new TreeItem($"{LastPartOf(name)} ({displayName})");
else
ti = new TreeItem(LastPartOf(name));
ti.Tag = name;
parent.AddChild(ti);
}
else
{
if (!dict.TryGetValue(name, out ti))
{
ti = new TreeItem(name)
{
Tag = name
};
if (addRootTop)
roots.Insert(0, ti);
else
roots.Add(ti);
}
}
return ti;
}
public static void ExpandTree(ObservableCollection<TreeItem> roots)
{
ExpandTreeInner(roots);
}
private static void ExpandTreeInner(ICollection<TreeItem> treeItems)
{
foreach (var treeItem in treeItems.Where(t => t.Children.Count > 0))
{
treeItem.IsExpanded = true;
ExpandTreeInner(treeItem.Children);
}
}
public static void RemoveTreeItem(Dictionary<string, TreeItem> dict, ObservableCollection<TreeItem> roots, string canonicalName)
{
// Takes care of the dictionary entries and the tree items
RemoveTreeItemInner(dict, roots, canonicalName);
}
// Returns true if the sought after tree item has been found and removed
private static bool RemoveTreeItemInner(Dictionary<string, TreeItem> dict, ICollection<TreeItem> treeItems, string canonicalName)
{
TreeItem toRemove = null;
foreach (var treeItem in treeItems)
{
if (treeItem.Children.Count == 0)
{
if (treeItem.Item is PropertyConfig config && config.CanonicalName == canonicalName)
{
toRemove = treeItem;
break;
}
}
else if (RemoveTreeItemInner(dict, treeItem.Children, canonicalName))
{
// If that parent is now empty, remove it too
if (treeItem.Children.Count == 0)
{
// Remove it from the dictionary of parents
dict.Remove(treeItem.Tag);
toRemove = treeItem;
}
else
return true;
}
}
if (toRemove != null)
{
treeItems.Remove(toRemove);
return true;
}
else
return false;
}
private static string FirstPartOf(string name, out string remainder)
{
int index = name.IndexOf('.');
remainder = index > 0 ? name.Substring(index + 1) : string.Empty;
return index >= 0 ? name.Substring(0, index) : name;
}
public static string FirstPartsOf(string name)
{
int index = name.LastIndexOf('.');
return index >= 0 ? name.Substring(0, index) : null;
}
private static string LastPartOf(string name)
{
int index = name.LastIndexOf('.');
return index >= 0 ? name.Substring(index + 1) : name;
}
}
}