RunModal.cs
8.6 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace Unity.PlasticSCM.Editor.UI
{
internal static class RunModal
{
static RunModal()
{
InitializeInfo();
}
internal static bool IsAvailable()
{
return mShowWithModeMethod != null
&& mCreateSavedGUIState != null
&& mApplyAndForgetMethod != null
&& mParentField != null
&& mParentWindowProp != null
&& mMakeModalMethod != null;
}
internal static void Dialog(EditorWindow window)
{
ShowAsUtility(window);
object savedGUIState = CreateSavedGUIState();
PushDispatcherContext(window);
MakeModal(window);
PopDispatcherContext(window);
ApplySavedGUIState(savedGUIState);
}
static void MakeModal(EditorWindow window)
{
// MakeModal(m_Parent.window);
var hostView = mParentField.GetValue(window);
var parentWindow = mParentWindowProp.GetValue(hostView, null);
mMakeModalMethod.Invoke(
mMakeModalMethod.IsStatic ? null : window,
new object[] { parentWindow });
}
static void ShowAsUtility(EditorWindow window)
{
// ShowWithMode(ShowMode.Utility);
mShowWithModeMethod.Invoke(window, new object[] { 2 });
}
static object CreateSavedGUIState()
{
// SavedGUIState guiState = SavedGUIState.Create();
return mCreateSavedGUIState.Invoke(null, null);
}
static void ApplySavedGUIState(object savedGUIState)
{
// guiState.ApplyAndForget();
mApplyAndForgetMethod.Invoke(savedGUIState, null);
}
static void PopDispatcherContext(EditorWindow window)
{
#if UNITY_2020_1_OR_NEWER
//UnityEngine.UIElements.EventDispatcher.editorDispatcher.PopDispatcherContext();
object editorDispatcher = mEditorDispatcherProp2020.GetValue(null);
mPopContextMethod2020.Invoke(editorDispatcher, null);
#else
// m_Parent.visualTree.panel.dispatcher?.PopDispatcherContext();
object dispatcher = GetDispatcher(window);
if (dispatcher != null)
mPopContextMethod.Invoke(dispatcher, null);
#endif
}
static void PushDispatcherContext(EditorWindow window)
{
#if UNITY_2020_1_OR_NEWER
//UnityEngine.UIElements.EventDispatcher.editorDispatcher.PushDispatcherContext();
object editorDispatcher = mEditorDispatcherProp2020.GetValue(null);
mPushContextMethod2020.Invoke(editorDispatcher, null);
#else
// m_Parent.visualTree.panel.dispatcher?.PushDispatcherContext();
object dispatcher = GetDispatcher(window);
if (dispatcher != null)
mPushContextMethod.Invoke(dispatcher, null);
#endif
}
static object GetDispatcher(EditorWindow window)
{
object dispatcher = null;
if (MayHaveDispatcher())
{
var parent = mParentField.GetValue(window);
if (parent != null)
{
var visualTree = mVisualTreeProp.GetValue(parent, null);
if (visualTree != null)
{
var panel = mPanelProp.GetValue(visualTree, null);
if (panel != null)
{
dispatcher = mDispatcherProp.GetValue(panel, null);
}
}
}
}
return dispatcher;
}
static bool MayHaveDispatcher()
{
return mDispatcherType != null
&& mPushContextMethod != null
&& mPopContextMethod != null;
}
static void InitializeInfo()
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static;
mMakeModalMethod = BuildMakeModalMethodInfo(flags);
mShowWithModeMethod = typeof(EditorWindow).GetMethod("ShowWithMode", flags);
mParentField = typeof(EditorWindow).GetField("m_Parent", flags);
var hostViewType = mParentField.FieldType;
mParentWindowProp = hostViewType.GetProperty("window", flags);
var savedGUIStateType = typeof(EditorWindow).Assembly.GetType("UnityEditor.SavedGUIState");
mCreateSavedGUIState = savedGUIStateType.GetMethod("Create", flags);
mApplyAndForgetMethod = savedGUIStateType.GetMethod("ApplyAndForget", flags);
#if UNITY_2020_1_OR_NEWER
mEditorDispatcherProp2020 = typeof(UnityEngine.UIElements.EventDispatcher).GetProperty("editorDispatcher", flags);
mPushContextMethod2020 = mEditorDispatcherProp2020.PropertyType.GetMethod("PushDispatcherContext", flags);
mPopContextMethod2020 = mEditorDispatcherProp2020.PropertyType.GetMethod("PopDispatcherContext", flags);
#endif
flags = BindingFlags.NonPublic
| BindingFlags.Instance
| BindingFlags.Public;
mParentField = typeof(EditorWindow).GetField("m_Parent", flags);
if (mParentField != null)
hostViewType = mParentField.FieldType;
if (hostViewType != null)
mVisualTreeProp = hostViewType.GetProperty("visualTree");
if (mVisualTreeProp != null)
{
var visualTreeType = mVisualTreeProp.PropertyType;
if (visualTreeType != null)
{
mPanelProp = visualTreeType.GetProperty("panel");
if (mPanelProp != null)
{
var panelType = mPanelProp.PropertyType;
if (panelType != null)
{
mDispatcherProp = panelType.GetProperty("dispatcher");
if (mDispatcherProp != null)
{
mDispatcherType = mDispatcherProp.PropertyType;
if (mDispatcherType != null)
{
mPushContextMethod = mDispatcherType.GetMethod("PushDispatcherContext", flags);
mPopContextMethod = mDispatcherType.GetMethod("PopDispatcherContext", flags);
}
}
}
}
}
}
}
static MethodInfo BuildMakeModalMethodInfo(BindingFlags flags)
{
if (EditorVersion.IsCurrentEditorOlderThan("2019.3.10f1"))
return typeof(EditorWindow).GetMethod("MakeModal", flags);
return typeof(EditorWindow).GetMethod("Internal_MakeModal", flags);
}
static FieldInfo mParentField;
static PropertyInfo mParentWindowProp;
static MethodInfo mMakeModalMethod;
static MethodInfo mShowWithModeMethod;
static MethodInfo mCreateSavedGUIState;
static MethodInfo mApplyAndForgetMethod;
static PropertyInfo mVisualTreeProp;
static Type mDispatcherType;
static MethodInfo mPushContextMethod;
static MethodInfo mPopContextMethod;
static PropertyInfo mPanelProp;
static PropertyInfo mDispatcherProp;
#if UNITY_2020_1_OR_NEWER
static PropertyInfo mEditorDispatcherProp2020;
static MethodInfo mPushContextMethod2020;
static MethodInfo mPopContextMethod2020;
#endif
// // How ContainerWindows are visualized. Used with ContainerWindow.Show
// internal enum ShowMode
// {
// // Show as a normal window with max, min & close buttons.
// NormalWindow = 0,
// // Used for a popup menu. On mac this means light shadow and no titlebar.
// PopupMenu = 1,
// // Utility window - floats above the app. Disappears when app loses focus.
// Utility = 2,
// // Window has no shadow or decorations. Used internally for dragging stuff around.
// NoShadow = 3,
// // The Unity main window. On mac, this is the same as NormalWindow, except window doesn't have a close button.
// MainWindow = 4,
// // Aux windows. The ones that close the moment you move the mouse out of them.
// AuxWindow = 5,
// // Like PopupMenu, but without keyboard focus
// Tooltip = 6
// }
}
}