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

Commit 3dcc13e

Browse files
Merge branch 'fixes/branches-view-remove-serialized-texture2d' into fixes/branches-view-globe-icon
2 parents fd4b6ed + 821b8dc commit 3dcc13e

File tree

4 files changed

+144
-87
lines changed

4 files changed

+144
-87
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Utility.cs

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,6 @@
77

88
namespace GitHub.Unity
99
{
10-
[Serializable]
11-
public class SerializableTexture2D
12-
{
13-
[SerializeField] private byte[] bytes;
14-
[SerializeField] private int height;
15-
[SerializeField] private int width;
16-
[SerializeField] private TextureFormat format;
17-
[SerializeField] private bool mipmap;
18-
[SerializeField] private Texture2D texture;
19-
20-
public Texture2D Texture
21-
{
22-
get
23-
{
24-
if (texture == null)
25-
{
26-
texture = new Texture2D(width, height, format, mipmap);
27-
texture.LoadRawTextureData(bytes);
28-
texture.Apply();
29-
}
30-
return texture;
31-
}
32-
set
33-
{
34-
texture = value;
35-
bytes = value.GetRawTextureData();
36-
height = value.height;
37-
width = value.width;
38-
format = value.format;
39-
mipmap = value.mipmapCount > 1;
40-
}
41-
}
42-
}
43-
4410
class Utility : ScriptableObject
4511
{
4612
public static Texture2D GetIcon(string filename, string filename2x = "")
@@ -50,12 +16,25 @@ public static Texture2D GetIcon(string filename, string filename2x = "")
5016
filename = filename2x;
5117
}
5218

19+
Texture2D texture2D = null;
20+
5321
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("GitHub.Unity.IconsAndLogos." + filename);
5422
if (stream != null)
55-
return stream.ToTexture2D();
23+
{
24+
texture2D = stream.ToTexture2D();
25+
}
26+
else
27+
{
28+
var iconPath = EntryPoint.Environment.ExtensionInstallPath.Combine("IconsAndLogos", filename).ToString(SlashMode.Forward);
29+
texture2D = AssetDatabase.LoadAssetAtPath<Texture2D>(iconPath);
30+
}
31+
32+
if (texture2D != null)
33+
{
34+
texture2D.hideFlags = HideFlags.HideAndDontSave;
35+
}
5636

57-
var iconPath = EntryPoint.Environment.ExtensionInstallPath.Combine("IconsAndLogos", filename).ToString(SlashMode.Forward);
58-
return AssetDatabase.LoadAssetAtPath<Texture2D>(iconPath);
37+
return texture2D;
5938
}
6039

6140
public static Texture2D GetTextureFromColor(Color color)

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

Lines changed: 76 additions & 13 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;
@@ -62,6 +62,7 @@ public override void InitializeView(IView parent)
6262
public override void OnEnable()
6363
{
6464
base.OnEnable();
65+
UpdateTreeIcons();
6566
AttachHandlers(Repository);
6667
Repository.CheckLocalAndRemoteBranchListChangedEvent(lastLocalAndRemoteBranchListChangedEvent);
6768
}
@@ -137,25 +138,87 @@ private void Render()
137138

138139
private void BuildTree()
139140
{
141+
if (treeLocals == null)
142+
{
143+
treeLocals = new BranchesTree();
144+
145+
treeRemotes = new BranchesTree();
146+
147+
UpdateTreeIcons();
148+
}
149+
140150
localBranches.Sort(CompareBranches);
141151
remoteBranches.Sort(CompareBranches);
142-
treeLocals = new Tree();
143-
treeLocals.ActiveNodeIcon = Styles.ActiveBranchIcon;
144-
treeLocals.NodeIcon = Styles.BranchIcon;
145-
treeLocals.RootFolderIcon = Styles.RootFolderIcon;
146-
treeLocals.FolderIcon = Styles.FolderIcon;
147-
148-
treeRemotes = new Tree();
149-
treeRemotes.ActiveNodeIcon = Styles.ActiveBranchIcon;
150-
treeRemotes.NodeIcon = Styles.BranchIcon;
151-
treeRemotes.RootFolderIcon = Styles.RootFolderIcon;
152-
treeRemotes.FolderIcon = Styles.FolderIcon;
153152

154153
treeLocals.Load(localBranches.Cast<ITreeData>(), LocalTitle);
155154
treeRemotes.Load(remoteBranches.Cast<ITreeData>(), RemoteTitle);
156155
Redraw();
157156
}
158157

