2025-04-28 15:15:44 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using BITKit;
|
|
|
|
using BITKit.Tween;
|
|
|
|
using BITKit.UX;
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
|
|
namespace Net.BITKit.UX.SnackBar
|
|
|
|
{
|
2025-06-09 15:55:24 +08:00
|
|
|
public static class UXSnackBarStatic
|
|
|
|
{
|
|
|
|
public static int Count;
|
|
|
|
}
|
|
|
|
public class UXSnackBar<TPanel> : UIToolkitSubPanel<TPanel>,IDisposable,ISnackBar where TPanel :IUXPanel
|
2025-04-28 15:15:44 +08:00
|
|
|
{
|
|
|
|
[UXBindPath("snack_bar-container")] private VisualElement _snackBarContainer;
|
|
|
|
|
|
|
|
private VisualTreeAsset _template;
|
|
|
|
|
|
|
|
private readonly ValidHandle _waitHandle = new ValidHandle();
|
2025-06-09 15:55:24 +08:00
|
|
|
|
|
|
|
private readonly Queue<(string message,Severity severity)> _queue=new();
|
2025-04-28 15:15:44 +08:00
|
|
|
|
|
|
|
public UXSnackBar(IServiceProvider serviceProvider) : base(serviceProvider)
|
|
|
|
{
|
2025-06-09 15:55:24 +08:00
|
|
|
if (UXSnackBarStatic.Count > 0)
|
|
|
|
{
|
|
|
|
throw new Exception("SnackBar can only be created once");
|
|
|
|
}
|
|
|
|
UXSnackBarStatic.Count++;
|
2025-04-28 15:15:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnInitiated()
|
|
|
|
{
|
|
|
|
base.OnInitiated();
|
|
|
|
_template = _snackBarContainer.Q<TemplateContainer>().templateSource;
|
|
|
|
|
|
|
|
_snackBarContainer.Clear();
|
|
|
|
|
|
|
|
BITAppForUnity.AllowCursor.AddListener(OnAllowCursor);
|
2025-06-09 15:55:24 +08:00
|
|
|
|
|
|
|
while (_queue.TryDequeue(out var temp ))
|
|
|
|
{
|
|
|
|
Add(temp.message,temp.severity);
|
|
|
|
}
|
2025-04-28 15:15:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnAllowCursor(bool obj)
|
|
|
|
{
|
|
|
|
if (obj)
|
|
|
|
{
|
|
|
|
_waitHandle.AddElement(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_waitHandle.RemoveElement(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-09 15:55:24 +08:00
|
|
|
public void Add(string message, Severity severity = Severity.Normal)
|
2025-04-28 15:15:44 +08:00
|
|
|
{
|
2025-06-09 15:55:24 +08:00
|
|
|
if (_snackBarContainer is null)
|
|
|
|
{
|
|
|
|
_queue.Enqueue(new ValueTuple<string, Severity>(message,severity));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-04-28 15:15:44 +08:00
|
|
|
var ve = _snackBarContainer.Create(_template);
|
|
|
|
|
|
|
|
ve.AddToClassList($"severity-{severity.ToString().ToLower()}");
|
|
|
|
|
|
|
|
ve.Get<Label>().text = message;
|
|
|
|
|
2025-06-09 15:55:24 +08:00
|
|
|
Continue();
|
|
|
|
|
|
|
|
return;
|
|
|
|
async void Continue()
|
2025-04-28 15:15:44 +08:00
|
|
|
{
|
2025-06-09 15:55:24 +08:00
|
|
|
if (ve.Q("VisualElement--1") is { } bar)
|
|
|
|
{
|
|
|
|
await BITween.Lerp(x => bar.style.width = new StyleLength(Length.Percent(x)), 0f, 100, 5, Mathf.Lerp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await UniTask.Delay(5000);
|
|
|
|
}
|
2025-04-28 15:15:44 +08:00
|
|
|
|
2025-06-09 15:55:24 +08:00
|
|
|
await _waitHandle;
|
2025-04-28 15:15:44 +08:00
|
|
|
|
2025-06-09 15:55:24 +08:00
|
|
|
ve.RemoveFromHierarchy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
UXSnackBarStatic.Count--;
|
2025-04-28 15:15:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|