- 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)
- {
-
- 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)
- {
-
- mShowWithModeMethod.Invoke(window, new object[] { 2 });
- }
- static object CreateSavedGUIState()
- {
-
- return mCreateSavedGUIState.Invoke(null, null);
- }
- static void ApplySavedGUIState(object savedGUIState)
- {
-
- mApplyAndForgetMethod.Invoke(savedGUIState, null);
- }
- static void PopDispatcherContext(EditorWindow window)
- {
-
- object editorDispatcher = mEditorDispatcherProp2020.GetValue(null);
- mPopContextMethod2020.Invoke(editorDispatcher, null);
- }
- static void PushDispatcherContext(EditorWindow window)
- {
-
- object editorDispatcher = mEditorDispatcherProp2020.GetValue(null);
- mPushContextMethod2020.Invoke(editorDispatcher, null);
- }
- 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);
- mEditorDispatcherProp2020 = typeof(UnityEngine.UIElements.EventDispatcher).GetProperty("editorDispatcher", flags);
- mPushContextMethod2020 = mEditorDispatcherProp2020.PropertyType.GetMethod("PushDispatcherContext", flags);
- mPopContextMethod2020 = mEditorDispatcherProp2020.PropertyType.GetMethod("PopDispatcherContext", flags);
- 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)
- {
- 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;
- static PropertyInfo mEditorDispatcherProp2020;
- static MethodInfo mPushContextMethod2020;
- static MethodInfo mPopContextMethod2020;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }