A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2
3using UnityEditor.IMGUI.Controls;
4
5namespace Unity.PlasticSCM.Editor.UI.Tree
6{
7 internal static class TableViewOperations
8 {
9 internal static int GetFirstSelectedRow(
10 TreeView treeView)
11 {
12 IList<int> selectedIds = treeView.GetSelection();
13
14 if (selectedIds.Count == 0)
15 return -1;
16
17 return selectedIds[0];
18 }
19
20 internal static void SelectFirstRow(
21 TreeView treeView)
22 {
23 int rowCount = treeView.GetRows().Count;
24
25 if (rowCount == 0)
26 return;
27
28 SetSelectionAndScroll(
29 treeView, new List<int> { 1 });
30 }
31
32 internal static void SelectDefaultRow(
33 TreeView treeView, int defaultRow)
34 {
35 int rowCount = treeView.GetRows().Count;
36
37 if (defaultRow == -1 || rowCount == 0)
38 return;
39
40 if (defaultRow >= rowCount)
41 defaultRow = rowCount - 1;
42
43 SetSelectionAndScroll(
44 treeView, new List<int> { defaultRow });
45 }
46
47 internal static void SetSelectionAndScroll(
48 TreeView treeView, List<int> idsToSelect)
49 {
50 treeView.SetSelection(
51 idsToSelect,
52 TreeViewSelectionOptions.FireSelectionChanged |
53 TreeViewSelectionOptions.RevealAndFrame);
54 }
55
56 internal static void ScrollToSelection(
57 TreeView treeView)
58 {
59 if (!treeView.HasSelection())
60 return;
61
62 int itemId = treeView.GetSelection()[0];
63
64 if (!IsVisible(itemId, treeView))
65 return;
66
67 treeView.FrameItem(itemId);
68 }
69
70 static bool IsVisible(int id, TreeView treeView)
71 {
72 foreach (TreeViewItem item in treeView.GetRows())
73 {
74 if (item.id == id)
75 return true;
76 }
77
78 return false;
79 }
80 }
81}