chore: move confirm exit logic to TaskQueueViewModel

This commit is contained in:
dantmnf
2023-05-03 23:53:07 +08:00
parent 4933dbe092
commit 90c6580e80
8 changed files with 92 additions and 17 deletions

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace MaaWpfGui.Helper
{
internal static class ApplicationExtensions
{
public static bool IsShuttingDown(this Application application)
{
try
{
application.ShutdownMode = application.ShutdownMode;
return false;
}
catch
{
return true;
}
}
}
}

View File

@@ -21,6 +21,11 @@ namespace MaaWpfGui.Helper
public static implicit operator WindowHandle(Window w)
{
if (w == null)
{
return None;
}
var interop = new WindowInteropHelper(w);
return new WindowHandle { _handle = interop.Handle };
}

View File

@@ -46,5 +46,11 @@ namespace MaaWpfGui.Services.Managers
/// </summary>
/// <param name="shouldMinimizeToTaskbar">Whether to minimize to taskbar.</param>
void SetMinimizeToTaskbar(bool shouldMinimizeToTaskbar);
/// <summary>
/// Get the main window if it is visible.
/// </summary>
/// <returns>The <see cref="Window"/> if it is visible, or <see cref="null"/>. </returns>
Window GetWindowIfVisible();
}
}

View File

@@ -108,5 +108,25 @@ namespace MaaWpfGui.Services.Managers
MainWindow.Visibility = Visibility.Hidden;
}
}
public virtual Window GetWindowIfVisible()
{
if (MainWindow == null)
{
return null;
}
if (MainWindow.WindowState == WindowState.Minimized)
{
return null;
}
if (MainWindow.Visibility != Visibility.Visible)
{
return null;
}
return MainWindow;
}
}
}

View File

@@ -93,6 +93,15 @@ namespace MaaWpfGui.Services
private void App_exit(object sender, EventArgs e)
{
if (Instances.TaskQueueViewModel.Running)
{
Instances.MainWindowManager.Show();
if (!Instances.TaskQueueViewModel.ConfirmExit())
{
return;
}
}
System.Windows.Application.Current.Shutdown();
}

View File

@@ -85,17 +85,5 @@ namespace MaaWpfGui.ViewModels.UI
{
Application.Current.Shutdown();
}
public void CloseWindowCommand(CancelEventArgs args)
{
if (Instances.TaskQueueViewModel.Running)
{
var result = MessageBoxHelper.ShowNative(Window.GetWindow(View), LocalizationHelper.GetString("ConfirmExitText"), LocalizationHelper.GetString("ConfirmExitTitle"), "MAA", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (result != MessageBoxResult.Yes)
{
args.Cancel = true;
}
}
}
}
}

View File

@@ -154,6 +154,33 @@ namespace MaaWpfGui.ViewModels.UI
private readonly DispatcherTimer _timer = new DispatcherTimer();
public bool ConfirmExit()
{
if (Application.Current.IsShuttingDown())
{
// allow close if application is shutting down
return true;
}
if (!Running)
{
// no need to confirm if no running task
return true;
}
var window = Instances.MainWindowManager.GetWindowIfVisible();
var result = MessageBoxHelper.ShowNative(window, LocalizationHelper.GetString("ConfirmExitText"), LocalizationHelper.GetString("ConfirmExitTitle"), "MAA", MessageBoxButton.YesNo, MessageBoxImage.Information, MessageBoxResult.No);
return result == MessageBoxResult.Yes;
}
#pragma warning disable CS0672 // Member overrides obsolete member
// WHY OBSOLETED?
protected override bool CanClose()
#pragma warning restore CS0672 // Member overrides obsolete member
{
return ConfirmExit();
}
private void InitTimer()
{
_timer.Interval = TimeSpan.FromSeconds(50);

View File

@@ -17,11 +17,6 @@
d:DataContext="{d:DesignInstance {x:Type ui:RootViewModel}}"
Icon="../../newlogo.ico"
mc:Ignorable="d">
<hc:Interaction.Triggers>
<hc:EventTrigger EventName="Closing">
<hc:EventToCommand Command="{s:Action CloseWindowCommand}" PassEventArgsToCommand="True" />
</hc:EventTrigger>
</hc:Interaction.Triggers>
<DockPanel>
<TabControl