暫無描述
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.

CheckinDialog.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using Codice.Client.BaseCommands.EventTracking;
  7. using Codice.Client.Common;
  8. using Codice.CM.Common;
  9. using GluonGui;
  10. using PlasticGui;
  11. using PlasticGui.Gluon;
  12. using Unity.PlasticSCM.Editor.AssetsOverlays;
  13. using Unity.PlasticSCM.Editor.AssetsOverlays.Cache;
  14. using Unity.PlasticSCM.Editor.AssetUtils;
  15. using Unity.PlasticSCM.Editor.UI;
  16. using Unity.PlasticSCM.Editor.UI.Progress;
  17. using Unity.PlasticSCM.Editor.UI.Tree;
  18. namespace Unity.PlasticSCM.Editor.AssetMenu.Dialogs
  19. {
  20. internal class CheckinDialog : PlasticDialog
  21. {
  22. protected override Rect DefaultRect
  23. {
  24. get
  25. {
  26. var baseRect = base.DefaultRect;
  27. return new Rect(baseRect.x, baseRect.y, 700, 450);
  28. }
  29. }
  30. protected override string GetTitle()
  31. {
  32. return PlasticLocalization.GetString(
  33. PlasticLocalization.Name.CheckinChanges);
  34. }
  35. internal static bool CheckinPaths(
  36. WorkspaceInfo wkInfo,
  37. List<string> paths,
  38. IAssetStatusCache assetStatusCache,
  39. bool isGluonMode,
  40. EditorWindow parentWindow,
  41. IWorkspaceWindow workspaceWindow,
  42. ViewHost viewHost,
  43. GuiMessage.IGuiMessage guiMessage,
  44. IMergeViewLauncher mergeViewLauncher,
  45. IGluonViewSwitcher gluonViewSwitcher)
  46. {
  47. MetaCache metaCache = new MetaCache();
  48. metaCache.Build(paths);
  49. CheckinDialog dialog = Create(
  50. wkInfo,
  51. paths,
  52. assetStatusCache,
  53. metaCache,
  54. isGluonMode,
  55. new ProgressControlsForDialogs(),
  56. workspaceWindow,
  57. viewHost,
  58. guiMessage,
  59. mergeViewLauncher,
  60. gluonViewSwitcher);
  61. return dialog.RunModal(parentWindow) == ResponseType.Ok;
  62. }
  63. protected override void OnModalGUI()
  64. {
  65. Title(PlasticLocalization.GetString(
  66. PlasticLocalization.Name.CheckinComment));
  67. GUI.SetNextControlName(CHECKIN_TEXTAREA_NAME);
  68. mComment = GUILayout.TextArea(
  69. mComment,
  70. EditorStyles.textArea,
  71. GUILayout.MinHeight(120));
  72. if (!mTextAreaFocused)
  73. {
  74. EditorGUI.FocusTextInControl(CHECKIN_TEXTAREA_NAME);
  75. mTextAreaFocused = true;
  76. }
  77. Title(PlasticLocalization.GetString(PlasticLocalization.Name.Files));
  78. DoFileList(
  79. mWkInfo,
  80. mPaths,
  81. mAssetStatusCache,
  82. mMetaCache);
  83. DrawProgressForDialogs.For(
  84. mProgressControls.ProgressData);
  85. DoButtonsArea();
  86. mProgressControls.ForcedUpdateProgress(this);
  87. }
  88. void DoFileList(
  89. WorkspaceInfo wkInfo,
  90. List<string> paths,
  91. IAssetStatusCache assetStatusCache,
  92. MetaCache metaCache)
  93. {
  94. mFileListScrollPosition = GUILayout.BeginScrollView(
  95. mFileListScrollPosition,
  96. EditorStyles.helpBox,
  97. GUILayout.ExpandHeight(true));
  98. foreach (string path in paths)
  99. {
  100. if (MetaPath.IsMetaPath(path))
  101. continue;
  102. Texture fileIcon = Directory.Exists(path) ?
  103. Images.GetDirectoryIcon() :
  104. Images.GetFileIcon(path);
  105. string label = WorkspacePath.GetWorkspaceRelativePath(
  106. wkInfo.ClientPath, path);
  107. if (metaCache.HasMeta(path))
  108. label = string.Concat(label, UnityConstants.TREEVIEW_META_LABEL);
  109. AssetsOverlays.AssetStatus assetStatus =
  110. assetStatusCache.GetStatus(path);
  111. Rect selectionRect = EditorGUILayout.GetControlRect();
  112. DoListViewItem(selectionRect, fileIcon, label, assetStatus);
  113. }
  114. GUILayout.EndScrollView();
  115. }
  116. void DoListViewItem(
  117. Rect itemRect,
  118. Texture fileIcon,
  119. string label,
  120. AssetsOverlays.AssetStatus statusToDraw)
  121. {
  122. Texture overlayIcon = DrawAssetOverlay.DrawOverlayIcon.
  123. GetOverlayIcon(statusToDraw);
  124. itemRect = DrawTreeViewItem.DrawIconLeft(
  125. itemRect,
  126. UnityConstants.TREEVIEW_ROW_HEIGHT,
  127. fileIcon,
  128. overlayIcon);
  129. GUI.Label(itemRect, label);
  130. }
  131. void DoButtonsArea()
  132. {
  133. using (new EditorGUILayout.HorizontalScope())
  134. {
  135. GUILayout.FlexibleSpace();
  136. if (Application.platform == RuntimePlatform.WindowsEditor)
  137. {
  138. DoCheckinButton();
  139. DoCancelButton();
  140. return;
  141. }
  142. DoCancelButton();
  143. DoCheckinButton();
  144. }
  145. }
  146. void DoCheckinButton()
  147. {
  148. GUI.enabled = !string.IsNullOrEmpty(mComment) && !mIsRunningCheckin;
  149. try
  150. {
  151. if (!AcceptButton(PlasticLocalization.GetString(
  152. PlasticLocalization.Name.CheckinButton)))
  153. return;
  154. }
  155. finally
  156. {
  157. if (!mSentCheckinTrackEvent)
  158. {
  159. TrackFeatureUseEvent.For(
  160. PlasticGui.Plastic.API.GetRepositorySpec(mWkInfo),
  161. TrackFeatureUseEvent.Features.ContextMenuCheckinDialogCheckin);
  162. mSentCheckinTrackEvent = true;
  163. }
  164. GUI.enabled = true;
  165. }
  166. OkButtonWithCheckinAction();
  167. }
  168. void DoCancelButton()
  169. {
  170. if (!NormalButton(PlasticLocalization.GetString(
  171. PlasticLocalization.Name.CancelButton)))
  172. return;
  173. if (!mSentCancelTrackEvent)
  174. {
  175. TrackFeatureUseEvent.For(
  176. PlasticGui.Plastic.API.GetRepositorySpec(mWkInfo),
  177. TrackFeatureUseEvent.Features.ContextMenuCheckinDialogCancel);
  178. mSentCancelTrackEvent = true;
  179. }
  180. CancelButtonAction();
  181. }
  182. void OkButtonWithCheckinAction()
  183. {
  184. bool isCancelled;
  185. SaveAssets.ForPathsWithConfirmation(mPaths, out isCancelled);
  186. if (isCancelled)
  187. return;
  188. mIsRunningCheckin = true;
  189. mPaths.AddRange(mMetaCache.GetExistingMeta(mPaths));
  190. if (mIsGluonMode)
  191. {
  192. CheckinDialogOperations.CheckinPathsPartial(
  193. mWkInfo,
  194. mPaths,
  195. mComment,
  196. mViewHost,
  197. this,
  198. mGuiMessage,
  199. mProgressControls,
  200. mGluonViewSwitcher);
  201. return;
  202. }
  203. CheckinDialogOperations.CheckinPaths(
  204. mWkInfo,
  205. mPaths,
  206. mComment,
  207. mWorkspaceWindow,
  208. this,
  209. mGuiMessage,
  210. mProgressControls,
  211. mMergeViewLauncher);
  212. }
  213. static CheckinDialog Create(
  214. WorkspaceInfo wkInfo,
  215. List<string> paths,
  216. IAssetStatusCache assetStatusCache,
  217. MetaCache metaCache,
  218. bool isGluonMode,
  219. ProgressControlsForDialogs progressControls,
  220. IWorkspaceWindow workspaceWindow,
  221. ViewHost viewHost,
  222. GuiMessage.IGuiMessage guiMessage,
  223. IMergeViewLauncher mergeViewLauncher,
  224. IGluonViewSwitcher gluonViewSwitcher)
  225. {
  226. var instance = CreateInstance<CheckinDialog>();
  227. instance.IsResizable = true;
  228. instance.minSize = new Vector2(520, 370);
  229. instance.mWkInfo = wkInfo;
  230. instance.mPaths = paths;
  231. instance.mAssetStatusCache = assetStatusCache;
  232. instance.mMetaCache = metaCache;
  233. instance.mIsGluonMode = isGluonMode;
  234. instance.mProgressControls = progressControls;
  235. instance.mWorkspaceWindow = workspaceWindow;
  236. instance.mViewHost = viewHost;
  237. instance.mGuiMessage = guiMessage;
  238. instance.mMergeViewLauncher = mergeViewLauncher;
  239. instance.mGluonViewSwitcher = gluonViewSwitcher;
  240. instance.mEscapeKeyAction = instance.CancelButtonAction;
  241. return instance;
  242. }
  243. WorkspaceInfo mWkInfo;
  244. List<string> mPaths;
  245. IAssetStatusCache mAssetStatusCache;
  246. MetaCache mMetaCache;
  247. bool mIsGluonMode;
  248. bool mTextAreaFocused;
  249. string mComment;
  250. bool mIsRunningCheckin;
  251. Vector2 mFileListScrollPosition;
  252. // IMGUI evaluates every frame, need to make sure feature tracks get sent only once
  253. bool mSentCheckinTrackEvent = false;
  254. bool mSentCancelTrackEvent = false;
  255. ProgressControlsForDialogs mProgressControls;
  256. IWorkspaceWindow mWorkspaceWindow;
  257. ViewHost mViewHost;
  258. IMergeViewLauncher mMergeViewLauncher;
  259. IGluonViewSwitcher mGluonViewSwitcher;
  260. GuiMessage.IGuiMessage mGuiMessage;
  261. const string CHECKIN_TEXTAREA_NAME = "checkin_textarea";
  262. class MetaCache
  263. {
  264. internal bool HasMeta(string path)
  265. {
  266. return mCache.Contains(MetaPath.GetMetaPath(path));
  267. }
  268. internal List<string> GetExistingMeta(List<string> paths)
  269. {
  270. List<string> result = new List<string>();
  271. foreach (string path in paths)
  272. {
  273. string metaPath = MetaPath.GetMetaPath(path);
  274. if (!mCache.Contains(metaPath))
  275. continue;
  276. result.Add(metaPath);
  277. }
  278. return result;
  279. }
  280. internal void Build(List<string> paths)
  281. {
  282. HashSet<string> indexedKeys = BuildIndexedKeys(paths);
  283. for (int i = paths.Count - 1; i >= 0; i--)
  284. {
  285. string currentPath = paths[i];
  286. if (!MetaPath.IsMetaPath(currentPath))
  287. continue;
  288. string realPath = MetaPath.GetPathFromMetaPath(currentPath);
  289. if (!indexedKeys.Contains(realPath))
  290. continue;
  291. // found foo.c and foo.c.meta
  292. // with the same chage types - move .meta to cache
  293. mCache.Add(currentPath);
  294. paths.RemoveAt(i);
  295. }
  296. }
  297. static HashSet<string> BuildIndexedKeys(List<string> paths)
  298. {
  299. HashSet<string> result = new HashSet<string>();
  300. foreach (string path in paths)
  301. {
  302. if (MetaPath.IsMetaPath(path))
  303. continue;
  304. result.Add(path);
  305. }
  306. return result;
  307. }
  308. HashSet<string> mCache =
  309. new HashSet<string>();
  310. }
  311. }
  312. }