complete avalonia flyouts

This commit is contained in:
2025-11-11 16:43:21 +01:00
parent 593cdfb92c
commit 71d27c1061
4 changed files with 39 additions and 45 deletions

View File

@@ -1,26 +1,39 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.VisualTree;
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
public static Task Show<T>(ContentControl owner, object? tag = null) where T : AvaloniaFlyoutBase
{
Show(t, owner, title, icon);
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 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
public static async Task<T> Show<T>(T content, ContentControl owner) where T : AvaloniaFlyoutBase
{
// Add styles
DialogHostStyles? style = null;
@@ -38,7 +51,7 @@ public partial class AvaloniaFlyoutBase
};
owner.Content = null;
dh.Content = parentContent;
dh.CloseOnClickAway = content.CancelButtonVisible;
dh.CloseOnClickAway = content.CancelButtonVisible && content.RegisterDialogCancel;
dh.CloseOnClickAwayParameter = "FlyoutDialogIdentifier_Cancel";
dh.DialogClosing += (ss, ee) =>
{
@@ -47,30 +60,20 @@ public partial class AvaloniaFlyoutBase
};
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);
}
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);
}
tcs.TrySetResult(r);
});
};
owner.Content = dh;
// Show dialog
await DialogHost.Show(content, dh.Identifier);