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