From 90c6580e80ee2c8222ee24b07fb6e73099641c26 Mon Sep 17 00:00:00 2001 From: dantmnf Date: Wed, 3 May 2023 23:53:07 +0800 Subject: [PATCH] chore: move confirm exit logic to TaskQueueViewModel --- src/MaaWpfGui/Helper/ApplicationExtensions.cs | 25 +++++++++++++++++ src/MaaWpfGui/Helper/WindowHandle.cs | 5 ++++ .../Services/Managers/IMainWindowManager.cs | 6 +++++ .../Services/Managers/MainWindowManager.cs | 20 ++++++++++++++ src/MaaWpfGui/Services/TrayIcon.cs | 9 +++++++ src/MaaWpfGui/ViewModels/UI/RootViewModel.cs | 12 --------- .../ViewModels/UI/TaskQueueViewModel.cs | 27 +++++++++++++++++++ src/MaaWpfGui/Views/UI/RootView.xaml | 5 ---- 8 files changed, 92 insertions(+), 17 deletions(-) create mode 100644 src/MaaWpfGui/Helper/ApplicationExtensions.cs diff --git a/src/MaaWpfGui/Helper/ApplicationExtensions.cs b/src/MaaWpfGui/Helper/ApplicationExtensions.cs new file mode 100644 index 0000000000..d723c1e732 --- /dev/null +++ b/src/MaaWpfGui/Helper/ApplicationExtensions.cs @@ -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; + } + } + } +} diff --git a/src/MaaWpfGui/Helper/WindowHandle.cs b/src/MaaWpfGui/Helper/WindowHandle.cs index 6cbdc37382..65dc5a95b2 100644 --- a/src/MaaWpfGui/Helper/WindowHandle.cs +++ b/src/MaaWpfGui/Helper/WindowHandle.cs @@ -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 }; } diff --git a/src/MaaWpfGui/Services/Managers/IMainWindowManager.cs b/src/MaaWpfGui/Services/Managers/IMainWindowManager.cs index 30af5647fc..a7ea1a2d4b 100644 --- a/src/MaaWpfGui/Services/Managers/IMainWindowManager.cs +++ b/src/MaaWpfGui/Services/Managers/IMainWindowManager.cs @@ -46,5 +46,11 @@ namespace MaaWpfGui.Services.Managers /// /// Whether to minimize to taskbar. void SetMinimizeToTaskbar(bool shouldMinimizeToTaskbar); + + /// + /// Get the main window if it is visible. + /// + /// The if it is visible, or . + Window GetWindowIfVisible(); } } diff --git a/src/MaaWpfGui/Services/Managers/MainWindowManager.cs b/src/MaaWpfGui/Services/Managers/MainWindowManager.cs index a0155ae424..2c2a4bbb8d 100644 --- a/src/MaaWpfGui/Services/Managers/MainWindowManager.cs +++ b/src/MaaWpfGui/Services/Managers/MainWindowManager.cs @@ -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; + } } } diff --git a/src/MaaWpfGui/Services/TrayIcon.cs b/src/MaaWpfGui/Services/TrayIcon.cs index 1c759b6821..71f80c966e 100644 --- a/src/MaaWpfGui/Services/TrayIcon.cs +++ b/src/MaaWpfGui/Services/TrayIcon.cs @@ -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(); } diff --git a/src/MaaWpfGui/ViewModels/UI/RootViewModel.cs b/src/MaaWpfGui/ViewModels/UI/RootViewModel.cs index f2808e0613..f15b73fd46 100644 --- a/src/MaaWpfGui/ViewModels/UI/RootViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/RootViewModel.cs @@ -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; - } - } - } } } diff --git a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs index b5f517e0dc..79df8dee08 100644 --- a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs @@ -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); diff --git a/src/MaaWpfGui/Views/UI/RootView.xaml b/src/MaaWpfGui/Views/UI/RootView.xaml index 836d635eda..530cd6ffe1 100644 --- a/src/MaaWpfGui/Views/UI/RootView.xaml +++ b/src/MaaWpfGui/Views/UI/RootView.xaml @@ -17,11 +17,6 @@ d:DataContext="{d:DesignInstance {x:Type ui:RootViewModel}}" Icon="../../newlogo.ico" mc:Ignorable="d"> - - - - -