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

Commit bccabdf

Browse files
Better control over styles and tree focus
1 parent 2099721 commit bccabdf

File tree

4 files changed

+106
-48
lines changed

4 files changed

+106
-48
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Styles.cs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -874,35 +874,68 @@ public static GUIStyle TreeNode
874874
treeNode = new GUIStyle(GUI.skin.label);
875875
treeNode.name = "Custom TreeNode";
876876

877-
var color = new Color(62f / 255f, 125f / 255f, 231f / 255f);
878-
var texture = Utility.GetTextureFromColor(color);
879-
treeNode.focused.background = texture;
880-
treeNode.onFocused.background = texture;
877+
var greyTexture = Utility.GetTextureFromColor(Color.gray);
878+
879+
treeNode.focused.background = greyTexture;
881880
treeNode.focused.textColor = Color.white;
882-
treeNode.onFocused.textColor = Color.white;
883881
}
884882

885883
return treeNode;
886884
}
887885
}
888886

889-
private static GUIStyle treeNodeActive;
890-
public static GUIStyle TreeNodeActive
887+
private static GUIStyle activeTreeNode;
888+
public static GUIStyle ActiveTreeNode
889+
{
890+
get
891+
{
892+
if (activeTreeNode == null)
893+
{
894+
activeTreeNode = new GUIStyle(TreeNode);
895+
activeTreeNode.name = "Custom Active TreeNode";
896+
897+
activeTreeNode.fontStyle = FontStyle.Bold;
898+
}
899+
900+
return activeTreeNode;
901+
}
902+
}
903+
904+
private static GUIStyle focusedTreeNode;
905+
public static GUIStyle FocusedTreeNode
891906
{
892907
get
893908
{
894-
if (treeNodeActive == null)
909+
if (focusedTreeNode == null)
895910
{
896-
treeNodeActive = new GUIStyle(TreeNode);
897-
treeNodeActive.name = "Custom TreeNode Active";
898-
treeNodeActive.fontStyle = FontStyle.Bold;
899-
treeNodeActive.focused.textColor = Color.white;
900-
treeNodeActive.active.textColor = Color.white;
911+
focusedTreeNode = new GUIStyle(TreeNode);
912+
focusedTreeNode.name = "Custom Focused TreeNode";
913+
914+
var blueColor = new Color(62f / 255f, 125f / 255f, 231f / 255f);
915+
var blueTexture = Utility.GetTextureFromColor(blueColor);
916+
917+
focusedTreeNode.focused.background = blueTexture;
901918
}
902919

903-
return treeNodeActive;
920+
return focusedTreeNode;
904921
}
905922
}
906923

