From e376da44820f3ac69602cce1f5e66c5c920af1b2 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Thu, 2 Oct 2025 19:29:46 +0800 Subject: [PATCH 1/4] Save settings before shutdown/restart to prevent data loss Added a call to `Context.API.SaveAppAllSettings()` before executing system shutdown, restart, or advanced restart operations. This ensures that any unsaved settings are persisted, reducing the risk of data loss during these actions. --- Plugins/Flow.Launcher.Plugin.Sys/Main.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs index 89067d44c0c..d0eb339fe34 100644 --- a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs @@ -211,6 +211,9 @@ private static List Commands(Query query) Localize.flowlauncher_plugin_sys_shutdown_computer(), MessageBoxButton.YesNo, MessageBoxImage.Warning); + // Save settings before shutdown to avoid data loss + Context.API.SaveAppAllSettings(); + if (result == MessageBoxResult.Yes) if (EnableShutdownPrivilege()) PInvoke.ExitWindowsEx(EXIT_WINDOWS_FLAGS.EWX_SHUTDOWN | EXIT_WINDOWS_FLAGS.EWX_POWEROFF, REASON); @@ -232,6 +235,9 @@ private static List Commands(Query query) Localize.flowlauncher_plugin_sys_restart_computer(), MessageBoxButton.YesNo, MessageBoxImage.Warning); + // Save settings before restart to avoid data loss + Context.API.SaveAppAllSettings(); + if (result == MessageBoxResult.Yes) if (EnableShutdownPrivilege()) PInvoke.ExitWindowsEx(EXIT_WINDOWS_FLAGS.EWX_REBOOT, REASON); @@ -253,6 +259,9 @@ private static List Commands(Query query) Localize.flowlauncher_plugin_sys_restart_computer(), MessageBoxButton.YesNo, MessageBoxImage.Warning); + // Save settings before restart to avoid data loss + Context.API.SaveAppAllSettings(); + if (result == MessageBoxResult.Yes) if (EnableShutdownPrivilege()) PInvoke.ExitWindowsEx(EXIT_WINDOWS_FLAGS.EWX_REBOOT | EXIT_WINDOWS_FLAGS.EWX_BOOTOPTIONS, REASON); From 167570559f82c8c7fa5bf51a5b33a0ca78c7c26d Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Thu, 2 Oct 2025 19:42:24 +0800 Subject: [PATCH 2/4] Move settings save to post-confirmation for actions Previously, `Context.API.SaveAppAllSettings()` was called unconditionally before user confirmation for shutdown, restart, and advanced restart actions. This change ensures settings are only saved if the user confirms the action by clicking "Yes" in the confirmation dialog. For all three functionalities: - Moved the settings save call inside the `if (result == MessageBoxResult.Yes)` block. - Retained the existing logic for executing the respective system commands, with checks for `EnableShutdownPrivilege()` to determine whether to use `PInvoke.ExitWindowsEx` or the `shutdown` command. This change prevents unnecessary settings saves when the user cancels the action. --- Plugins/Flow.Launcher.Plugin.Sys/Main.cs | 30 ++++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs index d0eb339fe34..b53c0261b0f 100644 --- a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs @@ -210,16 +210,16 @@ private static List Commands(Query query) Localize.flowlauncher_plugin_sys_dlgtext_shutdown_computer(), Localize.flowlauncher_plugin_sys_shutdown_computer(), MessageBoxButton.YesNo, MessageBoxImage.Warning); - - // Save settings before shutdown to avoid data loss - Context.API.SaveAppAllSettings(); - if (result == MessageBoxResult.Yes) + { + // Save settings before shutdown to avoid data loss + Context.API.SaveAppAllSettings(); + if (EnableShutdownPrivilege()) PInvoke.ExitWindowsEx(EXIT_WINDOWS_FLAGS.EWX_SHUTDOWN | EXIT_WINDOWS_FLAGS.EWX_POWEROFF, REASON); else Process.Start("shutdown", "/s /t 0"); - + } return true; } }, @@ -234,16 +234,16 @@ private static List Commands(Query query) Localize.flowlauncher_plugin_sys_dlgtext_restart_computer(), Localize.flowlauncher_plugin_sys_restart_computer(), MessageBoxButton.YesNo, MessageBoxImage.Warning); - - // Save settings before restart to avoid data loss - Context.API.SaveAppAllSettings(); - if (result == MessageBoxResult.Yes) + { + // Save settings before restart to avoid data loss + Context.API.SaveAppAllSettings(); + if (EnableShutdownPrivilege()) PInvoke.ExitWindowsEx(EXIT_WINDOWS_FLAGS.EWX_REBOOT, REASON); else Process.Start("shutdown", "/r /t 0"); - + } return true; } }, @@ -258,16 +258,16 @@ private static List Commands(Query query) Localize.flowlauncher_plugin_sys_dlgtext_restart_computer_advanced(), Localize.flowlauncher_plugin_sys_restart_computer(), MessageBoxButton.YesNo, MessageBoxImage.Warning); - - // Save settings before restart to avoid data loss - Context.API.SaveAppAllSettings(); - if (result == MessageBoxResult.Yes) + { + // Save settings before restart to avoid data loss + Context.API.SaveAppAllSettings(); + if (EnableShutdownPrivilege()) PInvoke.ExitWindowsEx(EXIT_WINDOWS_FLAGS.EWX_REBOOT | EXIT_WINDOWS_FLAGS.EWX_BOOTOPTIONS, REASON); else Process.Start("shutdown", "/r /o /t 0"); - + } return true; } }, From d08ee30a7a17c23f481302dad350ff7c042eb42e Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Thu, 2 Oct 2025 19:43:31 +0800 Subject: [PATCH 3/4] Refactor plugin actions to simplify logic Removed logoff operation logic and associated return statement. Eliminated return statement after recycle bin error handling. Removed async plugin data reload and success message logic. Simplified theme selector query handling by removing `return false`. These changes streamline the code and improve maintainability. --- Plugins/Flow.Launcher.Plugin.Sys/Main.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs index b53c0261b0f..cb3acf77f58 100644 --- a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs @@ -282,10 +282,8 @@ private static List Commands(Query query) Localize.flowlauncher_plugin_sys_dlgtext_logoff_computer(), Localize.flowlauncher_plugin_sys_log_off(), MessageBoxButton.YesNo, MessageBoxImage.Warning); - if (result == MessageBoxResult.Yes) PInvoke.ExitWindowsEx(EXIT_WINDOWS_FLAGS.EWX_LOGOFF, REASON); - return true; } }, @@ -351,7 +349,6 @@ private static List Commands(Query query) Localize.flowlauncher_plugin_sys_dlgtitle_error(), MessageBoxButton.OK, MessageBoxImage.Error); } - return true; } }, @@ -425,13 +422,11 @@ private static List Commands(Query query) { // Hide the window first then show msg after done because sometimes the reload could take a while, so not to make user think it's frozen. Context.API.HideMainWindow(); - _ = Context.API.ReloadAllPluginData().ContinueWith(_ => Context.API.ShowMsg( Localize.flowlauncher_plugin_sys_dlgtitle_success(), Localize.flowlauncher_plugin_sys_dlgtext_all_applicableplugins_reloaded()), TaskScheduler.Current); - return true; } }, @@ -511,7 +506,6 @@ private static List Commands(Query query) else { Context.API.ChangeQuery($"{query.ActionKeyword}{Plugin.Query.ActionKeywordSeparator}{ThemeSelector.Keyword}{Plugin.Query.ActionKeywordSeparator}"); - } return false; } From 5b0a30774e711fececa2088316657cc9338e01c8 Mon Sep 17 00:00:00 2001 From: Jack Ye Date: Thu, 2 Oct 2025 19:45:08 +0800 Subject: [PATCH 4/4] Fix code comment typo Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- Plugins/Flow.Launcher.Plugin.Sys/Main.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs index cb3acf77f58..57b9749f761 100644 --- a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs @@ -260,7 +260,7 @@ private static List Commands(Query query) MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { - // Save settings before restart to avoid data loss + // Save settings before advanced restart to avoid data loss Context.API.SaveAppAllSettings(); if (EnableShutdownPrivilege())