Skip to content

Commit

Permalink
PT Run Service notification improvements (#9772)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegiacometti committed Feb 19, 2021
1 parent a29b3aa commit ff4a78a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
Expand Down Expand Up @@ -75,11 +74,11 @@ public static void ChangeStatus(ServiceResult serviceResult, Action action, IPub

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);
}
}
Expand Down Expand Up @@ -192,19 +191,39 @@ private static string GetLocalizedStartType(ServiceStartMode startMode)
}
}

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
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
14 changes: 9 additions & 5 deletions src/modules/launcher/PowerLauncher/PublicAPIInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,18 @@ public void ShowMsg(string title, string subTitle = "", string iconPath = "", bo
});
}

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);
});
}
Expand Down
5 changes: 3 additions & 2 deletions src/modules/launcher/Wox.Plugin/IPublicAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public interface IPublicAPI
/// <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);
}
}

0 comments on commit ff4a78a

Please sign in to comment.