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

Commit 2936899

Browse files
Initial functionality to select file nodes
# Conflicts: # src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs
1 parent be6ae91 commit 2936899

File tree

1 file changed

+77
-15
lines changed

1 file changed

+77
-15
lines changed

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

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,16 @@ public Rect Render(Rect rect, Vector2 scroll, Action<TreeNode> singleClick = nul
160160
if (DisplayRootNode)
161161
{
162162
var titleNode = nodes[0];
163-
var selectionChanged = titleNode.Render(rect, Styles.TreeIndentation, selectedNode == titleNode, FolderStyle, TreeNodeStyle, ActiveTreeNodeStyle);
163+
var renderResult = titleNode.Render(rect, Styles.TreeIndentation, selectedNode == titleNode, FolderStyle, TreeNodeStyle, ActiveTreeNodeStyle);
164164

165-
if (selectionChanged)
165+
if (renderResult == TreeNodeRenderResult.VisibilityChange)
166166
{
167167
ToggleNodeVisibility(0, titleNode);
168168
}
169+
else if (renderResult == TreeNodeRenderResult.SelectionChange)
170+
{
171+
ToggleNodeSelection(0, titleNode);
172+
}
169173

170174
RequiresRepaint = HandleInput(rect, titleNode, 0);
171175
rect.y += ItemHeight + ItemSpacing;
@@ -182,13 +186,16 @@ public Rect Render(Rect rect, Vector2 scroll, Action<TreeNode> singleClick = nul
182186
{
183187
Indent();
184188
}
185-
var changed = node.Render(rect, Styles.TreeIndentation, selectedNode == node, FolderStyle, TreeNodeStyle, ActiveTreeNodeStyle);
189+
var renderResult = node.Render(rect, Styles.TreeIndentation, selectedNode == node, FolderStyle, TreeNodeStyle, ActiveTreeNodeStyle);
186190

187-
if (node.IsFolder && changed)
191+
if (renderResult == TreeNodeRenderResult.VisibilityChange)
188192
{
189-
// toggle visibility for all the nodes under this one
190193
ToggleNodeVisibility(i, node);
191194
}
195+
else if (renderResult == TreeNodeRenderResult.SelectionChange)
196+
{
197+
ToggleNodeSelection(i, node);
198+
}
192199

193200
if (node.Level < level)
194201
{
@@ -241,7 +248,28 @@ public void Blur()
241248
RequiresRepaint = true;
242249
}
243250

244-
private int ToggleNodeVisibility(int idx, TreeNode rootNode)
251+
private void ToggleNodeSelection(int idx, TreeNode node)
252+
{
253+
if (node.IsFolder)
254+
{
255+
256+
}
257+
else
258+
{
259+
switch (node.SelectionState)
260+
{
261+
case SelectionState.Unselected:
262+
node.SelectionState = SelectionState.Selected;
263+
break;
264+
265+
case SelectionState.Selected:
266+
node.SelectionState = SelectionState.Unselected;
267+
break;
268+
}
269+
}
270+
}
271+
272+
private void ToggleNodeVisibility(int idx, TreeNode node)
245273
{
246274
var nodeLevel = node.Level;
247275
node.IsCollapsed = !node.IsCollapsed;
@@ -260,7 +288,6 @@ private int ToggleNodeVisibility(int idx, TreeNode rootNode)
260288
{
261289
SelectedNode = node;
262290
}
263-
return idx;
264291
}
265292

266293
private bool HandleInput(Rect rect, TreeNode currentNode, int index, Action<TreeNode> singleClick = null, Action<TreeNode> doubleClick = null, Action<TreeNode> rightClick = null)
@@ -417,20 +444,23 @@ public class TreeNode
417444
public bool IsHidden;
418445
public bool IsActive;
419446
public GUIContent content;
420-
[NonSerialized] public Texture2D Icon;
421447
public bool Selectable;
448+
public SelectionState SelectionState;
449+
450+
[NonSerialized] public Texture2D Icon;
422451

423452
public void Load()
424453
{
425454
content = new GUIContent(Label, Icon);
426455
}
427456

428-
public bool Render(Rect rect, float indentation, bool isSelected, GUIStyle toggleStyle, GUIStyle nodeStyle, GUIStyle activeNodeStyle)
457+
public TreeNodeRenderResult Render(Rect rect, float indentation, bool isSelected, GUIStyle toggleStyle, GUIStyle nodeStyle, GUIStyle activeNodeStyle)
429458
{
459+
var renderResult = TreeNodeRenderResult.None;
460+
430461
if (IsHidden)
431-
return false;
462+
return renderResult;
432463

433-
var changed = false;
434464
var fillRect = rect;
435465
var nodeStartX = Level * indentation * (Selectable ? 2 : 1);
436466

@@ -468,7 +498,10 @@ public bool Render(Rect rect, float indentation, bool isSelected, GUIStyle toggl
468498
{
469499
GUI.Toggle(toggleRect, !IsCollapsed, GUIContent.none, GUIStyle.none);
470500
}
471-
changed = EditorGUI.EndChangeCheck();
501+
if (EditorGUI.EndChangeCheck())
502+
{
503+
renderResult = TreeNodeRenderResult.VisibilityChange;
504+
}
472505
}
473506

474507
if (Selectable)
@@ -479,11 +512,26 @@ public bool Render(Rect rect, float indentation, bool isSelected, GUIStyle toggl
479512

480513
nodeStartX += selectRect.width + 2;
481514

515+
var selectionStyle = GUI.skin.toggle;
516+
var selectionValue = false;
517+
518+
if (SelectionState == SelectionState.Selected)
519+
{
520+
selectionValue = true;
521+
}
522+
else if (SelectionState == SelectionState.Mixed)
523+
{
524+
selectionStyle = Styles.ToggleMixedStyle;
525+
}
526+
482527
EditorGUI.BeginChangeCheck();
483528
{
484-
GUI.Toggle(selectRect, false, GUIContent.none, Styles.ToggleMixedStyle);
529+
GUI.Toggle(selectRect, selectionValue, GUIContent.none, selectionStyle);
530+
}
531+
if (EditorGUI.EndChangeCheck())
532+
{
533+
renderResult = TreeNodeRenderResult.SelectionChange;
485534
}
486-
EditorGUI.EndChangeCheck();
487535
}
488536

489537
data += string.Format("ContentStart: {0} ", nodeStartX);
@@ -497,7 +545,7 @@ public bool Render(Rect rect, float indentation, bool isSelected, GUIStyle toggl
497545

498546
Debug.Log(data);
499547

500-
return changed;
548+
return renderResult;
501549
}
502550

503551
public override string ToString()
@@ -552,4 +600,18 @@ public void UpdateIcons(Texture2D activeBranchIcon, Texture2D branchIcon, Textur
552600
}
553601
}
554602
}
603+
604+
public enum TreeNodeRenderResult
605+
{
606+
None,
607+
VisibilityChange,
608+
SelectionChange
609+
}
610+
611+
public enum SelectionState
612+
{
613+
Unselected,
614+
Selected,
615+
Mixed
616+
}
555617
}

0 commit comments

Comments
 (0)