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.

AssetMenuItems.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using UnityEditor.VersionControl;
  2. using Codice.CM.Common;
  3. using Codice.Client.Common.EventTracking;
  4. using Codice.LogWrapper;
  5. using PlasticGui;
  6. using PlasticGui.WorkspaceWindow.Items;
  7. using Unity.PlasticSCM.Editor.AssetsOverlays.Cache;
  8. using Unity.PlasticSCM.Editor.AssetUtils.Processor;
  9. using Unity.PlasticSCM.Editor.UI;
  10. using Unity.PlasticSCM.Editor.Tool;
  11. namespace Unity.PlasticSCM.Editor.AssetMenu
  12. {
  13. internal class AssetMenuItems
  14. {
  15. internal static void Enable(
  16. WorkspaceInfo wkInfo,
  17. IAssetStatusCache assetStatusCache)
  18. {
  19. if (mIsEnabled)
  20. return;
  21. mLog.Debug("Enable");
  22. mWkInfo = wkInfo;
  23. mAssetStatusCache = assetStatusCache;
  24. mIsEnabled = true;
  25. mAssetSelection = new ProjectViewAssetSelection(UpdateFilterMenuItems);
  26. mFilterMenuBuilder = new AssetFilesFilterPatternsMenuBuilder(
  27. IGNORE_MENU_ITEMS_PRIORITY,
  28. HIDDEN_MENU_ITEMS_PRIORITY);
  29. AddMenuItems();
  30. }
  31. internal static void Disable()
  32. {
  33. mLog.Debug("Disable");
  34. mIsEnabled = false;
  35. RemoveMenuItems();
  36. if (mAssetSelection != null)
  37. mAssetSelection.Dispose();
  38. mWkInfo = null;
  39. mAssetStatusCache = null;
  40. mAssetSelection = null;
  41. mFilterMenuBuilder = null;
  42. mOperations = null;
  43. }
  44. internal static void BuildOperations(
  45. WorkspaceInfo wkInfo,
  46. WorkspaceWindow workspaceWindow,
  47. IViewSwitcher viewSwitcher,
  48. IHistoryViewLauncher historyViewLauncher,
  49. GluonGui.ViewHost viewHost,
  50. WorkspaceOperationsMonitor workspaceOperationsMonitor,
  51. PlasticGui.WorkspaceWindow.NewIncomingChangesUpdater incomingChangesUpdater,
  52. IAssetStatusCache assetStatusCache,
  53. IMergeViewLauncher mergeViewLauncher,
  54. PlasticGui.Gluon.IGluonViewSwitcher gluonViewSwitcher,
  55. LaunchTool.IShowDownloadPlasticExeWindow showDownloadPlasticExeWindow,
  56. bool isGluonMode)
  57. {
  58. if (!mIsEnabled)
  59. Enable(wkInfo, assetStatusCache);
  60. AssetOperations assetOperations = new AssetOperations(
  61. wkInfo,
  62. workspaceWindow,
  63. viewSwitcher,
  64. historyViewLauncher,
  65. viewHost,
  66. workspaceOperationsMonitor,
  67. incomingChangesUpdater,
  68. mAssetStatusCache,
  69. mergeViewLauncher,
  70. gluonViewSwitcher,
  71. mAssetSelection,
  72. showDownloadPlasticExeWindow,
  73. isGluonMode);
  74. mOperations = assetOperations;
  75. mFilterMenuBuilder.SetOperations(assetOperations);
  76. }
  77. static void RemoveMenuItems()
  78. {
  79. mFilterMenuBuilder.RemoveMenuItems();
  80. HandleMenuItem.RemoveMenuItem(
  81. PlasticLocalization.GetString(PlasticLocalization.Name.PrefixUnityVersionControlMenu));
  82. HandleMenuItem.UpdateAllMenus();
  83. }
  84. static void UpdateFilterMenuItems()
  85. {
  86. AssetList assetList = ((AssetOperations.IAssetSelection)
  87. mAssetSelection).GetSelectedAssets();
  88. SelectedPathsGroupInfo info = AssetsSelection.GetSelectedPathsGroupInfo(
  89. mWkInfo.ClientPath, assetList, mAssetStatusCache);
  90. FilterMenuActions actions =
  91. assetList.Count != info.SelectedCount ?
  92. new FilterMenuActions() :
  93. FilterMenuUpdater.GetMenuActions(info);
  94. mFilterMenuBuilder.UpdateMenuItems(actions);
  95. }
  96. static void AddMenuItems()
  97. {
  98. // TODO: Try removing this
  99. // Somehow first item always disappears. So this is a filler item
  100. HandleMenuItem.AddMenuItem(
  101. GetPlasticMenuItemName(PlasticLocalization.Name.PendingChangesPlasticMenu),
  102. PENDING_CHANGES_MENU_ITEM_PRIORITY,
  103. PendingChanges, ValidatePendingChanges);
  104. HandleMenuItem.AddMenuItem(
  105. GetPlasticMenuItemName(PlasticLocalization.Name.PendingChangesPlasticMenu),
  106. PENDING_CHANGES_MENU_ITEM_PRIORITY,
  107. PendingChanges, ValidatePendingChanges);
  108. HandleMenuItem.AddMenuItem(
  109. GetPlasticMenuItemName(PlasticLocalization.Name.AddPlasticMenu),
  110. ADD_MENU_ITEM_PRIORITY,
  111. Add, ValidateAdd);
  112. HandleMenuItem.AddMenuItem(
  113. GetPlasticMenuItemName(PlasticLocalization.Name.CheckoutPlasticMenu),
  114. CHECKOUT_MENU_ITEM_PRIORITY,
  115. Checkout, ValidateCheckout);
  116. HandleMenuItem.AddMenuItem(
  117. GetPlasticMenuItemName(PlasticLocalization.Name.CheckinPlasticMenu),
  118. CHECKIN_MENU_ITEM_PRIORITY,
  119. Checkin, ValidateCheckin);
  120. HandleMenuItem.AddMenuItem(
  121. GetPlasticMenuItemName(PlasticLocalization.Name.UndoPlasticMenu),
  122. UNDO_MENU_ITEM_PRIORITY,
  123. Undo, ValidateUndo);
  124. UpdateFilterMenuItems();
  125. HandleMenuItem.AddMenuItem(
  126. GetPlasticMenuItemName(PlasticLocalization.Name.DiffPlasticMenu),
  127. GetPlasticShortcut.ForAssetDiff(),
  128. DIFF_MENU_ITEM_PRIORITY,
  129. Diff, ValidateDiff);
  130. HandleMenuItem.AddMenuItem(
  131. GetPlasticMenuItemName(PlasticLocalization.Name.HistoryPlasticMenu),
  132. GetPlasticShortcut.ForHistory(),
  133. HISTORY_MENU_ITEM_PRIORITY,
  134. History, ValidateHistory);
  135. HandleMenuItem.UpdateAllMenus();
  136. }
  137. static void PendingChanges()
  138. {
  139. ShowWindow.Plastic();
  140. mOperations.ShowPendingChanges();
  141. }
  142. static bool ValidatePendingChanges()
  143. {
  144. return true;
  145. }
  146. static void Add()
  147. {
  148. if (mOperations == null)
  149. ShowWindow.Plastic();
  150. mOperations.Add();
  151. }
  152. static bool ValidateAdd()
  153. {
  154. return ShouldMenuItemBeEnabled(
  155. mWkInfo.ClientPath, mAssetSelection, mAssetStatusCache,
  156. AssetMenuOperations.Add);
  157. }
  158. static void Checkout()
  159. {
  160. if (mOperations == null)
  161. ShowWindow.Plastic();
  162. mOperations.Checkout();
  163. }
  164. static bool ValidateCheckout()
  165. {
  166. return ShouldMenuItemBeEnabled(
  167. mWkInfo.ClientPath, mAssetSelection, mAssetStatusCache,
  168. AssetMenuOperations.Checkout);
  169. }
  170. static void Checkin()
  171. {
  172. TrackFeatureUseEvent.For(
  173. PlasticGui.Plastic.API.GetRepositorySpec(mWkInfo),
  174. TrackFeatureUseEvent.Features.ContextMenuCheckinOption);
  175. if (mOperations == null)
  176. ShowWindow.Plastic();
  177. mOperations.Checkin();
  178. }
  179. static bool ValidateCheckin()
  180. {
  181. return ShouldMenuItemBeEnabled(
  182. mWkInfo.ClientPath, mAssetSelection, mAssetStatusCache,
  183. AssetMenuOperations.Checkin);
  184. }
  185. static void Undo()
  186. {
  187. if (mOperations == null)
  188. ShowWindow.Plastic();
  189. mOperations.Undo();
  190. }
  191. static bool ValidateUndo()
  192. {
  193. return ShouldMenuItemBeEnabled(
  194. mWkInfo.ClientPath, mAssetSelection, mAssetStatusCache,
  195. AssetMenuOperations.Undo);
  196. }
  197. static void Diff()
  198. {
  199. if (mOperations == null)
  200. ShowWindow.Plastic();
  201. mOperations.ShowDiff();
  202. }
  203. static bool ValidateDiff()
  204. {
  205. return ShouldMenuItemBeEnabled(
  206. mWkInfo.ClientPath, mAssetSelection, mAssetStatusCache,
  207. AssetMenuOperations.Diff);
  208. }
  209. static void History()
  210. {
  211. ShowWindow.Plastic();
  212. mOperations.ShowHistory();
  213. }
  214. static bool ValidateHistory()
  215. {
  216. return ShouldMenuItemBeEnabled(
  217. mWkInfo.ClientPath, mAssetSelection, mAssetStatusCache,
  218. AssetMenuOperations.History);
  219. }
  220. static bool ShouldMenuItemBeEnabled(
  221. string wkPath,
  222. AssetOperations.IAssetSelection assetSelection,
  223. IAssetStatusCache statusCache,
  224. AssetMenuOperations operation)
  225. {
  226. AssetList assetList = assetSelection.GetSelectedAssets();
  227. if (assetList.Count == 0)
  228. return false;
  229. SelectedAssetGroupInfo selectedGroupInfo = SelectedAssetGroupInfo.
  230. BuildFromAssetList(wkPath, assetList, statusCache);
  231. if (assetList.Count != selectedGroupInfo.SelectedCount)
  232. return false;
  233. AssetMenuOperations operations = AssetMenuUpdater.
  234. GetAvailableMenuOperations(selectedGroupInfo);
  235. return operations.HasFlag(operation);
  236. }
  237. static string GetPlasticMenuItemName(PlasticLocalization.Name name)
  238. {
  239. return string.Format("{0}/{1}",
  240. PlasticLocalization.GetString(PlasticLocalization.Name.PrefixUnityVersionControlMenu),
  241. PlasticLocalization.GetString(name));
  242. }
  243. static IAssetMenuOperations mOperations;
  244. static ProjectViewAssetSelection mAssetSelection;
  245. static AssetFilesFilterPatternsMenuBuilder mFilterMenuBuilder;
  246. static bool mIsEnabled;
  247. static IAssetStatusCache mAssetStatusCache;
  248. static WorkspaceInfo mWkInfo;
  249. #if UNITY_6000_0_OR_NEWER
  250. // Puts Unity Version Control in a new section, as it precedes the Create menu with the old value
  251. const int BASE_MENU_ITEM_PRIORITY = 71;
  252. #else
  253. // Puts Unity Version Control right below the Create menu
  254. const int BASE_MENU_ITEM_PRIORITY = 19;
  255. #endif
  256. // incrementing the "order" param by 11 causes the menu system to add a separator
  257. const int PENDING_CHANGES_MENU_ITEM_PRIORITY = BASE_MENU_ITEM_PRIORITY;
  258. const int ADD_MENU_ITEM_PRIORITY = PENDING_CHANGES_MENU_ITEM_PRIORITY + 11;
  259. const int CHECKOUT_MENU_ITEM_PRIORITY = PENDING_CHANGES_MENU_ITEM_PRIORITY + 12;
  260. const int CHECKIN_MENU_ITEM_PRIORITY = PENDING_CHANGES_MENU_ITEM_PRIORITY + 13;
  261. const int UNDO_MENU_ITEM_PRIORITY = PENDING_CHANGES_MENU_ITEM_PRIORITY + 14;
  262. const int IGNORE_MENU_ITEMS_PRIORITY = PENDING_CHANGES_MENU_ITEM_PRIORITY + 25;
  263. const int HIDDEN_MENU_ITEMS_PRIORITY = PENDING_CHANGES_MENU_ITEM_PRIORITY + 26;
  264. const int DIFF_MENU_ITEM_PRIORITY = PENDING_CHANGES_MENU_ITEM_PRIORITY + 37;
  265. const int HISTORY_MENU_ITEM_PRIORITY = PENDING_CHANGES_MENU_ITEM_PRIORITY + 38;
  266. static readonly ILog mLog = PlasticApp.GetLogger("AssetMenuItems");
  267. }
  268. }