924+
private static GUIStyle focusedActiveTreeNode;
925+
public static GUIStyle FocusedActiveTreeNode
926+
{
927+
get
928+
{
929+
if (focusedActiveTreeNode == null)
930+
{
931+
focusedActiveTreeNode = new GUIStyle(FocusedTreeNode);
932+
focusedActiveTreeNode.name = "Custom Focused Active TreeNode";
933+
934+
focusedActiveTreeNode.fontStyle = FontStyle.Bold;
935+
}
936+
937+
return focusedActiveTreeNode;
938+
}
939+
}
907940
}
908941
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,15 @@ private void OnTreeGUI(Rect rect)
301301
{
302302
treeLocals.FolderStyle = Styles.Foldout;
303303
treeLocals.TreeNodeStyle = Styles.TreeNode;
304-
treeLocals.ActiveTreeNodeStyle = Styles.TreeNodeActive;
304+
treeLocals.ActiveTreeNodeStyle = Styles.ActiveTreeNode;
305+
treeLocals.FocusedTreeNodeStyle = Styles.FocusedTreeNode;
306+
treeLocals.FocusedActiveTreeNodeStyle = Styles.FocusedActiveTreeNode;
305307

306308
treeRemotes.FolderStyle = Styles.Foldout;
307309
treeRemotes.TreeNodeStyle = Styles.TreeNode;
308-
treeRemotes.ActiveTreeNodeStyle = Styles.TreeNodeActive;
310+
treeRemotes.ActiveTreeNodeStyle = Styles.ActiveTreeNode;
311+
treeRemotes.FocusedTreeNodeStyle = Styles.FocusedTreeNode;
312+
treeRemotes.FocusedActiveTreeNodeStyle = Styles.FocusedActiveTreeNode;
309313

310314
var treeHadFocus = treeLocals.SelectedNode != null;
311315

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ private void OnTreeGUI(Rect rect)
101101
{
102102
treeChanges.FolderStyle = Styles.Foldout;
103103
treeChanges.TreeNodeStyle = Styles.TreeNode;
104-
treeChanges.ActiveTreeNodeStyle = Styles.TreeNodeActive;
104+
treeChanges.ActiveTreeNodeStyle = Styles.ActiveTreeNode;
105+
treeChanges.FocusedTreeNodeStyle = Styles.FocusedTreeNode;
106+
treeChanges.FocusedActiveTreeNodeStyle = Styles.FocusedActiveTreeNode;
105107

106108
rect = treeChanges.Render(initialRect, rect, scroll,
107109
node => { },

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

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ public abstract class Tree<TNode, TData>: TreeBase<TNode, TData>
2424
[SerializeField] public string pathSeparator = "/";
2525
[SerializeField] public bool displayRootNode = true;
2626
[SerializeField] public bool isCheckable = false;
27+
2728
[NonSerialized] public GUIStyle FolderStyle;
2829
[NonSerialized] public GUIStyle TreeNodeStyle;
2930
[NonSerialized] public GUIStyle ActiveTreeNodeStyle;
31+
[NonSerialized] public GUIStyle FocusedTreeNodeStyle;
32+
[NonSerialized] public GUIStyle FocusedActiveTreeNodeStyle;
3033

3134
[SerializeField] private List<TNode> nodes = new List<TNode>();
3235
[SerializeField] private TNode selectedNode = null;
3336

3437
[NonSerialized] private Stack<bool> indents = new Stack<bool>();
3538
[NonSerialized] private Action<TNode> rightClickNextRender;
3639
[NonSerialized] private TNode rightClickNextRenderNode;
40+
[NonSerialized] private int controlId;
3741

3842
public bool IsInitialized { get { return Nodes != null && Nodes.Count > 0 && !String.IsNullOrEmpty(Nodes[0].Path); } }
3943
public bool RequiresRepaint { get; private set; }
@@ -83,6 +87,18 @@ protected override List<TNode> Nodes
8387

8488
public Rect Render(Rect containingRect, Rect rect, Vector2 scroll, Action<TNode> singleClick = null, Action<TNode> doubleClick = null, Action<TNode> rightClick = null)
8589
{
90+
controlId = GUIUtility.GetControlID(FocusType.Keyboard);
91+
92+
var treeNodeStyle = TreeNodeStyle;
93+
var activeTreeNodeStyle = ActiveTreeNodeStyle;
94+
95+
var treeHasFocus = GUIUtility.keyboardControl == controlId;
96+
if (treeHasFocus)
97+
{
98+
treeNodeStyle = FocusedTreeNodeStyle;
99+
activeTreeNodeStyle = FocusedActiveTreeNodeStyle;
100+
}
101+
86102
if (Event.current.type != EventType.Repaint)
87103
{
88104
if (rightClickNextRender != null)
@@ -109,7 +125,8 @@ public Rect Render(Rect containingRect, Rect rect, Vector2 scroll, Action<TNode>
109125
var titleDisplay = !(rect.y > endDisplay || rect.yMax < startDisplay);
110126
if (titleDisplay)
111127
{
112-
renderResult = titleNode.Render(rect, Styles.TreeIndentation, selectedNode == titleNode, FolderStyle, TreeNodeStyle, ActiveTreeNodeStyle);
128+
var isSelected = selectedNode == titleNode;
129+
renderResult = titleNode.Render(rect, Styles.TreeIndentation, isSelected, FolderStyle, treeNodeStyle, activeTreeNodeStyle);
113130
}
114131

115132
if (renderResult == TreeNodeRenderResult.VisibilityChange)
@@ -142,7 +159,7 @@ public Rect Render(Rect containingRect, Rect rect, Vector2 scroll, Action<TNode>
142159
var display = !(rect.y > endDisplay || rect.yMax < startDisplay);
143160
if (display)
144161
{
145-
renderResult = node.Render(rect, Styles.TreeIndentation, selectedNode == node, FolderStyle, TreeNodeStyle, ActiveTreeNodeStyle);
162+
renderResult = node.Render(rect, Styles.TreeIndentation, selectedNode == node, FolderStyle, treeNodeStyle, activeTreeNodeStyle);
146163
}
147164

148165
if (renderResult == TreeNodeRenderResult.VisibilityChange)
@@ -213,6 +230,8 @@ private bool HandleInput(Rect rect, TNode currentNode, int index, Action<TNode>
213230
if (Event.current.type == EventType.MouseDown && clickRect.Contains(Event.current.mousePosition))
214231
{
215232
Event.current.Use();
233+
GUIUtility.keyboardControl = controlId;
234+
216235
SelectedNode = currentNode;
217236
requiresRepaint = true;
218237
var clickCount = Event.current.clickCount;
@@ -234,7 +253,7 @@ private bool HandleInput(Rect rect, TNode currentNode, int index, Action<TNode>
234253
}
235254

236255
// Keyboard navigation if this child is the current selection
237-
if (currentNode == selectedNode && Event.current.type == EventType.KeyDown)
256+
if (GUIUtility.keyboardControl == controlId && currentNode == selectedNode && Event.current.type == EventType.KeyDown)
238257
{
239258
int directionY = Event.current.keyCode == KeyCode.UpArrow ? -1 : Event.current.keyCode == KeyCode.DownArrow ? 1 : 0;
240259
int directionX = Event.current.keyCode == KeyCode.LeftArrow ? -1 : Event.current.keyCode == KeyCode.RightArrow ? 1 : 0;
@@ -465,34 +484,36 @@ public TreeNodeRenderResult Render(Rect rect, float indentation, bool isSelected
465484
var iconRect = new Rect(nodeStartX, nodeRect.y, fillRect.width - nodeStartX, nodeRect.height);
466485
var statusRect = new Rect(iconRect.x + 6, iconRect.yMax - 9, 9, 9);
467486

468-
// if (Event.current.type == EventType.repaint)
469-
// {
470-
// if (blackStyle == null)
471-
// blackStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.black) } };
472-
//
473-
// if (greenStyle == null)
474-
// greenStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.green) } };
475-
//
476-
// if (blueStyle == null)
477-
// blueStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.blue) } };
478-
//
479-
// if (yellowStyle == null)
480-
// yellowStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.yellow) } };
481-
//
482-
// if (magentaStyle == null)
483-
// magentaStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.magenta) } };
484-
//
485-
// GUI.Box(nodeRect, GUIContent.none, blackStyle);
486-
//
487-
// GUI.Box(toggleRect, GUIContent.none, isFolder ? greenStyle : blueStyle);
488-
//
489-
// GUI.Box(checkRect, GUIContent.none, yellowStyle);
490-
// GUI.Box(iconRect, GUIContent.none, magentaStyle);
491-
// }
487+
//if (Event.current.type == EventType.repaint)
488+
//{
489+
// if (blackStyle == null)
490+
// blackStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.black) } };
491+
//
492+
// if (greenStyle == null)
493+
// greenStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.green) } };
494+
//
495+
// if (blueStyle == null)
496+
// blueStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.blue) } };
497+
//
498+
// if (yellowStyle == null)
499+
// yellowStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.yellow) } };
500+
//
501+
// if (magentaStyle == null)
502+
// magentaStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.magenta) } };
503+
//
504+
// GUI.Box(nodeRect, GUIContent.none, blackStyle);
505+
//
506+
// GUI.Box(toggleRect, GUIContent.none, isFolder ? greenStyle : blueStyle);
507+
//
508+
// GUI.Box(checkRect, GUIContent.none, yellowStyle);
509+
// GUI.Box(iconRect, GUIContent.none, magentaStyle);
510+
//}
511+
512+
var contentStyle = IsActive ? activeNodeStyle : nodeStyle;
492513

493514
if (Event.current.type == EventType.repaint)
494515
{
495-
nodeStyle.Draw(fillRect, GUIContent.none, false, false, false, isSelected);
516+
contentStyle.Draw(fillRect, GUIContent.none, false, false, false, isSelected);
496517
}
497518

498519
var styleOn = false;
@@ -539,11 +560,9 @@ public TreeNodeRenderResult Render(Rect rect, float indentation, bool isSelected
539560
}
540561
}
541562

542-
var contentStyle = IsActive ? activeNodeStyle : nodeStyle;
543-
544563
if (Event.current.type == EventType.repaint)
545564
{
546-
contentStyle.Draw(iconRect, content, false, false, styleOn, isSelected);
565+
contentStyle.Draw(iconRect, content, false, false, false, isSelected);
547566
}
548567

549568
if (IconBadge != null)

0 commit comments

Comments
 (0)