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.

CooldownWindowDelayer.cs 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using UnityEditor;
  3. namespace Unity.PlasticSCM.Editor.UI
  4. {
  5. public class CooldownWindowDelayer
  6. {
  7. internal static bool IsUnitTesting { get; set; }
  8. public CooldownWindowDelayer(Action action, double cooldownSeconds)
  9. {
  10. mAction = action;
  11. mCooldownSeconds = cooldownSeconds;
  12. }
  13. public void Ping()
  14. {
  15. if (IsUnitTesting)
  16. {
  17. mAction();
  18. return;
  19. }
  20. if (mIsOnCooldown)
  21. {
  22. RefreshCooldown();
  23. return;
  24. }
  25. StartCooldown();
  26. }
  27. void RefreshCooldown()
  28. {
  29. mIsOnCooldown = true;
  30. mSecondsOnCooldown = mCooldownSeconds;
  31. }
  32. void StartCooldown()
  33. {
  34. mLastUpdateTime = EditorApplication.timeSinceStartup;
  35. EditorApplication.update += OnUpdate;
  36. RefreshCooldown();
  37. }
  38. void EndCooldown()
  39. {
  40. EditorApplication.update -= OnUpdate;
  41. mIsOnCooldown = false;
  42. mAction();
  43. }
  44. void OnUpdate()
  45. {
  46. double updateTime = EditorApplication.timeSinceStartup;
  47. double deltaSeconds = updateTime - mLastUpdateTime;
  48. mSecondsOnCooldown -= deltaSeconds;
  49. if (mSecondsOnCooldown < 0)
  50. EndCooldown();
  51. mLastUpdateTime = updateTime;
  52. }
  53. readonly Action mAction;
  54. readonly double mCooldownSeconds;
  55. double mLastUpdateTime;
  56. bool mIsOnCooldown;
  57. double mSecondsOnCooldown;
  58. }
  59. }