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

Commit 2cec819

Browse files
Functionality to provide custom icon and icon badge for the changes tree view
1 parent 56a348c commit 2cec819

File tree

6 files changed

+93
-28
lines changed

6 files changed

+93
-28
lines changed

src/GitHub.Api/Git/GitStatusEntry.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,4 @@ public override string ToString()
4747
return $"Path:'{Path}' Status:'{Status}' FullPath:'{FullPath}' ProjectPath:'{ProjectPath}' OriginalPath:'{OriginalPath}' Staged:'{Staged}'";
4848
}
4949
}
50-
51-
[Serializable]
52-
public struct GitStatusEntryTreeData : ITreeData
53-
{
54-
public static GitStatusEntryTreeData Default = new GitStatusEntryTreeData(GitStatusEntry.Default);
55-
56-
public GitStatusEntry gitStatusEntry;
57-
58-
public GitStatusEntryTreeData(GitStatusEntry gitStatusEntry)
59-
{
60-
this.gitStatusEntry = gitStatusEntry;
61-
}
62-
63-
public string Name => gitStatusEntry.FullPath;
64-
public bool IsActive => false;
65-
public GitStatusEntry GitStatusEntry => gitStatusEntry;
66-
}
6750
}

src/GitHub.Api/Git/TreeData.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public interface ITreeData
66
{
77
string Path { get; }
88
bool IsActive { get; }
9+
string CustomStringTag { get;}
10+
int CustomIntTag { get; }
911
}
1012

1113
[Serializable]
@@ -22,5 +24,30 @@ public GitBranchTreeData(GitBranch gitBranch)
2224

2325
public string Path => GitBranch.Name;
2426
public bool IsActive => GitBranch.IsActive;
27+
28+
public string CustomStringTag => null;
29+
30+
public int CustomIntTag => 0;
31+
}
32+
33+
[Serializable]
34+
public struct GitStatusEntryTreeData : ITreeData
35+
{
36+
public static GitStatusEntryTreeData Default = new GitStatusEntryTreeData(GitStatusEntry.Default);
37+
38+
public GitStatusEntry gitStatusEntry;
39+
40+
public GitStatusEntryTreeData(GitStatusEntry gitStatusEntry)
41+
{
42+
this.gitStatusEntry = gitStatusEntry;
43+
}
44+
45+
public string Path => gitStatusEntry.ProjectPath;
46+
public bool IsActive => false;
47+
public GitStatusEntry GitStatusEntry => gitStatusEntry;
48+
49+
public string CustomStringTag => gitStatusEntry.ProjectPath;
50+
51+
public int CustomIntTag => (int)gitStatusEntry.Status;
2552
}
2653
}

src/GitHub.Api/UI/TreeLoader.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace GitHub.Unity
66
{
77
public interface ITree
88
{
9-
void AddNode(string path, string label, int level, bool isFolder, bool isActive, bool isHidden, bool isCollapsed);
9+
void AddNode(string path, string label, int level, bool isFolder, bool isActive, bool isHidden, bool isCollapsed, int customIntTag = 0, string customStringTag = (string)null);
1010
void Clear();
1111
HashSet<string> GetCollapsedFolders();
1212
string Title { get; }
@@ -58,6 +58,9 @@ public static void Load(ITree tree, IEnumerable<ITreeData> treeDatas)
5858
}
5959

6060
var nodeIsCollapsed = false;
61+
string customStringTag = null;
62+
var customIntTag = 0;
63+
6164
if (isFolder)
6265
{
6366
folders.Add(nodePath);
@@ -74,8 +77,13 @@ public static void Load(ITree tree, IEnumerable<ITreeData> treeDatas)
7477
}
7578

7679
}
80+
else
81+
{
82+
customStringTag = treeData.CustomStringTag;
83+
customIntTag = treeData.CustomIntTag;
84+
}
7785

78-
tree.AddNode(path: nodePath, label: label, level: i + displayRootLevel, isFolder: isFolder, isActive: treeData.IsActive, isHidden: nodeIsHidden, isCollapsed: nodeIsCollapsed);
86+
tree.AddNode(path: nodePath, label: label, level: i + displayRootLevel, isFolder: isFolder, isActive: treeData.IsActive, isHidden: nodeIsHidden, isCollapsed: nodeIsCollapsed, customStringTag: customStringTag, customIntTag: customIntTag);
7987
}
8088
}
8189
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ private void BuildTree()
190190
treeChanges.Title = "Changes";
191191
treeChanges.DisplayRootNode = false;
192192
treeChanges.IsCheckable = true;
193-
treeChanges.PathIgnoreRoot = Environment.RepositoryPath + Environment.FileSystem.DirectorySeparatorChar;
194193
treeChanges.PathSeparator = Environment.FileSystem.DirectorySeparatorChar.ToString();
195194

196195
UpdateTreeIcons();

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

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public string PathSeparator
7777
set { pathSeparator = value; }
7878
}
7979

