From ff4a78a7f93a3b63e605ae955745d3f40d4608d4 Mon Sep 17 00:00:00 2001 From: Davide Giacometti Date: Fri, 19 Feb 2021 18:17:52 +0100 Subject: [PATCH] PT Run Service notification improvements (#9772) --- .../Helpers/ServiceHelper.cs | 33 +++++++++++++++---- .../Properties/Resources.Designer.cs | 33 +++++++++++++++++-- .../Properties/Resources.resx | 15 +++++++-- .../PowerLauncher/PublicAPIInstance.cs | 14 +++++--- src/modules/launcher/Wox.Plugin/IPublicAPI.cs | 5 +-- 5 files changed, 80 insertions(+), 20 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Service/Helpers/ServiceHelper.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Service/Helpers/ServiceHelper.cs index b2c4a69eea..c801560a16 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Service/Helpers/ServiceHelper.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Service/Helpers/ServiceHelper.cs @@ -6,7 +6,6 @@ using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; -using System.Globalization; using System.Linq; using System.Reflection; using System.ServiceProcess; @@ -75,11 +74,11 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Helpers if (exitCode == 0) { - contextAPI.ShowNotification(GetLocalizedMessage(serviceResult, action)); + contextAPI.ShowNotification(GetLocalizedMessage(action), serviceResult.DisplayName); } else { - contextAPI.ShowNotification("An error occurred"); + contextAPI.ShowNotification(GetLocalizedErrorMessage(action), serviceResult.DisplayName); Log.Error($"The command returned {exitCode}", MethodBase.GetCurrentMethod().DeclaringType); } } @@ -192,19 +191,39 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Helpers } } - private static string GetLocalizedMessage(ServiceResult serviceResult, Action action) + private static string GetLocalizedMessage(Action action) { if (action == Action.Start) { - return string.Format(CultureInfo.CurrentCulture, Resources.wox_plugin_service_started_notification, serviceResult.DisplayName); + return Resources.wox_plugin_service_started_notification; } else if (action == Action.Stop) { - return string.Format(CultureInfo.CurrentCulture, Resources.wox_plugin_service_stopped_notification, serviceResult.DisplayName); + return Resources.wox_plugin_service_stopped_notification; } else if (action == Action.Restart) { - return string.Format(CultureInfo.CurrentCulture, Resources.wox_plugin_service_restarted_notification, serviceResult.DisplayName); + return Resources.wox_plugin_service_restarted_notification; + } + else + { + return string.Empty; + } + } + + private static string GetLocalizedErrorMessage(Action action) + { + if (action == Action.Start) + { + return Resources.wox_plugin_service_start_error_notification; + } + else if (action == Action.Stop) + { + return Resources.wox_plugin_service_stop_error_notification; + } + else if (action == Action.Restart) + { + return Resources.wox_plugin_service_restart_error_notification; } else { diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Service/Properties/Resources.Designer.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Service/Properties/Resources.Designer.cs index 7d2c9e70cb..b1344a0563 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Service/Properties/Resources.Designer.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Service/Properties/Resources.Designer.cs @@ -124,7 +124,16 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties { } /// - /// Looks up a localized string similar to {0} has been restarted. + /// Looks up a localized string similar to An error occurred while restarting the service. + /// + internal static string wox_plugin_service_restart_error_notification { + get { + return ResourceManager.GetString("wox_plugin_service_restart_error_notification", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The service has been restarted. /// internal static string wox_plugin_service_restarted_notification { get { @@ -150,6 +159,15 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties { } } + /// + /// Looks up a localized string similar to An error occurred while starting the service. + /// + internal static string wox_plugin_service_start_error_notification { + get { + return ResourceManager.GetString("wox_plugin_service_start_error_notification", resourceCulture); + } + } + /// /// Looks up a localized string similar to Automatic. /// @@ -214,7 +232,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties { } /// - /// Looks up a localized string similar to {0} has been started. + /// Looks up a localized string similar to The service has been started. /// internal static string wox_plugin_service_started_notification { get { @@ -249,6 +267,15 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties { } } + /// + /// Looks up a localized string similar to An error occurred while stopping the service. + /// + internal static string wox_plugin_service_stop_error_notification { + get { + return ResourceManager.GetString("wox_plugin_service_stop_error_notification", resourceCulture); + } + } + /// /// Looks up a localized string similar to Stopping. /// @@ -268,7 +295,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties { } /// - /// Looks up a localized string similar to {0} has been stopped. + /// Looks up a localized string similar to The service has been stopped. /// internal static string wox_plugin_service_stopped_notification { get { diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Service/Properties/Resources.resx b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Service/Properties/Resources.resx index 9839e38102..42efb3e051 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Service/Properties/Resources.resx +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Service/Properties/Resources.resx @@ -139,7 +139,10 @@ Restart (Ctrl+R) - {0} has been restarted + The service has been restarted + + + An error occurred while restarting the service Running @@ -151,11 +154,14 @@ Started - {0} has been started + The service has been started Startup + + An error occurred while starting the service + Automatic @@ -184,7 +190,10 @@ Stopped - {0} has been stopped + The service has been stopped + + + An error occurred while stopping the service Stopping diff --git a/src/modules/launcher/PowerLauncher/PublicAPIInstance.cs b/src/modules/launcher/PowerLauncher/PublicAPIInstance.cs index 786cf44c96..52f5f2b1f6 100644 --- a/src/modules/launcher/PowerLauncher/PublicAPIInstance.cs +++ b/src/modules/launcher/PowerLauncher/PublicAPIInstance.cs @@ -84,14 +84,18 @@ namespace Wox }); } - public void ShowNotification(string text) + public void ShowNotification(string text, string secondaryText = null) { + var builder = new ToastContentBuilder().AddText(text); + + if (!string.IsNullOrWhiteSpace(secondaryText)) + { + builder.AddText(secondaryText); + } + Application.Current.Dispatcher.Invoke(() => { - ToastContent toastContent = new ToastContentBuilder() - .AddText(text) - .GetToastContent(); - var toast = new ToastNotification(toastContent.GetXml()); + var toast = new ToastNotification(builder.GetToastContent().GetXml()); DesktopNotificationManagerCompat.CreateToastNotifier().Show(toast); }); } diff --git a/src/modules/launcher/Wox.Plugin/IPublicAPI.cs b/src/modules/launcher/Wox.Plugin/IPublicAPI.cs index 13e1daa422..e7d96c6dd5 100644 --- a/src/modules/launcher/Wox.Plugin/IPublicAPI.cs +++ b/src/modules/launcher/Wox.Plugin/IPublicAPI.cs @@ -78,7 +78,8 @@ namespace Wox.Plugin /// /// Show toast notification /// - /// Notification text - void ShowNotification(string text); + /// Notification main text + /// Notification optional text + void ShowNotification(string text, string secondaryText = null); } }