Files
stas-barecky/Library/PackageCache/com.unity.collab-proxy@3351acaba9c9/Editor/UI/Progress/ProgressControlsForViews.cs
2026-01-08 20:43:08 +05:00

174 lines
4.7 KiB
C#

using UnityEditor;
using PlasticGui;
namespace Unity.PlasticSCM.Editor.UI.Progress
{
internal class ProgressControlsForViews : IProgressControls
{
internal class Data
{
internal bool IsOperationRunning;
internal float ProgressPercent;
internal string ProgressMessage;
internal MessageType NotificationType;
internal string NotificationMessage;
internal void CopyInto(Data other)
{
other.IsOperationRunning = IsOperationRunning;
other.ProgressPercent = ProgressPercent;
other.ProgressMessage = ProgressMessage;
other.NotificationType = NotificationType;
other.NotificationMessage = NotificationMessage;
}
}
internal Data ProgressData { get { return mData; } }
internal bool IsOperationRunning()
{
return mData.IsOperationRunning;
}
internal bool HasNotification()
{
return !string.IsNullOrEmpty(mData.NotificationMessage);
}
internal void UpdateDeterminateProgress(EditorWindow parentWindow)
{
if (IsOperationRunning() || mRequestedRepaint)
{
parentWindow.Repaint();
mRequestedRepaint = false;
}
}
internal void UpdateProgress(EditorWindow parentWindow)
{
if (IsOperationRunning() || mRequestedRepaint)
{
if (IsOperationRunning())
UpdateIndeterminateProgress();
parentWindow.Repaint();
mRequestedRepaint = false;
}
}
internal void ForcedUpdateProgress(EditorWindow parentWindow)
{
double updateTime;
float progressPercent;
GetUpdateProgress(
mLastUpdateTime, mData.ProgressPercent,
out updateTime, out progressPercent);
mLastUpdateTime = updateTime;
if (!IsOperationRunning())
return;
mData.ProgressPercent = progressPercent;
parentWindow.Repaint();
}
void IProgressControls.HideProgress()
{
HideNotification();
mData.IsOperationRunning = false;
mData.ProgressMessage = string.Empty;
mRequestedRepaint = true;
}
void IProgressControls.ShowProgress(string message)
{
HideNotification();
mData.IsOperationRunning = true;
mData.ProgressMessage = message;
mRequestedRepaint = true;
}
void IProgressControls.ShowError(string message)
{
mData.NotificationMessage = message;
mData.NotificationType = MessageType.Error;
mRequestedRepaint = true;
}
void IProgressControls.ShowNotification(string message)
{
mData.NotificationMessage = message;
mData.NotificationType = MessageType.Info;
mRequestedRepaint = true;
}
void IProgressControls.ShowSuccess(string message)
{
mData.NotificationMessage = message;
mData.NotificationType = MessageType.Info;
mRequestedRepaint = true;
}
void IProgressControls.ShowWarning(string message)
{
mData.NotificationMessage = message;
mData.NotificationType = MessageType.Warning;
mRequestedRepaint = true;
}
void HideNotification()
{
mData.NotificationMessage = string.Empty;
mData.NotificationType = MessageType.None;
mRequestedRepaint = true;
}
void UpdateIndeterminateProgress()
{
// NOTE(rafa): there is no support for indeterminate progress bar
// i use this neverending progress bar as workaround
mData.ProgressPercent += .003f;
if (mData.ProgressPercent > 1f)
mData.ProgressPercent = .1f;
}
static void GetUpdateProgress(
double lastUpdateTime, float lastProgressPercent,
out double updateTime, out float progressPercent)
{
updateTime = EditorApplication.timeSinceStartup;
double deltaTime = System.Math.Min(0.1, updateTime - lastUpdateTime);
double deltaPercent = (deltaTime / 0.1) * PERCENT_PER_SECONDS;
progressPercent = UnityEngine.Mathf.Repeat(
lastProgressPercent + (float)deltaPercent, 1f);
}
double mLastUpdateTime = 0.0;
Data mData = new Data();
bool mRequestedRepaint;
const double PERCENT_PER_SECONDS = 0.06;
}
}