2a8bbf76bf
Also put settings into proper dir
78 lines
2.4 KiB
C#
78 lines
2.4 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
|
using DialogHostAvalonia;
|
|
using MayShow.Interfaces;
|
|
using MayShow.ViewModels;
|
|
|
|
namespace MayShow;
|
|
|
|
public partial class MainWindow : Window, ITopLevelGrabber
|
|
{
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
DataContext = new MainWindowViewModel(this);
|
|
|
|
Closing += WindowIsClosing;
|
|
|
|
var lifetime = Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime;
|
|
// lifetime?.ShutdownRequested += ApplicationIsShuttingDown;
|
|
}
|
|
|
|
private async void WindowIsClosing(object? sender, WindowClosingEventArgs e)
|
|
{
|
|
e.Cancel = true; // async -> need to cancel immediately
|
|
if (await CheckIfClosePossible())
|
|
{
|
|
Closing -= WindowIsClosing;
|
|
var lifetime = Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime;
|
|
lifetime?.ShutdownRequested -= ApplicationIsShuttingDown;
|
|
Close();
|
|
}
|
|
}
|
|
|
|
private async void ApplicationIsShuttingDown(object? sender, ShutdownRequestedEventArgs e)
|
|
{
|
|
e.Cancel = true; // async -> need to cancel immediately
|
|
if (await CheckIfClosePossible())
|
|
{
|
|
Closing -= WindowIsClosing;
|
|
var lifetime = Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime;
|
|
lifetime?.ShutdownRequested -= ApplicationIsShuttingDown;
|
|
lifetime?.TryShutdown();
|
|
}
|
|
}
|
|
|
|
private async Task<bool> CheckIfClosePossible()
|
|
{
|
|
var canShutdown = true;
|
|
if (DataContext is MainWindowViewModel mwvm)
|
|
{
|
|
if (mwvm is ICanCheckShutdown canCheck)
|
|
{
|
|
canShutdown = await canCheck.CheckIsSafeToShutdown();
|
|
}
|
|
// only checking 1 level but for this app that is OK
|
|
if (canShutdown && mwvm.CurrentViewModel is ICanCheckShutdown currModel)
|
|
{
|
|
try
|
|
{
|
|
canShutdown = await currModel.CheckIsSafeToShutdown();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
canShutdown = true;
|
|
}
|
|
}
|
|
}
|
|
return canShutdown;
|
|
}
|
|
|
|
public TopLevel GetTopLevel()
|
|
{
|
|
return this;
|
|
}
|
|
} |