From 7dce10d12e07c72ec6dbe838b0833ebbbaba168d Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 14 Dec 2017 09:07:33 -0500 Subject: [PATCH 1/6] Undoing global selection --- .../GitHub.Unity/UI/ChangesTreeControl.cs | 4 ---- .../Editor/GitHub.Unity/UI/TreeControl.cs | 17 +---------------- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs index fdeab1f3b..bed377241 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs @@ -92,10 +92,6 @@ public override ChangesTreeNode SelectedNode set { selectedNode = value; - if (value != null && selectionObject) - { - Selection.activeObject = selectionObject; - } } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs index fe83ac211..27bbf1913 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs @@ -9,8 +9,6 @@ namespace GitHub.Unity [Serializable] public class TreeNodeDictionary : SerializableDictionary { } - public class TreeSelection : ScriptableObject { } - [Serializable] public abstract class Tree: TreeBase where TNode : TreeNode @@ -30,18 +28,12 @@ public abstract class Tree: TreeBase [NonSerialized] private TNode rightClickNextRenderNode; [NonSerialized] private int controlId; - [NonSerialized] protected TreeSelection selectionObject; public bool IsInitialized { get { return Nodes != null && Nodes.Count > 0 && !String.IsNullOrEmpty(Nodes[0].Path); } } public bool RequiresRepaint { get; private set; } public Rect Render(Rect treeDisplayRect, Vector2 scroll, Action singleClick = null, Action doubleClick = null, Action rightClick = null) { - if (Selection.activeObject != selectionObject) - { - SelectedNode = null; - } - controlId = GUIUtility.GetControlID(FocusType.Keyboard); var treeHasFocus = GUIUtility.keyboardControl == controlId; @@ -333,10 +325,7 @@ protected void LoadNodeIcons() public void OnEnable() { - if (!selectionObject) - { - selectionObject = ScriptableObject.CreateInstance(); - } + } } @@ -581,10 +570,6 @@ public override TreeNode SelectedNode set { selectedNode = value; - if (value != null && selectionObject) - { - Selection.activeObject = selectionObject; - } } } From 4af80f7847409b524a18c30febccac642a9ea194 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 14 Dec 2017 09:39:09 -0500 Subject: [PATCH 2/6] Functionality to make global selection optional --- .../Editor/GitHub.Unity/UI/BranchesView.cs | 2 - .../GitHub.Unity/UI/ChangesTreeControl.cs | 41 +++++++++++++++++++ .../Editor/GitHub.Unity/UI/ChangesView.cs | 1 - .../Editor/GitHub.Unity/UI/TreeControl.cs | 7 +--- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs index 49a13bb6b..435476808 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs @@ -179,13 +179,11 @@ private void TreeOnEnable() { if (treeLocals != null) { - treeLocals.OnEnable(); treeLocals.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon); } if (treeRemotes != null) { - treeRemotes.OnEnable(); treeRemotes.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon); } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs index bed377241..88a92c919 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs @@ -3,6 +3,7 @@ using System.Linq; using UnityEditor; using UnityEngine; +using Object = UnityEngine.Object; namespace GitHub.Unity { @@ -47,8 +48,10 @@ public class ChangesTree : Tree [SerializeField] public bool displayRootNode = true; [SerializeField] public bool isSelectable = true; [SerializeField] public bool isCheckable = false; + [SerializeField] public bool isUsingGlobalSelection = false; [SerializeField] private List nodes = new List(); [SerializeField] private ChangesTreeNode selectedNode = null; + [NonSerialized] private Object lastActiveObject; public override string Title { @@ -92,6 +95,27 @@ public override ChangesTreeNode SelectedNode set { selectedNode = value; + + if (!IsUsingGlobalSelection) + { + return; + } + + Object activeObject = null; + if (selectedNode != null) + { + var projectPath = selectedNode.ProjectPath; + if (projectPath.StartsWith("Assets")) + { + var assetGuid = AssetDatabase.AssetPathToGUID(projectPath); + activeObject = !string.IsNullOrEmpty(assetGuid) + ? AssetDatabase.LoadMainAssetAtPath(projectPath) + : null; + } + } + + lastActiveObject = activeObject; + Selection.activeObject = activeObject; } } @@ -100,6 +124,12 @@ protected override List Nodes get { return nodes; } } + public bool IsUsingGlobalSelection + { + get { return isUsingGlobalSelection; } + set { isUsingGlobalSelection = value; } + } + public void UpdateIcons(Texture2D folderIcon) { var needsLoad = FolderIcon == null; @@ -218,5 +248,16 @@ protected override void AddCheckedNode(ChangesTreeNode node) { checkedFileNodes.Add(((ITreeNode)node).Path, node); } + + public override Rect Render(Rect treeDisplayRect, Vector2 scroll, Action singleClick = null, Action doubleClick = null, + Action rightClick = null) + { + if (IsUsingGlobalSelection && lastActiveObject != null && Selection.activeObject != lastActiveObject) + { + SelectedNode = null; + } + + return base.Render(treeDisplayRect, scroll, singleClick, doubleClick, rightClick); + } } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs index 7cd219917..5becd42a6 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs @@ -227,7 +227,6 @@ private void TreeOnEnable() { if (treeChanges != null) { - treeChanges.OnEnable(); treeChanges.UpdateIcons(Styles.FolderIcon); } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs index 27bbf1913..fc49a6a67 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs @@ -32,7 +32,7 @@ public abstract class Tree: TreeBase public bool IsInitialized { get { return Nodes != null && Nodes.Count > 0 && !String.IsNullOrEmpty(Nodes[0].Path); } } public bool RequiresRepaint { get; private set; } - public Rect Render(Rect treeDisplayRect, Vector2 scroll, Action singleClick = null, Action doubleClick = null, Action rightClick = null) + public virtual Rect Render(Rect treeDisplayRect, Vector2 scroll, Action singleClick = null, Action doubleClick = null, Action rightClick = null) { controlId = GUIUtility.GetControlID(FocusType.Keyboard); var treeHasFocus = GUIUtility.keyboardControl == controlId; @@ -322,11 +322,6 @@ protected void LoadNodeIcons() SetNodeIcon(treeNode); } } - - public void OnEnable() - { - - } } [Serializable] From 765fa835154a0e004d348b851734b03e11d263a1 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 14 Dec 2017 09:40:37 -0500 Subject: [PATCH 3/6] Setting IsUsingGlobalSelection property --- src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs index 8e4bff5d9..cdf3f2d18 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs @@ -28,7 +28,7 @@ class ChangesView : Subview [SerializeField] private string currentBranch = "[unknown]"; [SerializeField] private Vector2 treeScroll; - [SerializeField] private ChangesTree treeChanges = new ChangesTree { DisplayRootNode = false, IsCheckable = true }; + [SerializeField] private ChangesTree treeChanges = new ChangesTree { DisplayRootNode = false, IsCheckable = true, IsUsingGlobalSelection = true }; [SerializeField] private HashSet gitLocks; [SerializeField] private List gitStatusEntries; From 60c338a43870165f78a0a97f10be77436de7fece Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 14 Dec 2017 09:42:57 -0500 Subject: [PATCH 4/6] Cleaning up some method calls --- .../Editor/GitHub.Unity/UI/BranchesView.cs | 25 ++++++++----------- .../Editor/GitHub.Unity/UI/ChangesView.cs | 15 +++++------ .../Editor/GitHub.Unity/UI/HistoryView.cs | 16 +++++------- 3 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs index 2c2501e0f..4158697ed 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs @@ -62,7 +62,17 @@ public override void InitializeView(IView parent) public override void OnEnable() { base.OnEnable(); - TreeOnEnable(); + + if (treeLocals != null) + { + treeLocals.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon); + } + + if (treeRemotes != null) + { + treeRemotes.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon); + } + AttachHandlers(Repository); Repository.CheckLocalAndRemoteBranchListChangedEvent(lastLocalAndRemoteBranchListChangedEvent); } @@ -163,19 +173,6 @@ private void BuildTree() Redraw(); } - private void TreeOnEnable() - { - if (treeLocals != null) - { - treeLocals.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon); - } - - if (treeRemotes != null) - { - treeRemotes.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon); - } - } - private void OnButtonBarGUI() { if (mode == BranchesMode.Default) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs index cdf3f2d18..a8ea1cbfe 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs @@ -42,7 +42,12 @@ class ChangesView : Subview public override void OnEnable() { base.OnEnable(); - TreeOnEnable(); + + if (treeChanges != null) + { + treeChanges.UpdateIcons(Styles.FolderIcon); + } + AttachHandlers(Repository); Repository.CheckCurrentBranchChangedEvent(lastCurrentBranchChangedEvent); Repository.CheckStatusEntriesChangedEvent(lastStatusEntriesChangedEvent); @@ -217,14 +222,6 @@ private void BuildTree() Redraw(); } - private void TreeOnEnable() - { - if (treeChanges != null) - { - treeChanges.UpdateIcons(Styles.FolderIcon); - } - } - private void OnCommitDetailsAreaGUI() { GUILayout.BeginHorizontal(); diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs index 5eb16a078..0d89b6400 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs @@ -351,7 +351,12 @@ class HistoryView : Subview public override void OnEnable() { base.OnEnable(); - TreeOnEnable(); + + if (treeChanges != null) + { + treeChanges.UpdateIcons(Styles.FolderIcon); + } + AttachHandlers(Repository); if (Repository != null) @@ -721,15 +726,6 @@ private void BuildTree() Redraw(); } - private void TreeOnEnable() - { - if (treeChanges != null) - { - treeChanges.OnEnable(); - treeChanges.UpdateIcons(Styles.FolderIcon); - } - } - public override bool IsBusy { get { return false; } From 98ce81fff13f5e695b7b44cb8133b7224751857d Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 14 Dec 2017 09:54:58 -0500 Subject: [PATCH 5/6] Changing SelectedNode based on Selection.activeObject --- .../GitHub.Unity/UI/ChangesTreeControl.cs | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs index 88a92c919..ff63bec63 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs @@ -104,18 +104,22 @@ public override ChangesTreeNode SelectedNode Object activeObject = null; if (selectedNode != null) { - var projectPath = selectedNode.ProjectPath; - if (projectPath.StartsWith("Assets")) + var path = selectedNode.Path; + if (path != null && path.StartsWith("Assets")) { - var assetGuid = AssetDatabase.AssetPathToGUID(projectPath); + var assetGuid = AssetDatabase.AssetPathToGUID(path); activeObject = !string.IsNullOrEmpty(assetGuid) - ? AssetDatabase.LoadMainAssetAtPath(projectPath) + ? AssetDatabase.LoadMainAssetAtPath(path) : null; } } lastActiveObject = activeObject; - Selection.activeObject = activeObject; + + if (activeObject != null) + { + Selection.activeObject = activeObject; + } } } @@ -252,9 +256,17 @@ protected override void AddCheckedNode(ChangesTreeNode node) public override Rect Render(Rect treeDisplayRect, Vector2 scroll, Action singleClick = null, Action doubleClick = null, Action rightClick = null) { - if (IsUsingGlobalSelection && lastActiveObject != null && Selection.activeObject != lastActiveObject) + if (IsUsingGlobalSelection) { - SelectedNode = null; + if (lastActiveObject != null && Selection.activeObject != lastActiveObject) + { + SelectedNode = null; + } + + if (Selection.activeObject != null) + { + + } } return base.Render(treeDisplayRect, scroll, singleClick, doubleClick, rightClick); From e7b5c77dc40abd498d48ee87a34a60f09cf8e6d3 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 14 Dec 2017 12:52:59 -0500 Subject: [PATCH 6/6] A large correction to the global selection mechanism --- .../Editor/GitHub.Unity/UI/BaseWindow.cs | 16 +++++ .../Editor/GitHub.Unity/UI/BranchesView.cs | 14 ++++ .../GitHub.Unity/UI/ChangesTreeControl.cs | 67 ++++++++++++------- .../Editor/GitHub.Unity/UI/ChangesView.cs | 19 +++++- .../Editor/GitHub.Unity/UI/HistoryView.cs | 12 ++++ .../Assets/Editor/GitHub.Unity/UI/IView.cs | 1 + .../Assets/Editor/GitHub.Unity/UI/Subview.cs | 9 ++- .../Editor/GitHub.Unity/UI/TreeControl.cs | 23 +++++-- .../Assets/Editor/GitHub.Unity/UI/Window.cs | 6 ++ 9 files changed, 133 insertions(+), 34 deletions(-) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BaseWindow.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BaseWindow.cs index 3cc00a234..df1be4f78 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BaseWindow.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BaseWindow.cs @@ -94,6 +94,21 @@ private void OnGUI() } } + private void OnFocus() + { + HasFocus = true; + OnFocusChanged(); + } + + private void OnLostFocus() + { + HasFocus = false; + OnFocusChanged(); + } + + public virtual void OnFocusChanged() + {} + public virtual void OnDestroy() {} @@ -103,6 +118,7 @@ public virtual void OnSelectionChange() public Rect Position { get { return position; } } public IApplicationManager Manager { get; private set; } public abstract bool IsBusy { get; } + public bool HasFocus { get; private set; } public IRepository Repository { get { return inLayout ? cachedRepository : Environment.Repository; } } public bool HasRepository { get { return Repository != null; } } public IUser User { get { return cachedUser; } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs index 4158697ed..a4bfff22e 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs @@ -63,13 +63,16 @@ public override void OnEnable() { base.OnEnable(); + var hasFocus = HasFocus; if (treeLocals != null) { + treeLocals.ViewHasFocus = hasFocus; treeLocals.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon); } if (treeRemotes != null) { + treeRemotes.ViewHasFocus = hasFocus; treeRemotes.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon); } @@ -95,6 +98,17 @@ public override void OnSelectionChange() Redraw(); } + public override void OnFocusChanged() + { + base.OnFocusChanged(); + if(treeLocals.ViewHasFocus != HasFocus || treeRemotes.ViewHasFocus != HasFocus) + { + treeLocals.ViewHasFocus = HasFocus; + treeRemotes.ViewHasFocus = HasFocus; + Redraw(); + } + } + private void RepositoryOnLocalAndRemoteBranchListChanged(CacheUpdateEvent cacheUpdateEvent) { if (!lastLocalAndRemoteBranchListChangedEvent.Equals(cacheUpdateEvent)) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs index ff63bec63..a648f9509 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs @@ -39,6 +39,7 @@ public bool IsLocked [Serializable] public class ChangesTree : Tree { + [SerializeField] public ChangesTreeNodeDictionary assets = new ChangesTreeNodeDictionary(); [SerializeField] public ChangesTreeNodeDictionary folders = new ChangesTreeNodeDictionary(); [SerializeField] public ChangesTreeNodeDictionary checkedFileNodes = new ChangesTreeNodeDictionary(); @@ -51,7 +52,8 @@ public class ChangesTree : Tree [SerializeField] public bool isUsingGlobalSelection = false; [SerializeField] private List nodes = new List(); [SerializeField] private ChangesTreeNode selectedNode = null; - [NonSerialized] private Object lastActiveObject; + [NonSerialized] private bool viewHasFocus; + [NonSerialized] private Object lastActivatedObject; public override string Title { @@ -101,22 +103,13 @@ public override ChangesTreeNode SelectedNode return; } - Object activeObject = null; - if (selectedNode != null) - { - var path = selectedNode.Path; - if (path != null && path.StartsWith("Assets")) - { - var assetGuid = AssetDatabase.AssetPathToGUID(path); - activeObject = !string.IsNullOrEmpty(assetGuid) - ? AssetDatabase.LoadMainAssetAtPath(path) - : null; - } - } + var activeObject = selectedNode != null + ? AssetDatabase.LoadMainAssetAtPath(selectedNode.Path) + : null; - lastActiveObject = activeObject; + lastActivatedObject = activeObject; - if (activeObject != null) + if (TreeHasFocus) { Selection.activeObject = activeObject; } @@ -134,6 +127,12 @@ public bool IsUsingGlobalSelection set { isUsingGlobalSelection = value; } } + public override bool ViewHasFocus + { + get { return viewHasFocus; } + set { viewHasFocus = value; } + } + public void UpdateIcons(Texture2D folderIcon) { var needsLoad = FolderIcon == null; @@ -216,7 +215,7 @@ protected override ChangesTreeNode CreateTreeNode(string path, string label, int CheckState = isChecked ? CheckState.Checked : CheckState.Empty, GitFileStatus = gitFileStatus, ProjectPath = projectPath, - IsLocked = isLocked + IsLocked = isLocked, }; if (isFolder && level >= 0) @@ -224,11 +223,25 @@ protected override ChangesTreeNode CreateTreeNode(string path, string label, int folders.Add(node.Path, node); } + if (IsUsingGlobalSelection) + { + if (node.Path != null) + { + var assetGuid = AssetDatabase.AssetPathToGUID(node.Path); + + if (!string.IsNullOrEmpty(assetGuid)) + { + assets.Add(assetGuid, node); + } + } + } + return node; } protected override void OnClear() { + assets.Clear(); folders.Clear(); checkedFileNodes.Clear(); } @@ -253,23 +266,25 @@ protected override void AddCheckedNode(ChangesTreeNode node) checkedFileNodes.Add(((ITreeNode)node).Path, node); } - public override Rect Render(Rect treeDisplayRect, Vector2 scroll, Action singleClick = null, Action doubleClick = null, - Action rightClick = null) + public bool OnSelectionChange() { - if (IsUsingGlobalSelection) + if (IsUsingGlobalSelection && !TreeHasFocus) { - if (lastActiveObject != null && Selection.activeObject != lastActiveObject) - { - SelectedNode = null; - } + ChangesTreeNode assetNode = null; - if (Selection.activeObject != null) + if (Selection.activeObject != lastActivatedObject) { - + var activeAssetPath = AssetDatabase.GetAssetPath(Selection.activeObject); + var activeAssetGuid = AssetDatabase.AssetPathToGUID(activeAssetPath); + + assets.TryGetValue(activeAssetGuid, out assetNode); } + + SelectedNode = assetNode; + return true; } - return base.Render(treeDisplayRect, scroll, singleClick, doubleClick, rightClick); + return false; } } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs index a8ea1cbfe..6603c035c 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs @@ -45,6 +45,7 @@ public override void OnEnable() if (treeChanges != null) { + treeChanges.ViewHasFocus = HasFocus; treeChanges.UpdateIcons(Styles.FolderIcon); } @@ -110,7 +111,23 @@ public override void OnGUI() public override void OnSelectionChange() { base.OnSelectionChange(); - Redraw(); + if (treeChanges.OnSelectionChange()) + { + Redraw(); + } + } + + public override void OnFocusChanged() + { + Logger.Debug("OnFocusChanged: {0}", HasFocus); + + base.OnFocusChanged(); + var hasFocus = HasFocus; + if (treeChanges.ViewHasFocus != hasFocus) + { + treeChanges.ViewHasFocus = hasFocus; + Redraw(); + } } private void OnTreeGUI(Rect rect) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs index 0d89b6400..b2da0f974 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs @@ -354,6 +354,7 @@ public override void OnEnable() if (treeChanges != null) { + treeChanges.ViewHasFocus = HasFocus; treeChanges.UpdateIcons(Styles.FolderIcon); } @@ -379,6 +380,17 @@ public override void OnDataUpdate() MaybeUpdateData(); } + public override void OnFocusChanged() + { + base.OnFocusChanged(); + var hasFocus = HasFocus; + if (treeChanges.ViewHasFocus != hasFocus) + { + treeChanges.ViewHasFocus = hasFocus; + Redraw(); + } + } + public override void OnGUI() { // History toolbar diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/IView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/IView.cs index 52cc69c2c..dae2d62c6 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/IView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/IView.cs @@ -18,5 +18,6 @@ interface IView bool HasUser { get; } IApplicationManager Manager { get; } bool IsBusy { get; } + bool HasFocus { get; } } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Subview.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Subview.cs index e144f48be..3d3aa648c 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Subview.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Subview.cs @@ -28,10 +28,13 @@ public virtual void OnDataUpdate() {} public virtual void OnGUI() - {} + { } public virtual void OnSelectionChange() - {} + { } + + public virtual void OnFocusChanged() + { } public virtual void Refresh() { @@ -54,6 +57,7 @@ public virtual void Finish(bool result) public bool HasRepository { get { return Parent.HasRepository; } } public IUser User { get { return Parent.User; } } public bool HasUser { get { return Parent.HasUser; } } + public bool HasFocus { get { return Parent != null && Parent.HasFocus; } } public abstract bool IsBusy { get; } protected ITaskManager TaskManager { get { return Manager.TaskManager; } } protected IGitClient GitClient { get { return Manager.GitClient; } } @@ -64,6 +68,7 @@ public virtual void Finish(bool result) public Vector2 Size { get; protected set; } private ILogging logger; + protected ILogging Logger { get diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs index f44fb1133..f3710516f 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs @@ -32,10 +32,9 @@ public abstract class Tree: TreeBase public bool IsInitialized { get { return Nodes != null && Nodes.Count > 0 && !String.IsNullOrEmpty(Nodes[0].Path); } } public bool RequiresRepaint { get; private set; } - public virtual Rect Render(Rect treeDisplayRect, Vector2 scroll, Action singleClick = null, Action doubleClick = null, Action rightClick = null) + public Rect Render(Rect treeDisplayRect, Vector2 scroll, Action singleClick = null, Action doubleClick = null, Action rightClick = null) { controlId = GUIUtility.GetControlID(FocusType.Keyboard); - var treeHasFocus = GUIUtility.keyboardControl == controlId; if (!Nodes.Any()) return new Rect(treeDisplayRect.x, treeDisplayRect.y, 0f, 0f); @@ -43,7 +42,7 @@ public virtual Rect Render(Rect treeDisplayRect, Vector2 scroll, Action s var treeNodeStyle = TreeNodeStyle; var activeTreeNodeStyle = ActiveTreeNodeStyle; - if (treeHasFocus) + if (ViewHasFocus && TreeHasFocus) { treeNodeStyle = FocusedTreeNodeStyle; activeTreeNodeStyle = FocusedActiveTreeNodeStyle; @@ -75,7 +74,7 @@ public virtual Rect Render(Rect treeDisplayRect, Vector2 scroll, Action s var titleDisplay = !(rect.y > endDisplay || rect.yMax < startDisplay); if (titleDisplay) { - var isSelected = SelectedNode == titleNode; + var isSelected = SelectedNode != null && SelectedNode.Path == titleNode.Path; renderResult = titleNode.Render(rect, Styles.TreeIndentation, isSelected, FolderStyle, treeNodeStyle, activeTreeNodeStyle); } @@ -109,7 +108,7 @@ public virtual Rect Render(Rect treeDisplayRect, Vector2 scroll, Action s var display = !(rect.y > endDisplay || rect.yMax < startDisplay); if (display) { - var isSelected = SelectedNode == node; + var isSelected = SelectedNode != null && SelectedNode.Path == node.Path; renderResult = node.Render(rect, Styles.TreeIndentation, isSelected, FolderStyle, treeNodeStyle, activeTreeNodeStyle); } @@ -147,6 +146,13 @@ public virtual Rect Render(Rect treeDisplayRect, Vector2 scroll, Action s return rect; } + protected bool TreeHasFocus + { + get { return GUIUtility.keyboardControl == controlId; } + } + + public abstract bool ViewHasFocus { get; set; } + public void Focus() { bool selectionChanged = false; @@ -522,6 +528,7 @@ public class BranchesTree : Tree [SerializeField] public bool isCheckable = false; [SerializeField] private List nodes = new List(); [SerializeField] private TreeNode selectedNode = null; + [NonSerialized] private bool viewFocus; public override string Title { @@ -573,6 +580,12 @@ protected override List Nodes get { return nodes; } } + public override bool ViewHasFocus + { + get { return viewFocus; } + set { viewFocus = value; } + } + public void UpdateIcons(Texture2D activeBranchIcon, Texture2D branchIcon, Texture2D folderIcon, Texture2D globeIcon) { var needsLoad = ActiveBranchIcon == null || BranchIcon == null || FolderIcon == null || GlobeIcon == null; diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Window.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Window.cs index e741af05e..54afa6a1e 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Window.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Window.cs @@ -126,6 +126,12 @@ public override void OnDataUpdate() ActiveView.OnDataUpdate(); } + public override void OnFocusChanged() + { + if (ActiveView != null) + ActiveView.OnFocusChanged(); + } + public override void OnRepositoryChanged(IRepository oldRepository) { base.OnRepositoryChanged(oldRepository);