private void OnDestroy()
 {
     if (IsRecording)
     {
         TestableEditorElements.Panic();
         if (recordedWindow != null)
         {
             recordedWindow.Close();
         }
     }
 }
        private void Abort()
        {
            IsRecording = false;
            TestableEditorElements.Panic();
            if (recordedWindow != null)
            {
                recordedWindow.Close();
            }

            Close();
        }
        private void OnGUI()
        {
            try
            {
                if (Event.current.type == EventType.ExecuteCommand && Event.current.commandName == "Abort")
                {
                    Abort();
                    return;
                }

                if (Event.current.type == EventType.ExecuteCommand && Event.current.commandName == "SaveAndTerminate")
                {
                    SaveAndTerminate();
                    return;
                }

                Rect newPos = recordedWindow.position;

                minSize  = newPos.size;
                maxSize  = newPos.size;
                position = newPos;

                MethodInfo onGui = recordedWindow.GetType().GetMethod("OnGUI", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

                if (Event.current.type != EventType.Used)
                {
                    if (ShouldRecordEvent())
                    {
                        if (userActions.Any())
                        {
                            userActions.Last().PrepickedSelections = TestableEditorElements.StopRecording();
                            TestableEditorElements.StartRecording();
                        }

                        Event toRecord = JsonConvert.DeserializeObject <Event>(JsonConvert.SerializeObject(Event.current, serializerSettings), serializerSettings);

                        UserAction userActionToRecord = new UserAction {
                            Event = toRecord
                        };

                        if (ShouldReplaceLastUserAction())
                        {
                            userActions[userActions.Count - 1] = userActionToRecord;
                        }
                        else
                        {
                            userActions.Add(userActionToRecord);
                        }
                    }

                    onGui.Invoke(recordedWindow, new object[0]);
                }

                Focus();
            }
            catch
            {
                IsRecording = false;
                TestableEditorElements.Panic();
                if (recordedWindow != null)
                {
                    recordedWindow.Close();
                }

                Close();
                throw;
            }
        }