Refactor fetching Avalonia TopLevel

This commit is contained in:
tyrrrz
2026-05-02 10:47:58 +03:00
parent ee163d6de7
commit e6472543e0

View File

@@ -8,14 +8,18 @@ internal static class AvaloniaExtensions
{
extension(IApplicationLifetime lifetime)
{
public Window? TryGetMainWindow() =>
lifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime
? desktopLifetime.MainWindow
: null;
public Control? TryGetMainView() =>
lifetime switch
{
IClassicDesktopStyleApplicationLifetime desktopLifetime =>
desktopLifetime.MainWindow,
public TopLevel? TryGetTopLevel() =>
lifetime.TryGetMainWindow()
?? (lifetime as ISingleViewApplicationLifetime)?.MainView?.Pipe(TopLevel.GetTopLevel);
ISingleViewApplicationLifetime singleViewLifetime => singleViewLifetime.MainView,
_ => null,
};
public TopLevel? TryGetTopLevel() => lifetime.TryGetMainView()?.Pipe(TopLevel.GetTopLevel);
public bool TryShutdown(int exitCode = 0)
{