Dispose mail webviews when closing the shell
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.UI.Windowing;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Windows.UI;
|
||||
@@ -20,8 +21,10 @@ using Wino.Mail.WinUI.Helpers;
|
||||
using Wino.Mail.WinUI.Interfaces;
|
||||
using Wino.Mail.WinUI.Models;
|
||||
using Wino.Mail.WinUI.Views;
|
||||
using Wino.Messaging.Client.Mails;
|
||||
using Wino.Messaging.Client.Shell;
|
||||
using Wino.Messaging.UI;
|
||||
using Wino.Views.Mail;
|
||||
using WinUIEx;
|
||||
|
||||
namespace Wino.Mail.WinUI;
|
||||
@@ -356,12 +359,16 @@ public sealed partial class ShellWindow : WindowEx, IWinoShellWindow,
|
||||
SynchronizeTitleBarSearchBox();
|
||||
}
|
||||
|
||||
private void OnAppWindowClosing(object sender, Microsoft.UI.Windowing.AppWindowClosingEventArgs e)
|
||||
private async void OnAppWindowClosing(object sender, AppWindowClosingEventArgs e)
|
||||
{
|
||||
if (_allowClose || (Application.Current as App)?.IsExiting == true)
|
||||
return;
|
||||
|
||||
e.Cancel = true;
|
||||
|
||||
if (!await PrepareMailModeForHideAsync())
|
||||
return;
|
||||
|
||||
var windowManager = WinoApplication.Current.Services.GetService<IWinoWindowManager>();
|
||||
windowManager?.HideWindow(this);
|
||||
}
|
||||
@@ -398,6 +405,55 @@ public sealed partial class ShellWindow : WindowEx, IWinoShellWindow,
|
||||
UnregisterRecipients();
|
||||
}
|
||||
|
||||
private async Task<bool> PrepareMailModeForHideAsync()
|
||||
{
|
||||
if (MainShellFrame.Content is not WinoAppShell shellPage)
|
||||
return true;
|
||||
|
||||
if (shellPage.GetShellFrame().Content is not MailListPage mailListPage)
|
||||
return true;
|
||||
|
||||
var renderingFrame = mailListPage.FindName("RenderingFrame") as Frame;
|
||||
|
||||
if (renderingFrame?.Content is ComposePage composePage)
|
||||
{
|
||||
var closeResult = await MailDialogService.ShowThreeButtonDialogAsync(
|
||||
Translator.DialogMessage_CloseDraftWindowConfirmationTitle,
|
||||
Translator.DialogMessage_CloseDraftWindowConfirmationMessage,
|
||||
Translator.Buttons_Save,
|
||||
Translator.Buttons_Discard,
|
||||
Translator.Buttons_Cancel,
|
||||
WinoCustomMessageDialogIcon.Warning);
|
||||
|
||||
if (closeResult == ThreeButtonDialogResult.Cancel)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (closeResult == ThreeButtonDialogResult.Primary)
|
||||
{
|
||||
await composePage.ViewModel.SaveDraftAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
await composePage.ViewModel.DiscardDraftAsync(requireConfirmation: false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MailDialogService.InfoBarMessage(Translator.GeneralTitle_Error, ex.Message, InfoBarMessageType.Error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
await mailListPage.ViewModel.MailCollection.UnselectAllAsync();
|
||||
WeakReferenceMessenger.Default.Send(new DisposeRenderingFrameRequested());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void RegisterRecipients()
|
||||
{
|
||||
WeakReferenceMessenger.Default.Register<TitleBarShellContentUpdated>(this);
|
||||
|
||||
Reference in New Issue
Block a user