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

PlasticWindow.cs 42KB

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