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

Commit 77f84bf

Browse files
Merge pull request #807 from github-for-unity/fixes/project-window-init
ProjectWindow Initialization cleanups
2 parents 598aa6f + 1d8ef57 commit 77f84bf

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

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

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ class ProjectWindowInterface : AssetPostprocessor
1111
private const string AssetsMenuRequestLock = "Assets/Request Lock";
1212
private const string AssetsMenuReleaseLock = "Assets/Release Lock";
1313
private const string AssetsMenuReleaseLockForced = "Assets/Release Lock (forced)";
14-
private static readonly List<GitStatusEntry> entries = new List<GitStatusEntry>();
14+
15+
private static List<GitStatusEntry> entries = new List<GitStatusEntry>();
1516
private static List<GitLock> locks = new List<GitLock>();
17+
private static List<string> guids = new List<string>();
18+
private static List<string> guidsLocks = new List<string>();
1619

17-
private static readonly List<string> guids = new List<string>();
18-
private static readonly List<string> guidsLocks = new List<string>();
1920
private static IApplicationManager manager;
2021
private static bool isBusy = false;
2122
private static ILogging logger;
2223
private static ILogging Logger { get { return logger = logger ?? LogHelper.GetLogger<ProjectWindowInterface>(); } }
2324
private static CacheUpdateEvent lastRepositoryStatusChangedEvent;
2425
private static CacheUpdateEvent lastLocksChangedEvent;
25-
private static IRepository Repository { get { return manager.Environment.Repository; } }
26+
private static IRepository Repository { get { return manager != null ? manager.Environment.Repository : null; } }
27+
private static bool IsInitialized { get { return Repository != null && Repository.CurrentRemote.HasValue; } }
2628

2729
public static void Initialize(IApplicationManager theManager)
2830
{
@@ -31,14 +33,27 @@ public static void Initialize(IApplicationManager theManager)
3133

3234
manager = theManager;
3335

34-
if (Repository != null)
36+
if (IsInitialized)
3537
{
3638
Repository.StatusEntriesChanged += RepositoryOnStatusEntriesChanged;
3739
Repository.LocksChanged += RepositoryOnLocksChanged;
3840
ValidateCachedData();
3941
}
4042
}
4143

44+
private static bool EnsureInitialized()
45+
{
46+
if (locks == null)
47+
locks = new List<GitLock>();
48+
if (entries == null)
49+
entries = new List<GitStatusEntry>();
50+
if (guids == null)
51+
guids = new List<string>();
52+
if (guidsLocks == null)
53+
guidsLocks = new List<string>();
54+
return IsInitialized;
55+
}
56+
4257
private static void ValidateCachedData()
4358
{
4459
Repository.CheckAndRaiseEventsIfCacheNewer(CacheType.GitStatus, lastRepositoryStatusChangedEvent);
@@ -69,16 +84,14 @@ private static void RepositoryOnLocksChanged(CacheUpdateEvent cacheUpdateEvent)
6984
[MenuItem(AssetsMenuRequestLock, true)]
7085
private static bool ContextMenu_CanLock()
7186
{
72-
if (isBusy)
87+
if (!EnsureInitialized())
7388
return false;
74-
if (Repository == null || !Repository.CurrentRemote.HasValue)
89+
if (isBusy)
7590
return false;
7691

7792
var selected = Selection.activeObject;
7893
if (selected == null)
7994
return false;
80-
if (locks == null)
81-
return false;
8295

8396
NPath assetPath = AssetDatabase.GetAssetPath(selected.GetInstanceID()).ToNPath();
8497
NPath repositoryPath = manager.Environment.GetRepositoryPath(assetPath);
@@ -113,7 +126,7 @@ private static void ContextMenu_Lock()
113126
{
114127
var error = ex.Message;
115128
if (error.Contains("exit status 255"))
116-
error = "Failed to unlock: no permissions";
129+
error = "Failed to lock: no permissions";
117130
EditorUtility.DisplayDialog(Localization.RequestLockActionTitle,
118131
error,
119132
Localization.Ok);
@@ -129,22 +142,19 @@ private static void ContextMenu_Lock()
129142
[MenuItem(AssetsMenuReleaseLock, true, 1000)]
130143
private static bool ContextMenu_CanUnlock()
131144
{
132-
if (isBusy)
145+
if (!EnsureInitialized())
133146
return false;
134-
if (Repository == null || !Repository.CurrentRemote.HasValue)
147+
if (isBusy)
135148
return false;
136149

137150
var selected = Selection.activeObject;
138151
if (selected == null)
139152
return false;
140-
if (locks == null || locks.Count == 0)
141-
return false;
142153

143154
NPath assetPath = AssetDatabase.GetAssetPath(selected.GetInstanceID()).ToNPath();
144155
NPath repositoryPath = manager.Environment.GetRepositoryPath(assetPath);
145156

146-
var isLocked = locks.Any(x => repositoryPath == x.Path);
147-
return isLocked;
157+
return locks.Any(x => repositoryPath == x.Path);
148158
}
149159

150160
[MenuItem(AssetsMenuReleaseLock, false, 1000)]
@@ -184,22 +194,19 @@ private static void ContextMenu_Unlock()
184194
[MenuItem(AssetsMenuReleaseLockForced, true, 1000)]
185195
private static bool ContextMenu_CanUnlockForce()
186196
{
187-
if (isBusy)
197+
if (!EnsureInitialized())
188198
return false;
189-
if (Repository == null || !Repository.CurrentRemote.HasValue)
199+
if (isBusy)
190200
return false;
191201

192202
var selected = Selection.activeObject;
193203
if (selected == null)
194204
return false;
195-
if (locks == null || locks.Count == 0)
196-
return false;
197205

198206
NPath assetPath = AssetDatabase.GetAssetPath(selected.GetInstanceID()).ToNPath();
199207
NPath repositoryPath = manager.Environment.GetRepositoryPath(assetPath);
200208

201-
var isLocked = locks.Any(x => repositoryPath == x.Path);
202-
return isLocked;
209+
return locks.Any(x => repositoryPath == x.Path);
203210
}
204211

205212
[MenuItem(AssetsMenuReleaseLockForced, false, 1000)]
@@ -238,12 +245,6 @@ private static void ContextMenu_UnlockForce()
238245

239246
private static void OnLocksUpdate()
240247
{
241-
if (locks == null)
242-
{
243-
return;
244-
}
245-
locks = locks.ToList();
246-
247248
guidsLocks.Clear();
248249
foreach (var lck in locks)
249250
{
@@ -272,6 +273,9 @@ private static void OnStatusUpdate()
272273

273274
private static void OnProjectWindowItemGUI(string guid, Rect itemRect)
274275
{
276+
if (!EnsureInitialized())
277+
return;
278+
275279
if (Event.current.type != EventType.Repaint || string.IsNullOrEmpty(guid))
276280
{
277281
return;

0 commit comments

Comments
 (0)