first
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
using Common.WindowsDesktop.Consts;
|
||||
using Common.WindowsDesktop.Enums;
|
||||
using FASS.Simulator.Models.Message;
|
||||
using Wpf.Ui;
|
||||
using Wpf.Ui.Controls;
|
||||
|
||||
namespace FASS.Simulator.Utility
|
||||
{
|
||||
public static class Message
|
||||
{
|
||||
private const string TimestampFormat = "yyyy-MM-dd HH:mm:ss.fff";
|
||||
|
||||
private static readonly SolidColorBrush InfoBrush = CreateBrush(AppConst.Color.Info);
|
||||
private static readonly SolidColorBrush SuccessBrush = CreateBrush(AppConst.Color.Success);
|
||||
private static readonly SolidColorBrush WarningBrush = CreateBrush(AppConst.Color.Warning);
|
||||
private static readonly SolidColorBrush ErrorBrush = CreateBrush(AppConst.Color.Error);
|
||||
private static readonly SolidColorBrush TextBrush = CreateBrush(AppConst.Color.Text);
|
||||
|
||||
public static void ShowSnackbar(string title, string message, ControlAppearance appearance, SymbolRegular symbol = SymbolRegular.Fluent24, int timeout = 2)
|
||||
{
|
||||
App.Current.GetRequiredService<ISnackbarService>()
|
||||
.Show(message, title, appearance, new SymbolIcon(symbol), TimeSpan.FromSeconds(timeout));
|
||||
}
|
||||
|
||||
public static void ShowSnackbarInfo(string message, SymbolRegular symbol = SymbolRegular.Info24, int timeout = 2) =>
|
||||
ShowSnackbar(message, "信息", ControlAppearance.Info, symbol, timeout);
|
||||
|
||||
public static void ShowSnackbarSuccess(string message, SymbolRegular symbol = SymbolRegular.CheckmarkCircle24, int timeout = 2) =>
|
||||
ShowSnackbar(message, "成功", ControlAppearance.Success, symbol, timeout);
|
||||
|
||||
public static void ShowSnackbarWarning(string message, SymbolRegular symbol = SymbolRegular.Warning24, int timeout = 2) =>
|
||||
ShowSnackbar(message, "警告", ControlAppearance.Caution, symbol, timeout);
|
||||
|
||||
public static void ShowSnackbarError(string message, SymbolRegular symbol = SymbolRegular.DismissCircle24, int timeout = 2) =>
|
||||
ShowSnackbar(message, "错误", ControlAppearance.Danger, symbol, timeout);
|
||||
|
||||
public static Task<Wpf.Ui.Controls.MessageBoxResult> ShowMessage(string message, string title)
|
||||
{
|
||||
var messageBox = new Wpf.Ui.Controls.MessageBox
|
||||
{
|
||||
Title = title,
|
||||
Content = message,
|
||||
CloseButtonText = "关闭"
|
||||
};
|
||||
return messageBox.ShowDialogAsync();
|
||||
}
|
||||
|
||||
public static Task<Wpf.Ui.Controls.MessageBoxResult> ShowMessageInfo(string message) => ShowMessage(message, "信息");
|
||||
|
||||
public static Task<Wpf.Ui.Controls.MessageBoxResult> ShowMessageSuccess(string message) => ShowMessage(message, "成功");
|
||||
|
||||
public static Task<Wpf.Ui.Controls.MessageBoxResult> ShowMessageWarning(string message) => ShowMessage(message, "警告");
|
||||
|
||||
public static Task<Wpf.Ui.Controls.MessageBoxResult> ShowMessageError(string message) => ShowMessage(message, "错误");
|
||||
|
||||
public static async Task<Wpf.Ui.Controls.MessageBoxResult> ShowMessageConfirm(string message, Action? ok = null, Action? cancel = null)
|
||||
{
|
||||
var messageBox = new Wpf.Ui.Controls.MessageBox
|
||||
{
|
||||
Content = message,
|
||||
Title = "确认",
|
||||
IsPrimaryButtonEnabled = true,
|
||||
PrimaryButtonText = "确定",
|
||||
CloseButtonText = "取消"
|
||||
};
|
||||
var result = await messageBox.ShowDialogAsync();
|
||||
if (result == Wpf.Ui.Controls.MessageBoxResult.Primary)
|
||||
{
|
||||
ok?.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
cancel?.Invoke();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Task AddMessage(MessageType type, string message, Action<MessageItem> action) =>
|
||||
type switch
|
||||
{
|
||||
MessageType.Info => AddMessageInfo(message, action),
|
||||
MessageType.Success => AddMessageSuccess(message, action),
|
||||
MessageType.Warning => AddMessageWarning(message, action),
|
||||
MessageType.Error => AddMessageError(message, action),
|
||||
_ => Task.CompletedTask
|
||||
};
|
||||
|
||||
public static Task AddMessageInfo(string message, Action<MessageItem> action) =>
|
||||
AddMessageInternal(message, InfoBrush, action, includeTimestamp: true);
|
||||
|
||||
public static Task AddMessageSuccess(string message, Action<MessageItem> action) =>
|
||||
AddMessageInternal(message, SuccessBrush, action, includeTimestamp: true);
|
||||
|
||||
public static Task AddMessageWarning(string message, Action<MessageItem> action) =>
|
||||
AddMessageInternal(message, WarningBrush, action, includeTimestamp: true);
|
||||
|
||||
public static Task AddMessageError(string message, Action<MessageItem> action) =>
|
||||
AddMessageInternal(message, ErrorBrush, action, includeTimestamp: true);
|
||||
|
||||
public static Task AddText(string text, Action<MessageItem> action, string textColor = AppConst.Color.Text)
|
||||
{
|
||||
var brush = textColor == AppConst.Color.Text ? TextBrush : CreateBrush(textColor);
|
||||
return AddMessageInternal(text, brush, action, includeTimestamp: false);
|
||||
}
|
||||
|
||||
public static Task AddTextInfo(string text, Action<MessageItem> action, string textColor = AppConst.Color.Info) =>
|
||||
AddText(text, action, textColor);
|
||||
|
||||
public static Task AddTextSuccess(string text, Action<MessageItem> action, string textColor = AppConst.Color.Success) =>
|
||||
AddText(text, action, textColor);
|
||||
|
||||
public static Task AddTextWarning(string text, Action<MessageItem> action, string textColor = AppConst.Color.Warning) =>
|
||||
AddText(text, action, textColor);
|
||||
|
||||
public static Task AddTextError(string text, Action<MessageItem> action, string textColor = AppConst.Color.Error) =>
|
||||
AddText(text, action, textColor);
|
||||
|
||||
private static Task AddMessageInternal(string message, Brush brush, Action<MessageItem> action, bool includeTimestamp)
|
||||
{
|
||||
var text = includeTimestamp ? $"[ {DateTime.Now.ToString(TimestampFormat)} ]=>[ {message} ]" : message;
|
||||
action(new MessageItem(text, brush));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static SolidColorBrush CreateBrush(string color)
|
||||
{
|
||||
var brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(color));
|
||||
brush.Freeze();
|
||||
return brush;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user