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

Commit 67adb7b

Browse files
Merge pull request #536 from github-for-unity/fixes/global-selection-abuse
Correcting global selection abuse
2 parents dc43da1 + 663b3e0 commit 67adb7b

File tree

9 files changed

+182
-67
lines changed

9 files changed

+182
-67
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: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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))
@@ -163,21 +187,6 @@ private void BuildTree()
163187
Redraw();
164188
}
165189

166-
private void TreeOnEnable()
167-
{
168-
if (treeLocals != null)
169-
{
170-
treeLocals.OnEnable();
171-
treeLocals.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon);
172-
}
173-
174-
if (treeRemotes != null)
175-
{
176-
treeRemotes.OnEnable();
177-
treeRemotes.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon);
178-
}
179-
}
180-
181190
private void OnButtonBarGUI()
182191
{
183192
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: 25 additions & 12 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 = new ChangesTree { DisplayRootNode = false, IsCheckable = true };
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)
@@ -217,15 +239,6 @@ private void BuildTree()
217239
Redraw();
218240
}
219241

220-
private void TreeOnEnable()
221-
{
222-
if (treeChanges != null)
223-
{
224-
treeChanges.OnEnable();
225-
treeChanges.UpdateIcons(Styles.FolderIcon);
226-
}
227-
}
228-
229242
private void OnCommitDetailsAreaGUI()
230243
{
231244
GUILayout.BeginHorizontal();

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,13 @@ class HistoryView : Subview
351351
public override void OnEnable()
352352
{
353353
base.OnEnable();
354-
TreeOnEnable();
354+
355+
if (treeChanges != null)
356+
{
357+
treeChanges.ViewHasFocus = HasFocus;
358+
treeChanges.UpdateIcons(Styles.FolderIcon);
359+
}
360+
355361
AttachHandlers(Repository);
356362

357363
if (Repository != null)
@@ -374,6 +380,17 @@ public override void OnDataUpdate()
374380
MaybeUpdateData();
375381
}
376382

383+
public override void OnFocusChanged()
384+
{
385+
base.OnFocusChanged();
386+
var hasFocus = HasFocus;
387+
if (treeChanges.ViewHasFocus != hasFocus)
388+
{
389+
treeChanges.ViewHasFocus = hasFocus;
390+
Redraw();
391+
}
392+
}
393+
377394
public override void OnGUI()
378395
{
379396
// History toolbar
@@ -721,15 +738,6 @@ private void BuildTree()
721738
Redraw();
722739
}
723740

724-
private void TreeOnEnable()
725-
{
726-
if (treeChanges != null)
727-
{
728-
treeChanges.OnEnable();
729-
treeChanges.UpdateIcons(Styles.FolderIcon);
730-
}
731-
}
732-
733741
public override bool IsBusy
734742
{
735743
get { return false; }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ interface IView
1818
bool HasUser { get; }
1919
IApplicationManager Manager { get; }
2020
bool IsBusy { get; }
21+
bool HasFocus { get; }
2122
}
2223
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ public virtual void OnDataUpdate()
2828
{}
2929

3030
public virtual void OnGUI()
31-
{}
31+
{ }
3232

3333
public virtual void OnSelectionChange()
34-
{}
34+
{ }
35+
36+
public virtual void OnFocusChanged()
37+
{ }
3538

3639
public virtual void Refresh()
3740
{
@@ -54,6 +57,7 @@ public virtual void Finish(bool result)
5457
public bool HasRepository { get { return Parent.HasRepository; } }
5558
public IUser User { get { return Parent.User; } }
5659
public bool HasUser { get { return Parent.HasUser; } }
60+
public bool HasFocus { get { return Parent != null && Parent.HasFocus; } }
5761
public abstract bool IsBusy { get; }
5862
protected ITaskManager TaskManager { get { return Manager.TaskManager; } }
5963
protected IGitClient GitClient { get { return Manager.GitClient; } }
@@ -64,6 +68,7 @@ public virtual void Finish(bool result)
6468
public Vector2 Size { get; protected set; }
6569

6670
private ILogging logger;
71+
6772
protected ILogging Logger
6873
{
6974
get

0 commit comments

Comments
 (0)