Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit b908ac1

Browse files
Isolating TreeLoader
1 parent bcbb79a commit b908ac1

File tree

4 files changed

+99
-85
lines changed

4 files changed

+99
-85
lines changed

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
<Compile Include="UI\FileTreeNode.cs" />
239239
<Compile Include="UI\GitCommitTarget.cs" />
240240
<Compile Include="UI\TreeBuilder.cs" />
241+
<Compile Include="UI\TreeLoader.cs" />
241242
</ItemGroup>
242243
<Choose>
243244
<When Condition="$(Buildtype) == 'Internal'">
@@ -294,6 +295,7 @@
294295
<ItemGroup>
295296
<None Include="packages.config" />
296297
</ItemGroup>
298+
<ItemGroup />
297299
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
298300
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
299301
Other similar extension points exist, see Microsoft.Common.targets.

src/GitHub.Api/UI/TreeLoader.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace GitHub.Unity
6+
{
7+
public interface ITree
8+
{
9+
void AddNode(string fullPath, string path, string label, int level, bool isFolder, bool isActive, bool isHidden, bool isCollapsed);
10+
void Clear();
11+
HashSet<string> GetCollapsedFolders();
12+
bool DisplayRootNode { get; }
13+
bool IsCheckable { get; }
14+
string PathSeparator { get; }
15+
string PathIgnoreRoot { get; }
16+
}
17+
18+
public static class TreeLoader
19+
{
20+
public static void Load(ITree tree, IEnumerable<ITreeData> data, string title)
21+
{
22+
var collapsedFolders = tree.GetCollapsedFolders();
23+
24+
tree.Clear();
25+
26+
var displayRootLevel = tree.DisplayRootNode ? 1 : 0;
27+
28+
tree.AddNode(fullPath: title, path: title, label: title, level: -1 + displayRootLevel, isFolder: true, isActive: false, isHidden: false, isCollapsed: false);
29+
30+
var hideChildren = false;
31+
var hideChildrenBelowLevel = 0;
32+
33+
var folders = new HashSet<string>();
34+
35+
foreach (var d in data)
36+
{
37+
var path = d.Path;
38+
if (tree.PathIgnoreRoot != null)
39+
{
40+
var indexOf = path.IndexOf(tree.PathIgnoreRoot);
41+
if (indexOf != -1)
42+
{
43+
path = path.Substring(indexOf + tree.PathIgnoreRoot.Length);
44+
}
45+
}
46+
47+
var parts = path.Split(new[] { tree.PathSeparator }, StringSplitOptions.None);
48+
for (int i = 0; i < parts.Length; i++)
49+
{
50+
var label = parts[i];
51+
var level = i + 1;
52+
var nodePath = String.Join(tree.PathSeparator, parts, 0, level);
53+
var isFolder = i < parts.Length - 1;
54+
var alreadyExists = folders.Contains(nodePath);
55+
if (!alreadyExists)
56+
{
57+
var nodeIsHidden = false;
58+
if (hideChildren)
59+
{
60+
if (level <= hideChildrenBelowLevel)
61+
{
62+
hideChildren = false;
63+
}
64+
else
65+
{
66+
nodeIsHidden = true;
67+
}
68+
}
69+
70+
var nodeIsCollapsed = false;
71+
if (isFolder)
72+
{
73+
folders.Add(nodePath);
74+
75+
if (collapsedFolders.Contains(nodePath))
76+
{
77+
nodeIsCollapsed = true;
78+
79+
if (!hideChildren)
80+
{
81+
hideChildren = true;
82+
hideChildrenBelowLevel = level;
83+
}
84+
}
85+
86+
}
87+
88+
tree.AddNode(fullPath: d.FullPath, path: nodePath, label: label, level: i + displayRootLevel, isFolder: isFolder, isActive: d.IsActive, isHidden: nodeIsHidden, isCollapsed: nodeIsCollapsed);
89+
}
90+
}
91+
}
92+
}
93+
94+
}
95+
}

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ private void BuildTree()
151151
localBranches.Sort(CompareBranches);
152152
remoteBranches.Sort(CompareBranches);
153153

