CollabToolbarWindow.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using UnityEngine;
  2. using UnityEditor.Collaboration;
  3. using UnityEditor.Web;
  4. using UnityEditor.Connect;
  5. namespace UnityEditor
  6. {
  7. [InitializeOnLoad]
  8. internal class WebViewStatic : ScriptableSingleton<WebViewStatic>
  9. {
  10. [SerializeField]
  11. WebView m_WebView;
  12. static public WebView GetWebView()
  13. {
  14. return instance.m_WebView;
  15. }
  16. static public void SetWebView(WebView webView)
  17. {
  18. instance.m_WebView = webView;
  19. }
  20. }
  21. [InitializeOnLoad]
  22. internal class CollabToolbarWindow : WebViewEditorStaticWindow, IHasCustomMenu
  23. {
  24. internal override WebView webView
  25. {
  26. get {return WebViewStatic.GetWebView(); }
  27. set {WebViewStatic.SetWebView(value); }
  28. }
  29. private const string kWindowName = "Unity Collab Toolbar";
  30. private static long s_LastClosedTime;
  31. private static CollabToolbarWindow s_CollabToolbarWindow;
  32. public static bool s_ToolbarIsVisible = false;
  33. const int kWindowWidth = 320;
  34. const int kWindowHeight = 350;
  35. public static void CloseToolbar()
  36. {
  37. foreach (CollabToolbarWindow window in Resources.FindObjectsOfTypeAll<CollabToolbarWindow>())
  38. window.Close();
  39. }
  40. [MenuItem("Window/Asset Management/Collab Toolbar", false /*IsValidateFunction*/, 2, true /* IsInternalMenu */)]
  41. public static CollabToolbarWindow ShowToolbarWindow()
  42. {
  43. //Create a new window if it does not exist
  44. if (s_CollabToolbarWindow == null)
  45. {
  46. s_CollabToolbarWindow = GetWindow<CollabToolbarWindow>(false, kWindowName) as CollabToolbarWindow;
  47. }
  48. return s_CollabToolbarWindow;
  49. }
  50. [MenuItem("Window/Asset Management/Collab Toolbar", true /*IsValidateFunction*/)]
  51. public static bool ValidateShowToolbarWindow()
  52. {
  53. return true;
  54. }
  55. public static bool IsVisible()
  56. {
  57. return s_ToolbarIsVisible;
  58. }
  59. public static bool ShowCenteredAtPosition(Rect buttonRect)
  60. {
  61. buttonRect.x -= kWindowWidth / 2;
  62. // We could not use realtimeSinceStartUp since it is set to 0 when entering/exitting playmode, we assume an increasing time when comparing time.
  63. long nowMilliSeconds = System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond;
  64. bool justClosed = nowMilliSeconds < s_LastClosedTime + 50;
  65. if (!justClosed)
  66. {
  67. // Method may have been triggered programmatically, without a user event to consume.
  68. if (Event.current.type != EventType.Layout)
  69. {
  70. Event.current.Use();
  71. }
  72. if (s_CollabToolbarWindow == null)
  73. s_CollabToolbarWindow = CreateInstance<CollabToolbarWindow>() as CollabToolbarWindow;
  74. var windowSize = new Vector2(kWindowWidth, kWindowHeight);
  75. s_CollabToolbarWindow.initialOpenUrl = "file:///" + EditorApplication.userJavascriptPackagesPath + "unityeditor-collab-toolbar/dist/index.html";
  76. s_CollabToolbarWindow.Init();
  77. s_CollabToolbarWindow.ShowAsDropDown(buttonRect, windowSize);
  78. s_CollabToolbarWindow.OnFocus();
  79. return true;
  80. }
  81. return false;
  82. }
  83. // Receives HTML title
  84. public void OnReceiveTitle(string title)
  85. {
  86. titleContent.text = title;
  87. }
  88. public new void OnInitScripting()
  89. {
  90. base.OnInitScripting();
  91. }
  92. public override void OnEnable()
  93. {
  94. minSize = new Vector2(kWindowWidth, kWindowHeight);
  95. maxSize = new Vector2(kWindowWidth, kWindowHeight);
  96. initialOpenUrl = "file:///" + EditorApplication.userJavascriptPackagesPath + "unityeditor-collab-toolbar/dist/index.html";
  97. base.OnEnable();
  98. s_ToolbarIsVisible = true;
  99. }
  100. internal new void OnDisable()
  101. {
  102. s_LastClosedTime = System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond;
  103. if (s_CollabToolbarWindow)
  104. {
  105. s_ToolbarIsVisible = false;
  106. NotifyVisibility(s_ToolbarIsVisible);
  107. }
  108. s_CollabToolbarWindow = null;
  109. base.OnDisable();
  110. }
  111. public new void OnDestroy()
  112. {
  113. OnLostFocus();
  114. base.OnDestroy();
  115. }
  116. }
  117. }