PT Run Service notification improvements (#9772)

This commit is contained in:
Davide Giacometti
2021-02-19 18:17:52 +01:00
committed by GitHub
parent a29b3aa500
commit ff4a78a7f9
5 changed files with 80 additions and 20 deletions

View File

@@ -6,7 +6,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.ServiceProcess; using System.ServiceProcess;
@@ -75,11 +74,11 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Helpers
if (exitCode == 0) if (exitCode == 0)
{ {
contextAPI.ShowNotification(GetLocalizedMessage(serviceResult, action)); contextAPI.ShowNotification(GetLocalizedMessage(action), serviceResult.DisplayName);
} }
else else
{ {
contextAPI.ShowNotification("An error occurred"); contextAPI.ShowNotification(GetLocalizedErrorMessage(action), serviceResult.DisplayName);
Log.Error($"The command returned {exitCode}", MethodBase.GetCurrentMethod().DeclaringType); 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) 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) 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) 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 else
{ {

View File

@@ -124,7 +124,16 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties {
} }
/// <summary> /// <summary>
/// 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.
/// </summary>
internal static string wox_plugin_service_restart_error_notification {
get {
return ResourceManager.GetString("wox_plugin_service_restart_error_notification", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The service has been restarted.
/// </summary> /// </summary>
internal static string wox_plugin_service_restarted_notification { internal static string wox_plugin_service_restarted_notification {
get { get {
@@ -150,6 +159,15 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties {
} }
} }
/// <summary>
/// Looks up a localized string similar to An error occurred while starting the service.
/// </summary>
internal static string wox_plugin_service_start_error_notification {
get {
return ResourceManager.GetString("wox_plugin_service_start_error_notification", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Automatic. /// Looks up a localized string similar to Automatic.
/// </summary> /// </summary>
@@ -214,7 +232,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties {
} }
/// <summary> /// <summary>
/// Looks up a localized string similar to {0} has been started. /// Looks up a localized string similar to The service has been started.
/// </summary> /// </summary>
internal static string wox_plugin_service_started_notification { internal static string wox_plugin_service_started_notification {
get { get {
@@ -249,6 +267,15 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties {
} }
} }
/// <summary>
/// Looks up a localized string similar to An error occurred while stopping the service.
/// </summary>
internal static string wox_plugin_service_stop_error_notification {
get {
return ResourceManager.GetString("wox_plugin_service_stop_error_notification", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Stopping. /// Looks up a localized string similar to Stopping.
/// </summary> /// </summary>
@@ -268,7 +295,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties {
} }
/// <summary> /// <summary>
/// Looks up a localized string similar to {0} has been stopped. /// Looks up a localized string similar to The service has been stopped.
/// </summary> /// </summary>
internal static string wox_plugin_service_stopped_notification { internal static string wox_plugin_service_stopped_notification {
get { get {

View File

@@ -139,7 +139,10 @@
<value>Restart (Ctrl+R)</value> <value>Restart (Ctrl+R)</value>
</data> </data>
<data name="wox_plugin_service_restarted_notification" xml:space="preserve"> <data name="wox_plugin_service_restarted_notification" xml:space="preserve">
<value>{0} has been restarted</value> <value>The service has been restarted</value>
</data>
<data name="wox_plugin_service_restart_error_notification" xml:space="preserve">
<value>An error occurred while restarting the service</value>
</data> </data>
<data name="wox_plugin_service_running" xml:space="preserve"> <data name="wox_plugin_service_running" xml:space="preserve">
<value>Running</value> <value>Running</value>
@@ -151,11 +154,14 @@
<value>Started</value> <value>Started</value>
</data> </data>
<data name="wox_plugin_service_started_notification" xml:space="preserve"> <data name="wox_plugin_service_started_notification" xml:space="preserve">
<value>{0} has been started</value> <value>The service has been started</value>
</data> </data>
<data name="wox_plugin_service_startup" xml:space="preserve"> <data name="wox_plugin_service_startup" xml:space="preserve">
<value>Startup</value> <value>Startup</value>
</data> </data>
<data name="wox_plugin_service_start_error_notification" xml:space="preserve">
<value>An error occurred while starting the service</value>
</data>
<data name="wox_plugin_service_start_mode_automatic" xml:space="preserve"> <data name="wox_plugin_service_start_mode_automatic" xml:space="preserve">
<value>Automatic</value> <value>Automatic</value>
</data> </data>
@@ -184,7 +190,10 @@
<value>Stopped</value> <value>Stopped</value>
</data> </data>
<data name="wox_plugin_service_stopped_notification" xml:space="preserve"> <data name="wox_plugin_service_stopped_notification" xml:space="preserve">
<value>{0} has been stopped</value> <value>The service has been stopped</value>
</data>
<data name="wox_plugin_service_stop_error_notification" xml:space="preserve">
<value>An error occurred while stopping the service</value>
</data> </data>
<data name="wox_plugin_service_stop_pending" xml:space="preserve"> <data name="wox_plugin_service_stop_pending" xml:space="preserve">
<value>Stopping</value> <value>Stopping</value>

View File

@@ -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(() => Application.Current.Dispatcher.Invoke(() =>
{ {
ToastContent toastContent = new ToastContentBuilder() var toast = new ToastNotification(builder.GetToastContent().GetXml());
.AddText(text)
.GetToastContent();
var toast = new ToastNotification(toastContent.GetXml());
DesktopNotificationManagerCompat.CreateToastNotifier().Show(toast); DesktopNotificationManagerCompat.CreateToastNotifier().Show(toast);
}); });
} }

View File

@@ -78,7 +78,8 @@ namespace Wox.Plugin
/// <summary> /// <summary>
/// Show toast notification /// Show toast notification
/// </summary> /// </summary>
/// <param name="text">Notification text</param> /// <param name="text">Notification main text</param>
void ShowNotification(string text); /// <param name="secondaryText">Notification optional text</param>
void ShowNotification(string text, string secondaryText = null);
} }
} }