暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DownloadPlasticExeDialog.cs 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System.Linq;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. using Codice.Client.Common;
  6. using Codice.Client.Common.EventTracking;
  7. using Codice.CM.Common;
  8. using PlasticGui;
  9. using Unity.PlasticSCM.Editor.Tool;
  10. using Unity.PlasticSCM.Editor.UI;
  11. using Unity.PlasticSCM.Editor.UI.UIElements;
  12. using Unity.PlasticSCM.Editor.Views.Welcome;
  13. namespace Unity.PlasticSCM.Editor.Views
  14. {
  15. internal class DownloadPlasticExeDialog : PlasticDialog, DownloadAndInstallOperation.INotify
  16. {
  17. internal bool IsPlasticInstalling { get { return mIsPlasticInstalling; } }
  18. protected override Rect DefaultRect
  19. {
  20. get
  21. {
  22. var baseRect = base.DefaultRect;
  23. return new Rect(baseRect.x, baseRect.y, DIALOG_WIDTH, DIALOG_HEIGHT);
  24. }
  25. }
  26. internal static ProgressControlsForDialogs.Data Show(
  27. RepositorySpec repSpec,
  28. bool isGluonMode,
  29. string installCloudFrom,
  30. string installEnterpriseFrom,
  31. string cancelInstallFrom,
  32. ProgressControlsForDialogs.Data progressData)
  33. {
  34. DownloadPlasticExeDialog dialog;
  35. if (HasOpenInstances<DownloadPlasticExeDialog>())
  36. {
  37. dialog = GetWindow<DownloadPlasticExeDialog>(true);
  38. }
  39. else
  40. {
  41. dialog = Create(repSpec, isGluonMode, installCloudFrom, installEnterpriseFrom, cancelInstallFrom, progressData);
  42. dialog.RunUtility(focusedWindow);
  43. }
  44. return progressData != null ? progressData : dialog.mProgressControls.ProgressData;
  45. }
  46. static DownloadPlasticExeDialog Create(
  47. RepositorySpec repSpec,
  48. bool isGluonMode,
  49. string installCloudFrom,
  50. string installEnterpriseFrom,
  51. string cancelInstallFrom,
  52. ProgressControlsForDialogs.Data progressData)
  53. {
  54. var instance = CreateInstance<DownloadPlasticExeDialog>();
  55. instance.mRepSpec = repSpec;
  56. instance.mInstallCloudFrom = installCloudFrom;
  57. instance.mInstallEnterpriseFrom = installEnterpriseFrom;
  58. instance.mCancelInstallFrom = cancelInstallFrom;
  59. instance.mIsGluonMode = isGluonMode;
  60. instance.mProgressData = progressData;
  61. instance.mInstallerFile = GetInstallerTmpFileName.ForPlatform();
  62. instance.mIsCloudEdition = EditionToken.IsCloudEdition();
  63. instance.mTitle = PlasticLocalization.Name.UnityVersionControl.GetString();
  64. return instance;
  65. }
  66. protected override string GetTitle()
  67. {
  68. return mTitle;
  69. }
  70. void OnEnable()
  71. {
  72. BuildComponents();
  73. }
  74. void OnDestroy()
  75. {
  76. Dispose();
  77. }
  78. protected override void OnModalGUI()
  79. {
  80. if (InstallationFinished())
  81. {
  82. mMessageLabel.text = PlasticLocalization.Name.UnityVersionControlInstalled.GetString();
  83. mCancelButton.text = PlasticLocalization.Name.CloseButton.GetString();
  84. mConfirmMessageLabel.Collapse();
  85. mDownloadButton.Collapse();
  86. maxSize = new Vector2(DIALOG_WIDTH, REDUCED_DIALOG_HEIGHT);
  87. return;
  88. }
  89. if (mProgressData != null)
  90. {
  91. if (mProgressControlsContainer.Children().OfType<ProgressControlsForDialogs>().Any())
  92. {
  93. mProgressControlsContainer.RemoveAt(0);
  94. mProgressLabel = new Label(GetMessageFromProgressData(mProgressData));
  95. mProgressControlsContainer.Add(mProgressLabel);
  96. }
  97. mProgressLabel.text = GetMessageFromProgressData(mProgressData);
  98. UpdateButtonsStatuses();
  99. }
  100. }
  101. void CancelButton_Clicked()
  102. {
  103. if (!IsExeAvailable.ForMode(mIsGluonMode))
  104. TrackFeatureUseEvent.For(mRepSpec, mCancelInstallFrom);
  105. Close();
  106. }
  107. void DownloadButton_Clicked()
  108. {
  109. if (mIsCloudEdition)
  110. {
  111. TrackFeatureUseEvent.For(mRepSpec, mInstallCloudFrom);
  112. DownloadAndInstallOperation.Run(
  113. Edition.Cloud, mInstallerFile, mProgressControls, this);
  114. }
  115. else
  116. {
  117. TrackFeatureUseEvent.For(mRepSpec, mInstallEnterpriseFrom);
  118. DownloadAndInstallOperation.Run(
  119. Edition.Enterprise, mInstallerFile, mProgressControls, this);
  120. }
  121. }
  122. void DownloadAndInstallOperation.INotify.InstallationStarted()
  123. {
  124. mIsPlasticInstalling = true;
  125. }
  126. void DownloadAndInstallOperation.INotify.InstallationFinished()
  127. {
  128. mIsPlasticInstalling = false;
  129. }
  130. bool InstallationFinished()
  131. {
  132. return mCancelButton.enabledSelf && IsExeAvailable.ForMode(mIsGluonMode);
  133. }
  134. void UpdateButtonsStatuses()
  135. {
  136. if (!string.IsNullOrEmpty(mProgressData.StatusMessage))
  137. {
  138. mCancelButton.SetEnabled(true);
  139. mCancelButton.text = PlasticLocalization.Name.CloseButton.GetString();
  140. mDownloadButton.Collapse();
  141. return;
  142. }
  143. if (IsExeAvailable.ForMode(mIsGluonMode))
  144. {
  145. mCancelButton.SetEnabled(true);
  146. return;
  147. }
  148. mDownloadButton.SetEnabled(false);
  149. mCancelButton.SetEnabled(false);
  150. }
  151. static string GetMessageFromProgressData(ProgressControlsForDialogs.Data data)
  152. {
  153. if (!string.IsNullOrEmpty(data.StatusMessage))
  154. {
  155. return data.StatusMessage;
  156. }
  157. else
  158. {
  159. if (data.ProgressPercent >= 0)
  160. return string.Format("{0} ({1}%)", data.ProgressMessage, (int)(data.ProgressPercent * 100));
  161. else
  162. return data.ProgressMessage;
  163. }
  164. }
  165. void Dispose()
  166. {
  167. mDownloadButton.clicked -= DownloadButton_Clicked;
  168. mCancelButton.clicked -= CancelButton_Clicked;
  169. }
  170. void BuildComponents()
  171. {
  172. VisualElement root = rootVisualElement;
  173. root.Clear();
  174. InitializeLayoutAndStyles();
  175. mMessageLabel = root.Q<Label>("title");
  176. mConfirmMessageLabel = root.Q<Label>("message");
  177. mDownloadButton = root.Q<Button>("download");
  178. mCancelButton = root.Q<Button>("cancel");
  179. mProgressControlsContainer = root.Q<VisualElement>("progressControlsContainer");
  180. mMessageLabel.text = PlasticLocalization.Name.InstallUnityVersionControl.GetString();
  181. mConfirmMessageLabel.text = PlasticLocalization.Name.InstallConfirmationMessage.GetString();
  182. mDownloadButton.text = PlasticLocalization.Name.YesButton.GetString();
  183. mDownloadButton.clicked += DownloadButton_Clicked;
  184. mCancelButton.text = PlasticLocalization.Name.NoButton.GetString();
  185. mCancelButton.clicked += CancelButton_Clicked;
  186. mProgressControls = new ProgressControlsForDialogs(
  187. new VisualElement[] {
  188. mDownloadButton,
  189. mCancelButton
  190. });
  191. mProgressControlsContainer.Add(mProgressControls);
  192. }
  193. void InitializeLayoutAndStyles()
  194. {
  195. rootVisualElement.LoadLayout(typeof(DownloadPlasticExeDialog).Name);
  196. rootVisualElement.LoadStyle(typeof(DownloadPlasticExeDialog).Name);
  197. }
  198. string mTitle;
  199. Label mMessageLabel;
  200. Label mConfirmMessageLabel;
  201. Label mProgressLabel;
  202. Button mDownloadButton;
  203. Button mCancelButton;
  204. ProgressControlsForDialogs mProgressControls;
  205. VisualElement mProgressControlsContainer;
  206. RepositorySpec mRepSpec;
  207. string mInstallCloudFrom;
  208. string mInstallEnterpriseFrom;
  209. string mCancelInstallFrom;
  210. string mInstallerFile;
  211. bool mIsCloudEdition;
  212. bool mIsGluonMode;
  213. bool mIsPlasticInstalling;
  214. ProgressControlsForDialogs.Data mProgressData;
  215. const float DIALOG_WIDTH = 500;
  216. const float DIALOG_HEIGHT = 250;
  217. const float REDUCED_DIALOG_HEIGHT = 125;
  218. }
  219. }