using Avalonia.Controls; using Avalonia.Media; using Avalonia.VisualTree; using DialogHostAvalonia; namespace Pilz.UI.AvaloniaUI.Dialogs; public partial class AvaloniaFlyoutBase { public static Task Show(ContentControl owner, object? tag = null) where T : AvaloniaFlyoutBase { return Show(CreatePanelInstance(tag), owner); } public static Task Show(ContentControl owner, string? title, IImage? icon, object? tag = null) where T : AvaloniaFlyoutBase { return Show(CreatePanelInstance(tag), owner, title, icon); } public static Task Show(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(object? tag) where T : AvaloniaFlyoutBase { var dialogPanel = Activator.CreateInstance(); dialogPanel.Tag = tag; return dialogPanel; } public static async Task Show(T content, ContentControl owner) where T : AvaloniaFlyoutBase { // Add styles DialogHostStyles? style = null; if (!owner.Styles.OfType().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 && content.RegisterDialogCancel; dh.CloseOnClickAwayParameter = "FlyoutDialogIdentifier_Cancel"; dh.DialogClosing += (ss, ee) => { if (ee.Parameter?.ToString() == "FlyoutDialogIdentifier_Cancel") content.Close(null); }; content.OnClose += (s, e) => { if (s is AvaloniaFlyoutBase flyout && flyout.FindAncestorOfType() 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; } }