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

Commit 91b9afe

Browse files
Merge branch 'enhancements/tree-improvements' into enhancements/changes-tree-view
2 parents 45e24f5 + 2bf63ba commit 91b9afe

File tree

8 files changed

+221
-114
lines changed

8 files changed

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

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<Compile Include="Git\ManagedCacheExtensions.cs" />
107107
<Compile Include="Git\Tasks\GitLfsVersionTask.cs" />
108108
<Compile Include="Git\Tasks\GitVersionTask.cs" />
109+
<Compile Include="Git\TreeData.cs" />
109110
<Compile Include="Git\ValidateGitInstallResult.cs" />
110111
<Compile Include="Helpers\AssemblyResources.cs" />
111112
<Compile Include="Authentication\IKeychain.cs" />
@@ -237,6 +238,7 @@
237238
<Compile Include="UI\FileTreeNode.cs" />
238239
<Compile Include="UI\GitCommitTarget.cs" />
239240
<Compile Include="UI\TreeBuilder.cs" />
241+
<Compile Include="UI\TreeLoader.cs" />
240242
</ItemGroup>
241243
<Choose>
242244
<When Condition="$(Buildtype) == 'Internal'">
@@ -293,6 +295,7 @@
293295
<ItemGroup>
294296
<None Include="packages.config" />
295297
</ItemGroup>
298+
<ItemGroup />
296299
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
297300
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
298301
Other similar extension points exist, see Microsoft.Common.targets.

src/GitHub.Api/UI/TreeLoader.cs

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

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ private void BuildTree()
141141
if (treeLocals == null)
142142
{
143143
treeLocals = new BranchesTree();
144+
treeLocals.Title = LocalTitle;
144145

145146
treeRemotes = new BranchesTree();
147+
treeRemotes.Title = RemoteTitle;
146148
treeRemotes.IsRemote = true;
147149

148150
UpdateTreeIcons();
@@ -151,8 +153,8 @@ private void BuildTree()
151153
localBranches.Sort(CompareBranches);
152154
remoteBranches.Sort(CompareBranches);
153155

154-
treeLocals.Load(localBranches.Cast<ITreeData>(), LocalTitle);
155-
treeRemotes.Load(remoteBranches.Cast<ITreeData>(), RemoteTitle);
156+
TreeLoader.Load(treeLocals, localBranches.Select(branch => (ITreeData) new GitBranchTreeData(branch)));
157+
TreeLoader.Load(treeRemotes, remoteBranches.Select(branch => (ITreeData) new GitBranchTreeData(branch)));
156158
Redraw();
157159
}
158160

@@ -179,7 +181,7 @@ private void OnButtonBarGUI()
179181
{
180182
if (GUILayout.Button(DeleteBranchButton, EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
181183
{
182-
DeleteLocalBranch(treeLocals.SelectedNode.Name);
184+
DeleteLocalBranch(treeLocals.SelectedNode.Path);
183185
}
184186
}
185187
EditorGUI.EndDisabledGroup();
@@ -245,7 +247,7 @@ private void OnButtonBarGUI()
245247
// Effectuate create
246248
if (createBranch)
247249
{
248-
GitClient.CreateBranch(newBranchName, treeLocals.SelectedNode.Name)
250+
GitClient.CreateBranch(newBranchName, treeLocals.SelectedNode.Path)
249251
.FinallyInUI((success, e) =>
250252
{
251253
if (success)
@@ -299,7 +301,7 @@ private void OnTreeGUI(Rect rect)
299301
if (node.IsFolder)
300302
return;
301303

302-
SwitchBranch(node.Name);
304+
SwitchBranch(node.Path);
303305
},
304306
node => {
305307
if (node.IsFolder)
@@ -327,7 +329,7 @@ private void OnTreeGUI(Rect rect)
327329
if (node.IsFolder)
328330
return;
329331

330-
CheckoutRemoteBranch(node.Name);
332+
CheckoutRemoteBranch(node.Path);
331333
},
332334
node => {
333335
if (node.IsFolder)
@@ -364,11 +366,11 @@ private GenericMenu CreateContextMenuForLocalBranchNode(TreeNode node)
364366
else
365367
{
366368
genericMenu.AddItem(deleteGuiContent, false, () => {
367-
DeleteLocalBranch(node.Name);
369+
DeleteLocalBranch(node.Path);
368370
});
369371

370372
genericMenu.AddItem(switchGuiContent, false, () => {
371-
SwitchBranch(node.Name);
373+
SwitchBranch(node.Path);
372374
});
373375
}
374376

@@ -382,7 +384,7 @@ private GenericMenu CreateContextMenuForRemoteBranchNode(TreeNode node)
382384
var checkoutGuiContent = new GUIContent(CheckoutBranchContextMenuLabel);
383385

384386
genericMenu.AddItem(checkoutGuiContent, false, () => {
385-
CheckoutRemoteBranch(node.Name);
387+
CheckoutRemoteBranch(node.Path);
386388
});
387389

388390
return genericMenu;

0 commit comments

Comments
 (0)