149 lines
4.6 KiB
C#
149 lines
4.6 KiB
C#
using System;
|
|
using System.Reactive.Disposables;
|
|
using System.Threading.Tasks;
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
|
using Avalonia.Controls.Notifications;
|
|
using Avalonia.VisualTree;
|
|
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!;
|
|
|
|
private static LoadingContainer _loadingContainer = null!;
|
|
|
|
private static int _loadingReferenceCount = 0;
|
|
|
|
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 LoadingContainer LoadingContainer
|
|
{
|
|
get
|
|
{
|
|
if (_loadingContainer != null) return _loadingContainer!;
|
|
|
|
var lf = ((IClassicDesktopStyleApplicationLifetime)App.Current.ApplicationLifetime);
|
|
_loadingContainer = lf.MainWindow!.FindDescendantOfType<LoadingContainer>();
|
|
if (_loadingContainer == null)
|
|
throw new Exception("Can not get loading mask");
|
|
|
|
return _loadingContainer!;
|
|
}
|
|
}
|
|
|
|
public static void UpdateLoadingStatus()
|
|
{
|
|
LoadingContainer.IsLoading = _loadingReferenceCount > 0;
|
|
}
|
|
|
|
public static IDisposable MakeLoading(string? msg)
|
|
{
|
|
_loadingReferenceCount++;
|
|
LoadingContainer.LoadingMessage = msg;
|
|
UpdateLoadingStatus();
|
|
|
|
return Disposable.Create(() =>
|
|
{
|
|
_loadingReferenceCount--;
|
|
UpdateLoadingStatus();
|
|
});
|
|
}
|
|
|
|
|
|
|
|
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 OverlayDialogOptions DefaultOverlayDialogOptionsYesNo()
|
|
{
|
|
return new OverlayDialogOptions
|
|
{
|
|
FullScreen = false,
|
|
Buttons = DialogButton.YesNo,
|
|
CanResize = false,
|
|
CanDragMove = false,
|
|
IsCloseButtonVisible = true,
|
|
CanLightDismiss = true,
|
|
Mode = DialogMode.None
|
|
};
|
|
}
|
|
|
|
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);
|
|
}
|
|
} |