Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 947e2b9

Browse files
Merge branch 'fixes/global-selection-abuse' into fixes/tree-on-busy
2 parents d6df408 + e7b5c77 commit 947e2b9

File tree

9 files changed

+221
-88
lines changed

9 files changed

+221
-88
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BaseWindow.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ private void OnGUI()
9494
}
9595
}
9696

97+
private void OnFocus()
98+
{
99+
HasFocus = true;
100+
OnFocusChanged();
101+
}
102+
103+
private void OnLostFocus()
104+
{
105+
HasFocus = false;
106+
OnFocusChanged();
107+
}
108+
109+
public virtual void OnFocusChanged()
110+
{}
111+
97112
public virtual void OnDestroy()
98113
{}
99114

@@ -103,6 +118,7 @@ public virtual void OnSelectionChange()
103118
public Rect Position { get { return position; } }
104119
public IApplicationManager Manager { get; private set; }
105120
public abstract bool IsBusy { get; }
121+
public bool HasFocus { get; private set; }
106122
public IRepository Repository { get { return inLayout ? cachedRepository : Environment.Repository; } }
107123
public bool HasRepository { get { return Repository != null; } }
108124
public IUser User { get { return cachedUser; } }

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class BranchesView : Subview
3939
[NonSerialized] private int listID = -1;
4040
[NonSerialized] private BranchesMode targetMode;
4141

42-
[SerializeField] private BranchesTree treeLocals;
43-
[SerializeField] private BranchesTree treeRemotes;
42+
[SerializeField] private BranchesTree treeLocals = new BranchesTree { Title = LocalTitle };
43+
[SerializeField] private BranchesTree treeRemotes = new BranchesTree { Title = RemoteTitle, IsRemote = true };
4444
[SerializeField] private BranchesMode mode = BranchesMode.Default;
4545
[SerializeField] private string newBranchName;
4646
[SerializeField] private Vector2 scroll;
@@ -62,7 +62,20 @@ public override void InitializeView(IView parent)
6262
public override void OnEnable()
6363
{
6464
base.OnEnable();
65-
TreeOnEnable();
65+
66+
var hasFocus = HasFocus;
67+
if (treeLocals != null)
68+
{
69+
treeLocals.ViewHasFocus = hasFocus;
70+
treeLocals.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon);
71+
}
72+
73+
if (treeRemotes != null)
74+
{
75+
treeRemotes.ViewHasFocus = hasFocus;
76+
treeRemotes.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon);
77+
}
78+
6679
AttachHandlers(Repository);
6780
Repository.CheckLocalAndRemoteBranchListChangedEvent(lastLocalAndRemoteBranchListChangedEvent);
6881
}
@@ -85,6 +98,17 @@ public override void OnSelectionChange()
8598
Redraw();
8699
}
87100

