Skip to content

Commit

Permalink
Prevent closing app while doing critical process (#681)
Browse files Browse the repository at this point in the history
* Prevent closing app while doing critical process

* Use async Task on CloseApp

* Inline Dialog_EnsureExit in CloseApp method
  • Loading branch information
bagusnl authored Feb 6, 2025
1 parent 3fdaefa commit 0d37d44
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CollapseLauncher/Classes/Helper/WindowUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ private static IntPtr MainWndProc(IntPtr hwnd, uint msg, UIntPtr wParam, IntPtr
// Deal with close message from system shell.
if (CurrentWindow is MainWindow mainWindow)
{
mainWindow.CloseApp();
_ = mainWindow.CloseApp();
}
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private async void StartRoutine(object sender, RoutedEventArgs e)
if (!await CheckForAdminAccess(this))
{
if (WindowUtility.CurrentWindow is MainWindow mainWindow)
mainWindow.CloseApp();
_ = mainWindow.CloseApp();
return;
}

Expand Down
12 changes: 10 additions & 2 deletions CollapseLauncher/XAMLs/MainApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System;
using System.Threading.Tasks;
using Windows.UI;
using static CollapseLauncher.Dialogs.SimpleDialogs;
using static CollapseLauncher.InnerLauncherConfig;
using static Hi3Helper.Logger;
using static Hi3Helper.Shared.Region.LauncherConfig;
Expand All @@ -32,6 +33,8 @@ namespace CollapseLauncher
public sealed partial class MainWindow : Window
{
private static bool _isForceDisableIntro;

public static bool IsCriticalOpInProgress { get; set; }

public void InitializeWindowProperties(bool startOobe = false)
{
Expand Down Expand Up @@ -248,13 +251,18 @@ private void MinimizeButton_Click(object sender, RoutedEventArgs e)
WindowUtility.WindowMinimize();
}

private void CloseButton_Click(object sender, RoutedEventArgs e) => CloseApp();
private void CloseButton_Click(object sender, RoutedEventArgs e) => _ = CloseApp();

/// <summary>
/// Close app and do necessary events before closing
/// </summary>
public void CloseApp()
public async Task CloseApp()
{
if (IsCriticalOpInProgress)
{
if (await Dialog_EnsureExit(Content) != ContentDialogResult.Primary)
return;
}
SentryHelper.StopSentrySdk();
_TrayIcon?.Dispose();
Close();
Expand Down
4 changes: 4 additions & 0 deletions CollapseLauncher/XAMLs/MainApp/Pages/CachesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public async void RunCheckRoutine(object sender, bool isFast, bool isMainButton)
SetMainCheckUpdateBtnProperty(sender);
}

MainWindow.IsCriticalOpInProgress = true;
Sleep.PreventSleep(ILoggerHelper.GetILogger());
AddEvent();

Expand Down Expand Up @@ -98,6 +99,7 @@ public async void RunCheckRoutine(object sender, bool isFast, bool isMainButton)
{
RemoveEvent();
Sleep.RestoreSleep();
MainWindow.IsCriticalOpInProgress = false;
}
}

Expand All @@ -108,6 +110,7 @@ public async void StartCachesUpdate(object sender, RoutedEventArgs e)
UpdateCachesBtn.IsEnabled = false;
CancelBtn.IsEnabled = true;

MainWindow.IsCriticalOpInProgress = true;
Sleep.PreventSleep(ILoggerHelper.GetILogger());
AddEvent();

Expand Down Expand Up @@ -147,6 +150,7 @@ public async void StartCachesUpdate(object sender, RoutedEventArgs e)
finally
{
Sleep.RestoreSleep();
MainWindow.IsCriticalOpInProgress = false;
RemoveEvent();
}
}
Expand Down
10 changes: 10 additions & 0 deletions CollapseLauncher/XAMLs/MainApp/Pages/Dialogs/SimpleDialogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,16 @@ public static Task<ContentDialogResult> Dialog_UninstallGame(UIElement content,
Lang._Misc.Cancel,
ContentDialogButton.Secondary,
ContentDialogTheme.Error);

public static Task<ContentDialogResult> Dialog_EnsureExit(UIElement content) =>
SpawnDialog(Lang._Dialogs.EnsureExitTitle,
Lang._Dialogs.EnsureExitSubtitle,
content,
Lang._Misc.NoCancel,
Lang._Misc.Yes,
null,
ContentDialogButton.Close,
ContentDialogTheme.Warning);

public static Task<ContentDialogResult> Dialog_ClearMetadata(UIElement content) =>
SpawnDialog(string.Format(Lang._SettingsPage.AppFiles_ClearMetadataDialog),
Expand Down
6 changes: 6 additions & 0 deletions CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,7 @@ private async void PredownloadDialog(object sender, RoutedEventArgs e)

try
{
MainWindow.IsCriticalOpInProgress = true;
// Prevent device from sleep
Sleep.PreventSleep(ILoggerHelper.GetILogger());
// Set the notification trigger to "Running" state
Expand Down Expand Up @@ -1356,6 +1357,7 @@ private async void PredownloadDialog(object sender, RoutedEventArgs e)

// Turn the sleep back on
Sleep.RestoreSleep();
MainWindow.IsCriticalOpInProgress = false;
}
}

Expand Down Expand Up @@ -1392,6 +1394,7 @@ private async void InstallGameDialog(object sender, RoutedEventArgs e)
bool isUseSophon = CurrentGameProperty.GameInstall.IsUseSophon;
try
{
MainWindow.IsCriticalOpInProgress = true;
// Prevent device from sleep
Sleep.PreventSleep(ILoggerHelper.GetILogger());
// Set the notification trigger to "Running" state
Expand Down Expand Up @@ -1562,6 +1565,7 @@ await SpawnDialog(Lang._HomePage.InstallFolderRootTitle,

// Turn the sleep back on
Sleep.RestoreSleep();
MainWindow.IsCriticalOpInProgress = false;
}
}

Expand Down Expand Up @@ -2634,6 +2638,7 @@ private async void UpdateGameDialog(object sender, RoutedEventArgs e)

try
{
MainWindow.IsCriticalOpInProgress = true;
// Prevent device from sleep
Sleep.PreventSleep(ILoggerHelper.GetILogger());
// Set the notification trigger to "Running" state
Expand Down Expand Up @@ -2741,6 +2746,7 @@ private async void UpdateGameDialog(object sender, RoutedEventArgs e)

// Turn the sleep back on
Sleep.RestoreSleep();
MainWindow.IsCriticalOpInProgress = false;
}
}
#endregion
Expand Down
4 changes: 4 additions & 0 deletions CollapseLauncher/XAMLs/MainApp/Pages/RepairPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private void StartGameCheck(object sender, RoutedEventArgs e)

