80 lines
2.3 KiB
C#
80 lines
2.3 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Media;
|
|
using DialogHostAvalonia;
|
|
using MsBox.Avalonia.ViewModels;
|
|
|
|
namespace Pilz.UI.AvaloniaUI.Dialogs;
|
|
|
|
public partial class AvaloniaFlyoutBase
|
|
{
|
|
public static void Show<T>(ContentControl owner, string? title, IImage? icon, object? tag = null) where T : AvaloniaFlyoutBase
|
|
{
|
|
Show(t, owner, title, icon);
|
|
}
|
|
|
|
public static void Showy<T>(T content, ContentControl owner, string? title, IImage? icon) where T : AvaloniaFlyoutBase
|
|
{
|
|
Show(t, owner, title, icon);
|
|
}
|
|
|
|
public async static Task<T> Show<T>(T content, ContentControl owner) where T : AvaloniaFlyoutBase
|
|
{
|
|
// Add styles
|
|
DialogHostStyles? style = null;
|
|
if (!owner.Styles.OfType<DialogHostStyles>().Any())
|
|
{
|
|
style = [];
|
|
owner.Styles.Add(style);
|
|
}
|
|
|
|
// Prepair content
|
|
var parentContent = owner.Content;
|
|
var dh = new DialogHost
|
|
{
|
|
Identifier = "FlyoutDialogIdentifier" + Guid.NewGuid()
|
|
};
|
|
owner.Content = null;
|
|
dh.Content = parentContent;
|
|
dh.CloseOnClickAway = content.CancelButtonVisible;
|
|
dh.CloseOnClickAwayParameter = "FlyoutDialogIdentifier_Cancel";
|
|
dh.DialogClosing += (ss, ee) =>
|
|
{
|
|
if (ee.Parameter?.ToString() == "FlyoutDialogIdentifier_Cancel")
|
|
content.Close(null);
|
|
};
|
|
content.OnClose += (s, e) =>
|
|
{
|
|
// ...
|
|
};
|
|
owner.Content = dh;
|
|
|
|
// Handle close
|
|
var tcs = new TaskCompletionSource<T>();
|
|
content.SetCloseAction(() =>
|
|
{
|
|
var r = content.GetButtonResult();
|
|
|
|
if (dh.CurrentSession != null && dh.CurrentSession.IsEnded == false)
|
|
{
|
|
DialogHost.Close(dh.Identifier);
|
|
}
|
|
|
|
owner.Content = null;
|
|
dh.Content = null;
|
|
owner.Content = parentContent;
|
|
if (style != null)
|
|
{
|
|
owner.Styles.Remove(style);
|
|
}
|
|
tcs.TrySetResult(r);
|
|
});
|
|
|
|
// Show dialog
|
|
await DialogHost.Show(content, dh.Identifier);
|
|
|
|
return content;
|
|
}
|
|
} |