No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DiffTreeView.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. using System.Collections.Generic;
  2. using UnityEditor.IMGUI.Controls;
  3. using UnityEngine;
  4. using Codice.Client.Commands;
  5. using Codice.Client.Common;
  6. using Codice.CM.Common;
  7. using Codice.Utils;
  8. using PlasticGui;
  9. using PlasticGui.WorkspaceWindow.Diff;
  10. using Unity.PlasticSCM.Editor.UI;
  11. using Unity.PlasticSCM.Editor.UI.Tree;
  12. namespace Unity.PlasticSCM.Editor.Views.Diff
  13. {
  14. internal class DiffTreeView : TreeView
  15. {
  16. internal DiffTreeView(DiffTreeViewMenu menu)
  17. : base(new TreeViewState())
  18. {
  19. mMenu = menu;
  20. customFoldoutYOffset = UnityConstants.TREEVIEW_FOLDOUT_Y_OFFSET;
  21. rowHeight = UnityConstants.TREEVIEW_ROW_HEIGHT;
  22. showAlternatingRowBackgrounds = false;
  23. mCooldownFilterAction = new CooldownWindowDelayer(
  24. DelayedSearchChanged, UnityConstants.SEARCH_DELAYED_INPUT_ACTION_INTERVAL);
  25. EnableHorizontalScrollbar();
  26. }
  27. public override IList<TreeViewItem> GetRows()
  28. {
  29. return mRows;
  30. }
  31. public override void OnGUI(Rect rect)
  32. {
  33. base.OnGUI(rect);
  34. Event e = Event.current;
  35. if (e.type != EventType.KeyDown)
  36. return;
  37. bool isProcessed = mMenu.ProcessKeyActionIfNeeded(e);
  38. if (isProcessed)
  39. e.Use();
  40. }
  41. protected override bool CanChangeExpandedState(TreeViewItem item)
  42. {
  43. return item is ChangeCategoryTreeViewItem
  44. || item is MergeCategoryTreeViewItem;
  45. }
  46. protected override TreeViewItem BuildRoot()
  47. {
  48. return new TreeViewItem(0, -1, string.Empty);
  49. }
  50. protected override IList<TreeViewItem> BuildRows(TreeViewItem rootItem)
  51. {
  52. try
  53. {
  54. RegenerateRows(
  55. mDiffTree,
  56. mTreeViewItemIds,
  57. this,
  58. rootItem,
  59. mRows,
  60. mExpandCategories);
  61. }
  62. finally
  63. {
  64. mExpandCategories = false;
  65. }
  66. return mRows;
  67. }
  68. protected override void CommandEventHandling()
  69. {
  70. // NOTE - empty override to prevent crash when pressing ctrl-a in the treeview
  71. }
  72. protected override void SearchChanged(string newSearch)
  73. {
  74. mCooldownFilterAction.Ping();
  75. }
  76. protected override void ContextClickedItem(int id)
  77. {
  78. mMenu.Popup();
  79. Repaint();
  80. }
  81. protected override void BeforeRowsGUI()
  82. {
  83. int firstRowVisible;
  84. int lastRowVisible;
  85. GetFirstAndLastVisibleRows(out firstRowVisible, out lastRowVisible);
  86. GUI.DrawTexture(new Rect(0,
  87. firstRowVisible * rowHeight,
  88. GetRowRect(0).width + 500,
  89. (lastRowVisible * rowHeight) + 1500),
  90. Images.GetTreeviewBackgroundTexture());
  91. DrawTreeViewItem.InitializeStyles();
  92. mLargestRowWidth = 0;
  93. base.BeforeRowsGUI();
  94. }
  95. protected override void RowGUI(RowGUIArgs args)
  96. {
  97. float itemWidth;
  98. TreeViewItemGUI(
  99. args.item, args.rowRect, rowHeight, mDiffTree, args.selected, args.focused, out itemWidth);
  100. float rowWidth = baseIndent + args.item.depth * depthIndentWidth +
  101. itemWidth + UnityConstants.TREEVIEW_ROW_WIDTH_OFFSET;
  102. if (rowWidth > mLargestRowWidth)
  103. mLargestRowWidth = rowWidth;
  104. }
  105. protected override void AfterRowsGUI()
  106. {
  107. if (mHorizontalColumn != null)
  108. mHorizontalColumn.width = mLargestRowWidth;
  109. base.AfterRowsGUI();
  110. }
  111. internal void ClearModel()
  112. {
  113. mTreeViewItemIds.Clear();
  114. mDiffTree = new UnityDiffTree();
  115. }
  116. internal void BuildModel(
  117. WorkspaceInfo wkInfo,
  118. List<ClientDiff> diffs,
  119. BranchResolver brResolver,
  120. bool skipMergeTracking)
  121. {
  122. mTreeViewItemIds.Clear();
  123. mDiffTree.BuildCategories(wkInfo, diffs, brResolver, skipMergeTracking);
  124. }
  125. internal void Refilter()
  126. {
  127. Filter filter = new Filter(searchString);
  128. mDiffTree.Filter(filter, ColumnsNames);
  129. mExpandCategories = true;
  130. }
  131. internal void Sort()
  132. {
  133. mDiffTree.Sort(
  134. PlasticLocalization.GetString(PlasticLocalization.Name.PathColumn),
  135. sortAscending: true);
  136. }
  137. internal ClientDiffInfo GetMetaDiff(ClientDiffInfo diff)
  138. {
  139. return mDiffTree.GetMetaDiff(diff);
  140. }
  141. internal bool SelectionHasMeta()
  142. {
  143. if (!HasSelection())
  144. return false;
  145. ClientDiffInfo selectedDiff = GetSelectedDiffs(false)[0];
  146. if (selectedDiff == null)
  147. return false;
  148. return mDiffTree.HasMeta(selectedDiff);
  149. }
  150. internal List<ClientDiffInfo> GetSelectedDiffs(bool includeMetaFiles)
  151. {
  152. List<ClientDiffInfo> result = new List<ClientDiffInfo>();
  153. IList<int> selectedIds = GetSelection();
  154. if (selectedIds.Count == 0)
  155. return result;
  156. foreach (KeyValuePair<ITreeViewNode, int> item
  157. in mTreeViewItemIds.GetInfoItems())
  158. {
  159. if (!selectedIds.Contains(item.Value))
  160. continue;
  161. if (!(item.Key is ClientDiffInfo))
  162. continue;
  163. result.Add((ClientDiffInfo)item.Key);
  164. }
  165. if (includeMetaFiles)
  166. mDiffTree.FillWithMeta(result);
  167. return result;
  168. }
  169. void DelayedSearchChanged()
  170. {
  171. Refilter();
  172. Sort();
  173. Reload();
  174. TableViewOperations.ScrollToSelection(this);
  175. }
  176. void EnableHorizontalScrollbar()
  177. {
  178. mHorizontalColumn = new MultiColumnHeaderState.Column();
  179. mHorizontalColumn.autoResize = false;
  180. MultiColumnHeaderState.Column[] cols = { mHorizontalColumn };
  181. MultiColumnHeaderState headerState = new MultiColumnHeaderState(cols);
  182. multiColumnHeader = new MultiColumnHeader(headerState);
  183. multiColumnHeader.height = 0f;
  184. }
  185. static void RegenerateRows(
  186. UnityDiffTree diffTree,
  187. TreeViewItemIds<IDiffCategory, ITreeViewNode> treeViewItemIds,
  188. TreeView treeView,
  189. TreeViewItem rootItem,
  190. List<TreeViewItem> rows,
  191. bool expandCategories)
  192. {
  193. ClearRows(rootItem, rows);
  194. List<IDiffCategory> categories = diffTree.GetNodes();
  195. if (categories == null)
  196. return;
  197. foreach (IDiffCategory category in categories)
  198. {
  199. if (category is CategoryGroup &&
  200. ((CategoryGroup)category).CategoryType == CategoryGroup.Type.MergeCategory)
  201. {
  202. AddMergeCategory(
  203. rootItem,
  204. category,
  205. rows,
  206. treeViewItemIds,
  207. treeView,
  208. expandCategories);
  209. }
  210. if (category is ChangeCategory)
  211. {
  212. AddChangeCategory(
  213. rootItem,
  214. category,
  215. rows,
  216. treeViewItemIds,
  217. treeView,
  218. expandCategories);
  219. }
  220. }
  221. if (!expandCategories)
  222. return;
  223. treeView.state.expandedIDs = treeViewItemIds.GetCategoryIds();
  224. }
  225. static void ClearRows(
  226. TreeViewItem rootItem,
  227. List<TreeViewItem> rows)
  228. {
  229. if (rootItem.hasChildren)
  230. rootItem.children.Clear();
  231. rows.Clear();
  232. }
  233. static void AddMergeCategory(
  234. TreeViewItem rootItem,
  235. IDiffCategory category,
  236. List<TreeViewItem> rows,
  237. TreeViewItemIds<IDiffCategory, ITreeViewNode> treeViewItemIds,
  238. TreeView treeView,
  239. bool expandCategories)
  240. {
  241. int categoryId;
  242. if (!treeViewItemIds.TryGetCategoryItemId(category, out categoryId))
  243. categoryId = treeViewItemIds.AddCategoryItem(category);
  244. MergeCategoryTreeViewItem mergeCategoryTreeViewItem =
  245. new MergeCategoryTreeViewItem(
  246. categoryId,
  247. rootItem.depth + 1,
  248. (CategoryGroup)category);
  249. rootItem.AddChild(mergeCategoryTreeViewItem);
  250. rows.Add(mergeCategoryTreeViewItem);
  251. if (!expandCategories &&
  252. !treeView.IsExpanded(mergeCategoryTreeViewItem.id))
  253. return;
  254. for (int i = 0; i < category.GetChildrenCount(); i++)
  255. {
  256. IDiffCategory child = (IDiffCategory)((ITreeViewNode)category)
  257. .GetChild(i);
  258. AddChangeCategory(
  259. mergeCategoryTreeViewItem,
  260. child,
  261. rows,
  262. treeViewItemIds,
  263. treeView,
  264. expandCategories);
  265. }
  266. }
  267. static void AddChangeCategory(
  268. TreeViewItem parentItem,
  269. IDiffCategory category,
  270. List<TreeViewItem> rows,
  271. TreeViewItemIds<IDiffCategory, ITreeViewNode> treeViewItemIds,
  272. TreeView treeView,
  273. bool expandCategories)
  274. {
  275. int categoryId;
  276. if (!treeViewItemIds.TryGetCategoryItemId(category, out categoryId))
  277. categoryId = treeViewItemIds.AddCategoryItem(category);
  278. ChangeCategoryTreeViewItem changeCategoryTreeViewItem =
  279. new ChangeCategoryTreeViewItem(
  280. categoryId,
  281. parentItem.depth + 1,
  282. (ChangeCategory)category);
  283. parentItem.AddChild(changeCategoryTreeViewItem);
  284. rows.Add(changeCategoryTreeViewItem);
  285. if (!expandCategories &&
  286. !treeView.IsExpanded(changeCategoryTreeViewItem.id))
  287. return;
  288. AddClientDiffs(
  289. changeCategoryTreeViewItem,
  290. (ITreeViewNode)category,
  291. rows,
  292. treeViewItemIds);
  293. }
  294. static void AddClientDiffs(
  295. TreeViewItem parentItem,
  296. ITreeViewNode parentNode,
  297. List<TreeViewItem> rows,
  298. TreeViewItemIds<IDiffCategory, ITreeViewNode> treeViewItemIds)
  299. {
  300. for (int i = 0; i < parentNode.GetChildrenCount(); i++)
  301. {
  302. ITreeViewNode child = parentNode.GetChild(i);
  303. int nodeId;
  304. if (!treeViewItemIds.TryGetInfoItemId(child, out nodeId))
  305. nodeId = treeViewItemIds.AddInfoItem(child);
  306. TreeViewItem changeTreeViewItem =
  307. new ClientDiffTreeViewItem(
  308. nodeId,
  309. parentItem.depth + 1,
  310. (ClientDiffInfo)child);
  311. parentItem.AddChild(changeTreeViewItem);
  312. rows.Add(changeTreeViewItem);
  313. }
  314. }
  315. static void TreeViewItemGUI(
  316. TreeViewItem item,
  317. Rect rowRect,
  318. float rowHeight,
  319. UnityDiffTree diffTree,
  320. bool isSelected,
  321. bool isFocused,
  322. out float itemWidth)
  323. {
  324. if (item is MergeCategoryTreeViewItem)
  325. {
  326. MergeCategoryTreeViewItemGUI(
  327. rowRect,
  328. (MergeCategoryTreeViewItem)item,
  329. isSelected,
  330. isFocused,
  331. out itemWidth);
  332. return;
  333. }
  334. if (item is ChangeCategoryTreeViewItem)
  335. {
  336. ChangeCategoryTreeViewItemGUI(
  337. rowRect,
  338. (ChangeCategoryTreeViewItem)item,
  339. isSelected,
  340. isFocused,
  341. out itemWidth);
  342. return;
  343. }
  344. if (item is ClientDiffTreeViewItem)
  345. {
  346. ClientDiffTreeViewItemGUI(
  347. rowRect,
  348. rowHeight,
  349. diffTree,
  350. (ClientDiffTreeViewItem)item,
  351. isSelected,
  352. isFocused,
  353. out itemWidth);
  354. return;
  355. }
  356. itemWidth = 0;
  357. }
  358. static void MergeCategoryTreeViewItemGUI(
  359. Rect rowRect,
  360. MergeCategoryTreeViewItem item,
  361. bool isSelected,
  362. bool isFocused,
  363. out float itemWidth)
  364. {
  365. string label = item.Category.CategoryName;
  366. string infoLabel = PlasticLocalization.Name.ItemsCount.GetString(
  367. item.Category.GetChildrenCount());
  368. itemWidth = CalculateLabelWidth(label);
  369. DrawTreeViewItem.ForCategoryItem(
  370. rowRect,
  371. item.depth,
  372. label,
  373. infoLabel,
  374. isSelected,
  375. isFocused);
  376. }
  377. static void ChangeCategoryTreeViewItemGUI(
  378. Rect rowRect,
  379. ChangeCategoryTreeViewItem item,
  380. bool isSelected,
  381. bool isFocused,
  382. out float itemWidth)
  383. {
  384. string label = item.Category.CategoryName;
  385. string infoLabel = PlasticLocalization.Name.ItemsCount.GetString(
  386. item.Category.GetChildrenCount());
  387. itemWidth = CalculateLabelWidth(label);
  388. DrawTreeViewItem.ForCategoryItem(
  389. rowRect,
  390. item.depth,
  391. label,
  392. infoLabel,
  393. isSelected,
  394. isFocused);
  395. }
  396. static void ClientDiffTreeViewItemGUI(
  397. Rect rowRect,
  398. float rowHeight,
  399. UnityDiffTree diffTree,
  400. ClientDiffTreeViewItem item,
  401. bool isSelected,
  402. bool isFocused,
  403. out float itemWidth)
  404. {
  405. string label = ClientDiffView.GetColumnText(
  406. item.Difference.DiffWithMount.Mount.RepSpec,
  407. item.Difference.DiffWithMount.Difference,
  408. PlasticLocalization.GetString(PlasticLocalization.Name.PathColumn));
  409. if (diffTree.HasMeta(item.Difference))
  410. label = string.Concat(label, UnityConstants.TREEVIEW_META_LABEL);
  411. Texture icon = GetClientDiffIcon(
  412. item.Difference.DiffWithMount.Difference.IsDirectory,
  413. label);
  414. itemWidth = CalculateItemWidth(label, icon, rowHeight);
  415. DrawTreeViewItem.ForItemCell(
  416. rowRect,
  417. rowHeight,
  418. item.depth,
  419. icon,
  420. null,
  421. label,
  422. isSelected,
  423. isFocused,
  424. false,
  425. false);
  426. }
  427. static float CalculateItemWidth(
  428. string label,
  429. Texture icon,
  430. float rowHeight)
  431. {
  432. float labelWidth = CalculateLabelWidth(label);
  433. float iconWidth = rowHeight * ((float)icon.width / icon.height);
  434. return labelWidth + iconWidth;
  435. }
  436. static float CalculateLabelWidth(string label)
  437. {
  438. GUIContent content = new GUIContent(label);
  439. Vector2 contentSize = DefaultStyles.label.CalcSize(content);
  440. return contentSize.x;
  441. }
  442. static Texture GetClientDiffIcon(bool isDirectory, string path)
  443. {
  444. if (isDirectory)
  445. return Images.GetDirectoryIcon();
  446. return Images.GetFileIconFromCmPath(path);
  447. }
  448. bool mExpandCategories;
  449. TreeViewItemIds<IDiffCategory, ITreeViewNode> mTreeViewItemIds =
  450. new TreeViewItemIds<IDiffCategory, ITreeViewNode>();
  451. List<TreeViewItem> mRows = new List<TreeViewItem>();
  452. UnityDiffTree mDiffTree = new UnityDiffTree();
  453. MultiColumnHeaderState.Column mHorizontalColumn;
  454. float mLargestRowWidth;
  455. readonly CooldownWindowDelayer mCooldownFilterAction;
  456. static readonly List<string> ColumnsNames = new List<string> {
  457. PlasticLocalization.GetString(PlasticLocalization.Name.PathColumn)};
  458. readonly DiffTreeViewMenu mMenu;
  459. }
  460. }