Dispenser/DispenserCommon/Utils/MessageBoxHelper.cs

154 lines
5.5 KiB
C#

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;
/// <summary>
/// 用于弹窗提示
/// </summary>
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);
/// <summary>
/// 构建弹窗对象
/// </summary>
/// <param name="title">消息标题,用于显示在弹窗最顶方</param>
/// <param name="msg">消息内容</param>
/// <param name="contentTitle">内容标题,用于显示在内容的顶部</param>
/// <param name="icon">弹窗左上角icon</param>
/// <returns></returns>
private static IMsBox<string> CreateMessageBox(
string title,
string msg,
string contentTitle = "",
string icon = "Assets/info.png")
{
return MessageBoxManager.GetMessageBoxCustom(
new MessageBoxCustomParams
{
ButtonDefinitions = new List<ButtonDefinition>
{
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, "弹窗出现异常");
}
}
/// <summary>
/// 消息级别弹窗
/// </summary>
/// <param name="msg">消息内容</param>
/// <param name="contentTitle">消息标题,默认为空字符串,不显示</param>
/// <param name="title">弹窗标题</param>
/// <param name="filter">是否过滤弹窗</param>
/// <param name="callback"></param>
public static void Info(string msg, string contentTitle = "", string title = "提示", bool filter = true,
Action? callback = null)
{
Show(title, msg, INFO, contentTitle, filter, callback);
}
/// <summary>
/// 告警级别弹窗
/// </summary>
/// <param name="msg">消息内容</param>
/// <param name="contentTitle">消息标题,默认为空字符串,不显示</param>
/// <param name="title">弹窗标题</param>
/// <param name="filter">是否过滤弹窗</param>
/// <param name="callback"></param>
public static void Warning(string msg, string contentTitle = "", string title = "警告", bool filter = true,
Action? callback = null)
{
Show(title, msg, WARNING, contentTitle, filter, callback);
}
/// <summary>
/// 错误级别弹窗
/// </summary>
/// <param name="msg">消息内容</param>
/// <param name="contentTitle">消息标题,默认为空字符串,不显示</param>
/// <param name="title">弹窗标题</param>
/// <param name="filter">是否过滤弹窗</param>
/// <param name="callback"></param>
public static void Error(string msg, string contentTitle = "", string title = "错误", bool filter = true,
Action? callback = null)
{
Show(title, msg, ERROR, contentTitle, filter, callback);
}
/// <summary>
/// 通知级别弹窗
/// </summary>
/// <param name="msg">消息内容</param>
/// <param name="contentTitle">消息标题,默认为空字符串,不显示</param>
/// <param name="title">弹窗标题</param>
/// <param name="filter">是否过滤弹窗</param>
/// <param name="callback"></param>
public static void Notify(string msg, string contentTitle = "", string title = "通知", bool filter = true,
Action? callback = null)
{
Show(title, msg, NOTIFY, contentTitle, filter, callback);
}
/// <summary>
/// 成功通知级别弹窗
/// </summary>
/// <param name="msg">消息内容</param>
/// <param name="contentTitle">消息标题,默认为空字符串,不显示</param>
/// <param name="title">弹窗标题</param>
/// <param name="filter">是否过滤弹窗</param>
/// <param name="callback"></param>
public static void Success(string msg, string contentTitle = "", string title = "成功", bool filter = true,
Action? callback = null)
{
Show(title, msg, SUCCESS, contentTitle, filter, callback);
}
}