DiffTreeViewMenu.cs 10 KB

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