RunModal.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Reflection;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace Unity.PlasticSCM.Editor.UI
  6. {
  7. [InitializeOnLoad]
  8. internal static class RunModal
  9. {
  10. static RunModal()
  11. {
  12. InitializeInfo();
  13. }
  14. internal static bool IsAvailable()
  15. {
  16. return mShowWithModeMethod != null
  17. && mCreateSavedGUIState != null
  18. && mApplyAndForgetMethod != null
  19. && mParentField != null
  20. && mParentWindowProp != null
  21. && mMakeModalMethod != null;
  22. }
  23. internal static void Dialog(EditorWindow window)
  24. {
  25. ShowAsUtility(window);
  26. object savedGUIState = CreateSavedGUIState();
  27. PushDispatcherContext(window);
  28. MakeModal(window);
  29. PopDispatcherContext(window);
  30. ApplySavedGUIState(savedGUIState);
  31. }
  32. static void MakeModal(EditorWindow window)
  33. {
  34. // MakeModal(m_Parent.window);
  35. var hostView = mParentField.GetValue(window);
  36. var parentWindow = mParentWindowProp.GetValue(hostView, null);
  37. mMakeModalMethod.Invoke(
  38. mMakeModalMethod.IsStatic ? null : window,
  39. new object[] { parentWindow });
  40. }
  41. static void ShowAsUtility(EditorWindow window)
  42. {
  43. // ShowWithMode(ShowMode.Utility);
  44. mShowWithModeMethod.Invoke(window, new object[] { 2 });
  45. }
  46. static object CreateSavedGUIState()
  47. {
  48. // SavedGUIState guiState = SavedGUIState.Create();
  49. return mCreateSavedGUIState.Invoke(null, null);
  50. }
  51. static void ApplySavedGUIState(object savedGUIState)
  52. {
  53. // guiState.ApplyAndForget();
  54. mApplyAndForgetMethod.Invoke(savedGUIState, null);
  55. }
  56. static void PopDispatcherContext(EditorWindow window)
  57. {
  58. #if UNITY_2020_1_OR_NEWER
  59. //UnityEngine.UIElements.EventDispatcher.editorDispatcher.PopDispatcherContext();
  60. object editorDispatcher = mEditorDispatcherProp2020.GetValue(null);
  61. mPopContextMethod2020.Invoke(editorDispatcher, null);
  62. #else
  63. // m_Parent.visualTree.panel.dispatcher?.PopDispatcherContext();
  64. object dispatcher = GetDispatcher(window);
  65. if (dispatcher != null)
  66. mPopContextMethod.Invoke(dispatcher, null);
  67. #endif
  68. }
  69. static void PushDispatcherContext(EditorWindow window)
  70. {
  71. #if UNITY_2020_1_OR_NEWER
  72. //UnityEngine.UIElements.EventDispatcher.editorDispatcher.PushDispatcherContext();
  73. object editorDispatcher = mEditorDispatcherProp2020.GetValue(null);
  74. mPushContextMethod2020.Invoke(editorDispatcher, null);
  75. #else
  76. // m_Parent.visualTree.panel.dispatcher?.PushDispatcherContext();
  77. object dispatcher = GetDispatcher(window);
  78. if (dispatcher != null)
  79. mPushContextMethod.Invoke(dispatcher, null);
  80. #endif
  81. }
  82. static object GetDispatcher(EditorWindow window)
  83. {
  84. object dispatcher = null;
  85. if (MayHaveDispatcher())
  86. {
  87. var parent = mParentField.GetValue(window);
  88. if (parent != null)
  89. {
  90. var visualTree = mVisualTreeProp.GetValue(parent, null);
  91. if (visualTree != null)
  92. {
  93. var panel = mPanelProp.GetValue(visualTree, null);
  94. if (panel != null)
  95. {
  96. dispatcher = mDispatcherProp.GetValue(panel, null);
  97. }
  98. }
  99. }
  100. }
  101. return dispatcher;
  102. }
  103. static bool MayHaveDispatcher()
  104. {
  105. return mDispatcherType != null
  106. && mPushContextMethod != null
  107. && mPopContextMethod != null;
  108. }
  109. static void InitializeInfo()
  110. {
  111. var flags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static;
  112. mMakeModalMethod = BuildMakeModalMethodInfo(flags);
  113. mShowWithModeMethod = typeof(EditorWindow).GetMethod("ShowWithMode", flags);
  114. mParentField = typeof(EditorWindow).GetField("m_Parent", flags);
  115. var hostViewType = mParentField.FieldType;
  116. mParentWindowProp = hostViewType.GetProperty("window", flags);
  117. var savedGUIStateType = typeof(EditorWindow).Assembly.GetType("UnityEditor.SavedGUIState");
  118. mCreateSavedGUIState = savedGUIStateType.GetMethod("Create", flags);
  119. mApplyAndForgetMethod = savedGUIStateType.GetMethod("ApplyAndForget", flags);
  120. #if UNITY_2020_1_OR_NEWER
  121. mEditorDispatcherProp2020 = typeof(UnityEngine.UIElements.EventDispatcher).GetProperty("editorDispatcher", flags);
  122. mPushContextMethod2020 = mEditorDispatcherProp2020.PropertyType.GetMethod("PushDispatcherContext", flags);
  123. mPopContextMethod2020 = mEditorDispatcherProp2020.PropertyType.GetMethod("PopDispatcherContext", flags);
  124. #endif
  125. flags = BindingFlags.NonPublic
  126. | BindingFlags.Instance
  127. | BindingFlags.Public;
  128. mParentField = typeof(EditorWindow).GetField("m_Parent", flags);
  129. if (mParentField != null)
  130. hostViewType = mParentField.FieldType;
  131. if (hostViewType != null)
  132. mVisualTreeProp = hostViewType.GetProperty("visualTree");
  133. if (mVisualTreeProp != null)
  134. {
  135. var visualTreeType = mVisualTreeProp.PropertyType;
  136. if (visualTreeType != null)
  137. {
  138. mPanelProp = visualTreeType.GetProperty("panel");
  139. if (mPanelProp != null)
  140. {
  141. var panelType = mPanelProp.PropertyType;
  142. if (panelType != null)
  143. {
  144. mDispatcherProp = panelType.GetProperty("dispatcher");
  145. if (mDispatcherProp != null)
  146. {
  147. mDispatcherType = mDispatcherProp.PropertyType;
  148. if (mDispatcherType != null)
  149. {
  150. mPushContextMethod = mDispatcherType.GetMethod("PushDispatcherContext", flags);
  151. mPopContextMethod = mDispatcherType.GetMethod("PopDispatcherContext", flags);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. static MethodInfo BuildMakeModalMethodInfo(BindingFlags flags)
  160. {
  161. if (EditorVersion.IsCurrentEditorOlderThan("2019.3.10f1"))
  162. return typeof(EditorWindow).GetMethod("MakeModal", flags);
  163. return typeof(EditorWindow).GetMethod("Internal_MakeModal", flags);
  164. }
  165. static FieldInfo mParentField;
  166. static PropertyInfo mParentWindowProp;
  167. static MethodInfo mMakeModalMethod;
  168. static MethodInfo mShowWithModeMethod;
  169. static MethodInfo mCreateSavedGUIState;
  170. static MethodInfo mApplyAndForgetMethod;
  171. static PropertyInfo mVisualTreeProp;
  172. static Type mDispatcherType;
  173. static MethodInfo mPushContextMethod;
  174. static MethodInfo mPopContextMethod;
  175. static PropertyInfo mPanelProp;
  176. static PropertyInfo mDispatcherProp;
  177. #if UNITY_2020_1_OR_NEWER
  178. static PropertyInfo mEditorDispatcherProp2020;
  179. static MethodInfo mPushContextMethod2020;
  180. static MethodInfo mPopContextMethod2020;
  181. #endif
  182. // // How ContainerWindows are visualized. Used with ContainerWindow.Show
  183. // internal enum ShowMode
  184. // {
  185. // // Show as a normal window with max, min & close buttons.
  186. // NormalWindow = 0,
  187. // // Used for a popup menu. On mac this means light shadow and no titlebar.
  188. // PopupMenu = 1,
  189. // // Utility window - floats above the app. Disappears when app loses focus.
  190. // Utility = 2,
  191. // // Window has no shadow or decorations. Used internally for dragging stuff around.
  192. // NoShadow = 3,
  193. // // The Unity main window. On mac, this is the same as NormalWindow, except window doesn't have a close button.
  194. // MainWindow = 4,
  195. // // Aux windows. The ones that close the moment you move the mouse out of them.
  196. // AuxWindow = 5,
  197. // // Like PopupMenu, but without keyboard focus
  198. // Tooltip = 6
  199. // }
  200. }
  201. }