CollabHistoryItem.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Linq;
  3. using System.Security.Cryptography;
  4. using UnityEditor.Connect;
  5. using UnityEditor.Web;
  6. using UnityEngine;
  7. #if UNITY_2019_1_OR_NEWER
  8. using UnityEngine.UIElements;
  9. #else
  10. using UnityEngine.Experimental.UIElements;
  11. using UnityEngine.Experimental.UIElements.StyleEnums;
  12. #endif
  13. namespace UnityEditor.Collaboration
  14. {
  15. internal class CollabHistoryItem : VisualElement
  16. {
  17. public static RevisionAction s_OnRestore;
  18. public static RevisionAction s_OnGoBack;
  19. public static RevisionAction s_OnUpdate;
  20. public static ShowBuildAction s_OnShowBuild;
  21. public static Action s_OnShowServices;
  22. private readonly string m_RevisionId;
  23. private readonly string m_FullDescription;
  24. private readonly DateTime m_TimeStamp;
  25. private readonly Button m_Button;
  26. private readonly HistoryProgressSpinner m_ProgressSpinner;
  27. private VisualElement m_ActionsTray;
  28. private VisualElement m_Details;
  29. private Label m_Description;
  30. private Label m_TimeAgo;
  31. private readonly Button m_ExpandCollapseButton;
  32. private bool m_Expanded;
  33. private const int kMaxDescriptionChars = 500;
  34. public bool RevisionActionsEnabled
  35. {
  36. set
  37. {
  38. m_Button.SetEnabled(value);
  39. }
  40. }
  41. public DateTime timeStamp
  42. {
  43. get { return m_TimeStamp; }
  44. }
  45. public CollabHistoryItem(RevisionData data)
  46. {
  47. m_RevisionId = data.id;
  48. m_TimeStamp = data.timeStamp;
  49. name = "HistoryItem";
  50. m_ActionsTray = new VisualElement {name = "HistoryItemActionsTray"};
  51. m_ProgressSpinner = new HistoryProgressSpinner();
  52. m_Details = new VisualElement {name = "HistoryDetail"};
  53. var author = new Label(data.authorName) {name = "Author"};
  54. m_TimeAgo = new Label(TimeAgo.GetString(m_TimeStamp));
  55. m_FullDescription = data.comment;
  56. var shouldTruncate = ShouldTruncateDescription(m_FullDescription);
  57. if (shouldTruncate)
  58. {
  59. m_Description = new Label(GetTruncatedDescription(m_FullDescription));
  60. }
  61. else
  62. {
  63. m_Description = new Label(m_FullDescription);
  64. }
  65. m_Description.name = "RevisionDescription";
  66. var dropdown = new CollabHistoryDropDown(data.changes, data.changesTotal, data.changesTruncated, data.id);
  67. if (data.current)
  68. {
  69. m_Button = new Button(Restore) {name = "ActionButton", text = "Restore"};
  70. }
  71. else if (data.obtained)
  72. {
  73. m_Button = new Button(GoBackTo) {name = "ActionButton", text = "Go back to..."};
  74. }
  75. else
  76. {
  77. m_Button = new Button(UpdateTo) {name = "ActionButton", text = "Update"};
  78. }
  79. m_Button.SetEnabled(data.enabled);
  80. m_ProgressSpinner.ProgressEnabled = data.inProgress;
  81. m_ActionsTray.Add(m_ProgressSpinner);
  82. m_ActionsTray.Add(m_Button);
  83. m_Details.Add(author);
  84. m_Details.Add(m_TimeAgo);
  85. m_Details.Add(m_Description);
  86. if (shouldTruncate)
  87. {
  88. m_ExpandCollapseButton = new Button(ToggleDescription) { name = "ToggleDescription", text = "Show More" };
  89. m_Details.Add(m_ExpandCollapseButton);
  90. }
  91. if (data.buildState != BuildState.None)
  92. {
  93. BuildStatusButton buildButton;
  94. if (data.buildState == BuildState.Configure)
  95. buildButton = new BuildStatusButton(ShowServicePage);
  96. else
  97. buildButton = new BuildStatusButton(ShowBuildForCommit, data.buildState, data.buildFailures);
  98. m_Details.Add(buildButton);
  99. }
  100. m_Details.Add(m_ActionsTray);
  101. m_Details.Add(dropdown);
  102. Add(m_Details);
  103. this.schedule.Execute(UpdateTimeAgo).Every(1000 * 20);
  104. }
  105. public static void SetUpCallbacks(RevisionAction Restore, RevisionAction GoBack, RevisionAction Update)
  106. {
  107. s_OnRestore = Restore;
  108. s_OnGoBack = GoBack;
  109. s_OnUpdate = Update;
  110. }
  111. public void SetInProgressStatus(string revisionIdInProgress)
  112. {
  113. if (String.IsNullOrEmpty(revisionIdInProgress))
  114. {
  115. m_Button.SetEnabled(true);
  116. m_ProgressSpinner.ProgressEnabled = false;
  117. }
  118. else
  119. {
  120. m_Button.SetEnabled(false);
  121. if (m_RevisionId.Equals(revisionIdInProgress))
  122. {
  123. m_ProgressSpinner.ProgressEnabled = true;
  124. }
  125. }
  126. }
  127. void ShowBuildForCommit()
  128. {
  129. CollabAnalytics.SendUserAction(CollabAnalytics.historyCategoryString, "ShowBuild");
  130. if (s_OnShowBuild != null)
  131. {
  132. s_OnShowBuild(m_RevisionId);
  133. }
  134. }
  135. void ShowServicePage()
  136. {
  137. CollabAnalytics.SendUserAction(CollabAnalytics.historyCategoryString, "ShowServices");
  138. if (s_OnShowServices != null)
  139. {
  140. s_OnShowServices();
  141. }
  142. }
  143. void Restore()
  144. {
  145. CollabAnalytics.SendUserAction(CollabAnalytics.historyCategoryString, "Restore");
  146. if (s_OnRestore != null)
  147. {
  148. s_OnRestore(m_RevisionId, false);
  149. }
  150. }
  151. void GoBackTo()
  152. {
  153. CollabAnalytics.SendUserAction(CollabAnalytics.historyCategoryString, "GoBackTo");
  154. if (s_OnGoBack != null)
  155. {
  156. s_OnGoBack(m_RevisionId, false);
  157. }
  158. }
  159. void UpdateTo()
  160. {
  161. CollabAnalytics.SendUserAction(CollabAnalytics.historyCategoryString, "Update");
  162. if (s_OnUpdate != null)
  163. {
  164. s_OnUpdate(m_RevisionId, true);
  165. }
  166. }
  167. void UpdateTimeAgo()
  168. {
  169. m_TimeAgo.text = TimeAgo.GetString(m_TimeStamp);
  170. }
  171. bool ShouldTruncateDescription(string description)
  172. {
  173. return description.Contains(Environment.NewLine) || description.Length > kMaxDescriptionChars;
  174. }
  175. string GetTruncatedDescription(string description)
  176. {
  177. string result = description.Contains(Environment.NewLine) ?
  178. description.Substring(0, description.IndexOf(Environment.NewLine)) : description;
  179. if (result.Length > kMaxDescriptionChars)
  180. {
  181. result = result.Substring(0, kMaxDescriptionChars) + "...";
  182. }
  183. return result;
  184. }
  185. void ToggleDescription()
  186. {
  187. if (m_Expanded)
  188. {
  189. CollabAnalytics.SendUserAction(CollabAnalytics.historyCategoryString, "CollapseDescription");
  190. m_Expanded = false;
  191. m_ExpandCollapseButton.text = "Show More";
  192. m_Description.text = GetTruncatedDescription(m_FullDescription);
  193. }
  194. else
  195. {
  196. CollabAnalytics.SendUserAction(CollabAnalytics.historyCategoryString, "ExpandDescription");
  197. m_Expanded = true;
  198. m_ExpandCollapseButton.text = "Show Less";
  199. m_Description.text = m_FullDescription;
  200. }
  201. }
  202. }
  203. }