101+
public override void OnFocusChanged()
102+
{
103+
base.OnFocusChanged();
104+
if(treeLocals.ViewHasFocus != HasFocus || treeRemotes.ViewHasFocus != HasFocus)
105+
{
106+
treeLocals.ViewHasFocus = HasFocus;
107+
treeRemotes.ViewHasFocus = HasFocus;
108+
Redraw();
109+
}
110+
}
111+
88112
private void RepositoryOnLocalAndRemoteBranchListChanged(CacheUpdateEvent cacheUpdateEvent)
89113
{
90114
if (!lastLocalAndRemoteBranchListChangedEvent.Equals(cacheUpdateEvent))
@@ -155,18 +179,6 @@ private void Render()
155179

156180
private void BuildTree()
157181
{
158-
if (treeLocals == null)
159-
{
160-
treeLocals = new BranchesTree();
161-
treeLocals.Title = LocalTitle;
162-
163-
treeRemotes = new BranchesTree();
164-
treeRemotes.Title = RemoteTitle;
165-
treeRemotes.IsRemote = true;
166-
167-
TreeOnEnable();
168-
}
169-
170182
localBranches.Sort(CompareBranches);
171183
remoteBranches.Sort(CompareBranches);
172184

@@ -175,21 +187,6 @@ private void BuildTree()
175187
Redraw();
176188
}
177189

178-
private void TreeOnEnable()
179-
{
180-
if (treeLocals != null)
181-
{
182-
treeLocals.OnEnable();
183-
treeLocals.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon);
184-
}
185-
186-
if (treeRemotes != null)
187-
{
188-
treeRemotes.OnEnable();
189-
treeRemotes.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon);
190-
}
191-
}
192-
193190
private void OnButtonBarGUI()
194191
{
195192
if (mode == BranchesMode.Default)

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesTreeControl.cs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using UnityEditor;
55
using UnityEngine;
6+
using Object = UnityEngine.Object;
67

78
namespace GitHub.Unity
89
{
@@ -38,6 +39,7 @@ public bool IsLocked
3839
[Serializable]
3940
public class ChangesTree : Tree<ChangesTreeNode, GitStatusEntryTreeData>
4041
{
42+
[SerializeField] public ChangesTreeNodeDictionary assets = new ChangesTreeNodeDictionary();
4143
[SerializeField] public ChangesTreeNodeDictionary folders = new ChangesTreeNodeDictionary();
4244
[SerializeField] public ChangesTreeNodeDictionary checkedFileNodes = new ChangesTreeNodeDictionary();
4345

@@ -47,8 +49,11 @@ public class ChangesTree : Tree<ChangesTreeNode, GitStatusEntryTreeData>
4749
[SerializeField] public bool displayRootNode = true;
4850
[SerializeField] public bool isSelectable = true;
4951
[SerializeField] public bool isCheckable = false;
52+
[SerializeField] public bool isUsingGlobalSelection = false;
5053
[SerializeField] private List<ChangesTreeNode> nodes = new List<ChangesTreeNode>();
5154
[SerializeField] private ChangesTreeNode selectedNode = null;
55+
[NonSerialized] private bool viewHasFocus;
56+
[NonSerialized] private Object lastActivatedObject;
5257

5358
public override string Title
5459
{
@@ -92,9 +97,21 @@ public override ChangesTreeNode SelectedNode
9297
set
9398
{
9499
selectedNode = value;
95-
if (value != null && selectionObject)
100+
101+
if (!IsUsingGlobalSelection)
102+
{
103+
return;
104+
}
105+
106+
var activeObject = selectedNode != null
107+
? AssetDatabase.LoadMainAssetAtPath(selectedNode.Path)
108+
: null;
109+
110+
lastActivatedObject = activeObject;
111+
112+
if (TreeHasFocus)
96113
{
97-
Selection.activeObject = selectionObject;
114+
Selection.activeObject = activeObject;
98115
}
99116
}
100117
}
@@ -104,6 +121,18 @@ protected override List<ChangesTreeNode> Nodes
104121
get { return nodes; }
105122
}
106123

124+
public bool IsUsingGlobalSelection
125+
{
126+
get { return isUsingGlobalSelection; }
127+
set { isUsingGlobalSelection = value; }
128+
}
129+
130+
public override bool ViewHasFocus
131+
{
132+
get { return viewHasFocus; }
133+
set { viewHasFocus = value; }
134+
}
135+
107136
public void UpdateIcons(Texture2D folderIcon)
108137
{
109138
var needsLoad = FolderIcon == null;
@@ -186,19 +215,33 @@ protected override ChangesTreeNode CreateTreeNode(string path, string label, int
186215
CheckState = isChecked ? CheckState.Checked : CheckState.Empty,
187216
GitFileStatus = gitFileStatus,
188217
ProjectPath = projectPath,
189-
IsLocked = isLocked
218+
IsLocked = isLocked,
190219
};
191220

192221
if (isFolder && level >= 0)
193222
{
194223
folders.Add(node.Path, node);
195224
}
196225

226+
if (IsUsingGlobalSelection)
227+
{
228+
if (node.Path != null)
229+
{
230+
var assetGuid = AssetDatabase.AssetPathToGUID(node.Path);
231+
232+
if (!string.IsNullOrEmpty(assetGuid))
233+
{
234+
assets.Add(assetGuid, node);
235+
}
236+
}
237+
}
238+
197239
return node;
198240
}
199241

200242
protected override void OnClear()
201243
{
244+
assets.Clear();
202245
folders.Clear();
203246
checkedFileNodes.Clear();
204247
}
@@ -222,5 +265,26 @@ protected override void AddCheckedNode(ChangesTreeNode node)
222265
{
223266
checkedFileNodes.Add(((ITreeNode)node).Path, node);
224267
}
268+
269+
public bool OnSelectionChange()
270+
{
271+
if (IsUsingGlobalSelection && !TreeHasFocus)
272+
{
273+
ChangesTreeNode assetNode = null;
274+
275+
if (Selection.activeObject != lastActivatedObject)
276+
{
277+
var activeAssetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
278+
var activeAssetGuid = AssetDatabase.AssetPathToGUID(activeAssetPath);
279+
280+
assets.TryGetValue(activeAssetGuid, out assetNode);
281+
}
282+
283+
SelectedNode = assetNode;
284+
return true;
285+
}
286+
287+
return false;
288+
}
225289
}
226290
}

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ChangesView : Subview
2828
[SerializeField] private string currentBranch = "[unknown]";
2929

