71 lines
1.9 KiB
C#
71 lines
1.9 KiB
C#
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
|
|
{
|
|
public class UXSnackBar<TPanel> : UIToolkitSubPanel<TPanel>,ISnackBar where TPanel :IUXPanel
|
|
{
|
|
[UXBindPath("snack_bar-container")] private VisualElement _snackBarContainer;
|
|
|
|
private VisualTreeAsset _template;
|
|
|
|
private readonly ValidHandle _waitHandle = new ValidHandle();
|
|
|
|
public UXSnackBar(IServiceProvider serviceProvider) : base(serviceProvider)
|
|
{
|
|
}
|
|
|
|
protected override void OnInitiated()
|
|
{
|
|
base.OnInitiated();
|
|
_template = _snackBarContainer.Q<TemplateContainer>().templateSource;
|
|
|
|
_snackBarContainer.Clear();
|
|
|
|
BITAppForUnity.AllowCursor.AddListener(OnAllowCursor);
|
|
}
|
|
|
|
private void OnAllowCursor(bool obj)
|
|
{
|
|
if (obj)
|
|
{
|
|
_waitHandle.AddElement(this);
|
|
}
|
|
else
|
|
{
|
|
_waitHandle.RemoveElement(this);
|
|
}
|
|
}
|
|
|
|
public async void Add(string message, Severity severity = Severity.Normal)
|
|
{
|
|
var ve = _snackBarContainer.Create(_template);
|
|
|
|
ve.AddToClassList($"severity-{severity.ToString().ToLower()}");
|
|
|
|
ve.Get<Label>().text = message;
|
|
|
|
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);
|
|
}
|
|
|
|
await _waitHandle;
|
|
|
|
ve.RemoveFromHierarchy();
|
|
}
|
|
}
|
|
|
|
}
|