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

Commit 9ef7838

Browse files
Merge pull request #476 from github-for-unity/enhancements/tree-improvements
Tree Improvements
2 parents 7e19110 + 9a0caf9 commit 9ef7838

File tree

8 files changed

+366
-157
lines changed

8 files changed

+366
-157
lines changed

src/GitHub.Api/Git/GitBranch.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@
22

33
namespace GitHub.Unity
44
{
5-
public interface ITreeData
6-
{
7-
string Name { get; }
8-
bool IsActive { get; }
9-
}
10-
115
[Serializable]
12-
public struct GitBranch : ITreeData
6+
public struct GitBranch
137
{
148
public static GitBranch Default = new GitBranch();
159

src/GitHub.Api/Git/TreeData.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
3+
namespace GitHub.Unity
4+
{
5+
public interface ITreeData
6+
{
7+
string Path { get; }
8+
bool IsActive { get; }
9+
}
10+
11+
[Serializable]
12+
public struct GitBranchTreeData : ITreeData
13+
{
14+
public static GitBranchTreeData Default = new GitBranchTreeData(Unity.GitBranch.Default);
15+
16+
public GitBranch GitBranch;
17+
18+
public GitBranchTreeData(GitBranch gitBranch)
19+
{
20+
GitBranch = gitBranch;
21+
}
22+
23+
public string Path => GitBranch.Name;
24+
public bool IsActive => GitBranch.IsActive;
25+
}
26+
}

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Compile Include="Git\Tasks\GitAheadBehindStatusTask.cs" />
108108
<Compile Include="Git\Tasks\GitLfsVersionTask.cs" />
109109
<Compile Include="Git\Tasks\GitVersionTask.cs" />
110+
<Compile Include="Git\TreeData.cs" />
110111
<Compile Include="Git\ValidateGitInstallResult.cs" />
111112
<Compile Include="Helpers\AssemblyResources.cs" />
112113
<Compile Include="Authentication\IKeychain.cs" />
@@ -238,6 +239,7 @@
238239
<Compile Include="UI\FileTreeNode.cs" />
239240
<Compile Include="UI\GitCommitTarget.cs" />
240241
<Compile Include="UI\TreeBuilder.cs" />
242+
<Compile Include="UI\TreeLoader.cs" />
241243
</ItemGroup>
242244
<Choose>
243245
<When Condition="$(Buildtype) == 'Internal'">
@@ -294,6 +296,7 @@
294296
<ItemGroup>
295297
<None Include="packages.config" />
296298
</ItemGroup>
299+
<ItemGroup />
297300
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
298301
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
299302
Other similar extension points exist, see Microsoft.Common.targets.

src/GitHub.Api/UI/TreeLoader.cs

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

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,10 @@ private void BuildTree()
153153
if (treeLocals == null)
154154
{
155155
treeLocals = new BranchesTree();
156+
treeLocals.Title = LocalTitle;
156157

157158
treeRemotes = new BranchesTree();
159+
treeRemotes.Title = RemoteTitle;
158160
treeRemotes.IsRemote = true;
159161

160162
UpdateTreeIcons();
@@ -163,8 +165,8 @@ private void BuildTree()
163165
localBranches.Sort(CompareBranches);
164166
remoteBranches.Sort(CompareBranches);
165167

166-
treeLocals.Load(localBranches.Cast<ITreeData>(), LocalTitle);
167-
treeRemotes.Load(remoteBranches.Cast<ITreeData>(), RemoteTitle);
168+
TreeLoader.Load(treeLocals, localBranches.Select(branch => (ITreeData) new GitBranchTreeData(branch)));
169+
TreeLoader.Load(treeRemotes, remoteBranches.Select(branch => (ITreeData) new GitBranchTreeData(branch)));
168170
Redraw();
169171
}
170172

@@ -208,7 +210,7 @@ private void OnButtonBarGUI()
208210
{
209211
if (GUILayout.Button(DeleteBranchButton, EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
210212
{
211-
DeleteLocalBranch(treeLocals.SelectedNode.Name);
213+
DeleteLocalBranch(treeLocals.SelectedNode.Path);
212214
}
213215
}
214216
EditorGUI.EndDisabledGroup();
@@ -278,7 +280,7 @@ private void OnButtonBarGUI()
278280
// Effectuate create
279281
if (createBranch)
280282
{
281-
GitClient.CreateBranch(newBranchName, treeLocals.SelectedNode.Name)
283+
GitClient.CreateBranch(newBranchName, treeLocals.SelectedNode.Path)
282284
.FinallyInUI((success, e) =>
283285
{
284286
if (success)
@@ -326,7 +328,7 @@ private void OnTreeGUI(Rect rect)
326328
if(node.IsActive)
327329
return;
328330

329-
SwitchBranch(node.Name);
331+
SwitchBranch(node.Path);
330332
},
331333
node => {
332334
if (node.IsFolder)
@@ -354,7 +356,7 @@ private void OnTreeGUI(Rect rect)
354356
if (node.IsFolder)
355357
return;
356358

357-
CheckoutRemoteBranch(node.Name);
359+
CheckoutRemoteBranch(node.Path);
358360
},
359361
node => {
360362
if (node.IsFolder)
@@ -391,11 +393,11 @@ private GenericMenu CreateContextMenuForLocalBranchNode(TreeNode node)
391393
else
392394
{
393395
genericMenu.AddItem(deleteGuiContent, false, () => {
394-
DeleteLocalBranch(node.Name);
396+
DeleteLocalBranch(node.Path);
395397
});
396398

397399
genericMenu.AddItem(switchGuiContent, false, () => {
398-
SwitchBranch(node.Name);
400+
SwitchBranch(node.Path);
399401
});
400402
}
401403

@@ -409,7 +411,7 @@ private GenericMenu CreateContextMenuForRemoteBranchNode(TreeNode node)
409411
var checkoutGuiContent = new GUIContent(CheckoutBranchContextMenuLabel);
410412

411413
genericMenu.AddItem(checkoutGuiContent, false, () => {
412-
CheckoutRemoteBranch(node.Name);
414+
CheckoutRemoteBranch(node.Path);
413415
});
414416

415417
return genericMenu;

0 commit comments

Comments
 (0)