From e6472543e0ec2d001dbe8b51b3262fe54d8d7f2d Mon Sep 17 00:00:00 2001 From: tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Sat, 2 May 2026 10:47:58 +0300 Subject: [PATCH] Refactor fetching Avalonia TopLevel --- .../Utils/Extensions/AvaloniaExtensions.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/DiscordChatExporter.Gui/Utils/Extensions/AvaloniaExtensions.cs b/DiscordChatExporter.Gui/Utils/Extensions/AvaloniaExtensions.cs index 98d5b4d3..f89ffd63 100644 --- a/DiscordChatExporter.Gui/Utils/Extensions/AvaloniaExtensions.cs +++ b/DiscordChatExporter.Gui/Utils/Extensions/AvaloniaExtensions.cs @@ -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) {