Skip to content

Commit

Permalink
improved getting toplevel
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrxx99 committed Feb 26, 2024
1 parent 4bbe9fa commit 88d778e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
10 changes: 2 additions & 8 deletions YoutubeDownloader/ViewModels/Framework/DialogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Platform.Storage;
using DialogHostAvalonia;
using YoutubeDownloader.Views.Framework;
Expand Down Expand Up @@ -56,9 +54,7 @@ void OnScreenClosed(object? closeSender, EventArgs closeArgs)
string defaultFilePath = ""
)
{
var topLevel = (
Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime
)?.MainWindow;
var topLevel = viewManager.GetTopLevel();

var storageProvider = topLevel?.StorageProvider;
if (storageProvider is null)
Expand All @@ -85,9 +81,7 @@ void OnScreenClosed(object? closeSender, EventArgs closeArgs)

public async Task<string?> PromptDirectoryPath(string defaultDirPath = "")
{
var topLevel = (
Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime
)?.MainWindow;
var topLevel = viewManager.GetTopLevel();

var storageProvider = topLevel?.StorageProvider;
if (storageProvider is null)
Expand Down
12 changes: 3 additions & 9 deletions YoutubeDownloader/Views/Framework/ViewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@ public interface IViewManager
public TopLevel? GetTopLevel();
}

public class ViewManager : IViewManager
public class ViewManager(IApplicationLifetime applicationLifetime) : IViewManager
{
private readonly IApplicationLifetime _applicationLifetime;
private readonly ViewLocator _viewLocator = new();

public ViewManager(IApplicationLifetime applicationLifetime)
{
_applicationLifetime = applicationLifetime;
}

public Control? CreateAndBindViewForModelIfNecessary(object? model)
{
var view = _viewLocator.Match(model) ? _viewLocator.Build(model) : null;
Expand All @@ -35,7 +29,7 @@ public ViewManager(IApplicationLifetime applicationLifetime)
public TopLevel? GetTopLevel()
{
if (
_applicationLifetime is IClassicDesktopStyleApplicationLifetime
applicationLifetime is IClassicDesktopStyleApplicationLifetime
{
MainWindow: { } window
}
Expand All @@ -44,7 +38,7 @@ _applicationLifetime is IClassicDesktopStyleApplicationLifetime
return window;
}

if (_applicationLifetime is ISingleViewApplicationLifetime { MainView: { } mainView })
if (applicationLifetime is ISingleViewApplicationLifetime { MainView: { } mainView })
{
var visualRoot = mainView.GetVisualRoot();
if (visualRoot is TopLevel topLevel)
Expand Down

0 comments on commit 88d778e

Please sign in to comment.