Firstborn/Library/PackageCache/com.unity.collab-proxy@1.17.7/Editor/PlasticSCM/UI/CooldownWindowDelayer.cs

80 lines
1.7 KiB
C#
Raw Normal View History

2023-03-28 13:24:16 -04:00
using System;
using UnityEditor;
namespace Unity.PlasticSCM.Editor.UI
{
internal class CooldownWindowDelayer
{
internal static bool IsUnitTesting { get; set; }
internal CooldownWindowDelayer(Action action, double cooldownSeconds)
{
mAction = action;
mCooldownSeconds = cooldownSeconds;
}
internal void Ping()
{
if (IsUnitTesting)
{
mAction();
return;
}
if (mIsOnCooldown)
{
RefreshCooldown();
return;
}
StartCooldown();
}
void RefreshCooldown()
{
mIsOnCooldown = true;
mSecondsOnCooldown = mCooldownSeconds;
}
void StartCooldown()
{
mLastUpdateTime = EditorApplication.timeSinceStartup;
EditorApplication.update += OnUpdate;
RefreshCooldown();
}
void EndCooldown()
{
EditorApplication.update -= OnUpdate;
mIsOnCooldown = false;
mAction();
}
void OnUpdate()
{
double updateTime = EditorApplication.timeSinceStartup;
double deltaSeconds = updateTime - mLastUpdateTime;
mSecondsOnCooldown -= deltaSeconds;
if (mSecondsOnCooldown < 0)
EndCooldown();
mLastUpdateTime = updateTime;
}
readonly Action mAction;
readonly double mCooldownSeconds;
double mLastUpdateTime;
bool mIsOnCooldown;
double mSecondsOnCooldown;
}
}