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.

74 lines
2.2 KiB

  1. using System;
  2. using UnityEngine.Experimental.UIElements;
  3. namespace UnityEditor.PackageManager.UI
  4. {
  5. #if !UNITY_2018_3_OR_NEWER
  6. internal class AlertFactory : UxmlFactory<Alert>
  7. {
  8. protected override Alert DoCreate(IUxmlAttributes bag, CreationContext cc)
  9. {
  10. return new Alert();
  11. }
  12. }
  13. #endif
  14. internal class Alert : VisualElement
  15. {
  16. #if UNITY_2018_3_OR_NEWER
  17. internal new class UxmlFactory : UxmlFactory<Alert> { }
  18. #endif
  19. private const string TemplatePath = PackageManagerWindow.ResourcesPath + "Templates/Alert.uxml";
  20. private readonly VisualElement root;
  21. private const float originalPositionRight = 5.0f;
  22. private const float positionRightWithScroll = 12.0f;
  23. public Action OnCloseError;
  24. public Alert()
  25. {
  26. UIUtils.SetElementDisplay(this, false);
  27. root = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(TemplatePath).CloneTree(null);
  28. Add(root);
  29. root.StretchToParentSize();
  30. CloseButton.clickable.clicked += () =>
  31. {
  32. if (null != OnCloseError)
  33. OnCloseError();
  34. ClearError();
  35. };
  36. }
  37. public void SetError(Error error)
  38. {
  39. var message = "An error occured.";
  40. if (error != null)
  41. message = error.message ?? string.Format("An error occurred ({0})", error.errorCode.ToString());
  42. AlertMessage.text = message;
  43. UIUtils.SetElementDisplay(this, true);
  44. }
  45. public void ClearError()
  46. {
  47. UIUtils.SetElementDisplay(this, false);
  48. AdjustSize(false);
  49. AlertMessage.text = "";
  50. OnCloseError = null;
  51. }
  52. public void AdjustSize(bool verticalScrollerVisible)
  53. {
  54. if (verticalScrollerVisible)
  55. style.positionRight = originalPositionRight + positionRightWithScroll;
  56. else
  57. style.positionRight = originalPositionRight;
  58. }
  59. private Label AlertMessage { get { return root.Q<Label>("alertMessage"); } }
  60. private Button CloseButton { get { return root.Q<Button>("close"); } }
  61. }
  62. }