ToolbarButton.cs 7.8 KB

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