80-
public void AddNode(string path, string label, int level, bool isFolder, bool isActive, bool isHidden, bool isCollapsed)
80+
public void AddNode(string path, string label, int level, bool isFolder, bool isActive, bool isHidden, bool isCollapsed, int customIntTag = 0, string customStringTag = (string)null)
8181
{
8282
var node = new TreeNode
8383
{
@@ -88,7 +88,9 @@ public void AddNode(string path, string label, int level, bool isFolder, bool is
8888
IsActive = isActive,
8989
IsHidden = isHidden,
9090
IsCollapsed = isCollapsed,
91-
TreeIsCheckable = IsCheckable
91+
TreeIsCheckable = IsCheckable,
92+
CustomStringTag = customStringTag,
93+
CustomIntTag = customIntTag
9294
};
9395

9496
SetNodeIcon(node);
@@ -391,10 +393,12 @@ private void Unindent()
391393
private void SetNodeIcon(TreeNode node)
392394
{
393395
node.Icon = GetNodeIcon(node);
396+
node.IconBadge = GetNodeIconBadge(node);
394397
node.Load();
395398
}
396399

397-
protected abstract Texture2D GetNodeIcon(TreeNode node);
400+
protected abstract Texture GetNodeIcon(TreeNode node);
401+
protected abstract Texture GetNodeIconBadge(TreeNode node);
398402

399403
protected void LoadNodeIcons()
400404
{
@@ -419,7 +423,11 @@ public class TreeNode
419423
public bool TreeIsCheckable;
420424
public CheckState CheckState;
421425

422-
[NonSerialized] public Texture2D Icon;
426+
public string CustomStringTag;
427+
public int CustomIntTag;
428+
429+
[NonSerialized] public Texture Icon;
430+
[NonSerialized] public Texture IconBadge;
423431

424432
public void Load()
425433
{
@@ -515,6 +523,17 @@ public TreeNodeRenderResult Render(Rect rect, float indentation, bool isSelected
515523
contentStyle.Draw(contentRect, content, false, false, styleOn, isSelected);
516524
}
517525

526+
if (IconBadge != null)
527+
{
528+
var statusRect = new Rect(
529+
contentRect.x + 6,
530+
contentRect.yMax - 7,
531+
9,
532+
9);
533+
534+
GUI.DrawTexture(statusRect, IconBadge);
535+
}
536+
518537
Debug.Log(data);
519538

520539
return renderResult;
@@ -537,7 +556,7 @@ public class BranchesTree : Tree
537556
[NonSerialized] public Texture2D FolderIcon;
538557
[NonSerialized] public Texture2D GlobeIcon;
539558

540-
protected override Texture2D GetNodeIcon(TreeNode node)
559+
protected override Texture GetNodeIcon(TreeNode node)
541560
{
542561
Texture2D nodeIcon;
543562
if (node.IsActive)
@@ -557,6 +576,10 @@ protected override Texture2D GetNodeIcon(TreeNode node)
557576
return nodeIcon;
558577
}
559578

579+
protected override Texture GetNodeIconBadge(TreeNode node)
580+
{
581+
return null;
582+
}
560583

561584
public void UpdateIcons(Texture2D activeBranchIcon, Texture2D branchIcon, Texture2D folderIcon, Texture2D globeIcon)
562585
{
@@ -578,20 +601,43 @@ public class ChangesTree : Tree
578601
{
579602
[NonSerialized] public Texture2D FolderIcon;
580603

581-
protected override Texture2D GetNodeIcon(TreeNode node)
604+
protected override Texture GetNodeIcon(TreeNode node)
582605
{
583-
Texture2D nodeIcon = null;
606+
Texture nodeIcon = null;
584607
if (node.IsFolder)
585608
{
586609
nodeIcon = FolderIcon;
587610
}
588611
else
589612
{
590-
//nodeIcon = AssetDatabase.GetCachedIcon(node.p);
613+
if (!string.IsNullOrEmpty(node.CustomStringTag))
614+
{
615+
nodeIcon = AssetDatabase.GetCachedIcon(node.CustomStringTag);
616+
}
617+
618+
if (nodeIcon != null)
619+
{
620+
nodeIcon.hideFlags = HideFlags.HideAndDontSave;
621+
}
622+
else
623+
{
624+
nodeIcon = Styles.DefaultAssetIcon;
625+
}
591626
}
627+
592628
return nodeIcon;
593629
}
594630

631+
protected override Texture GetNodeIconBadge(TreeNode node)
632+
{
633+
if (node.IsFolder)
634+
{
635+
return null;
636+
}
637+
638+
var gitFileStatus = (GitFileStatus)node.CustomIntTag;
639+
return Styles.GetFileStatusIcon(gitFileStatus, false);
640+
}
595641

596642
public void UpdateIcons(Texture2D activeBranchIcon, Texture2D branchIcon, Texture2D folderIcon, Texture2D globeIcon)
597643
{

src/tests/UnitTests/UI/TreeLoaderTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public class TestTreeData : ITreeData
88
public string FullPath { get; set; }
99
public string Path { get; set; }
1010
public bool IsActive { get; set; }
11+
public string CustomStringTag { get; set; }
12+
public int CustomIntTag { get; set; }
1113
}
1214

1315
[TestFixture]

0 commit comments

Comments
 (0)