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

Commit e7b5c77

Browse files
A large correction to the global selection mechanism
1 parent 98ce81f commit e7b5c77

File tree

9 files changed

+133
-34
lines changed

9 files changed

+133
-34
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,16 @@ public override void OnEnable()
6363
{
6464
base.OnEnable();
6565

66+
var hasFocus = HasFocus;
6667
if (treeLocals != null)
6768
{
69+
treeLocals.ViewHasFocus = hasFocus;
6870
treeLocals.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon);
6971
}
7072

7173
if (treeRemotes != null)
7274
{
75+
treeRemotes.ViewHasFocus = hasFocus;
7376
treeRemotes.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.GlobeIcon);
7477
}
7578

@@ -95,6 +98,17 @@ public override void OnSelectionChange()
9598
Redraw();
9699
}
97100

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+
98112
private void RepositoryOnLocalAndRemoteBranchListChanged(CacheUpdateEvent cacheUpdateEvent)
99113
{
100114
if (!lastLocalAndRemoteBranchListChangedEvent.Equals(cacheUpdateEvent))

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

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public bool IsLocked
3939
[Serializable]
4040
public class ChangesTree : Tree<ChangesTreeNode, GitStatusEntryTreeData>
4141
{
42+
[SerializeField] public ChangesTreeNodeDictionary assets = new ChangesTreeNodeDictionary();
4243
[SerializeField] public ChangesTreeNodeDictionary folders = new ChangesTreeNodeDictionary();
4344
[SerializeField] public ChangesTreeNodeDictionary checkedFileNodes = new ChangesTreeNodeDictionary();
4445

@@ -51,7 +52,8 @@ public class ChangesTree : Tree<ChangesTreeNode, GitStatusEntryTreeData>
5152
[SerializeField] public bool isUsingGlobalSelection = false;
5253
[SerializeField] private List<ChangesTreeNode> nodes = new List<ChangesTreeNode>();
5354
[SerializeField] private ChangesTreeNode selectedNode = null;
54-
[NonSerialized] private Object lastActiveObject;
55+
[NonSerialized] private bool viewHasFocus;
56+
[NonSerialized] private Object lastActivatedObject;
5557

5658
public override string Title
5759
{
@@ -101,22 +103,13 @@ public override ChangesTreeNode SelectedNode
101103
return;
102104
}
103105

104-
Object activeObject = null;
105-
if (selectedNode != null)
106-
{
107-
var path = selectedNode.Path;
108-
if (path != null && path.StartsWith("Assets"))
109-
{
110-
var assetGuid = AssetDatabase.AssetPathToGUID(path);
111-
activeObject = !string.IsNullOrEmpty(assetGuid)
112-
? AssetDatabase.LoadMainAssetAtPath(path)
113-
: null;
114-
}
115-
}
106+
var activeObject = selectedNode != null
107+
? AssetDatabase.LoadMainAssetAtPath(selectedNode.Path)
108+
: null;
116109

117-
lastActiveObject = activeObject;
110+
lastActivatedObject = activeObject;
118111

119-
if (activeObject != null)
112+
if (TreeHasFocus)
120113
{
121114
Selection.activeObject = activeObject;
122115
}
@@ -134,6 +127,12 @@ public bool IsUsingGlobalSelection
134127
set { isUsingGlobalSelection = value; }
135128
}
136129

130+
public override bool ViewHasFocus
131+
{
132+
get { return viewHasFocus; }
133+
set { viewHasFocus = value; }
134+
}
135+
137136
public void UpdateIcons(Texture2D folderIcon)
138137
{
139138
var needsLoad = FolderIcon == null;
@@ -216,19 +215,33 @@ protected override ChangesTreeNode CreateTreeNode(string path, string label, int
216215
CheckState = isChecked ? CheckState.Checked : CheckState.Empty,
217216
GitFileStatus = gitFileStatus,
218217
ProjectPath = projectPath,
219-
IsLocked = isLocked
218+
IsLocked = isLocked,
220219
};
221220

222221
if (isFolder && level >= 0)
223222
{
224223
folders.Add(node.Path, node);
225224
}
226225

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+
227239
return node;
228240
}
229241

230242
protected override void OnClear()
231243
{
244+
assets.Clear();
232245
folders.Clear();
233246
checkedFileNodes.Clear();
234247
}
@@ -253,23 +266,25 @@ protected override void AddCheckedNode(ChangesTreeNode node)
253266
checkedFileNodes.Add(((ITreeNode)node).Path, node);
254267
}
255268

256-
public override Rect Render(Rect treeDisplayRect, Vector2 scroll, Action<ChangesTreeNode> singleClick = null, Action<ChangesTreeNode> doubleClick = null,
257-
Action<ChangesTreeNode> rightClick = null)
269+
public bool OnSelectionChange()
258270
{
259-
if (IsUsingGlobalSelection)
271+
if (IsUsingGlobalSelection && !TreeHasFocus)
260272
{
261-
if (lastActiveObject != null && Selection.activeObject != lastActiveObject)
262-
{
263-
SelectedNode = null;
264-
}
273+
ChangesTreeNode assetNode = null;
265274

266-
if (Selection.activeObject != null)
275+
if (Selection.activeObject != lastActivatedObject)
267276
{
268-
277+
var activeAssetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
278+
var activeAssetGuid = AssetDatabase.AssetPathToGUID(activeAssetPath);
279+
280+
assets.TryGetValue(activeAssetGuid, out assetNode);
269281
}
282+
283+
SelectedNode = assetNode;
284+
return true;
270285
}
271286

272-
return base.Render(treeDisplayRect, scroll, singleClick, doubleClick, rightClick);
287+
return false;
273288
}
274289
}
275290
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public override void OnEnable()
4545

