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 4ae3c6e17..a4bfff22e 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs @@ -62,7 +62,20 @@ public override void InitializeView(IView parent) public override void OnEnable() { base.OnEnable(); - TreeOnEnable(); + + 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); + } + AttachHandlers(Repository); Repository.CheckLocalAndRemoteBranchListChangedEvent(lastLocalAndRemoteBranchListChangedEvent); } @@ -85,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)) @@ -163,21 +187,6 @@ private void BuildTree() Redraw(); } - 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); - } - } - private void OnButtonBarGUI() { if (mode == BranchesMode.Default) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs index fdeab1f3b..a648f9509 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 { @@ -38,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(); @@ -47,8 +49,11 @@ 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 bool viewHasFocus; + [NonSerialized] private Object lastActivatedObject; public override string Title { @@ -92,9 +97,21 @@ public override ChangesTreeNode SelectedNode set { selectedNode = value; - if (value != null && selectionObject) + + if (!IsUsingGlobalSelection) + { + return; + } + + var activeObject = selectedNode != null + ? AssetDatabase.LoadMainAssetAtPath(selectedNode.Path) + : null; + + lastActivatedObject = activeObject; + + if (TreeHasFocus) { - Selection.activeObject = selectionObject; + Selection.activeObject = activeObject; } } } @@ -104,6 +121,18 @@ protected override List Nodes get { return nodes; } } + public bool IsUsingGlobalSelection + { + get { return isUsingGlobalSelection; } + set { isUsingGlobalSelection = value; } + } + + public override bool ViewHasFocus + { + get { return viewHasFocus; } + set { viewHasFocus = value; } + } + public void UpdateIcons(Texture2D folderIcon) { var needsLoad = FolderIcon == null; @@ -186,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) @@ -194,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(); } @@ -222,5 +265,26 @@ protected override void AddCheckedNode(ChangesTreeNode node) { checkedFileNodes.Add(((ITreeNode)node).Path, node); } + + public bool OnSelectionChange() + { + if (IsUsingGlobalSelection && !TreeHasFocus) + { + ChangesTreeNode assetNode = 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 false; + } } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs index f6b31fa54..6603c035c 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; @@ -42,7 +42,13 @@ class ChangesView : Subview public override void OnEnable() { base.OnEnable(); - TreeOnEnable(); + + if (treeChanges != null) + { + treeChanges.ViewHasFocus = HasFocus; + treeChanges.UpdateIcons(Styles.FolderIcon); + } + AttachHandlers(Repository); Repository.CheckCurrentBranchChangedEvent(lastCurrentBranchChangedEvent); Repository.CheckStatusEntriesChangedEvent(lastStatusEntriesChangedEvent); @@ -105,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) @@ -217,15 +239,6 @@ private void BuildTree() Redraw(); } - private void TreeOnEnable() - { - if (treeChanges != null) - { - treeChanges.OnEnable(); - 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..b2da0f974 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs @@ -351,7 +351,13 @@ class HistoryView : Subview public override void OnEnable() { base.OnEnable(); - TreeOnEnable(); + + if (treeChanges != null) + { + treeChanges.ViewHasFocus = HasFocus; + treeChanges.UpdateIcons(Styles.FolderIcon); + } + AttachHandlers(Repository); if (Repository != null) @@ -374,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 @@ -721,15 +738,6 @@ private void BuildTree() Redraw(); } - private void TreeOnEnable() - { - if (treeChanges != null) - { - treeChanges.OnEnable(); - treeChanges.UpdateIcons(Styles.FolderIcon); - } - } - public override bool IsBusy { get { return false; } 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 89aae1eac..f3710516f 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,20 +28,13 @@ 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; if (!Nodes.Any()) return new Rect(treeDisplayRect.x, treeDisplayRect.y, 0f, 0f); @@ -51,7 +42,7 @@ public Rect Render(Rect treeDisplayRect, Vector2 scroll, Action singleCli var treeNodeStyle = TreeNodeStyle; var activeTreeNodeStyle = ActiveTreeNodeStyle; - if (treeHasFocus) + if (ViewHasFocus && TreeHasFocus) { treeNodeStyle = FocusedTreeNodeStyle; activeTreeNodeStyle = FocusedActiveTreeNodeStyle; @@ -83,7 +74,7 @@ public Rect Render(Rect treeDisplayRect, Vector2 scroll, Action singleCli 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); } @@ -117,7 +108,7 @@ public Rect Render(Rect treeDisplayRect, Vector2 scroll, Action singleCli 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); } @@ -155,6 +146,13 @@ public Rect Render(Rect treeDisplayRect, Vector2 scroll, Action singleCli return rect; } + protected bool TreeHasFocus + { + get { return GUIUtility.keyboardControl == controlId; } + } + + public abstract bool ViewHasFocus { get; set; } + public void Focus() { bool selectionChanged = false; @@ -330,14 +328,6 @@ protected void LoadNodeIcons() SetNodeIcon(treeNode); } } - - public void OnEnable() - { - if (!selectionObject) - { - selectionObject = ScriptableObject.CreateInstance(); - } - } } [Serializable] @@ -538,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 { @@ -581,10 +572,6 @@ public override TreeNode SelectedNode set { selectedNode = value; - if (value != null && selectionObject) - { - Selection.activeObject = selectionObject; - } } } @@ -593,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);