private async void RunCheckRoutine(object sender, bool isFast, bool isMainButton)
{
MainWindow.IsCriticalOpInProgress = true;
Sleep.PreventSleep(ILoggerHelper.GetILogger());

CheckFilesBtn.Flyout.Hide();
Expand Down Expand Up @@ -99,13 +100,15 @@ private async void RunCheckRoutine(object sender, bool isFast, bool isMainButton
{
RemoveEvent();
Sleep.RestoreSleep();
MainWindow.IsCriticalOpInProgress = false;
}
}

private async void StartGameRepair(object sender, RoutedEventArgs e)
{
try
{
MainWindow.IsCriticalOpInProgress = true;
Sleep.PreventSleep(ILoggerHelper.GetILogger());
RepairFilesBtn.IsEnabled = false;
CancelBtn.IsEnabled = true;
Expand Down Expand Up @@ -149,6 +152,7 @@ private async void StartGameRepair(object sender, RoutedEventArgs e)
{
RemoveEvent();
Sleep.RestoreSleep();
MainWindow.IsCriticalOpInProgress = false;
}
}

Expand Down
2 changes: 2 additions & 0 deletions Hi3Helper.Core/Lang/Locale/LangDialogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ public sealed partial class LangDialogs
public string UACWarningContent { get; set; } = LangFallback?._Dialogs.UACWarningContent;
public string UACWarningLearnMore { get; set; } = LangFallback?._Dialogs.UACWarningLearnMore;
public string UACWarningDontShowAgain { get; set; } = LangFallback?._Dialogs.UACWarningDontShowAgain;
public string EnsureExitTitle { get; set; } = LangFallback?._Dialogs.EnsureExitTitle;
public string EnsureExitSubtitle { get; set; } = LangFallback?._Dialogs.EnsureExitSubtitle;
}
}
#endregion
Expand Down
5 changes: 4 additions & 1 deletion Hi3Helper.Core/Lang/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,10 @@
"UACWarningTitle": "Warning: UAC Disabled Detected",
"UACWarningContent": "Disabling User Account Control (UAC) is never a good idea.\nThe security of the OS will be compromised and the game may not run properly.\n\nClick the \"Learn More\" button to see how to enable UAC.\nThe related entry is: Run all administrators in Admin Approval Mode.",
"UACWarningLearnMore": "Learn More",
"UACWarningDontShowAgain": "Don't Show Again"
"UACWarningDontShowAgain": "Don't Show Again",

"EnsureExitTitle": "Exiting Application",
"EnsureExitSubtitle": "There are critical operations running in the background. Are you sure you want to exit?"
},

"_FileMigrationProcess": {
Expand Down

0 comments on commit 0d37d44

Please sign in to comment.