PackageStatusBar.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Experimental.UIElements;
  5. namespace UnityEditor.PackageManager.UI
  6. {
  7. #if !UNITY_2018_3_OR_NEWER
  8. internal class PackageStatusBarFactory : UxmlFactory<PackageStatusBar>
  9. {
  10. protected override PackageStatusBar DoCreate(IUxmlAttributes bag, CreationContext cc)
  11. {
  12. return new PackageStatusBar();
  13. }
  14. }
  15. #endif
  16. internal class PackageStatusBar : VisualElement
  17. {
  18. #if UNITY_2018_3_OR_NEWER
  19. internal new class UxmlFactory : UxmlFactory<PackageStatusBar> {}
  20. #endif
  21. private readonly VisualElement root;
  22. private string LastErrorMessage;
  23. private List<IBaseOperation> operationsInProgress;
  24. private enum StatusType { Normal, Loading, Error };
  25. private StatusType currentStatus;
  26. public PackageStatusBar()
  27. {
  28. root = Resources.GetTemplate("PackageStatusBar.uxml");
  29. Add(root);
  30. MoreAddOptionsButton.clickable.clicked += OnMoreAddOptionsButtonClick;
  31. LastErrorMessage = string.Empty;
  32. operationsInProgress = new List<IBaseOperation>();
  33. SetDefaultMessage();
  34. PackageCollection.Instance.ListSignal.WhenOperation(OnListOrSearchOperation);
  35. PackageCollection.Instance.SearchSignal.WhenOperation(OnListOrSearchOperation);
  36. LoadingText.RegisterCallback<MouseDownEvent>(RefreshAll);
  37. }
  38. private void RefreshAll(MouseDownEvent evt)
  39. {
  40. if (evt.button == 0 && currentStatus != StatusType.Loading)
  41. {
  42. PackageCollection.Instance.FetchListOfflineCache(true);
  43. PackageCollection.Instance.FetchListCache(true);
  44. PackageCollection.Instance.FetchSearchCache(true);
  45. }
  46. }
  47. private void SetDefaultMessage()
  48. {
  49. if (!string.IsNullOrEmpty(PackageCollection.Instance.lastUpdateTime))
  50. SetStatusMessage(StatusType.Normal, "Last update " + PackageCollection.Instance.lastUpdateTime);
  51. else
  52. SetStatusMessage(StatusType.Normal, string.Empty);
  53. }
  54. private void OnListOrSearchOperation(IBaseOperation operation)
  55. {
  56. if (operation == null || operation.IsCompleted)
  57. return;
  58. operationsInProgress.Add(operation);
  59. operation.OnOperationFinalized += () => { OnOperationFinalized(operation); };
  60. operation.OnOperationError += OnOperationError;
  61. SetStatusMessage(StatusType.Loading, "Loading packages...");
  62. }
  63. private void OnOperationFinalized(IBaseOperation operation)
  64. {
  65. operationsInProgress.Remove(operation);
  66. if (operationsInProgress.Any()) return;
  67. var errorMessage = LastErrorMessage;
  68. if (Application.internetReachability == NetworkReachability.NotReachable)
  69. {
  70. EditorApplication.update -= CheckInternetReachability;
  71. EditorApplication.update += CheckInternetReachability;
  72. errorMessage = "You seem to be offline.";
  73. }
  74. if (!string.IsNullOrEmpty(errorMessage))
  75. SetStatusMessage(StatusType.Error, errorMessage);
  76. else
  77. SetDefaultMessage();
  78. }
  79. private void OnOperationError(Error error)
  80. {
  81. LastErrorMessage = "Cannot load packages, see console.";
  82. }
  83. private void CheckInternetReachability()
  84. {
  85. if (Application.internetReachability == NetworkReachability.NotReachable) return;
  86. PackageCollection.Instance.FetchListCache(true);
  87. PackageCollection.Instance.FetchSearchCache(true);
  88. EditorApplication.update -= CheckInternetReachability;
  89. }
  90. private void SetStatusMessage(StatusType status, string message)
  91. {
  92. currentStatus = status;
  93. if (currentStatus == StatusType.Loading)
  94. LoadingSpinner.Start();
  95. else
  96. LoadingSpinner.Stop();
  97. UIUtils.SetElementDisplay(LoadingIcon, currentStatus == StatusType.Error);
  98. if (currentStatus == StatusType.Error)
  99. LoadingText.AddToClassList("icon");
  100. else
  101. LoadingText.RemoveFromClassList("icon");
  102. LoadingText.text = message;
  103. LoadingText.tooltip = currentStatus != StatusType.Loading ? "Click to refresh packages" : string.Empty;
  104. }
  105. private void OnMoreAddOptionsButtonClick()
  106. {
  107. var menu = new GenericMenu();
  108. var addPackageFromDiskItem = new GUIContent("Add package from disk...");
  109. /* // Disable adding from url field before the feature is ready
  110. var addPackageFromUrlItem = new GUIContent("Add package from URL...");
  111. menu.AddItem(addPackageFromUrlItem, false, delegate
  112. {
  113. AddFromUrlField.Show(true);
  114. });
  115. */
  116. menu.AddItem(addPackageFromDiskItem, false, delegate
  117. {
  118. var path = EditorUtility.OpenFilePanelWithFilters("Select package on disk", "", new[] { "package.json file", "json" });
  119. if (!string.IsNullOrEmpty(path) && !Package.AddRemoveOperationInProgress)
  120. Package.AddFromLocalDisk(path);
  121. });
  122. var menuPosition = MoreAddOptionsButton.LocalToWorld(new Vector2(MoreAddOptionsButton.layout.width, 0));
  123. var menuRect = new Rect(menuPosition, Vector2.zero);
  124. menu.DropDown(menuRect);
  125. }
  126. private PackageAddFromUrlField AddFromUrlField { get { return root.Q<PackageAddFromUrlField>("packageAddFromUrlField"); }}
  127. private VisualElement LoadingSpinnerContainer { get { return root.Q<VisualElement>("loadingSpinnerContainer"); }}
  128. private LoadingSpinner LoadingSpinner { get { return root.Q<LoadingSpinner>("packageSpinner"); }}
  129. private Label LoadingIcon { get { return root.Q<Label>("loadingIcon"); }}
  130. private Label LoadingText { get { return root.Q<Label>("loadingText"); }}
  131. private Button MoreAddOptionsButton { get { return root.Q<Button>("moreAddOptionsButton"); }}
  132. }
  133. }