using Avalonia.Controls;
using Avalonia.Threading;
using MsBox.Avalonia;
using MsBox.Avalonia.Base;
using MsBox.Avalonia.Dto;
using MsBox.Avalonia.Models;
using Serilog;
namespace DispenserCommon.Utils;
///
/// 用于弹窗提示
///
public static class MessageBoxHelper
{
private const string SUCCESS = "Assets/success.png";
private const string INFO = "Assets/info.png";
private const string WARNING = "Assets/warning.png";
private const string ERROR = "Assets/error.png";
private const string NOTIFY = "Assets/notify.png";
// 定义 5分钟的滑动时间窗口
private static readonly SlidingWindow SlidingWindow = new(5 * 60 * 1000);
///
/// 构建弹窗对象
///
/// 消息标题,用于显示在弹窗最顶方
/// 消息内容
/// 内容标题,用于显示在内容的顶部
/// 弹窗左上角icon
///
private static IMsBox CreateMessageBox(
string title,
string msg,
string contentTitle = "",
string icon = "Assets/info.png")
{
return MessageBoxManager.GetMessageBoxCustom(
new MessageBoxCustomParams
{
ButtonDefinitions = new List
{
new() { Name = "关闭", IsCancel = true }
},
WindowIcon = new WindowIcon(icon),
ContentTitle = title,
ContentHeader = contentTitle,
ContentMessage = msg,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
CanResize = false,
MinWidth = 300,
MinHeight = 200,
MaxWidth = 800,
MaxHeight = 1000,
SizeToContent = SizeToContent.WidthAndHeight,
ShowInCenter = true,
Topmost = true
});
}
private static async void Show(string title, string msg, string icon, string contentTitle = "", bool filter = true,
Action? callback = null)
{
try
{
// 对弹窗内容进行滤波,减少相同内容的弹窗频次
if (filter && !SlidingWindow.AllowValue(msg)) return;
await Dispatcher.UIThread.InvokeAsync(async () =>
{
var box = CreateMessageBox(title, msg, contentTitle, icon);
await box.ShowWindowAsync();
});
if (callback != null) callback();
}
catch (Exception e)
{
Log.Error(e, "弹窗出现异常");
}
}
///
/// 消息级别弹窗
///
/// 消息内容
/// 消息标题,默认为空字符串,不显示
/// 弹窗标题
/// 是否过滤弹窗
///
public static void Info(string msg, string contentTitle = "", string title = "提示", bool filter = true,
Action? callback = null)
{
Show(title, msg, INFO, contentTitle, filter, callback);
}
///
/// 告警级别弹窗
///
/// 消息内容
/// 消息标题,默认为空字符串,不显示
/// 弹窗标题
/// 是否过滤弹窗
///
public static void Warning(string msg, string contentTitle = "", string title = "警告", bool filter = true,
Action? callback = null)
{
Show(title, msg, WARNING, contentTitle, filter, callback);
}
///
/// 错误级别弹窗
///
/// 消息内容
/// 消息标题,默认为空字符串,不显示
/// 弹窗标题
/// 是否过滤弹窗
///
public static void Error(string msg, string contentTitle = "", string title = "错误", bool filter = true,
Action? callback = null)
{
Show(title, msg, ERROR, contentTitle, filter, callback);
}
///
/// 通知级别弹窗
///
/// 消息内容
/// 消息标题,默认为空字符串,不显示
/// 弹窗标题
/// 是否过滤弹窗
///
public static void Notify(string msg, string contentTitle = "", string title = "通知", bool filter = true,
Action? callback = null)
{
Show(title, msg, NOTIFY, contentTitle, filter, callback);
}
///
/// 成功通知级别弹窗
///
/// 消息内容
/// 消息标题,默认为空字符串,不显示
/// 弹窗标题
/// 是否过滤弹窗
///
public static void Success(string msg, string contentTitle = "", string title = "成功", bool filter = true,
Action? callback = null)
{
Show(title, msg, SUCCESS, contentTitle, filter, callback);
}
}