1
0

96 lines
3.0 KiB
C#

using System;
using System.Threading.Tasks;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Notifications;
using Flawless.Client.ViewModels.ModalBox;
using Flawless.Client.Views.ModalBox;
using Ursa.Controls;
using Notification = Ursa.Controls.Notification;
using WindowNotificationManager = Ursa.Controls.WindowNotificationManager;
namespace Flawless.Client;
public static class UIHelper
{
private static WindowNotificationManager _notificationManager = null!;
public static WindowNotificationManager Notify
{
get
{
if (_notificationManager != null) return _notificationManager!;
var lf = ((IClassicDesktopStyleApplicationLifetime)App.Current.ApplicationLifetime);
if (!WindowNotificationManager.TryGetNotificationManager(lf.MainWindow!, out _notificationManager))
throw new Exception("Can not get notification manager");
_notificationManager!.Position = NotificationPosition.TopCenter;
return _notificationManager!;
}
}
public static void NotifyError(Exception ex)
{
try
{
var content = ex.ToString();
if (content.Length > 100) content = content.Substring(0, 100) + "...";
var nf = new Notification(ex.GetType().Name, content, NotificationType.Error);
Notify.Show(nf);
}
catch (Exception e)
{
Console.WriteLine("Can not notify error to users: " + e);
}
}
public static void NotifyError(string title, string content)
{
try
{
var nf = new Notification(title, content, NotificationType.Error);
Notify.Show(nf);
}
catch (Exception e)
{
Console.WriteLine($"Can not notify error to users: {title} - {content}, {e}");
}
}
public static Task<DialogResult> SimpleAskAsync(string content, DialogMode mode = DialogMode.None)
{
var opt = new OverlayDialogOptions
{
FullScreen = false,
Buttons = DialogButton.YesNo,
CanResize = false,
CanDragMove = false,
IsCloseButtonVisible = true,
CanLightDismiss = true,
Mode = mode
};
var vm = new SimpleMessageDialogViewModel(content);
return OverlayDialog.ShowModal<SimpleMessageDialogView, SimpleMessageDialogViewModel>(vm, AppDefaultValues.HostId, opt);
}
public static Task SimpleAlert(string content, DialogMode mode = DialogMode.Error)
{
var opt = new OverlayDialogOptions
{
FullScreen = false,
Buttons = DialogButton.YesNo,
CanResize = false,
CanDragMove = false,
IsCloseButtonVisible = true,
CanLightDismiss = true,
Mode = mode
};
var vm = new SimpleMessageDialogViewModel(content);
return OverlayDialog.ShowModal<SimpleMessageDialogView, SimpleMessageDialogViewModel>(vm, AppDefaultValues.HostId, opt);
}
}