mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-18 10:10:45 +08:00
chore: move confirm exit logic to TaskQueueViewModel
This commit is contained in:
25
src/MaaWpfGui/Helper/ApplicationExtensions.cs
Normal file
25
src/MaaWpfGui/Helper/ApplicationExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user