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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 20 deletions

View file

@ -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
{

View file

@ -124,7 +124,16 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties {
}
/// <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>
internal static string wox_plugin_service_restarted_notification {
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>
/// Looks up a localized string similar to Automatic.
/// </summary>
@ -214,7 +232,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties {
}
/// <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>
internal static string wox_plugin_service_started_notification {
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>
/// Looks up a localized string similar to Stopping.
/// </summary>
@ -268,7 +295,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Properties {
}
/// <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>
internal static string wox_plugin_service_stopped_notification {
get {

View file

@ -139,7 +139,10 @@
<value>Restart (Ctrl+R)</value>
</data>
<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 name="wox_plugin_service_running" xml:space="preserve">
<value>Running</value>
@ -151,11 +154,14 @@
<value>Started</value>
</data>
<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 name="wox_plugin_service_startup" xml:space="preserve">
<value>Startup</value>
</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">
<value>Automatic</value>
</data>
@ -184,7 +190,10 @@
<value>Stopped</value>
</data>
<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 name="wox_plugin_service_stop_pending" xml:space="preserve">
<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(() =>
{
ToastContent toastContent = new ToastContentBuilder()
.AddText(text)
.GetToastContent();
var toast = new ToastNotification(toastContent.GetXml());
var toast = new ToastNotification(builder.GetToastContent().GetXml());
DesktopNotificationManagerCompat.CreateToastNotifier().Show(toast);
});
}

View file

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