diff --git a/src/GitHub.Api/Application/ApplicationManagerBase.cs b/src/GitHub.Api/Application/ApplicationManagerBase.cs index 34c2fa9bc..c8a9c8302 100644 --- a/src/GitHub.Api/Application/ApplicationManagerBase.cs +++ b/src/GitHub.Api/Application/ApplicationManagerBase.cs @@ -234,15 +234,13 @@ public void Dispose() public CancellationToken CancellationToken { get { return TaskManager.Token; } } public ITaskManager TaskManager { get; protected set; } public IGitClient GitClient { get; protected set; } - - - protected TaskScheduler UIScheduler { get; private set; } - protected SynchronizationContext SynchronizationContext { get; private set; } - protected IRepositoryManager RepositoryManager { get { return repositoryManager; } } - public ISettings LocalSettings { get; protected set; } public ISettings SystemSettings { get; protected set; } public ISettings UserSettings { get; protected set; } public IUsageTracker UsageTracker { get; protected set; } + + protected TaskScheduler UIScheduler { get; private set; } + protected SynchronizationContext SynchronizationContext { get; private set; } + protected IRepositoryManager RepositoryManager { get { return repositoryManager; } } } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationManager.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationManager.cs index f8e636ad2..aa096fb93 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationManager.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationManager.cs @@ -23,6 +23,7 @@ public ApplicationManager(IMainThreadSynchronizationContext synchronizationConte protected override void SetupMetrics() { + new ActionTask(CancellationToken, () => SetupMetrics(Environment.UnityVersion, ApplicationCache.Instance.FirstRun)) { Affinity = TaskAffinity.UI } @@ -33,7 +34,7 @@ protected override void InitializeUI() { Logger.Trace("Restarted {0}", Environment.Repository); ProjectWindowInterface.Initialize(Environment.Repository); - var view = Window.GetView(); + var view = Window.GetWindow(); if (view != null) view.Initialize(this); } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/EntryPoint.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/EntryPoint.cs index e64141314..323308227 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/EntryPoint.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/EntryPoint.cs @@ -11,8 +11,6 @@ namespace GitHub.Unity [InitializeOnLoad] class EntryPoint : ScriptableObject { - private static ApplicationManager appManager; - // this may run on the loader thread if it's an appdomain restart static EntryPoint() { @@ -69,6 +67,7 @@ private static bool ServerCertificateValidationCallback(object sender, X509Certi return true; } + private static ApplicationManager appManager; public static IApplicationManager ApplicationManager { get diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationView.cs index bbd8282f7..bded38e4e 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationView.cs @@ -61,14 +61,14 @@ public override void InitializeView(IView parent) need2fa = busy = false; } - public override void OnShow() + public override void OnEnable() { - base.OnShow(); + base.OnEnable(); } - public override void OnHide() + public override void OnDisable() { - base.OnHide(); + base.OnDisable(); } public override void OnGUI() diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationWindow.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationWindow.cs index fbfbb6fc8..18fe1b32c 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationWindow.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationWindow.cs @@ -27,45 +27,45 @@ public static IView Open(Action onClose = null) return authWindow; } - public override void OnGUI() + public override void Initialize(IApplicationManager applicationManager) { + base.Initialize(applicationManager); if (authView == null) - { - CreateViews(); - } - authView.OnGUI(); - } - - public override void Refresh() - { - authView.Refresh(); + authView = new AuthenticationView(); + authView.InitializeView(this); } public override void OnEnable() { + base.OnEnable(); + // Set window title titleContent = new GUIContent(Title, Styles.SmallLogo); + authView.OnEnable(); + } - Utility.UnregisterReadyCallback(CreateViews); - Utility.RegisterReadyCallback(CreateViews); - - Utility.UnregisterReadyCallback(ShowActiveView); - Utility.RegisterReadyCallback(ShowActiveView); + public override void OnDisable() + { + base.OnDisable(); + authView.OnDisable(); } - private void CreateViews() + public override void OnUI() { - if (authView == null) - authView = new AuthenticationView(); + base.OnUI(); + authView.OnGUI(); + } - Initialize(EntryPoint.ApplicationManager); - authView.InitializeView(this); + public override void Refresh() + { + base.Refresh(); + authView.Refresh(); } - private void ShowActiveView() + public override void OnSelectionChange() { - authView.OnShow(); - Refresh(); + base.OnSelectionChange(); + authView.OnSelectionChange(); } public override void Finish(bool result) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs index 4ce7c2a5f..385badde2 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs @@ -44,9 +44,9 @@ public override void InitializeView(IView parent) targetMode = mode; } - public override void OnShow() + public override void OnEnable() { - base.OnShow(); + base.OnEnable(); if (Repository != null) { Repository.OnLocalBranchListChanged += RunRefreshEmbeddedOnMainThread; @@ -55,9 +55,9 @@ public override void OnShow() } } - public override void OnHide() + public override void OnDisable() { - base.OnHide(); + base.OnDisable(); if (Repository != null) { Repository.OnLocalBranchListChanged -= RunRefreshEmbeddedOnMainThread; diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs index 9830b7059..e9e847c51 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs @@ -33,9 +33,9 @@ public override void InitializeView(IView parent) tree.InitializeView(this); } - public override void OnShow() + public override void OnEnable() { - base.OnShow(); + base.OnEnable(); if (Repository == null) return; @@ -44,9 +44,9 @@ public override void OnShow() Repository.Refresh(); } - public override void OnHide() + public override void OnDisable() { - base.OnHide(); + base.OnDisable(); if (Repository == null) return; Repository.OnRepositoryChanged -= RunStatusUpdateOnMainThread; diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs index 5c711e580..57d801d60 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs @@ -59,19 +59,14 @@ class HistoryView : Subview [SerializeField] private List history = new List(); [SerializeField] private bool isBusy; - public override void Initialize(IApplicationManager applicationManager) + public override void InitializeView(IView parent) { - base.Initialize(applicationManager); + base.InitializeView(parent); + if (Manager != null) { UpdateLog(); } - } - - public override void InitializeView(IView parent) - { - Logger.Trace("InitializeView(IView)"); - base.InitializeView(parent); lastWidth = Position.width; selectionIndex = newSelectionIndex = -1; @@ -86,9 +81,9 @@ public override void InitializeView(IView parent) } } - public override void OnShow() + public override void OnEnable() { - base.OnShow(); + base.OnEnable(); if (Repository != null) { Repository.OnCommitChanged += UpdateLogOnMainThread; @@ -97,9 +92,9 @@ public override void OnShow() UpdateLog(); } - public override void OnHide() + public override void OnDisable() { - base.OnHide(); + base.OnDisable(); if (Repository != null) { Repository.OnCommitChanged -= UpdateLogOnMainThread; diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/IView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/IView.cs index fdca7162e..f04d0e909 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/IView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/IView.cs @@ -5,7 +5,8 @@ namespace GitHub.Unity { interface IView { - void Initialize(IApplicationManager applicationManager); + void OnEnable(); + void OnDisable(); void Refresh(); void Redraw(); Rect Position { get; } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/SettingsView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/SettingsView.cs index 6637ebcdd..93320e21b 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/SettingsView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/SettingsView.cs @@ -119,9 +119,9 @@ private void Repository_OnActiveRemoteChanged(string remote) repositoryRemoteUrl = currentRemote.Value.Url; } - public override void OnShow() + public override void OnEnable() { - base.OnShow(); + base.OnEnable(); if (Repository == null) return; @@ -140,9 +140,9 @@ public override void OnShow() gitEmail = Repository.User.Email; } - public override void OnHide() + public override void OnDisable() { - base.OnHide(); + base.OnDisable(); } private void RunLocksUpdateOnMainThread(IEnumerable locks) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Subview.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Subview.cs index 2494756ef..0657443a7 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Subview.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Subview.cs @@ -6,13 +6,28 @@ namespace GitHub.Unity { abstract class BaseWindow : EditorWindow, IView { - private bool finishCalled = false; + [NonSerialized] private bool finishCalled = false; + [NonSerialized] private bool initialized = false; + + [NonSerialized] private IApplicationManager cachedManager; + [NonSerialized] private bool initializeWasCalled; + [NonSerialized] private bool inLayout; public event Action OnClose; public virtual void Initialize(IApplicationManager applicationManager) { + Logger.Trace("Initialize ApplicationManager:{0} Initialized:{1}", applicationManager, initialized); + + if (inLayout) + { + initializeWasCalled = true; + cachedManager = applicationManager; + return; + } + Manager = applicationManager; + initialized = true; } public virtual void Redraw() @@ -23,6 +38,7 @@ public virtual void Redraw() public virtual void Refresh() { } + public virtual void Finish(bool result) { finishCalled = true; @@ -36,37 +52,65 @@ protected void RaiseOnClose(bool result) public virtual void Awake() { + Logger.Trace("Awake Initialized:{0}", initialized); + if (!initialized) + Initialize(EntryPoint.ApplicationManager); } public virtual void OnEnable() { + Logger.Trace("OnEnable Initialized:{0}", initialized); + if (!initialized) + Initialize(EntryPoint.ApplicationManager); } - public virtual void OnDisable() + public virtual void OnDisable() {} + + public virtual void Update() {} + + // OnGUI calls this everytime, so override it to render as you would OnGUI + public virtual void OnUI() {} + + // This is Unity's magic method + private void OnGUI() { + if (Event.current.type == EventType.layout) + { + inLayout = true; + } + + OnUI(); + + if (Event.current.type == EventType.repaint) + { + inLayout = false; + if (initializeWasCalled) + { + initializeWasCalled = false; + Initialize(cachedManager); + } + } } - public virtual void Update() {} - public virtual void OnGUI() {} public virtual void OnDestroy() { if (!finishCalled) { RaiseOnClose(false); } - } + public virtual void OnSelectionChange() {} public virtual Rect Position { get { return position; } } - public IApplicationManager Manager { get; private set; } - public IRepository Repository { get { return Manager != null ? Manager.Environment.Repository : null; } } - public ITaskManager TaskManager { get { return Manager != null ? Manager.TaskManager : null; } } - protected IGitClient GitClient { get { return Manager != null ? Manager.GitClient : null; } } - protected IEnvironment Environment { get { return Manager != null ? Manager.Environment : null; } } - protected IPlatform Platform { get { return Manager != null ? Manager.Platform : null; } } + public IApplicationManager Manager { get; private set; } + public IRepository Repository { get { return Environment.Repository; } } + public ITaskManager TaskManager { get { return Manager.TaskManager; } } + protected IGitClient GitClient { get { return Manager.GitClient; } } + protected IEnvironment Environment { get { return Manager.Environment; } } + protected IPlatform Platform { get { return Manager.Platform; } } private ILogging logger; protected ILogging Logger @@ -86,41 +130,26 @@ abstract class Subview : IView private const string NullParentError = "Subview parent is null"; protected IView Parent { get; private set; } - public IApplicationManager Manager { get; private set; } - public IRepository Repository { get { return Manager != null ? Manager.Environment.Repository : null; } } - public ITaskManager TaskManager { get { return Manager != null ? Manager.TaskManager : null; } } - protected IGitClient GitClient { get { return Manager != null ? Manager.GitClient : null; } } - protected IEnvironment Environment { get { return Manager != null ? Manager.Environment : null; } } - protected IPlatform Platform { get { return Manager != null ? Manager.Platform : null; } } - - public virtual void Initialize(IApplicationManager applicationManager) - { - Logger.Trace("Initialize(IApplicationManager: {0})", applicationManager != null); - Manager = applicationManager; - } + public IApplicationManager Manager { get { return Parent.Manager; } } + public IRepository Repository { get { return Manager.Environment.Repository; } } + public ITaskManager TaskManager { get { return Manager.TaskManager; } } + protected IGitClient GitClient { get { return Manager.GitClient; } } + protected IEnvironment Environment { get { return Manager.Environment; } } + protected IPlatform Platform { get { return Manager.Platform; } } public virtual void InitializeView(IView parent) { Debug.Assert(parent != null, NullParentError); Parent = parent; - ((IView)this).Initialize(parent.Manager); } - public virtual void OnShow() - { - } - - public virtual void OnHide() - { - } - - public virtual void OnUpdate() + public virtual void OnEnable() {} - public virtual void OnGUI() + public virtual void OnDisable() {} - public virtual void OnDestroy() + public virtual void OnGUI() {} public virtual void OnSelectionChange() diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Window.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Window.cs index da7723368..f8c0c9eea 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Window.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Window.cs @@ -57,9 +57,20 @@ public static void ShowWindow(IApplicationManager applicationManager) window.Show(); } - public static IView GetView() + public static Window GetWindow() { - return Resources.FindObjectsOfTypeAll(typeof(Window)).FirstOrDefault() as IView; + return Resources.FindObjectsOfTypeAll(typeof(Window)).FirstOrDefault() as Window; + } + + public override void Initialize(IApplicationManager applicationManager) + { + base.Initialize(applicationManager); + + HistoryTab.InitializeView(this); + ChangesTab.InitializeView(this); + BranchesTab.InitializeView(this); + SettingsTab.InitializeView(this); + ActiveTab.InitializeView(this); } public override void OnEnable() @@ -73,48 +84,34 @@ public override void OnEnable() // Set window title titleContent = new GUIContent(Title, Styles.SmallLogo); - historyTab.InitializeView(this); - changesTab.InitializeView(this); - branchesTab.InitializeView(this); - settingsTab.InitializeView(this); - } - - public override void Refresh() - { if (ActiveTab != null) - ActiveTab.Refresh(); + ActiveTab.OnEnable(); } - private void ShowActiveView() + public override void OnDisable() { - if (Repository == null) - return; - + base.OnDisable(); if (ActiveTab != null) - ActiveTab.OnShow(); - Refresh(); + ActiveTab.OnDisable(); } - private void SwitchView(Subview from, Subview to) + public override void OnSelectionChange() { - GUI.FocusControl(null); - from.OnHide(); - to.OnShow(); - Refresh(); + base.OnSelectionChange(); + if (ActiveTab != null) + ActiveTab.OnSelectionChange(); } - public override void OnDisable() + public override void Refresh() { - base.OnDisable(); - + base.Refresh(); if (ActiveTab != null) - ActiveTab.OnHide(); + ActiveTab.Refresh(); } - - public override void OnGUI() + public override void OnUI() { - base.OnGUI(); + base.OnUI(); if (Repository != null) { @@ -130,6 +127,28 @@ public override void OnGUI() } } + public override void Update() + { + base.Update(); + + // Notification auto-clear timer override + if (notificationClearTime > 0f && EditorApplication.timeSinceStartup > notificationClearTime) + { + notificationClearTime = -1f; + RemoveNotification(); + Redraw(); + } + } + + private void SwitchView(Subview from, Subview to) + { + GUI.FocusControl(null); + if (from != null) + from.OnDisable(); + to.OnEnable(); + Refresh(); + } + private void DoHeaderGUI() { GUILayout.BeginHorizontal(Styles.HeaderBoxStyle); @@ -253,17 +272,6 @@ private bool ValidateSettings() return true; } - public override void Update() - { - // Notification auto-clear timer override - if (notificationClearTime > 0f && EditorApplication.timeSinceStartup > notificationClearTime) - { - notificationClearTime = -1f; - RemoveNotification(); - Redraw(); - } - } - public new void ShowNotification(GUIContent content) { ShowNotification(content, DefaultNotificationTimeout); @@ -282,12 +290,6 @@ private static SubTab TabButton(SubTab tab, string title, SubTab activeTab) return GUILayout.Toggle(activeTab == tab, title, EditorStyles.toolbarButton) ? tab : activeTab; } - public override void OnSelectionChange() - { - if (ActiveTab != null) - ActiveTab.OnSelectionChange(); - } - public HistoryView HistoryTab { get { return historyTab; } @@ -339,16 +341,5 @@ private enum SubTab Branches, Settings } - - public override void Initialize(IApplicationManager applicationManager) - { - base.Initialize(applicationManager); - - HistoryTab.InitializeView(this); - ChangesTab.InitializeView(this); - BranchesTab.InitializeView(this); - SettingsTab.InitializeView(this); - ActiveTab.InitializeView(this); - } } }