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.

BranchesTab.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEditor.IMGUI.Controls;
  5. using UnityEngine;
  6. using Codice.Client.Common.Threading;
  7. using Codice.CM.Common;
  8. using PlasticGui;
  9. using PlasticGui.WorkspaceWindow;
  10. using PlasticGui.WorkspaceWindow.QueryViews;
  11. using PlasticGui.WorkspaceWindow.QueryViews.Branches;
  12. using PlasticGui.WorkspaceWindow.Update;
  13. using Unity.PlasticSCM.Editor.AssetUtils;
  14. using Unity.PlasticSCM.Editor.UI;
  15. using Unity.PlasticSCM.Editor.UI.Progress;
  16. using Unity.PlasticSCM.Editor.UI.Tree;
  17. using Unity.PlasticSCM.Editor.Views.Branches.Dialogs;
  18. using Unity.PlasticSCM.Editor.Views.Changesets;
  19. using GluonNewIncomingChangesUpdater = PlasticGui.Gluon.WorkspaceWindow.NewIncomingChangesUpdater;
  20. namespace Unity.PlasticSCM.Editor.Views.Branches
  21. {
  22. internal class BranchesTab :
  23. IRefreshableView,
  24. IQueryRefreshableView,
  25. IBranchMenuOperations
  26. {
  27. internal BranchesListView Table { get { return mBranchesListView; } }
  28. internal IBranchMenuOperations Operations { get { return this; } }
  29. internal BranchesTab(
  30. WorkspaceInfo wkInfo,
  31. IWorkspaceWindow workspaceWindow,
  32. IViewSwitcher viewSwitcher,
  33. IMergeViewLauncher mergeViewLauncher,
  34. IUpdateReport updateReport,
  35. NewIncomingChangesUpdater developerNewIncomingChangesUpdater,
  36. GluonNewIncomingChangesUpdater gluonNewIncomingChangesUpdater,
  37. EditorWindow parentWindow)
  38. {
  39. mWkInfo = wkInfo;
  40. mParentWindow = parentWindow;
  41. mProgressControls = new ProgressControlsForViews();
  42. mDeveloperNewIncomingChangesUpdater = developerNewIncomingChangesUpdater;
  43. mGluonNewIncomingChangesUpdater = gluonNewIncomingChangesUpdater;
  44. BuildComponents(
  45. wkInfo,
  46. workspaceWindow,
  47. viewSwitcher,
  48. mergeViewLauncher,
  49. updateReport,
  50. developerNewIncomingChangesUpdater,
  51. parentWindow);
  52. ((IRefreshableView)this).Refresh();
  53. }
  54. internal void OnEnable()
  55. {
  56. mSearchField.downOrUpArrowKeyPressed +=
  57. SearchField_OnDownOrUpArrowKeyPressed;
  58. }
  59. internal void OnDisable()
  60. {
  61. mSearchField.downOrUpArrowKeyPressed -=
  62. SearchField_OnDownOrUpArrowKeyPressed;
  63. TreeHeaderSettings.Save(
  64. mBranchesListView.multiColumnHeader.state,
  65. UnityConstants.BRANCHES_TABLE_SETTINGS_NAME);
  66. }
  67. internal void Update()
  68. {
  69. mProgressControls.UpdateProgress(mParentWindow);
  70. }
  71. internal void OnGUI()
  72. {
  73. DoActionsToolbar(mProgressControls);
  74. DoBranchesArea(
  75. mBranchesListView,
  76. mProgressControls.IsOperationRunning());
  77. }
  78. internal void DrawSearchFieldForBranchesTab()
  79. {
  80. DrawSearchField.For(
  81. mSearchField,
  82. mBranchesListView,
  83. UnityConstants.SEARCH_FIELD_WIDTH);
  84. }
  85. internal void DrawDateFilter()
  86. {
  87. GUI.enabled = !mProgressControls.IsOperationRunning();
  88. EditorGUI.BeginChangeCheck();
  89. mDateFilter.FilterType = (DateFilter.Type)
  90. EditorGUILayout.EnumPopup(
  91. mDateFilter.FilterType,
  92. EditorStyles.toolbarDropDown,
  93. GUILayout.Width(100));
  94. if (EditorGUI.EndChangeCheck())
  95. {
  96. EnumPopupSetting<DateFilter.Type>.Save(
  97. mDateFilter.FilterType,
  98. UnityConstants.BRANCHES_DATE_FILTER_SETTING_NAME);
  99. ((IRefreshableView)this).Refresh();
  100. }
  101. GUI.enabled = true;
  102. }
  103. internal void SetWorkingObjectInfo(WorkingObjectInfo homeInfo)
  104. {
  105. lock(mLock)
  106. {
  107. mLoadedBranchId = homeInfo.BranchInfo.BranchId;
  108. }
  109. mBranchesListView.SetLoadedBranchId(mLoadedBranchId);
  110. }
  111. void IRefreshableView.Refresh()
  112. {
  113. // VCS-1005209 - There are scenarios where the list of branches need to check for incoming changes.
  114. // For example, deleting the active branch will automatically switch your workspace to the parent changeset,
  115. // which might have incoming changes.
  116. if (mDeveloperNewIncomingChangesUpdater != null)
  117. mDeveloperNewIncomingChangesUpdater.Update(DateTime.Now);
  118. if (mGluonNewIncomingChangesUpdater != null)
  119. mGluonNewIncomingChangesUpdater.Update(DateTime.Now);
  120. string query = GetBranchesQuery(mDateFilter);
  121. FillBranches(mWkInfo, query, BranchesSelection.
  122. GetSelectedRepObjectInfos(mBranchesListView));
  123. }
  124. //IQueryRefreshableView
  125. public void RefreshAndSelect(RepObjectInfo repObj)
  126. {
  127. string query = GetBranchesQuery(mDateFilter);
  128. FillBranches(mWkInfo, query, new List<RepObjectInfo> { repObj });
  129. }
  130. int IBranchMenuOperations.GetSelectedBranchesCount()
  131. {
  132. return BranchesSelection.GetSelectedBranchesCount(mBranchesListView);
  133. }
  134. void IBranchMenuOperations.CreateBranch()
  135. {
  136. RepositorySpec repSpec = BranchesSelection.GetSelectedRepository(mBranchesListView);
  137. BranchInfo branchInfo = BranchesSelection.GetSelectedBranch(mBranchesListView);
  138. BranchCreationData branchCreationData = CreateBranchDialog.CreateBranchFromLastParentBranchChangeset(
  139. mParentWindow,
  140. repSpec,
  141. branchInfo);
  142. mBranchOperations.CreateBranch(
  143. branchCreationData,
  144. RefreshAsset.BeforeLongAssetOperation,
  145. RefreshAsset.AfterLongAssetOperation);
  146. }
  147. void IBranchMenuOperations.CreateTopLevelBranch() { }
  148. void IBranchMenuOperations.SwitchToBranch()
  149. {
  150. RepositorySpec repSpec = BranchesSelection.GetSelectedRepository(mBranchesListView);
  151. BranchInfo branchInfo = BranchesSelection.GetSelectedBranch(mBranchesListView);
  152. mBranchOperations.SwitchToBranch(
  153. repSpec,
  154. branchInfo,
  155. RefreshAsset.BeforeLongAssetOperation,
  156. RefreshAsset.AfterLongAssetOperation);
  157. }
  158. void IBranchMenuOperations.MergeBranch() { }
  159. void IBranchMenuOperations.CherrypickBranch() { }
  160. void IBranchMenuOperations.MergeToBranch() { }
  161. void IBranchMenuOperations.PullBranch() { }
  162. void IBranchMenuOperations.PullRemoteBranch() { }
  163. void IBranchMenuOperations.SyncWithGit() { }
  164. void IBranchMenuOperations.PushBranch() { }
  165. void IBranchMenuOperations.DiffBranch() { }
  166. void IBranchMenuOperations.DiffWithAnotherBranch() { }
  167. void IBranchMenuOperations.ViewChangesets() { }
  168. void IBranchMenuOperations.RenameBranch()
  169. {
  170. RepositorySpec repSpec = BranchesSelection.GetSelectedRepository(mBranchesListView);
  171. BranchInfo branchInfo = BranchesSelection.GetSelectedBranch(mBranchesListView);
  172. BranchRenameData branchRenameData = RenameBranchDialog.GetBranchRenameData(
  173. repSpec,
  174. branchInfo,
  175. mParentWindow);
  176. mBranchOperations.RenameBranch(branchRenameData);
  177. }
  178. void IBranchMenuOperations.DeleteBranch()
  179. {
  180. var branchesToDelete = BranchesSelection.GetSelectedBranches(mBranchesListView);
  181. if (!DeleteBranchDialog.ConfirmDelete(branchesToDelete))
  182. return;
  183. mBranchOperations.DeleteBranch(
  184. BranchesSelection.GetSelectedRepositories(mBranchesListView),
  185. branchesToDelete,
  186. DeleteBranchOptions.IncludeChangesets);
  187. }
  188. void IBranchMenuOperations.CreateCodeReview() { }
  189. void IBranchMenuOperations.ViewPermissions() { }
  190. void SearchField_OnDownOrUpArrowKeyPressed()
  191. {
  192. mBranchesListView.SetFocusAndEnsureSelectedItem();
  193. }
  194. void OnBranchesListViewSizeChanged()
  195. {
  196. if (!mShouldScrollToSelection)
  197. return;
  198. mShouldScrollToSelection = false;
  199. TableViewOperations.ScrollToSelection(mBranchesListView);
  200. }
  201. void FillBranches(
  202. WorkspaceInfo wkInfo,
  203. string query,
  204. List<RepObjectInfo> branchesToSelect)
  205. {
  206. if (mIsRefreshing)
  207. return;
  208. mIsRefreshing = true;
  209. int defaultRow = TableViewOperations.
  210. GetFirstSelectedRow(mBranchesListView);
  211. ((IProgressControls)mProgressControls).ShowProgress(
  212. PlasticLocalization.GetString(
  213. PlasticLocalization.Name.LoadingBranches));
  214. ViewQueryResult queryResult = null;
  215. IThreadWaiter waiter = ThreadWaiter.GetWaiter();
  216. waiter.Execute(
  217. /*threadOperationDelegate*/ delegate
  218. {
  219. long loadedBranchId = GetLoadedBranchId(wkInfo);
  220. lock(mLock)
  221. {
  222. mLoadedBranchId = loadedBranchId;
  223. }
  224. queryResult = new ViewQueryResult(
  225. PlasticGui.Plastic.API.FindQuery(wkInfo, query));
  226. },
  227. /*afterOperationDelegate*/ delegate
  228. {
  229. try
  230. {
  231. if (waiter.Exception != null)
  232. {
  233. ExceptionsHandler.DisplayException(waiter.Exception);
  234. return;
  235. }
  236. UpdateBranchesList(
  237. mBranchesListView,
  238. queryResult,
  239. mLoadedBranchId);
  240. int branchesCount = GetBranchesCount(queryResult);
  241. if (branchesCount == 0)
  242. {
  243. return;
  244. }
  245. BranchesSelection.SelectBranches(
  246. mBranchesListView, branchesToSelect, defaultRow);
  247. }
  248. finally
  249. {
  250. ((IProgressControls)mProgressControls).HideProgress();
  251. mIsRefreshing = false;
  252. }
  253. });
  254. }
  255. static void UpdateBranchesList(
  256. BranchesListView branchesListView,
  257. ViewQueryResult queryResult,
  258. long loadedBranchId)
  259. {
  260. branchesListView.BuildModel(
  261. queryResult, loadedBranchId);
  262. branchesListView.Refilter();
  263. branchesListView.Sort();
  264. branchesListView.Reload();
  265. }
  266. static long GetLoadedBranchId(WorkspaceInfo wkInfo)
  267. {
  268. BranchInfo brInfo = PlasticGui.Plastic.API.GetWorkingBranch(wkInfo);
  269. if (brInfo != null)
  270. return brInfo.BranchId;
  271. return -1;
  272. }
  273. static int GetBranchesCount(
  274. ViewQueryResult queryResult)
  275. {
  276. if (queryResult == null)
  277. return 0;
  278. return queryResult.Count();
  279. }
  280. static string GetBranchesQuery(DateFilter dateFilter)
  281. {
  282. if (dateFilter.FilterType == DateFilter.Type.AllTime)
  283. return QueryConstants.BranchesBeginningQuery;
  284. string whereClause = QueryConstants.GetDateWhereClause(
  285. dateFilter.GetTimeAgo());
  286. return string.Format("{0} {1}",
  287. QueryConstants.BranchesBeginningQuery,
  288. whereClause);
  289. }
  290. static void DoActionsToolbar(ProgressControlsForViews progressControls)
  291. {
  292. EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
  293. if (progressControls.IsOperationRunning())
  294. {
  295. DrawProgressForViews.ForIndeterminateProgress(
  296. progressControls.ProgressData);
  297. }
  298. GUILayout.FlexibleSpace();
  299. EditorGUILayout.EndHorizontal();
  300. }
  301. static void DoBranchesArea(
  302. BranchesListView branchesListView,
  303. bool isOperationRunning)
  304. {
  305. EditorGUILayout.BeginVertical();
  306. GUI.enabled = !isOperationRunning;
  307. Rect rect = GUILayoutUtility.GetRect(0, 100000, 0, 100000);
  308. branchesListView.OnGUI(rect);
  309. GUI.enabled = true;
  310. EditorGUILayout.EndVertical();
  311. }
  312. void BuildComponents(
  313. WorkspaceInfo wkInfo,
  314. IWorkspaceWindow workspaceWindow,
  315. IViewSwitcher viewSwitcher,
  316. IMergeViewLauncher mergeViewLauncher,
  317. IUpdateReport updateReport,
  318. NewIncomingChangesUpdater developerNewIncomingChangesUpdater,
  319. EditorWindow parentWindow)
  320. {
  321. mSearchField = new SearchField();
  322. mSearchField.downOrUpArrowKeyPressed += SearchField_OnDownOrUpArrowKeyPressed;
  323. DateFilter.Type dateFilterType =
  324. EnumPopupSetting<DateFilter.Type>.Load(
  325. UnityConstants.BRANCHES_DATE_FILTER_SETTING_NAME,
  326. DateFilter.Type.LastMonth);
  327. mDateFilter = new DateFilter(dateFilterType);
  328. BranchesListHeaderState headerState =
  329. BranchesListHeaderState.GetDefault();
  330. TreeHeaderSettings.Load(headerState,
  331. UnityConstants.BRANCHES_TABLE_SETTINGS_NAME,
  332. (int)BranchesListColumn.CreationDate, false);
  333. mBranchesListView = new BranchesListView(
  334. headerState,
  335. BranchesListHeaderState.GetColumnNames(),
  336. new BranchesViewMenu(this),
  337. sizeChangedAction: OnBranchesListViewSizeChanged);
  338. mBranchesListView.Reload();
  339. mBranchOperations = new BranchOperations(
  340. wkInfo,
  341. workspaceWindow,
  342. viewSwitcher,
  343. mergeViewLauncher,
  344. this,
  345. ViewType.BranchesView,
  346. mProgressControls,
  347. updateReport,
  348. new ContinueWithPendingChangesQuestionerBuilder(viewSwitcher, parentWindow),
  349. developerNewIncomingChangesUpdater);
  350. }
  351. SearchField mSearchField;
  352. bool mIsRefreshing;
  353. DateFilter mDateFilter;
  354. bool mShouldScrollToSelection;
  355. BranchesListView mBranchesListView;
  356. BranchOperations mBranchOperations;
  357. long mLoadedBranchId = -1;
  358. object mLock = new object();
  359. readonly WorkspaceInfo mWkInfo;
  360. readonly ProgressControlsForViews mProgressControls;
  361. readonly EditorWindow mParentWindow;
  362. readonly NewIncomingChangesUpdater mDeveloperNewIncomingChangesUpdater;
  363. readonly GluonNewIncomingChangesUpdater mGluonNewIncomingChangesUpdater;
  364. }
  365. }