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,5 +1,3 @@
using System;
using System.Threading.Tasks;
using Avalonia.Controls;
namespace Pilz.UI.AvaloniaUI.Dialogs;
@@ -8,12 +6,12 @@ public partial class AvaloniaDialogBase
{
public static T Show<T>(string? title, object? icon, object? tag = null) where T : AvaloniaFlyoutBase
{
return Show(CreatePanelInstance<T>(tag), title, icon);
return Show(AvaloniaFlyoutBase.CreatePanelInstance<T>(tag), title, icon);
}
public static Task<T> ShowDialog<T>(Window parent, string? title, object? icon, object? tag = null) where T : AvaloniaFlyoutBase
{
return ShowDialog(CreatePanelInstance<T>(tag), parent, title, icon);
return ShowDialog(AvaloniaFlyoutBase.CreatePanelInstance<T>(tag), parent, title, icon);
}
public static T Show<T>(T dialogPanel, string? title, object? icon) where T : AvaloniaFlyoutBase
@@ -28,13 +26,6 @@ public partial class AvaloniaDialogBase
return dialogPanel;
}
internal static T CreatePanelInstance<T>(object? tag) where T : AvaloniaFlyoutBase
{
var dialogPanel = Activator.CreateInstance<T>();
dialogPanel.Tag = tag;
return dialogPanel;
}
internal static AvaloniaDialogBase CreateForm<T>(T dialogPanel, string? title, object? icon, WindowStartupLocation startPosition) where T : AvaloniaFlyoutBase
{
var dialog = new AvaloniaDialogBase

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);

View File

@@ -1,4 +1,3 @@
using System;
using System.ComponentModel;
using Avalonia;
using Avalonia.Controls;

View File

@@ -7,12 +7,13 @@
</PropertyGroup>
<PropertyGroup>
<Version>1.2.5</Version>
<Version>1.2.6</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.3.8" />
<PackageReference Include="Avalonia.Svg" Version="11.3.0" />
<PackageReference Include="DialogHost.Avalonia" Version="0.9.3" />
</ItemGroup>
<ItemGroup>