ToolbarButton.cs
7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.Collaboration;
using UnityEditor.Connect;
using UnityEngine;
namespace Unity.Cloud.Collaborate.UserInterface
{
internal class ToolbarButton : SubToolbar
{
protected enum ToolbarButtonState
{
NeedToEnableCollab,
UpToDate,
Conflict,
OperationError,
ServerHasChanges,
FilesToPush,
InProgress,
Disabled,
Offline
}
ToolbarButtonState m_CurrentState;
string m_ErrorMessage;
readonly Dictionary<ToolbarButtonState, GUIContent> m_IconCache = new Dictionary<ToolbarButtonState, GUIContent>();
ButtonWithAnimatedIconRotation m_CollabButton;
public ToolbarButton()
{
Collab.instance.StateChanged += OnCollabStateChanged;
UnityConnect.instance.StateChanged += OnUnityConnectStateChanged;
UnityConnect.instance.UserStateChanged += OnUnityConnectUserStateChanged;
}
~ToolbarButton()
{
Collab.instance.StateChanged -= OnCollabStateChanged;
UnityConnect.instance.StateChanged -= OnUnityConnectStateChanged;
UnityConnect.instance.UserStateChanged -= OnUnityConnectUserStateChanged;
}
void OnUnityConnectUserStateChanged(UserInfo state)
{
Update();
}
void OnUnityConnectStateChanged(ConnectInfo state)
{
Update();
}
void OnCollabStateChanged(CollabInfo info)
{
Update();
}
[CanBeNull]
static Texture LoadIcon([NotNull] string iconName)
{
var hidpi = EditorGUIUtility.pixelsPerPoint > 1f ? "@2x" : string.Empty;
return AssetDatabase.LoadAssetAtPath<Texture>($"{CollaborateWindow.IconPath}/{iconName}-{(EditorGUIUtility.isProSkin ? "dark" : "light")}{hidpi}.png");
}
[NotNull]
GUIContent GetIconForState()
{
// Get cached icon, or construct it.
if (!m_IconCache.TryGetValue(m_CurrentState, out var content))
{
string iconName;
string tooltip;
switch (m_CurrentState)
{
case ToolbarButtonState.NeedToEnableCollab:
iconName = "collaborate";
tooltip = "You need to enable collab.";
break;
case ToolbarButtonState.UpToDate:
iconName = "collaborate";
tooltip = "You are up to date.";
break;
case ToolbarButtonState.Conflict:
iconName = "collaborate-error";
tooltip = "Please fix your conflicts prior to publishing.";
break;
case ToolbarButtonState.OperationError:
iconName = "collaborate-error";
tooltip = "Last operation failed. Please retry later.";
break;
case ToolbarButtonState.ServerHasChanges:
iconName = "collaborate-incoming";
tooltip = "Please update, there are server changes.";
break;
case ToolbarButtonState.FilesToPush:
iconName = "collaborate-available-changes";
tooltip = "You have files to publish.";
break;
case ToolbarButtonState.InProgress:
iconName = "collaborate-progress";
tooltip = "Operation in progress.";
break;
case ToolbarButtonState.Disabled:
iconName = "collaborate";
tooltip = "Collab is disabled.";
break;
case ToolbarButtonState.Offline:
iconName = "collaborate-offline";
tooltip = "Please check your network connection.";
break;
default:
throw new ArgumentOutOfRangeException();
}
// Create icon with tooltip and cache.
content = new GUIContent(LoadIcon(iconName), $"Collaborate • {tooltip}");
m_IconCache[m_CurrentState] = content;
}
// Add error message tooltip if there's a message.
var icon = new GUIContent(content);
if (!string.IsNullOrEmpty(m_ErrorMessage))
{
icon.tooltip = $"Collaborate • {m_ErrorMessage}";
}
return icon;
}
public override void OnGUI(Rect rect)
{
GUIStyle collabButtonStyle = "AppCommand";
var disable = EditorApplication.isPlaying;
using (new EditorGUI.DisabledScope(disable))
{
var icon = GetIconForState();
EditorGUIUtility.SetIconSize(new Vector2(16, 16));
if (GUI.Button(rect, icon, collabButtonStyle))
{
CollaborateWindow.Init();
}
EditorGUIUtility.SetIconSize(Vector2.zero);
}
}
public void Update()
{
var currentState = GetCurrentState();
if (m_CurrentState == currentState) return;
m_CurrentState = currentState;
Toolbar.RepaintToolbar();
}
protected virtual ToolbarButtonState GetCurrentState()
{
var currentState = ToolbarButtonState.UpToDate;
var networkAvailable = UnityConnect.instance.connectInfo.online && UnityConnect.instance.connectInfo.loggedIn;
m_ErrorMessage = string.Empty;
if (UnityConnect.instance.isDisableCollabWindow)
{
currentState = ToolbarButtonState.Disabled;
}
else if (networkAvailable)
{
var collab = Collab.instance;
var currentInfo = collab.collabInfo;
if (!currentInfo.ready)
{
currentState = ToolbarButtonState.InProgress;
}
else if (collab.GetError(UnityConnect.UnityErrorFilter.ByContext | UnityConnect.UnityErrorFilter.ByChild, out var errInfo) &&
errInfo.priority <= (int)UnityConnect.UnityErrorPriority.Error)
{
currentState = ToolbarButtonState.OperationError;
m_ErrorMessage = errInfo.shortMsg;
}
else if (currentInfo.inProgress)
{
currentState = ToolbarButtonState.InProgress;
}
else
{
var collabEnabled = Collab.instance.IsCollabEnabledForCurrentProject();
if (UnityConnect.instance.projectInfo.projectBound == false || !collabEnabled)
{
currentState = ToolbarButtonState.NeedToEnableCollab;
}
else if (currentInfo.update)
{
currentState = ToolbarButtonState.ServerHasChanges;
}
else if (currentInfo.conflict)
{
currentState = ToolbarButtonState.Conflict;
}
else if (currentInfo.publish)
{
currentState = ToolbarButtonState.FilesToPush;
}
}
}
else
{
currentState = ToolbarButtonState.Offline;
}
return currentState;
}
}
}