154-
Tree.Load(treeLocals, localBranches.Select(branch => (ITreeData) new GitBranchTreeData(branch)), LocalTitle);
155-
Tree.Load(treeRemotes, remoteBranches.Select(branch => (ITreeData) new GitBranchTreeData(branch)), RemoteTitle);
154+
TreeLoader.Load(treeLocals, localBranches.Select(branch => (ITreeData) new GitBranchTreeData(branch)), LocalTitle);
155+
TreeLoader.Load(treeRemotes, remoteBranches.Select(branch => (ITreeData) new GitBranchTreeData(branch)), RemoteTitle);
156156
Redraw();
157157
}
158158

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ namespace GitHub.Unity
1111
[Serializable]
1212
public class TreeNodeDictionary : SerializableDictionary<string, TreeNode> { }
1313

14-
public interface ITree
15-
{
16-
void AddNode(string fullPath, string path, string label, int level, bool isFolder, bool isActive, bool isHidden, bool isCollapsed);
17-
void Clear();
18-
HashSet<string> GetCollapsedFolders();
19-
bool DisplayRootNode { get; }
20-
bool IsCheckable { get; }
21-
string PathSeparator { get; }
22-
string PathIgnoreRoot { get; }
23-
}
2414

2515
[Serializable]
2616
public abstract class Tree: ITree
@@ -68,79 +58,6 @@ private set
6858
public string PathSeparator { get { return pathSeparator; } }
6959
public string PathIgnoreRoot { get { return pathIgnoreRoot; } }
7060

71-
public static void Load(ITree tree, IEnumerable<ITreeData> data, string title)
72-
{
73-
var collapsedFolders = tree.GetCollapsedFolders();
74-
75-
tree.Clear();
76-
77-
var displayRootLevel = tree.DisplayRootNode ? 1 : 0;
78-
79-
tree.AddNode(fullPath: title, path: title, label: title, level: -1 + displayRootLevel, isFolder: true, isActive: false, isHidden: false, isCollapsed: false);
80-
81-
var hideChildren = false;
82-
var hideChildrenBelowLevel = 0;
83-
84-
var folders = new HashSet<string>();
85-
86-
foreach (var d in data)
87-
{
88-
var path = d.Path;
89-
if (tree.PathIgnoreRoot != null)
90-
{
91-
var indexOf = path.IndexOf(tree.PathIgnoreRoot);
92-
if (indexOf != -1)
93-
{
94-
path = path.Substring(indexOf + tree.PathIgnoreRoot.Length);
95-
}
96-
}
97-
98-
var parts = path.Split(new [] { tree.PathSeparator }, StringSplitOptions.None);
99-
for (int i = 0; i < parts.Length; i++)
100-
{
101-
var label = parts[i];
102-
var level = i + 1;
103-
var nodePath = String.Join(tree.PathSeparator, parts, 0, level);
104-
var isFolder = i < parts.Length - 1;
105-
var alreadyExists = folders.Contains(nodePath);
106-
if (!alreadyExists)
107-
{
108-
var nodeIsHidden = false;
109-
if (hideChildren)
110-
{
111-
if (level <= hideChildrenBelowLevel)
112-
{
113-
hideChildren = false;
114-
}
115-
else
116-
{
117-
nodeIsHidden = true;
118-
}
119-
}
120-
121-
var nodeIsCollapsed = false;
122-
if (isFolder)
123-
{
124-
folders.Add(nodePath);
125-
126-
if (collapsedFolders.Contains(nodePath))
127-
{
128-
nodeIsCollapsed = true;
129-
130-
if (!hideChildren)
131-
{
132-
hideChildren = true;
133-
hideChildrenBelowLevel = level;
134-
}
135-
}
136-
137-
}
138-
139-
tree.AddNode(fullPath: d.FullPath, path: nodePath, label: label, level: i + displayRootLevel, isFolder: isFolder, isActive: d.IsActive, isHidden: nodeIsHidden, isCollapsed: nodeIsCollapsed);
140-
}
141-
}
142-
}
143-
}
14461

14562
public void AddNode(string fullPath, string path, string label, int level, bool isFolder, bool isActive, bool isHidden, bool isCollapsed)
14663
{

0 commit comments

Comments
 (0)