Files
Shrlalgo.RvKits/Melskin/Controls/Toast/ToastManager.cs
2026-02-17 22:17:13 +08:00

43 lines
1.6 KiB
C#

using System.Windows.Media.Animation;
using System.Windows.Threading;
namespace Melskin.Controls;
#region Internal Implementation
internal class ToastManager(IToastHost host) : IToastService
{
public void Show(string message, ToastType type, TimeSpan? duration = null)
{
Application.Current.Dispatcher.Invoke(() =>
{
var toast = new ToastControl { Message = message, Type = type };
var fadeInAnim = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(350));
var timer = new DispatcherTimer { Interval = duration ?? TimeSpan.FromSeconds(4) };
timer.Tick += (_, _) =>
{
timer.Stop();
var fadeOutAnim = new DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(350));
fadeOutAnim.Completed += (_, _) => host.RemoveToast(toast);
toast.BeginAnimation(UIElement.OpacityProperty, fadeOutAnim);
};
toast.Loaded += (_, _) =>
{
toast.BeginAnimation(UIElement.OpacityProperty, fadeInAnim);
timer.Start();
};
host.AddToast(toast);
});
}
public void ShowSuccess(string message, TimeSpan? duration = null) => Show(message, ToastType.Success, duration);
public void ShowError(string message, TimeSpan? duration = null) => Show(message, ToastType.Error, duration);
public void ShowWarning(string message, TimeSpan? duration = null) => Show(message, ToastType.Warning, duration);
public void ShowInfo(string message, TimeSpan? duration = null) => Show(message, ToastType.Info, duration);
}
#endregion