4646
if (treeChanges != null)
4747
{
48+
treeChanges.ViewHasFocus = HasFocus;
4849
treeChanges.UpdateIcons(Styles.FolderIcon);
4950
}
5051

@@ -110,7 +111,23 @@ public override void OnGUI()
110111
public override void OnSelectionChange()
111112
{
112113
base.OnSelectionChange();
113-
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+
}
114131
}
115132

116133
private void OnTreeGUI(Rect rect)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ public override void OnEnable()
354354

355355
if (treeChanges != null)
356356
{
357+
treeChanges.ViewHasFocus = HasFocus;
357358
treeChanges.UpdateIcons(Styles.FolderIcon);
358359
}
359360

@@ -379,6 +380,17 @@ public override void OnDataUpdate()
379380
MaybeUpdateData();
380381
}
381382

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+
382394
public override void OnGUI()
383395
{
384396
// History toolbar

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

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,17 @@ public abstract class Tree<TNode, TData>: TreeBase<TNode, TData>
3232
public bool IsInitialized { get { return Nodes != null && Nodes.Count > 0 && !String.IsNullOrEmpty(Nodes[0].Path); } }
3333
public bool RequiresRepaint { get; private set; }
3434

35-
public virtual Rect Render(Rect treeDisplayRect, Vector2 scroll, Action<TNode> singleClick = null, Action<TNode> doubleClick = null, Action<TNode> rightClick = null)
35+
public Rect Render(Rect treeDisplayRect, Vector2 scroll, Action<TNode> singleClick = null, Action<TNode> doubleClick = null, Action<TNode> rightClick = null)
3636
{
3737
controlId = GUIUtility.GetControlID(FocusType.Keyboard);
38-
var treeHasFocus = GUIUtility.keyboardControl == controlId;
3938

4039
if (!Nodes.Any())
4140
return new Rect(treeDisplayRect.x, treeDisplayRect.y, 0f, 0f);
4241

4342
var treeNodeStyle = TreeNodeStyle;
4443
var activeTreeNodeStyle = ActiveTreeNodeStyle;
4544

46-
if (treeHasFocus)
45+
if (ViewHasFocus && TreeHasFocus)
4746
{
4847
treeNodeStyle = FocusedTreeNodeStyle;
4948
activeTreeNodeStyle = FocusedActiveTreeNodeStyle;
@@ -75,7 +74,7 @@ public virtual Rect Render(Rect treeDisplayRect, Vector2 scroll, Action<TNode> s
7574
var titleDisplay = !(rect.y > endDisplay || rect.yMax < startDisplay);
7675
if (titleDisplay)
7776
{
78-
var isSelected = SelectedNode == titleNode;
77+
var isSelected = SelectedNode != null && SelectedNode.Path == titleNode.Path;
7978
renderResult = titleNode.Render(rect, Styles.TreeIndentation, isSelected, FolderStyle, treeNodeStyle, activeTreeNodeStyle);
8079
}
8180

@@ -109,7 +108,7 @@ public virtual Rect Render(Rect treeDisplayRect, Vector2 scroll, Action<TNode> s
109108
var display = !(rect.y > endDisplay || rect.yMax < startDisplay);
110109
if (display)
111110
{
112-
var isSelected = SelectedNode == node;
111+
var isSelected = SelectedNode != null && SelectedNode.Path == node.Path;
113112
renderResult = node.Render(rect, Styles.TreeIndentation, isSelected, FolderStyle, treeNodeStyle, activeTreeNodeStyle);
114113
}
115114

@@ -147,6 +146,13 @@ public virtual Rect Render(Rect treeDisplayRect, Vector2 scroll, Action<TNode> s
147146
return rect;
148147
}
149148

149+
protected bool TreeHasFocus
150+
{
151+
get { return GUIUtility.keyboardControl == controlId; }
152+
}
153+
154+
public abstract bool ViewHasFocus { get; set; }
155+
150156
public void Focus()
151157
{
152158
bool selectionChanged = false;
@@ -522,6 +528,7 @@ public class BranchesTree : Tree<TreeNode, GitBranchTreeData>
522528
[SerializeField] public bool isCheckable = false;
523529
[SerializeField] private List<TreeNode> nodes = new List<TreeNode>();
524530
[SerializeField] private TreeNode selectedNode = null;
531+
[NonSerialized] private bool viewFocus;
525532

526533
public override string Title
527534
{
@@ -573,6 +580,12 @@ protected override List<TreeNode> Nodes
573580
get { return nodes; }
574581
}
575582

583+
public override bool ViewHasFocus
584+
{
585+
get { return viewFocus; }
586+
set { viewFocus = value; }
587+
}
588+
576589
public void UpdateIcons(Texture2D activeBranchIcon, Texture2D branchIcon, Texture2D folderIcon, Texture2D globeIcon)
577590
{
578591
var needsLoad = ActiveBranchIcon == null || BranchIcon == null || FolderIcon == null || GlobeIcon == null;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ public override void OnDataUpdate()
126126
ActiveView.OnDataUpdate();
127127
}
128128

129+
public override void OnFocusChanged()
130+
{
131+
if (ActiveView != null)
132+
ActiveView.OnFocusChanged();
133+
}
134+
129135
public override void OnRepositoryChanged(IRepository oldRepository)
130136
{
131137
base.OnRepositoryChanged(oldRepository);

0 commit comments

Comments
 (0)