3030
[SerializeField] private Vector2 treeScroll;
31-
[SerializeField] private ChangesTree treeChanges;
31+
[SerializeField] private ChangesTree treeChanges = new ChangesTree { DisplayRootNode = false, IsCheckable = true, IsUsingGlobalSelection = true };
3232

3333
[SerializeField] private HashSet<string> gitLocks;
3434
[SerializeField] private List<GitStatusEntry> gitStatusEntries;
@@ -42,7 +42,13 @@ class ChangesView : Subview
4242
public override void OnEnable()
4343
{
4444
base.OnEnable();
45-
TreeOnEnable();
45+
46+
if (treeChanges != null)
47+
{
48+
treeChanges.ViewHasFocus = HasFocus;
49+
treeChanges.UpdateIcons(Styles.FolderIcon);
50+
}
51+
4652
AttachHandlers(Repository);
4753
Repository.CheckCurrentBranchChangedEvent(lastCurrentBranchChangedEvent);
4854
Repository.CheckStatusEntriesChangedEvent(lastStatusEntriesChangedEvent);
@@ -105,7 +111,23 @@ public override void OnGUI()
105111
public override void OnSelectionChange()
106112
{
107113
base.OnSelectionChange();
108-
Redraw();
114+
if (treeChanges.OnSelectionChange())
115+
{
116+
Redraw();
117+
}
118+
}
119+
120+
public override void OnFocusChanged()
121+
{
122+
Logger.Debug("OnFocusChanged: {0}", HasFocus);
123+
124+
base.OnFocusChanged();
125+
var hasFocus = HasFocus;
126+
if (treeChanges.ViewHasFocus != hasFocus)
127+
{
128+
treeChanges.ViewHasFocus = hasFocus;
129+
Redraw();
130+
}
109131
}
110132

111133
private void OnTreeGUI(Rect rect)
@@ -212,30 +234,11 @@ private void MaybeUpdateData()
212234

213235
private void BuildTree()
214236
{
215-
if (treeChanges == null)
216-
{
217-
treeChanges = new ChangesTree();
218-
treeChanges.Title = "Changes";
219-
treeChanges.DisplayRootNode = false;
220-
treeChanges.IsCheckable = true;
221-
treeChanges.PathSeparator = Environment.FileSystem.DirectorySeparatorChar.ToString();
222-
223-
TreeOnEnable();
224-
}
225-
237+
treeChanges.PathSeparator = Environment.FileSystem.DirectorySeparatorChar.ToString();
226238
treeChanges.Load(gitStatusEntries.Select(entry => new GitStatusEntryTreeData(entry, gitLocks.Contains(entry.Path))));
227239
Redraw();
228240
}
229241

230-
private void TreeOnEnable()
231-
{
232-
if (treeChanges != null)
233-
{
234-
treeChanges.OnEnable();
235-
treeChanges.UpdateIcons(Styles.FolderIcon);
236-
}
237-
}
238-
239242
private void OnCommitDetailsAreaGUI()
240243
{
241244
GUILayout.BeginHorizontal();

0 commit comments

Comments
 (0)