forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebuggerTreeView.cs
More file actions
317 lines (247 loc) · 10.7 KB
/
Copy pathDebuggerTreeView.cs
File metadata and controls
317 lines (247 loc) · 10.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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.UIElements.Debugger
{
internal class DebuggerTreeView : VisualElement
{
public bool hierarchyHasChanged { get; set; }
public IList<ITreeViewItem> treeRootItems
{
get
{
return m_TreeRootItems;
}
private set {}
}
public IEnumerable<ITreeViewItem> treeItems
{
get
{
return m_TreeView.items;
}
}
private IList<ITreeViewItem> m_TreeRootItems = new List<ITreeViewItem>();
private TreeView m_TreeView;
private HighlightOverlayPainter m_TreeViewHoverOverlay;
private VisualElement m_Container;
private DebuggerSearchBar m_SearchBar;
private Action<VisualElement> m_SelectElementCallback;
private List<VisualElement> m_SearchResultsHightlights;
private DebuggerSelection m_DebuggerSelection;
private IPanelDebug m_CurrentPanelDebug;
public DebuggerTreeView(DebuggerSelection debuggerSelection, Action<VisualElement> selectElementCallback)
{
this.focusable = true;
m_DebuggerSelection = debuggerSelection;
m_DebuggerSelection.onPanelDebugChanged += pdbg => RebuildTree(pdbg);
m_DebuggerSelection.onSelectedElementChanged += element => SelectElement(element, null);
m_SelectElementCallback = selectElementCallback;
hierarchyHasChanged = true;
m_SearchResultsHightlights = new List<VisualElement>();
this.RegisterCallback<FocusEvent>(e => m_TreeView?.Focus());
m_TreeViewHoverOverlay = new HighlightOverlayPainter();
m_Container = new VisualElement();
m_Container.style.flexGrow = 1f;
Add(m_Container);
m_SearchBar = new DebuggerSearchBar(this);
Add(m_SearchBar);
}
public void DrawOverlay(MeshGenerationContext mgc)
{
m_TreeViewHoverOverlay.Draw(mgc);
}
private void ActivateSearchBar(ExecuteCommandEvent evt)
{
Debug.Log(evt.commandName);
if (evt.commandName == "Find")
m_SearchBar.Focus();
}
private void FillItem(VisualElement element, ITreeViewItem item)
{
element.Clear();
var target = (item as TreeViewItem<VisualElement>).data;
element.userData = target;
var labelCont = new VisualElement();
labelCont.AddToClassList("unity-debugger-tree-item-label-cont");
element.Add(labelCont);
var label = new Label(target.typeName);
label.AddToClassList("unity-debugger-tree-item-label");
label.AddToClassList("unity-debugger-tree-item-type");
labelCont.Add(label);
if (!string.IsNullOrEmpty(target.name))
{
var nameLabelCont = new VisualElement();
nameLabelCont.AddToClassList("unity-debugger-tree-item-label-cont");
element.Add(nameLabelCont);
var nameLabel = new Label("#" + target.name);
nameLabel.AddToClassList("unity-debugger-tree-item-label");
nameLabel.AddToClassList("unity-debugger-tree-item-name");
nameLabel.AddToClassList("unity-debugger-tree-item-name-label");
nameLabelCont.Add(nameLabel);
}
foreach (var ussClass in target.GetClasses())
{
var classLabelCont = new VisualElement();
classLabelCont.AddToClassList("unity-debugger-tree-item-label-cont");
element.Add(classLabelCont);
var classLabel = new Label("." + ussClass);
classLabel.AddToClassList("unity-debugger-tree-item-label");
classLabel.AddToClassList("unity-debugger-tree-item-classlist");
classLabel.AddToClassList("unity-debugger-tree-item-classlist-label");
classLabelCont.Add(classLabel);
}
}
private void HighlightItemInTargetWindow(VisualElement item)
{
m_TreeViewHoverOverlay.ClearOverlay();
m_TreeViewHoverOverlay.AddOverlay(item.userData as VisualElement);
m_CurrentPanelDebug?.MarkDirtyRepaint();
}
private void UnhighlightItemInTargetWindow(VisualElement item)
{
m_TreeViewHoverOverlay.ClearOverlay();
m_CurrentPanelDebug?.MarkDirtyRepaint();
}
public void RebuildTree(IPanelDebug panelDebug)
{
if (!hierarchyHasChanged && m_CurrentPanelDebug == panelDebug)
return;
m_CurrentPanelDebug = panelDebug;
m_Container.Clear();
int nextId = 1;
m_TreeRootItems.Clear();
var visualTree = panelDebug?.visualTree;
if (visualTree != null)
{
var rootItem = new TreeViewItem<VisualElement>(nextId++, visualTree);
m_TreeRootItems.Add(rootItem);
var childItems = new List<ITreeViewItem>();
AddTreeItemsForElement(childItems, visualTree, ref nextId);
rootItem.AddChildren(childItems);
}
Func<VisualElement> makeItem = () =>
{
var element = new VisualElement();
element.name = "unity-treeview-item-content";
element.RegisterCallback<MouseEnterEvent>((e) =>
{
HighlightItemInTargetWindow(e.target as VisualElement);
});
element.RegisterCallback<MouseLeaveEvent>((e) =>
{
UnhighlightItemInTargetWindow(e.target as VisualElement);
});
return element;
};
// Clear selection which would otherwise persist via view data persistence.
m_TreeView?.ClearSelection();
m_TreeView = new TreeView(m_TreeRootItems, 20, makeItem, FillItem);
m_TreeView.style.flexGrow = 1;
m_TreeView.onSelectionChange += items =>
{
if (m_SelectElementCallback == null)
return;
if (!items.Any())
{
m_SelectElementCallback(null);
return;
}
var item = items.First() as TreeViewItem<VisualElement>;
var element = item != null ? item.data : null;
m_SelectElementCallback(element);
};
m_Container.Add(m_TreeView);
hierarchyHasChanged = false;
}
private TreeViewItem<VisualElement> FindElement(IEnumerable<ITreeViewItem> list, VisualElement element)
{
if (list == null)
return null;
foreach (var item in list)
{
var treeItem = item as TreeViewItem<VisualElement>;
if (treeItem.data == element)
return treeItem;
TreeViewItem<VisualElement> itemFoundInChildren = null;
if (treeItem.hasChildren)
itemFoundInChildren = FindElement(treeItem.children, element);
if (itemFoundInChildren != null)
return itemFoundInChildren;
}
return null;
}
public void ClearSearchResults()
{
foreach (var hl in m_SearchResultsHightlights)
hl.RemoveFromHierarchy();
m_SearchResultsHightlights.Clear();
}
public void SelectElement(VisualElement element, string query)
{
SelectElement(element, query, SearchHighlight.None);
}
public void SelectElement(VisualElement element, string query, SearchHighlight searchHighlight)
{
ClearSearchResults();
var item = FindElement(m_TreeRootItems, element);
if (item == null)
return;
m_TreeView.SetSelection(item.id);
if (string.IsNullOrEmpty(query))
return;
var selected = m_TreeView.Q(className: "unity-list-view__item--selected");
if (selected == null || searchHighlight == SearchHighlight.None)
return;
var content = selected.Q("unity-treeview-item-content");
var labelContainers = content.Query(classes: "unity-debugger-tree-item-label-cont").ToList();
foreach (var labelContainer in labelContainers)
{
var label = labelContainer.Q<Label>();
if (label.ClassListContains("unity-debugger-tree-item-type") && searchHighlight != SearchHighlight.Type)
continue;
if (label.ClassListContains("unity-debugger-tree-item-name") && searchHighlight != SearchHighlight.Name)
continue;
if (label.ClassListContains("unity-debugger-tree-item-classlist") && searchHighlight != SearchHighlight.Class)
continue;
var text = label.text;
var indexOf = text.IndexOf(query, StringComparison.OrdinalIgnoreCase);
if (indexOf < 0)
continue;
var highlight = new VisualElement();
m_SearchResultsHightlights.Add(highlight);
highlight.AddToClassList("unity-debugger-highlight");
int letterSize = 8;
highlight.style.width = query.Length * letterSize;
highlight.style.left = indexOf * letterSize;
labelContainer.Insert(0, highlight);
break;
}
}
private void AddTreeItemsForElement(IList<ITreeViewItem> items, VisualElement ve, ref int nextId)
{
if (ve == null)
return;
int count = ve.hierarchy.childCount;
if (count == 0)
return;
for (int i = 0; i < count; i++)
{
var child = ve.hierarchy[i];
var treeItem = new TreeViewItem<VisualElement>(nextId, child);
items.Add(treeItem);
nextId++;
var childItems = new List<ITreeViewItem>();
AddTreeItemsForElement(childItems, child, ref nextId);
if (childItems.Count == 0)
continue;
treeItem.AddChildren(childItems);
}
}
}
}