88 lines
2.8 KiB
C#
88 lines
2.8 KiB
C#
using Avalonia.Controls;
|
|
using Avalonia.Media;
|
|
using Avalonia.VisualTree;
|
|
using DialogHostAvalonia;
|
|
|
|
namespace Pilz.UI.AvaloniaUI.Dialogs;
|
|
|
|
public partial class AvaloniaFlyoutBase
|
|
{
|
|
public static IImage? DefaultCancelImage { get; set; }
|
|
public static IImage? DefaultConfirmImage { get; set; }
|
|
public static int DefaultPadding { get; set; } = 3;
|
|
|
|
public static Task Show<T>(ContentControl owner, object? tag = null) where T : AvaloniaFlyoutBase
|
|
{
|
|
return Show(CreatePanelInstance<T>(tag), owner);
|
|
}
|
|
|
|
public static Task Show<T>(ContentControl owner, string? title, IImage? icon, object? tag = null) where T : AvaloniaFlyoutBase
|
|
{
|
|
return Show(CreatePanelInstance<T>(tag), owner, title, icon);
|
|
}
|
|
|
|
public static Task Show<T>(T content, ContentControl owner, string? title, IImage? icon) where T : AvaloniaFlyoutBase
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(title))
|
|
content.Title = title;
|
|
if (icon != null)
|
|
content.TitleIcon = icon;
|
|
return Show(content, owner);
|
|
}
|
|
|
|
internal static T CreatePanelInstance<T>(object? tag) where T : AvaloniaFlyoutBase
|
|
{
|
|
var dialogPanel = Activator.CreateInstance<T>();
|
|
dialogPanel.Tag = tag;
|
|
return dialogPanel;
|
|
}
|
|
|
|
public static async 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
|
|
{
|
|
DialogMargin = new(DefaultPadding),
|
|
Identifier = "FlyoutDialogIdentifier" + Guid.NewGuid(),
|
|
};
|
|
owner.Content = null;
|
|
dh.Content = parentContent;
|
|
dh.CloseOnClickAway = content.CancelButtonVisible && content.RegisterDialogCancel;
|
|
dh.CloseOnClickAwayParameter = "FlyoutDialogIdentifier_Cancel";
|
|
dh.DialogClosing += (_, ee) =>
|
|
{
|
|
if (ee.Parameter?.ToString() == "FlyoutDialogIdentifier_Cancel")
|
|
content.Close(null);
|
|
};
|
|
content.OnClose += (s, _) =>
|
|
{
|
|
if (s is AvaloniaFlyoutBase flyout
|
|
&& flyout.FindAncestorOfType<DialogHost>() is
|
|
{
|
|
CurrentSession.IsEnded: false,
|
|
} host)
|
|
DialogHost.Close(host.Identifier);
|
|
|
|
owner.Content = null;
|
|
dh.Content = null;
|
|
owner.Content = parentContent;
|
|
if (style != null)
|
|
owner.Styles.Remove(style);
|
|
};
|
|
owner.Content = dh;
|
|
|
|
// Show dialog
|
|
await DialogHost.Show(content, dh.Identifier);
|
|
|
|
return content;
|
|
}
|
|
} |