158+
private void UpdateTreeIcons()
159+
{
160+
var localsLoaded = false;
161+
var remotesLoaded = false;
162+
163+
if (treeLocals != null)
164+
{
165+
if (treeLocals.ActiveNodeIcon == null)
166+
{
167+
localsLoaded = true;
168+
treeLocals.ActiveNodeIcon = Styles.ActiveBranchIcon;
169+
}
170+
171+
if (treeLocals.NodeIcon == null)
172+
{
173+
localsLoaded = true;
174+
treeLocals.NodeIcon = Styles.BranchIcon;
175+
}
176+
177+
if (treeLocals.FolderIcon == null)
178+
{
179+
localsLoaded = true;
180+
treeLocals.FolderIcon = Styles.FolderIcon;
181+
}
182+
}
183+
184+
if (treeRemotes != null)
185+
{
186+
if (treeRemotes.ActiveNodeIcon == null)
187+
{
188+
remotesLoaded = true;
189+
treeRemotes.ActiveNodeIcon = Styles.ActiveBranchIcon;
190+
}
191+
192+
if (treeRemotes.NodeIcon == null)
193+
{
194+
remotesLoaded = true;
195+
treeRemotes.NodeIcon = Styles.BranchIcon;
196+
}
197+
198+
if (treeRemotes.RootFolderIcon == null)
199+
{
200+
remotesLoaded = true;
201+
treeRemotes.RootFolderIcon = Styles.RootFolderIcon;
202+
}
203+
204+
if (treeRemotes.FolderIcon == null)
205+
{
206+
remotesLoaded = true;
207+
treeRemotes.FolderIcon = Styles.FolderIcon;
208+
}
209+
}
210+
211+
if (localsLoaded)
212+
{
213+
treeLocals.LoadNodeIcons();
214+
}
215+
216+
if (remotesLoaded)
217+
{
218+
treeRemotes.LoadNodeIcons();
219+
}
220+
}
221+
159222
private void OnButtonBarGUI()
160223
{
161224
if (mode == BranchesMode.Default)

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

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +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-
[SerializeField] private SerializableTexture2D activeNodeIcon = new SerializableTexture2D();
21-
public Texture2D ActiveNodeIcon { get { return activeNodeIcon.Texture; } set { activeNodeIcon.Texture = value; } }
22-
23-
[SerializeField] private SerializableTexture2D nodeIcon = new SerializableTexture2D();
24-
public Texture2D NodeIcon { get { return nodeIcon.Texture; } set { nodeIcon.Texture = value; } }
25-
26-
[SerializeField] private SerializableTexture2D folderIcon = new SerializableTexture2D();
27-
public Texture2D FolderIcon { get { return folderIcon.Texture; } set { folderIcon.Texture = value; } }
28-
29-
[SerializeField] private SerializableTexture2D rootFolderIcon = new SerializableTexture2D();
30-
public Texture2D RootFolderIcon { get { return rootFolderIcon.Texture; } set { rootFolderIcon.Texture = value; } }
31-
3250
[SerializeField] public GUIStyle FolderStyle;
3351
[SerializeField] public GUIStyle TreeNodeStyle;
3452
[SerializeField] public GUIStyle ActiveTreeNodeStyle;
@@ -89,7 +107,7 @@ public void Load(IEnumerable<ITreeData> data, string title)
89107
Level = 0,
90108
IsFolder = true
91109
};
92-
titleNode.Load();
110+
SetNodeIcon(titleNode);
93111
nodes.Add(titleNode);
94112

95113
foreach (var d in data)
@@ -117,9 +135,7 @@ public void Load(IEnumerable<ITreeData> data, string title)
117135
activeNode = node;
118136
}
119137

120-
ResetNodeIcons(node);
121-
122-
node.Load();
138+
SetNodeIcon(node);
123139

124140
nodes.Add(node);
125141
if (isFolder)
@@ -142,7 +158,6 @@ public Rect Render(Rect rect, Vector2 scroll, Action<TreeNode> singleClick = nul
142158
rect = new Rect(0f, rect.y, rect.width, ItemHeight);
143159

144160
var titleNode = nodes[0];
145-
ResetNodeIcons(titleNode);
146161
bool selectionChanged = titleNode.Render(rect, 0f, selectedNode == titleNode, FolderStyle, TreeNodeStyle, ActiveTreeNodeStyle);
147162

148163
if (selectionChanged)
@@ -160,8 +175,6 @@ public Rect Render(Rect rect, Vector2 scroll, Action<TreeNode> singleClick = nul
160175
for (; i < nodes.Count; i++)
161176
{
162177
var node = nodes[i];
163-
ResetNodeIcons(node);
164-
165178
if (node.Level > level && !node.IsHidden)
166179
{
167180
Indent();
@@ -381,24 +394,20 @@ private void Unindent()
381394
indents.Pop();
382395
}
383396

384-
private void ResetNodeIcons(TreeNode node)
397+
private void SetNodeIcon(TreeNode node)
385398
{
386-
if (node.IsActive)
387-
{
388-
node.Icon = ActiveNodeIcon;
389-
}
390-
else if (node.IsFolder)
391-
{
392-
if (node.Level == 1)
393-
node.Icon = RootFolderIcon;
394-
else
395-
node.Icon = FolderIcon;
396-
}
397-
else
399+
node.Icon = GetNodeIcon(node);
400+
node.Load();
401+
}
402+
403+
protected abstract Texture2D GetNodeIcon(TreeNode node);
404+
405+
public void LoadNodeIcons()
406+
{
407+
foreach (var treeNode in nodes)
398408
{
399-
node.Icon = NodeIcon;
409+
SetNodeIcon(treeNode);
400410
}
401-
node.Load();
402411
}
403412
}
404413

@@ -413,7 +422,7 @@ public class TreeNode
413422
public bool IsHidden;
414423
public bool IsActive;
415424
public GUIContent content;
416-
public Texture2D Icon;
425+
[NonSerialized] public Texture2D Icon;
417426

418427
public void Load()
419428
{

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class Window : BaseWindow
4040
[SerializeField] private CacheUpdateEvent lastCurrentBranchAndRemoteChangedEvent;
4141
[NonSerialized] private bool currentBranchAndRemoteHasUpdate;
4242

43+
[NonSerialized] private Texture2D smallLogo;
44+
4345
[MenuItem(LaunchMenu)]
4446
public static void Window_GitHub()
4547
{
@@ -102,7 +104,11 @@ public override void OnEnable()
102104
Selection.activeObject = this;
103105
#endif
104106
// Set window title
105-
titleContent = new GUIContent(Title, Styles.SmallLogo);
107+
if (smallLogo == null)
108+
{
109+
smallLogo = Styles.SmallLogo;
110+
titleContent = new GUIContent(Title, smallLogo);
111+
}
106112

107113
if (Repository != null)
108114
Repository.CheckCurrentBranchAndRemoteChangedEvent(lastCurrentBranchAndRemoteChangedEvent);

0 commit comments

Comments
 (0)