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.

DiffTreeViewMenu.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using UnityEditor;
  2. using UnityEngine;
  3. using Codice.Client.Common.EventTracking;
  4. using Codice.CM.Common;
  5. using PlasticGui;
  6. using PlasticGui.WorkspaceWindow.Diff;
  7. using Unity.PlasticSCM.Editor.UI;
  8. using Unity.PlasticSCM.Editor.Tool;
  9. namespace Unity.PlasticSCM.Editor.Views.Diff
  10. {
  11. internal class DiffTreeViewMenu
  12. {
  13. internal interface IMetaMenuOperations
  14. {
  15. bool SelectionHasMeta();
  16. void DiffMeta();
  17. void HistoryMeta();
  18. }
  19. internal DiffTreeViewMenu(
  20. IDiffTreeViewMenuOperations operations,
  21. IMetaMenuOperations metaMenuOperations)
  22. {
  23. mOperations = operations;
  24. mMetaMenuOperations = metaMenuOperations;
  25. BuildComponents();
  26. }
  27. internal void Popup()
  28. {
  29. GenericMenu menu = new GenericMenu();
  30. UpdateMenuItems(menu);
  31. menu.ShowAsContext();
  32. }
  33. internal bool ProcessKeyActionIfNeeded(Event e)
  34. {
  35. DiffTreeViewMenuOperations operationToExecute = GetMenuOperation(e);
  36. if (operationToExecute == DiffTreeViewMenuOperations.None)
  37. return false;
  38. SelectedDiffsGroupInfo info =
  39. mOperations.GetSelectedDiffsGroupInfo();
  40. DiffTreeViewMenuOperations operations =
  41. DiffTreeViewMenuUpdater.GetAvailableMenuOperations(info);
  42. if (!operations.HasFlag(operationToExecute))
  43. return false;
  44. ProcessMenuOperation(operationToExecute);
  45. return true;
  46. }
  47. void SaveRevisionAsMenuItem_Click()
  48. {
  49. mOperations.SaveRevisionAs();
  50. }
  51. void DiffMenuItem_Click()
  52. {
  53. mOperations.Diff();
  54. }
  55. void DiffMetaMenuItem_Click()
  56. {
  57. mMetaMenuOperations.DiffMeta();
  58. }
  59. void HistoryMenuItem_Click()
  60. {
  61. mOperations.History();
  62. }
  63. void HistoryMetaMenuItem_Click()
  64. {
  65. mMetaMenuOperations.HistoryMeta();
  66. }
  67. void RevertMenuItem_Click()
  68. {
  69. mOperations.RevertChanges();
  70. }
  71. void UndeleteMenuItem_Click()
  72. {
  73. mOperations.Undelete();
  74. }
  75. void UndeleteToSpecifiedPathMenuItem_Click()
  76. {
  77. mOperations.UndeleteToSpecifiedPaths();
  78. }
  79. void UpdateMenuItems(GenericMenu menu)
  80. {
  81. SelectedDiffsGroupInfo groupInfo =
  82. mOperations.GetSelectedDiffsGroupInfo();
  83. DiffTreeViewMenuOperations operations =
  84. DiffTreeViewMenuUpdater.GetAvailableMenuOperations(groupInfo);
  85. if (operations == DiffTreeViewMenuOperations.None)
  86. {
  87. menu.AddDisabledItem(GetNoActionMenuItemContent());
  88. return;
  89. }
  90. bool isMultipleSelection = groupInfo.SelectedItemsCount > 1;
  91. bool selectionHasMeta = mMetaMenuOperations.SelectionHasMeta();
  92. if (operations.HasFlag(DiffTreeViewMenuOperations.SaveAs))
  93. menu.AddItem(mSaveRevisionAsMenuItemContent, false, SaveRevisionAsMenuItem_Click);
  94. if (operations.HasFlag(DiffTreeViewMenuOperations.Diff))
  95. menu.AddItem(mDiffMenuItemContent, false, DiffMenuItem_Click);
  96. else
  97. menu.AddDisabledItem(mDiffMenuItemContent, false);
  98. if (mMetaMenuOperations.SelectionHasMeta())
  99. {
  100. if (operations.HasFlag(DiffTreeViewMenuOperations.Diff))
  101. menu.AddItem(mDiffMetaMenuItemContent, false, DiffMetaMenuItem_Click);
  102. else
  103. menu.AddDisabledItem(mDiffMetaMenuItemContent);
  104. }
  105. menu.AddSeparator(string.Empty);
  106. if (operations.HasFlag(DiffTreeViewMenuOperations.History))
  107. menu.AddItem(mViewHistoryMenuItemContent, false, HistoryMenuItem_Click);
  108. else
  109. menu.AddDisabledItem(mViewHistoryMenuItemContent, false);
  110. if (mMetaMenuOperations.SelectionHasMeta())
  111. {
  112. if (operations.HasFlag(DiffTreeViewMenuOperations.History))
  113. menu.AddItem(mViewHistoryMetaMenuItemContent, false, HistoryMetaMenuItem_Click);
  114. else
  115. menu.AddDisabledItem(mViewHistoryMetaMenuItemContent, false);
  116. }
  117. if (operations.HasFlag(DiffTreeViewMenuOperations.RevertChanges))
  118. {
  119. menu.AddSeparator(string.Empty);
  120. mRevertMenuItemContent.text = GetRevertMenuItemText(
  121. isMultipleSelection,
  122. selectionHasMeta);
  123. menu.AddItem(mRevertMenuItemContent, false, RevertMenuItem_Click);
  124. }
  125. if (operations.HasFlag(DiffTreeViewMenuOperations.Undelete) ||
  126. operations.HasFlag(DiffTreeViewMenuOperations.UndeleteToSpecifiedPaths))
  127. {
  128. menu.AddSeparator(string.Empty);
  129. }
  130. if (operations.HasFlag(DiffTreeViewMenuOperations.Undelete))
  131. {
  132. mUndeleteMenuItemContent.text = GetUndeleteMenuItemText(
  133. isMultipleSelection,
  134. selectionHasMeta);
  135. menu.AddItem(mUndeleteMenuItemContent, false, UndeleteMenuItem_Click);
  136. }
  137. if (operations.HasFlag(DiffTreeViewMenuOperations.UndeleteToSpecifiedPaths))
  138. {
  139. mUndeleteToSpecifiedPathMenuItemContent.text = GetUndeleteToSpecifiedPathMenuItemText(
  140. isMultipleSelection,
  141. selectionHasMeta);
  142. menu.AddItem(mUndeleteToSpecifiedPathMenuItemContent, false, UndeleteToSpecifiedPathMenuItem_Click);
  143. }
  144. }
  145. GUIContent GetNoActionMenuItemContent()
  146. {
  147. if (mNoActionMenuItemContent == null)
  148. {
  149. mNoActionMenuItemContent = new GUIContent(
  150. PlasticLocalization.GetString(PlasticLocalization.
  151. Name.NoActionMenuItem));
  152. }
  153. return mNoActionMenuItemContent;
  154. }
  155. static string GetRevertMenuItemText(
  156. bool isMultipleSelection,
  157. bool selectionHasMeta)
  158. {
  159. if (selectionHasMeta && !isMultipleSelection)
  160. return PlasticLocalization.GetString(PlasticLocalization.Name.UndoThisChangePlusMeta);
  161. return isMultipleSelection ?
  162. PlasticLocalization.GetString(PlasticLocalization.Name.UndoSelectedChanges) :
  163. PlasticLocalization.GetString(PlasticLocalization.Name.UndoThisChange);
  164. }
  165. static string GetUndeleteMenuItemText(
  166. bool isMultipleSelection,
  167. bool selectionHasMeta)
  168. {
  169. if (selectionHasMeta && !isMultipleSelection)
  170. return PlasticLocalization.GetString(PlasticLocalization.Name.UndeleteRevisionPlusMeta);
  171. return isMultipleSelection ?
  172. PlasticLocalization.GetString(PlasticLocalization.Name.UndeleteSelectedRevisions) :
  173. PlasticLocalization.GetString(PlasticLocalization.Name.UndeleteRevisions);
  174. }
  175. static string GetUndeleteToSpecifiedPathMenuItemText(
  176. bool isMultipleSelection,
  177. bool selectionHasMeta)
  178. {
  179. if (selectionHasMeta && !isMultipleSelection)
  180. return PlasticLocalization.GetString(PlasticLocalization.Name.UndeleteRevisionPlusMetaPath);
  181. return isMultipleSelection ?
  182. PlasticLocalization.GetString(PlasticLocalization.Name.UndeleteSelectedRevisionsPaths) :
  183. PlasticLocalization.GetString(PlasticLocalization.Name.UndeleteRevisionPath);
  184. }
  185. void ProcessMenuOperation(DiffTreeViewMenuOperations operationToExecute)
  186. {
  187. if (operationToExecute == DiffTreeViewMenuOperations.SaveAs)
  188. {
  189. SaveRevisionAsMenuItem_Click();
  190. return;
  191. }
  192. if (operationToExecute == DiffTreeViewMenuOperations.Diff)
  193. {
  194. DiffMenuItem_Click();
  195. return;
  196. }
  197. if (operationToExecute == DiffTreeViewMenuOperations.History)
  198. {
  199. HistoryMenuItem_Click();
  200. }
  201. }
  202. static DiffTreeViewMenuOperations GetMenuOperation(Event e)
  203. {
  204. if (Keyboard.IsControlOrCommandKeyPressed(e) && Keyboard.IsKeyPressed(e, KeyCode.D))
  205. return DiffTreeViewMenuOperations.Diff;
  206. if (Keyboard.IsControlOrCommandKeyPressed(e) && Keyboard.IsKeyPressed(e, KeyCode.H))
  207. return DiffTreeViewMenuOperations.History;
  208. return DiffTreeViewMenuOperations.None;
  209. }
  210. void BuildComponents()
  211. {
  212. mSaveRevisionAsMenuItemContent = new GUIContent(
  213. PlasticLocalization.GetString(PlasticLocalization.Name.DiffMenuItemSaveRevisionAs));
  214. mDiffMenuItemContent = new GUIContent(
  215. string.Format("{0} {1}",
  216. PlasticLocalization.GetString(PlasticLocalization.Name.DiffMenuItem),
  217. GetPlasticShortcut.ForDiff()));
  218. mDiffMetaMenuItemContent = new GUIContent(
  219. PlasticLocalization.GetString(PlasticLocalization.Name.DiffMetaMenuItem));
  220. mViewHistoryMenuItemContent = new GUIContent(
  221. string.Format("{0} {1}",
  222. PlasticLocalization.GetString(PlasticLocalization.Name.ViewHistoryMenuItem),
  223. GetPlasticShortcut.ForHistory()));
  224. mViewHistoryMetaMenuItemContent = new GUIContent(
  225. PlasticLocalization.GetString(PlasticLocalization.Name.ViewHistoryMetaMenuItem));
  226. mRevertMenuItemContent = new GUIContent();
  227. mUndeleteMenuItemContent = new GUIContent();
  228. mUndeleteToSpecifiedPathMenuItemContent = new GUIContent();
  229. }
  230. GUIContent mNoActionMenuItemContent;
  231. GUIContent mSaveRevisionAsMenuItemContent;
  232. GUIContent mDiffMenuItemContent;
  233. GUIContent mDiffMetaMenuItemContent;
  234. GUIContent mViewHistoryMenuItemContent;
  235. GUIContent mViewHistoryMetaMenuItemContent;
  236. GUIContent mRevertMenuItemContent;
  237. GUIContent mUndeleteMenuItemContent;
  238. GUIContent mUndeleteToSpecifiedPathMenuItemContent;
  239. readonly IDiffTreeViewMenuOperations mOperations;
  240. readonly IMetaMenuOperations mMetaMenuOperations;
  241. }
  242. }