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

Commit 821b8dc

Browse files
Subclassing Tree in order to provide custom styling for a BranchTree
1 parent f98db05 commit 821b8dc

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class BranchesView : Subview
3939
[NonSerialized] private int listID = -1;
4040
[NonSerialized] private BranchesMode targetMode;
4141

42-
[SerializeField] private Tree treeLocals = new Tree();
43-
[SerializeField] private Tree treeRemotes = new Tree();
42+
[SerializeField] private BranchesTree treeLocals = new BranchesTree();
43+
[SerializeField] private BranchesTree treeRemotes = new BranchesTree();
4444
[SerializeField] private BranchesMode mode = BranchesMode.Default;
4545
[SerializeField] private string newBranchName;
4646
[SerializeField] private Vector2 scroll;
@@ -140,9 +140,9 @@ private void BuildTree()
140140
{
141141
if (treeLocals == null)
142142
{
143-
treeLocals = new Tree();
143+
treeLocals = new BranchesTree();
144144

145-
treeRemotes = new Tree();
145+
treeRemotes = new BranchesTree();
146146

147147
UpdateTreeIcons();
148148
}

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

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,44 @@
99
namespace GitHub.Unity
1010
{
1111
[Serializable]
12-
public class Tree
12+
public class BranchesTree: Tree
13+
{
14+
[NonSerialized] public Texture2D ActiveNodeIcon;
15+
[NonSerialized] public Texture2D NodeIcon;
16+
[NonSerialized] public Texture2D FolderIcon;
17+
[NonSerialized] public Texture2D RootFolderIcon;
18+
19+
protected override Texture2D GetNodeIcon(TreeNode node)
20+
{
21+
Texture2D nodeIcon;
22+
if (node.IsActive)
23+
{
24+
nodeIcon = ActiveNodeIcon;
25+
}
26+
else if (node.IsFolder)
27+
{
28+
if (node.Level == 1)
29+
nodeIcon = RootFolderIcon;
30+
else
31+
nodeIcon = FolderIcon;
32+
}
33+
else
34+
{
35+
nodeIcon = NodeIcon;
36+
}
37+
return nodeIcon;
38+
}
39+
}
40+
41+
[Serializable]
42+
public abstract class Tree
1343
{
1444
public static float ItemHeight { get { return EditorGUIUtility.singleLineHeight; } }
1545
public static float ItemSpacing { get { return EditorGUIUtility.standardVerticalSpacing; } }
1646

1747
[SerializeField] public Rect Margin = new Rect();
1848
[SerializeField] public Rect Padding = new Rect();
1949

20-
[NonSerialized] public Texture2D ActiveNodeIcon;
21-
[NonSerialized] public Texture2D NodeIcon;
22-
[NonSerialized] public Texture2D FolderIcon;
23-
[NonSerialized] public Texture2D RootFolderIcon;
24-
2550
[SerializeField] public GUIStyle FolderStyle;
2651
[SerializeField] public GUIStyle TreeNodeStyle;
2752
[SerializeField] public GUIStyle ActiveTreeNodeStyle;
@@ -82,7 +107,7 @@ public void Load(IEnumerable<ITreeData> data, string title)
82107
Level = 0,
83108
IsFolder = true
84109
};
85-
SetNodeIcons(titleNode);
110+
SetNodeIcon(titleNode);
86111
nodes.Add(titleNode);
87112

88113
foreach (var d in data)
@@ -110,7 +135,7 @@ public void Load(IEnumerable<ITreeData> data, string title)
110135
activeNode = node;
111136
}
112137

113-
SetNodeIcons(node);
138+
SetNodeIcon(node);
114139

115140
nodes.Add(node);
116141
if (isFolder)
@@ -369,32 +394,19 @@ private void Unindent()
369394
indents.Pop();
370395
}
371396

372-
private void SetNodeIcons(TreeNode node)
397+
private void SetNodeIcon(TreeNode node)
373398
{
374-
if (node.IsActive)
375-
{
376-
node.Icon = ActiveNodeIcon;
377-
}
378-
else if (node.IsFolder)
379-
{
380-
if (node.Level == 1)
381-
node.Icon = RootFolderIcon;
382-
else
383-
node.Icon = FolderIcon;
384-
}
385-
else
386-
{
387-
node.Icon = NodeIcon;
388-
}
389-
399+
node.Icon = GetNodeIcon(node);
390400
node.Load();
391401
}
392402

403+
protected abstract Texture2D GetNodeIcon(TreeNode node);
404+
393405
public void LoadNodeIcons()
394406
{
395407
foreach (var treeNode in nodes)
396408
{
397-
SetNodeIcons(treeNode);
409+
SetNodeIcon(treeNode);
398410
}
399411
}
400412
}

0 commit comments

Comments
 (0)