PlasticWindow.cs 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. using System;
  2. using System.Linq;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using Codice.Client.BaseCommands;
  6. using Codice.Client.BaseCommands.EventTracking;
  7. using Codice.Client.Common;
  8. using Codice.Client.Common.FsNodeReaders.Watcher;
  9. using Codice.Client.Common.Threading;
  10. using Codice.CM.Common;
  11. using Codice.LogWrapper;
  12. using GluonGui;
  13. using PlasticGui;
  14. using Unity.PlasticSCM.Editor.AssetMenu;
  15. using Unity.PlasticSCM.Editor.AssetUtils;
  16. using Unity.PlasticSCM.Editor.Configuration;
  17. using Unity.PlasticSCM.Editor.Configuration.CloudEdition.Welcome;
  18. using Unity.PlasticSCM.Editor.Inspector;
  19. using Unity.PlasticSCM.Editor.Tool;
  20. using Unity.PlasticSCM.Editor.UI;
  21. using Unity.PlasticSCM.Editor.UI.Avatar;
  22. using Unity.PlasticSCM.Editor.UI.Progress;
  23. using Unity.PlasticSCM.Editor.Views.CreateWorkspace;
  24. using Unity.PlasticSCM.Editor.Views.Welcome;
  25. using Unity.PlasticSCM.Editor.WebApi;
  26. using GluonCheckIncomingChanges = PlasticGui.Gluon.WorkspaceWindow.CheckIncomingChanges;
  27. using GluonNewIncomingChangesUpdater = PlasticGui.Gluon.WorkspaceWindow.NewIncomingChangesUpdater;
  28. using PlasticAssetModificationProcessor = Unity.PlasticSCM.Editor.AssetUtils.Processor.AssetModificationProcessor;
  29. namespace Unity.PlasticSCM.Editor
  30. {
  31. /// <summary>
  32. /// The Plastic SCM window.
  33. /// </summary>
  34. public class PlasticWindow : EditorWindow,
  35. PlasticGui.WorkspaceWindow.CheckIncomingChanges.IAutoRefreshIncomingChangesView,
  36. GluonCheckIncomingChanges.IAutoRefreshIncomingChangesView,
  37. CreateWorkspaceView.ICreateWorkspaceListener
  38. {
  39. internal WorkspaceWindow WorkspaceWindowForTesting { get { return mWorkspaceWindow; } }
  40. internal ViewSwitcher ViewSwitcherForTesting { get { return mViewSwitcher; } }
  41. internal CmConnection CmConnectionForTesting { get { return CmConnection.Get(); } }
  42. /// <summary>
  43. /// Open the Plastic SCM window.
  44. /// </summary>
  45. public static void Open()
  46. {
  47. ShowWindow.Plastic();
  48. }
  49. internal void SetupWindowTitle(PlasticNotification.Status status)
  50. {
  51. Texture windowIcon = PlasticNotification.GetIcon(status);
  52. // The titleContent icon does not update unless we also update the title text
  53. // Temporarily doing it by adding space characters
  54. string title = UnityConstants.PLASTIC_WINDOW_TITLE;
  55. title += String.Concat(Enumerable.Repeat(" ", (int)status));
  56. titleContent = new GUIContent(title, windowIcon);
  57. }
  58. internal void DisableCollabIfEnabledWhenLoaded()
  59. {
  60. mDisableCollabIfEnabledWhenLoaded = true;
  61. }
  62. internal void ShowPendingChanges()
  63. {
  64. ((IAssetMenuOperations)mAssetOperations).ShowPendingChanges();
  65. }
  66. internal void Add()
  67. {
  68. ((IAssetMenuOperations)mAssetOperations).Add();
  69. }
  70. internal void Checkout()
  71. {
  72. ((IAssetMenuOperations)mAssetOperations).Checkout();
  73. }
  74. internal void Checkin()
  75. {
  76. ((IAssetMenuOperations)mAssetOperations).Checkin();
  77. }
  78. internal void Undo()
  79. {
  80. ((IAssetMenuOperations)mAssetOperations).Undo();
  81. }
  82. internal void ShowDiff()
  83. {
  84. ((IAssetMenuOperations)mAssetOperations).ShowDiff();
  85. }
  86. internal void ShowHistory()
  87. {
  88. ((IAssetMenuOperations)mAssetOperations).ShowHistory();
  89. }
  90. internal void AddFilesFilterPatterns(
  91. FilterTypes type,
  92. FilterActions action,
  93. FilterOperationType operation)
  94. {
  95. ((IAssetFilesFilterPatternsMenuOperations)mAssetOperations)
  96. .AddFilesFilterPatterns(type, action, operation);
  97. }
  98. void PlasticGui.WorkspaceWindow.CheckIncomingChanges.IAutoRefreshIncomingChangesView.IfVisible()
  99. {
  100. mViewSwitcher.AutoRefreshIncomingChangesView();
  101. }
  102. void GluonCheckIncomingChanges.IAutoRefreshIncomingChangesView.IfVisible()
  103. {
  104. mViewSwitcher.AutoRefreshIncomingChangesView();
  105. }
  106. void CreateWorkspaceView.ICreateWorkspaceListener.OnWorkspaceCreated(
  107. WorkspaceInfo wkInfo, bool isGluonMode)
  108. {
  109. mWkInfo = wkInfo;
  110. mIsGluonMode = isGluonMode;
  111. mWelcomeView = null;
  112. PlasticPlugin.Enable();
  113. if (mIsGluonMode)
  114. ConfigurePartialWorkspace.AsFullyChecked(mWkInfo);
  115. InitializePlastic();
  116. Repaint();
  117. }
  118. internal void RefreshWorkspaceUI()
  119. {
  120. InitializePlastic();
  121. Repaint();
  122. OnFocus();
  123. }
  124. void OnEnable()
  125. {
  126. PlasticPlugin.Enable();
  127. wantsMouseMove = true;
  128. if (mException != null)
  129. return;
  130. minSize = new Vector2(
  131. UnityConstants.PLASTIC_WINDOW_MIN_SIZE_WIDTH,
  132. UnityConstants.PLASTIC_WINDOW_MIN_SIZE_HEIGHT);
  133. SetupWindowTitle(PlasticNotification.Status.None);
  134. RegisterApplicationFocusHandlers(this);
  135. InitializePlastic();
  136. }
  137. void OnDisable()
  138. {
  139. // We need to disable FSWatcher because otherwise it hangs
  140. // when you move the window between monitors with different scale
  141. MonoFileSystemWatcher.IsEnabled = false;
  142. if (mException != null)
  143. return;
  144. if (mWkInfo == null)
  145. {
  146. ClosePlasticWindow(this);
  147. return;
  148. }
  149. mViewSwitcher.OnDisable();
  150. ClosePlasticWindow(this);
  151. }
  152. void OnDestroy()
  153. {
  154. if (mException != null)
  155. return;
  156. if (mWkInfo == null)
  157. return;
  158. if (!mWorkspaceWindow.IsOperationInProgress())
  159. return;
  160. bool bCloseWindow = GuiMessage.ShowQuestion(
  161. PlasticLocalization.GetString(PlasticLocalization.Name.OperationRunning),
  162. PlasticLocalization.GetString(PlasticLocalization.Name.ConfirmClosingRunningOperation),
  163. PlasticLocalization.GetString(PlasticLocalization.Name.YesButton));
  164. if (bCloseWindow)
  165. return;
  166. mForceToOpen = true;
  167. ShowPlasticWindow(this);
  168. }
  169. void OnFocus()
  170. {
  171. if (mException != null)
  172. return;
  173. if (mWkInfo == null)
  174. return;
  175. // We don't want to auto-refresh the views when the window
  176. // is focused due to a right mouse button click because
  177. // if there is no internet connection a dialog appears and
  178. // it prevents being able to open the context menu in order
  179. // to close the Plastic SCM window
  180. if (Mouse.IsRightMouseButtonPressed(Event.current))
  181. return;
  182. mViewSwitcher.AutoRefreshPendingChangesView();
  183. mViewSwitcher.AutoRefreshIncomingChangesView();
  184. }
  185. void OnGUI()
  186. {
  187. if (mException != null)
  188. {
  189. DoExceptionErrorArea();
  190. return;
  191. }
  192. try
  193. {
  194. // IMPORTANT: disable collab (if needed)
  195. // must be executed before the next if statement
  196. // where we check if collab is enabled
  197. if (mDisableCollabIfEnabledWhenLoaded)
  198. {
  199. mDisableCollabIfEnabledWhenLoaded = false;
  200. DisableCollabIfEnabled(ProjectPath.FromApplicationDataPath(
  201. Application.dataPath));
  202. }
  203. if (CollabPlugin.IsEnabled())
  204. {
  205. // execute Close() once after all inspectors update
  206. // to avoid our window to be drawn in back color
  207. EditorApplication.delayCall = Close;
  208. return;
  209. }
  210. bool isPlasticExeAvailable = IsExeAvailable.ForMode(mIsGluonMode);
  211. bool clientNeedsConfiguration = UnityConfigurationChecker.NeedsConfiguration();
  212. var welcomeView = GetWelcomeView();
  213. if (clientNeedsConfiguration && welcomeView.autoLoginState == AutoLogin.State.Off)
  214. {
  215. welcomeView.autoLoginState = AutoLogin.State.Started;
  216. }
  217. if (welcomeView.autoLoginState == AutoLogin.State.OrganizationChoosed)
  218. {
  219. OnEnable();
  220. welcomeView.autoLoginState = AutoLogin.State.InitializingPlastic;
  221. }
  222. if (NeedsToDisplayWelcomeView(clientNeedsConfiguration, mWkInfo))
  223. {
  224. welcomeView.OnGUI(clientNeedsConfiguration);
  225. return;
  226. }
  227. //TODO: Codice - beta: hide the switcher until the update dialog is implemented
  228. //DrawGuiModeSwitcher.ForMode(
  229. // isGluonMode, plasticClient, changesTreeView, editorWindow);
  230. DoTabToolbar(
  231. isPlasticExeAvailable,
  232. mWkInfo,
  233. mViewSwitcher,
  234. mIsGluonMode);
  235. mViewSwitcher.TabViewGUI();
  236. if (mWorkspaceWindow.IsOperationInProgress())
  237. DrawProgressForOperations.For(
  238. mWorkspaceWindow, mWorkspaceWindow.Progress,
  239. position.width);
  240. mStatusBar.OnGUI(
  241. mWkInfo,
  242. mWorkspaceWindow,
  243. mViewSwitcher,
  244. mViewSwitcher,
  245. mIncomingChangesNotifier,
  246. mIsGluonMode);
  247. }
  248. catch (Exception ex)
  249. {
  250. if (IsExitGUIException(ex))
  251. throw;
  252. GUI.enabled = true;
  253. if (IsIMGUIPaintException(ex))
  254. {
  255. ExceptionsHandler.LogException("PlasticWindow", ex);
  256. return;
  257. }
  258. mException = ex;
  259. DoExceptionErrorArea();
  260. ExceptionsHandler.HandleException("OnGUI", ex);
  261. }
  262. }
  263. void Update()
  264. {
  265. if (mException != null)
  266. return;
  267. if (mWkInfo == null)
  268. return;
  269. try
  270. {
  271. double currentUpdateTime = EditorApplication.timeSinceStartup;
  272. double elapsedSeconds = currentUpdateTime - mLastUpdateTime;
  273. mViewSwitcher.Update();
  274. mWorkspaceWindow.OnParentUpdated(elapsedSeconds);
  275. if (mWelcomeView != null)
  276. mWelcomeView.Update();
  277. mLastUpdateTime = currentUpdateTime;
  278. }
  279. catch (Exception ex)
  280. {
  281. mException = ex;
  282. ExceptionsHandler.HandleException("Update", ex);
  283. }
  284. }
  285. internal void InitializePlastic()
  286. {
  287. if (mForceToOpen)
  288. {
  289. mForceToOpen = false;
  290. return;
  291. }
  292. try
  293. {
  294. if (UnityConfigurationChecker.NeedsConfiguration())
  295. return;
  296. mWkInfo = FindWorkspace.InfoForApplicationPath(
  297. Application.dataPath, PlasticApp.PlasticAPI);
  298. if (mWkInfo == null)
  299. return;
  300. PlasticPlugin.EnableForWorkspace();
  301. SetupCloudProjectIdIfNeeded(mWkInfo, PlasticApp.PlasticAPI);
  302. DisableVCSIfEnabled(mWkInfo.ClientPath);
  303. mIsGluonMode = PlasticApp.PlasticAPI.IsGluonWorkspace(mWkInfo);
  304. InitializeNewIncomingChanges(mWkInfo, mIsGluonMode);
  305. ViewHost viewHost = new ViewHost();
  306. mViewSwitcher = new ViewSwitcher(
  307. mWkInfo,
  308. viewHost,
  309. mIsGluonMode,
  310. mDeveloperNewIncomingChangesUpdater,
  311. mGluonNewIncomingChangesUpdater,
  312. mIncomingChangesNotifier,
  313. PlasticPlugin.AssetStatusCache,
  314. PlasticPlugin.WorkspaceOperationsMonitor,
  315. mStatusBar,
  316. this);
  317. mCooldownAutoRefreshPendingChangesAction = new CooldownWindowDelayer(
  318. mViewSwitcher.AutoRefreshPendingChangesView,
  319. UnityConstants.AUTO_REFRESH_PENDING_CHANGES_DELAYED_INTERVAL);
  320. mWorkspaceWindow = new WorkspaceWindow(
  321. mWkInfo,
  322. viewHost,
  323. mViewSwitcher,
  324. mViewSwitcher,
  325. mDeveloperNewIncomingChangesUpdater,
  326. this);
  327. mViewSwitcher.SetWorkspaceWindow(mWorkspaceWindow);
  328. mViewSwitcher.ShowInitialView();
  329. PlasticPlugin.WorkspaceOperationsMonitor.RegisterWindow(
  330. mWorkspaceWindow,
  331. viewHost,
  332. mDeveloperNewIncomingChangesUpdater);
  333. UnityStyles.Initialize(Repaint);
  334. AssetOperations.IAssetSelection projectViewAssetSelection =
  335. new ProjectViewAssetSelection();
  336. mAssetOperations =
  337. new AssetOperations(
  338. mWkInfo,
  339. mWorkspaceWindow,
  340. mViewSwitcher,
  341. mViewSwitcher,
  342. viewHost,
  343. mDeveloperNewIncomingChangesUpdater,
  344. PlasticPlugin.AssetStatusCache,
  345. mViewSwitcher,
  346. mViewSwitcher,
  347. this,
  348. projectViewAssetSelection,
  349. mIsGluonMode);
  350. DrawInspectorOperations.BuildOperations(
  351. mWkInfo,
  352. mWorkspaceWindow,
  353. mViewSwitcher,
  354. mViewSwitcher,
  355. viewHost,
  356. mDeveloperNewIncomingChangesUpdater,
  357. mViewSwitcher,
  358. mViewSwitcher,
  359. this,
  360. mIsGluonMode);
  361. mLastUpdateTime = EditorApplication.timeSinceStartup;
  362. mViewSwitcher.ShowBranchesViewIfNeeded();
  363. }
  364. catch (Exception ex)
  365. {
  366. mException = ex;
  367. ExceptionsHandler.HandleException("InitializePlastic", ex);
  368. }
  369. }
  370. void InitializeNewIncomingChanges(
  371. WorkspaceInfo wkInfo,
  372. bool bIsGluonMode)
  373. {
  374. if (bIsGluonMode)
  375. {
  376. Gluon.IncomingChangesNotifier gluonNotifier =
  377. new Gluon.IncomingChangesNotifier(this);
  378. mGluonNewIncomingChangesUpdater =
  379. NewIncomingChanges.BuildUpdaterForGluon(
  380. wkInfo,
  381. this,
  382. gluonNotifier,
  383. new GluonCheckIncomingChanges.CalculateIncomingChanges());
  384. mIncomingChangesNotifier = gluonNotifier;
  385. return;
  386. }
  387. Developer.IncomingChangesNotifier developerNotifier =
  388. new Developer.IncomingChangesNotifier(this);
  389. mDeveloperNewIncomingChangesUpdater =
  390. NewIncomingChanges.BuildUpdaterForDeveloper(
  391. wkInfo, this, developerNotifier);
  392. mIncomingChangesNotifier = developerNotifier;
  393. }
  394. void OnApplicationActivated()
  395. {
  396. if (mException != null)
  397. return;
  398. Reload.IfWorkspaceConfigChanged(
  399. PlasticApp.PlasticAPI, mWkInfo, mIsGluonMode,
  400. ExecuteFullReload);
  401. if (mWkInfo == null)
  402. return;
  403. ((IWorkspaceWindow)mWorkspaceWindow).UpdateTitle();
  404. NewIncomingChanges.LaunchUpdater(
  405. mDeveloperNewIncomingChangesUpdater,
  406. mGluonNewIncomingChangesUpdater);
  407. // When Unity Editor window is activated it writes some files to its Temp folder.
  408. // This causes the fswatcher to process those events.
  409. // We need to wait until the fswatcher finishes processing the events,
  410. // otherwise the NewChangesInWk method will return TRUE, causing
  411. // the pending changes view to unwanted auto-refresh.
  412. // So, we need to delay the auto-refresh call in order
  413. // to give the fswatcher enough time to process the events.
  414. // Note that the OnFocus event is not affected by this issue.
  415. mCooldownAutoRefreshPendingChangesAction.Ping();
  416. mViewSwitcher.AutoRefreshIncomingChangesView();
  417. }
  418. void OnApplicationDeactivated()
  419. {
  420. if (mException != null)
  421. return;
  422. if (mWkInfo == null)
  423. return;
  424. NewIncomingChanges.StopUpdater(
  425. mDeveloperNewIncomingChangesUpdater,
  426. mGluonNewIncomingChangesUpdater);
  427. }
  428. void ExecuteFullReload()
  429. {
  430. mException = null;
  431. DisposeNewIncomingChanges(this);
  432. InitializePlastic();
  433. }
  434. void DoExceptionErrorArea()
  435. {
  436. string labelText = PlasticLocalization.GetString(
  437. PlasticLocalization.Name.UnexpectedError);
  438. string buttonText = PlasticLocalization.GetString(
  439. PlasticLocalization.Name.ReloadButton);
  440. DrawActionHelpBox.For(
  441. Images.GetErrorDialogIcon(), labelText, buttonText,
  442. ExecuteFullReload);
  443. }
  444. internal WelcomeView GetWelcomeView()
  445. {
  446. if (mWelcomeView != null)
  447. return mWelcomeView;
  448. mWelcomeView = new WelcomeView(
  449. this,
  450. this,
  451. PlasticApp.PlasticAPI,
  452. CmConnection.Get(),
  453. PlasticApp.PlasticWebRestApi);
  454. return mWelcomeView;
  455. }
  456. static void DoSearchField(ViewSwitcher viewSwitcher)
  457. {
  458. if (viewSwitcher.IsViewSelected(ViewSwitcher.SelectedTab.PendingChanges))
  459. {
  460. viewSwitcher.PendingChangesTab.DrawSearchFieldForPendingChangesTab();
  461. return;
  462. }
  463. if (viewSwitcher.IsViewSelected(ViewSwitcher.SelectedTab.Changesets))
  464. {
  465. viewSwitcher.ChangesetsTab.DrawSearchFieldForChangesetsTab();
  466. return;
  467. }
  468. if (viewSwitcher.IsViewSelected(ViewSwitcher.SelectedTab.History))
  469. {
  470. viewSwitcher.HistoryTab.DrawSearchFieldForHistoryTab();
  471. return;
  472. }
  473. if (viewSwitcher.IsViewSelected(ViewSwitcher.SelectedTab.Branches))
  474. {
  475. viewSwitcher.BranchesTab.DrawSearchFieldForBranchesTab();
  476. return;
  477. }
  478. }
  479. static void DoTabToolbar(
  480. bool isPlasticExeAvailable,
  481. WorkspaceInfo workspaceInfo,
  482. ViewSwitcher viewSwitcher,
  483. bool isGluonMode)
  484. {
  485. EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
  486. viewSwitcher.TabButtonsGUI();
  487. GUILayout.FlexibleSpace();
  488. DoSearchField(viewSwitcher);
  489. DoLaunchButtons(
  490. isPlasticExeAvailable,
  491. workspaceInfo,
  492. viewSwitcher,
  493. isGluonMode);
  494. EditorGUILayout.EndHorizontal();
  495. }
  496. static void OpenBranchListViewAndSendEvent(
  497. WorkspaceInfo wkInfo,
  498. ViewSwitcher viewSwitcher)
  499. {
  500. viewSwitcher.ShowBranchesView();
  501. TrackFeatureUseEvent.For(
  502. PlasticGui.Plastic.API.GetRepositorySpec(wkInfo),
  503. TrackFeatureUseEvent.Features.OpenBranchesView);
  504. }
  505. static void ShowBranchesContextMenu(
  506. WorkspaceInfo wkInfo,
  507. ViewSwitcher viewSwitcher,
  508. bool isGluonMode)
  509. {
  510. GenericMenu menu = new GenericMenu();
  511. string branchesListView = PlasticLocalization.GetString(
  512. PlasticLocalization.Name.Branches);
  513. menu.AddItem(
  514. new GUIContent(branchesListView),
  515. false,
  516. () => OpenBranchListViewAndSendEvent(wkInfo, viewSwitcher));
  517. string branchExplorer = PlasticLocalization.GetString(
  518. PlasticLocalization.Name.BranchExplorerMenu);
  519. menu.AddItem(
  520. new GUIContent(branchExplorer),
  521. false,
  522. () => LaunchTool.OpenBranchExplorer(wkInfo, isGluonMode));
  523. menu.ShowAsContext();
  524. }
  525. static void ShowSettingsContextMenu(
  526. WorkspaceInfo wkInfo,
  527. bool isGluonMode)
  528. {
  529. GenericMenu menu = new GenericMenu();
  530. string openToolText = isGluonMode ?
  531. PlasticLocalization.GetString(PlasticLocalization.Name.LaunchGluonButton) :
  532. PlasticLocalization.GetString(PlasticLocalization.Name.LaunchPlasticButton);
  533. menu.AddItem(
  534. new GUIContent(openToolText),
  535. false,
  536. () => LaunchTool.OpenGUIForMode(wkInfo, isGluonMode));
  537. if (EditionToken.IsCloudEdition())
  538. {
  539. menu.AddItem(new GUIContent(
  540. PlasticLocalization.GetString(
  541. PlasticLocalization.Name.InviteMembers)),
  542. false,
  543. InviteMemberButton_clicked,
  544. PlasticGui.Plastic.API.GetRepositorySpec(wkInfo));
  545. }
  546. menu.AddSeparator("");
  547. menu.AddItem(
  548. new GUIContent(
  549. PlasticLocalization.GetString(
  550. PlasticLocalization.Name.Options)),
  551. false,
  552. () => SettingsService.OpenProjectSettings(UnityConstants.PROJECT_SETTINGS_MENU_TITLE));
  553. // If the user has the simplified UI key of type .txt in the Assets folder
  554. // TODO: Remove when Simplified UI is complete
  555. if (AssetDatabase.FindAssets("simplifieduikey t:textasset", new[] { "Assets" }).Any())
  556. menu.AddItem(new GUIContent("Try Simplified UI"),
  557. false,
  558. TrySimplifiedUIButton_Clicked,
  559. null);
  560. //TODO: Localization
  561. menu.AddItem(
  562. new GUIContent(PlasticAssetModificationProcessor.ForceCheckout ?
  563. PlasticLocalization.GetString(PlasticLocalization.Name.DisableForcedCheckout) :
  564. PlasticLocalization.GetString(PlasticLocalization.Name.EnableForcedCheckout)),
  565. false,
  566. ForceCheckout_Clicked,
  567. null);
  568. menu.AddSeparator("");
  569. menu.AddItem(
  570. new GUIContent(
  571. PlasticLocalization.GetString(
  572. PlasticLocalization.Name.TurnOffPlasticSCM)),
  573. false,
  574. TurnOffPlasticButton_Clicked,
  575. null);
  576. menu.ShowAsContext();
  577. }
  578. static void DoLaunchButtons(
  579. bool isPlasticExeAvailable,
  580. WorkspaceInfo wkInfo,
  581. ViewSwitcher viewSwitcher,
  582. bool isGluonMode)
  583. {
  584. //TODO: Codice - beta: hide the diff button until the behavior is implemented
  585. /*GUILayout.Button(PlasticLocalization.GetString(
  586. PlasticLocalization.Name.DiffWindowMenuItemDiff),
  587. EditorStyles.toolbarButton,
  588. GUILayout.Width(UnityConstants.REGULAR_BUTTON_WIDTH));*/
  589. if (viewSwitcher.IsViewSelected(ViewSwitcher.SelectedTab.Changesets))
  590. {
  591. viewSwitcher.ChangesetsTab.DrawDateFilter();
  592. }
  593. if (viewSwitcher.IsViewSelected(ViewSwitcher.SelectedTab.Branches))
  594. {
  595. viewSwitcher.BranchesTab.DrawDateFilter();
  596. }
  597. Texture refreshIcon = Images.GetRefreshIcon();
  598. string refreshIconTooltip = PlasticLocalization.GetString(
  599. PlasticLocalization.Name.RefreshButton);
  600. if (DrawLaunchButton(refreshIcon, refreshIconTooltip))
  601. {
  602. viewSwitcher.RefreshSelectedView();
  603. }
  604. if (viewSwitcher.IsViewSelected(ViewSwitcher.SelectedTab.PendingChanges))
  605. {
  606. Texture2D icon = Images.GetUndoIcon();
  607. string tooltip = PlasticLocalization.GetString(
  608. PlasticLocalization.Name.UndoSelectedChanges);
  609. if (DrawLaunchButton(icon, tooltip))
  610. {
  611. TrackFeatureUseEvent.For(
  612. PlasticGui.Plastic.API.GetRepositorySpec(wkInfo),
  613. TrackFeatureUseEvent.Features.UndoIconButton);
  614. viewSwitcher.PendingChangesTab.UndoForMode(wkInfo, isGluonMode);
  615. }
  616. }
  617. if (isGluonMode)
  618. {
  619. string label = PlasticLocalization.GetString(PlasticLocalization.Name.ConfigureGluon);
  620. if (DrawActionButton.For(label))
  621. LaunchTool.OpenWorkspaceConfiguration(wkInfo, isGluonMode);
  622. }
  623. else
  624. {
  625. Texture2D icon = Images.GetBranchIcon();
  626. string tooltip = PlasticLocalization.GetString(PlasticLocalization.Name.Branches);
  627. if (DrawLaunchButton(icon, tooltip))
  628. {
  629. ShowBranchesContextMenu(
  630. wkInfo,
  631. viewSwitcher,
  632. isGluonMode);
  633. }
  634. }
  635. //TODO: Add settings button tooltip localization
  636. if (DrawLaunchButton(Images.GetSettingsIcon(), string.Empty))
  637. {
  638. ShowSettingsContextMenu(
  639. wkInfo,
  640. isGluonMode);
  641. }
  642. }
  643. static bool DrawLaunchButton(Texture icon, string tooltip)
  644. {
  645. return GUILayout.Button(
  646. new GUIContent(icon, tooltip),
  647. EditorStyles.toolbarButton,
  648. GUILayout.Width(26));
  649. }
  650. static void InviteMemberButton_clicked(object obj)
  651. {
  652. RepositorySpec repSpec = (RepositorySpec)obj;
  653. string organizationName = ServerOrganizationParser.
  654. GetOrganizationFromServer(repSpec.Server);
  655. CurrentUserAdminCheckResponse response = null;
  656. IThreadWaiter waiter = ThreadWaiter.GetWaiter(50);
  657. waiter.Execute(
  658. /*threadOperationDelegate*/
  659. delegate
  660. {
  661. ServerProfile serverProfile = CmConnection.Get().
  662. GetProfileManager().GetProfileForServer(repSpec.Server);
  663. string authToken = serverProfile != null ?
  664. CmConnection.Get().
  665. BuildWebApiTokenForCloudEditionForUser(
  666. serverProfile.Server,
  667. serverProfile.GetSEIDWorkingMode(),
  668. serverProfile.SecurityConfig):
  669. CmConnection.Get().
  670. BuildWebApiTokenForCloudEditionForUser(
  671. repSpec.Server,
  672. ClientConfig.Get().GetSEIDWorkingMode(),
  673. ClientConfig.Get().GetSecurityConfig());
  674. if (string.IsNullOrEmpty(authToken))
  675. authToken = CmConnection.Get().
  676. BuildWebApiTokenForCloudEditionDefaultUser();
  677. if (string.IsNullOrEmpty(authToken))
  678. {
  679. return;
  680. }
  681. response = WebRestApiClient.PlasticScm.IsUserAdmin(
  682. organizationName,
  683. authToken);
  684. },
  685. /*afterOperationDelegate*/
  686. delegate
  687. {
  688. if (waiter.Exception != null)
  689. {
  690. ExceptionsHandler.LogException(
  691. "IsUserAdmin",
  692. waiter.Exception);
  693. OpenCloudDashboardUsersGroupsUrl(organizationName);
  694. return;
  695. }
  696. if (response == null)
  697. {
  698. mLog.DebugFormat(
  699. "Error checking if the user is the organization admin for {0}",
  700. organizationName);
  701. OpenCloudDashboardUsersGroupsUrl(organizationName);
  702. return;
  703. }
  704. if (response.Error != null)
  705. {
  706. mLog.DebugFormat(
  707. "Error checking if the user is the organization admin: {0}",
  708. string.Format("Unable to get IsUserAdminResponse: {0} [code {1}]",
  709. response.Error.Message,
  710. response.Error.ErrorCode));
  711. OpenCloudDashboardUsersGroupsUrl(organizationName);
  712. return;
  713. }
  714. if (response.IsCurrentUserAdmin)
  715. {
  716. OpenCloudDashboardUsersGroupsUrl(response.OrganizationName);
  717. return;
  718. }
  719. GuiMessage.ShowInformation(
  720. PlasticLocalization.GetString(PlasticLocalization.Name.InviteMembersTitle),
  721. PlasticLocalization.GetString(PlasticLocalization.Name.InviteMembersMessage));
  722. });
  723. }
  724. static void OpenCloudDashboardUsersGroupsUrl(string organization)
  725. {
  726. Application.OpenURL("https://www.plasticscm.com/dashboard/cloud/" +
  727. organization +
  728. "/users-and-groups");
  729. }
  730. static void TrySimplifiedUIButton_Clicked(object obj)
  731. {
  732. PlasticSCMWindow.ShowWindow();
  733. }
  734. static void TurnOffPlasticButton_Clicked(object obj)
  735. {
  736. ShowWindow.Plastic();
  737. TurnOffPlasticWindow.ShowWindow();
  738. PlasticPlugin.Disable();
  739. }
  740. static void ForceCheckout_Clicked(object obj)
  741. {
  742. PlasticAssetModificationProcessor.SetForceCheckoutOption(
  743. !PlasticAssetModificationProcessor.ForceCheckout);
  744. }
  745. static void SetupCloudProjectIdIfNeeded(
  746. WorkspaceInfo wkInfo,
  747. IPlasticAPI plasticApi)
  748. {
  749. if (SetupCloudProjectId.HasCloudProjectId())
  750. return;
  751. SetupCloudProjectId.ForWorkspace(wkInfo, plasticApi);
  752. mLog.DebugFormat("Setup CloudProjectId on Project: {0}",
  753. wkInfo.ClientPath);
  754. }
  755. static void DisableVCSIfEnabled(string projectPath)
  756. {
  757. if (!VCSPlugin.IsEnabled())
  758. return;
  759. VCSPlugin.Disable();
  760. mLog.DebugFormat("Disabled VCS Plugin on Project: {0}",
  761. projectPath);
  762. }
  763. static void DisposeNewIncomingChanges(PlasticWindow window)
  764. {
  765. NewIncomingChanges.DisposeUpdater(
  766. window.mDeveloperNewIncomingChangesUpdater,
  767. window.mGluonNewIncomingChangesUpdater);
  768. window.mDeveloperNewIncomingChangesUpdater = null;
  769. window.mGluonNewIncomingChangesUpdater = null;
  770. }
  771. static void RegisterApplicationFocusHandlers(PlasticWindow window)
  772. {
  773. EditorWindowFocus.OnApplicationActivated += window.OnApplicationActivated;
  774. EditorWindowFocus.OnApplicationDeactivated += window.OnApplicationDeactivated;
  775. }
  776. static void UnRegisterApplicationFocusHandlers(PlasticWindow window)
  777. {
  778. EditorWindowFocus.OnApplicationActivated -= window.OnApplicationActivated;
  779. EditorWindowFocus.OnApplicationDeactivated -= window.OnApplicationDeactivated;
  780. }
  781. static bool IsExitGUIException(Exception ex)
  782. {
  783. return ex is ExitGUIException;
  784. }
  785. static bool IsIMGUIPaintException(Exception ex)
  786. {
  787. if (!(ex is ArgumentException))
  788. return false;
  789. return ex.Message.StartsWith("Getting control") &&
  790. ex.Message.Contains("controls when doing repaint");
  791. }
  792. static void ClosePlasticWindow(PlasticWindow window)
  793. {
  794. UnRegisterApplicationFocusHandlers(window);
  795. PlasticPlugin.WorkspaceOperationsMonitor.UnRegisterWindow();
  796. DisposeNewIncomingChanges(window);
  797. AvatarImages.Dispose();
  798. }
  799. static void ShowPlasticWindow(PlasticWindow window)
  800. {
  801. EditorWindow dockWindow = FindEditorWindow.ToDock<PlasticWindow>();
  802. PlasticWindow newPlasticWindow = InstantiateFrom(window);
  803. if (DockEditorWindow.IsAvailable())
  804. DockEditorWindow.To(dockWindow, newPlasticWindow);
  805. newPlasticWindow.Show();
  806. newPlasticWindow.Focus();
  807. }
  808. static bool NeedsToDisplayWelcomeView(
  809. bool clientNeedsConfiguration,
  810. WorkspaceInfo wkInfo)
  811. {
  812. if (clientNeedsConfiguration)
  813. return true;
  814. if (wkInfo == null)
  815. return true;
  816. return false;
  817. }
  818. static void DisableCollabIfEnabled(string projectPath)
  819. {
  820. if (!CollabPlugin.IsEnabled())
  821. return;
  822. CollabPlugin.Disable();
  823. mLog.DebugFormat("Disabled Collab Plugin on Project: {0}",
  824. projectPath);
  825. }
  826. static PlasticWindow InstantiateFrom(PlasticWindow window)
  827. {
  828. PlasticWindow result = Instantiate(window);
  829. result.mWkInfo = window.mWkInfo;
  830. result.mWorkspaceWindow = window.mWorkspaceWindow;
  831. result.mViewSwitcher = window.mViewSwitcher;
  832. result.mCooldownAutoRefreshPendingChangesAction = window.mCooldownAutoRefreshPendingChangesAction;
  833. result.mDeveloperNewIncomingChangesUpdater = window.mDeveloperNewIncomingChangesUpdater;
  834. result.mGluonNewIncomingChangesUpdater = window.mGluonNewIncomingChangesUpdater;
  835. result.mException = window.mException;
  836. result.mLastUpdateTime = window.mLastUpdateTime;
  837. result.mIsGluonMode = window.mIsGluonMode;
  838. result.mIncomingChangesNotifier = window.mIncomingChangesNotifier;
  839. result.mStatusBar = window.mStatusBar;
  840. result.mWelcomeView = window.mWelcomeView;
  841. return result;
  842. }
  843. internal PlasticProjectSettingsProvider.IAutoRefreshView GetPendingChangesView()
  844. {
  845. return mViewSwitcher != null ? mViewSwitcher.PendingChangesTab : null;
  846. }
  847. static class Reload
  848. {
  849. internal static void IfWorkspaceConfigChanged(
  850. IPlasticAPI plasticApi,
  851. WorkspaceInfo lastWkInfo,
  852. bool lastIsGluonMode,
  853. Action reloadAction)
  854. {
  855. string applicationPath = Application.dataPath;
  856. bool isGluonMode = false;
  857. WorkspaceInfo wkInfo = null;
  858. IThreadWaiter waiter = ThreadWaiter.GetWaiter(10);
  859. waiter.Execute(
  860. /*threadOperationDelegate*/ delegate
  861. {
  862. wkInfo = FindWorkspace.
  863. InfoForApplicationPath(applicationPath, plasticApi);
  864. if (wkInfo != null)
  865. isGluonMode = plasticApi.IsGluonWorkspace(wkInfo);
  866. },
  867. /*afterOperationDelegate*/ delegate
  868. {
  869. if (waiter.Exception != null)
  870. return;
  871. if (!IsWorkspaceConfigChanged(
  872. lastWkInfo, wkInfo,
  873. lastIsGluonMode, isGluonMode))
  874. return;
  875. reloadAction();
  876. });
  877. }
  878. static bool IsWorkspaceConfigChanged(
  879. WorkspaceInfo lastWkInfo,
  880. WorkspaceInfo currentWkInfo,
  881. bool lastIsGluonMode,
  882. bool currentIsGluonMode)
  883. {
  884. if (lastIsGluonMode != currentIsGluonMode)
  885. return true;
  886. if (lastWkInfo == null || currentWkInfo == null)
  887. return true;
  888. return !lastWkInfo.Equals(currentWkInfo);
  889. }
  890. }
  891. [SerializeField]
  892. bool mForceToOpen;
  893. [NonSerialized]
  894. WorkspaceInfo mWkInfo;
  895. Exception mException;
  896. internal IIncomingChangesNotifier mIncomingChangesNotifier;
  897. double mLastUpdateTime = 0f;
  898. CooldownWindowDelayer mCooldownAutoRefreshPendingChangesAction;
  899. internal ViewSwitcher mViewSwitcher;
  900. WelcomeView mWelcomeView;
  901. StatusBar mStatusBar = new StatusBar();
  902. PlasticGui.WorkspaceWindow.NewIncomingChangesUpdater mDeveloperNewIncomingChangesUpdater;
  903. GluonNewIncomingChangesUpdater mGluonNewIncomingChangesUpdater;
  904. WorkspaceWindow mWorkspaceWindow;
  905. bool mIsGluonMode;
  906. bool mDisableCollabIfEnabledWhenLoaded;
  907. AssetOperations mAssetOperations;
  908. static readonly ILog mLog = LogManager.GetLogger("PlasticWindow");
  909. }
  910. }