mirror of
https://github.com/greenshot/greenshot
synced 2025-07-16 10:03:44 -07:00
Improving stability for the notification issue reported in #182
This commit is contained in:
parent
3055d42689
commit
87f3b6a871
3 changed files with 204 additions and 177 deletions
|
@ -43,6 +43,10 @@ namespace GreenshotPlugin.Core
|
||||||
|
|
||||||
foreach (var service in services)
|
foreach (var service in services)
|
||||||
{
|
{
|
||||||
|
if (service == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
currentServices.Add(service);
|
currentServices.Add(service);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,176 +1,199 @@
|
||||||
/*
|
/*
|
||||||
* Greenshot - a free and open source screenshot tool
|
* Greenshot - a free and open source screenshot tool
|
||||||
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
|
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
|
||||||
*
|
*
|
||||||
* For more information see: http://getgreenshot.org/
|
* For more information see: http://getgreenshot.org/
|
||||||
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
|
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 1 of the License, or
|
* the Free Software Foundation, either version 1 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Drawing.Imaging;
|
using System.Drawing.Imaging;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Windows.UI.Notifications;
|
using Windows.Foundation.Metadata;
|
||||||
using GreenshotPlugin.Core;
|
using Windows.UI.Notifications;
|
||||||
using GreenshotPlugin.IniFile;
|
using GreenshotPlugin.Core;
|
||||||
using GreenshotPlugin.Interfaces;
|
using GreenshotPlugin.IniFile;
|
||||||
using GreenshotWin10Plugin.Native;
|
using GreenshotPlugin.Interfaces;
|
||||||
using log4net;
|
using GreenshotWin10Plugin.Native;
|
||||||
|
using log4net;
|
||||||
namespace GreenshotWin10Plugin
|
|
||||||
{
|
namespace GreenshotWin10Plugin
|
||||||
/// <summary>
|
{
|
||||||
/// This service provides a way to inform (notify) the user.
|
/// <summary>
|
||||||
/// </summary>
|
/// This service provides a way to inform (notify) the user.
|
||||||
public class ToastNotificationService : INotificationService
|
/// </summary>
|
||||||
{
|
public class ToastNotificationService : INotificationService
|
||||||
private static readonly ILog Log = LogManager.GetLogger(typeof(ToastNotificationService));
|
{
|
||||||
private static readonly CoreConfiguration CoreConfiguration = IniConfig.GetIniSection<CoreConfiguration>();
|
private static readonly ILog Log = LogManager.GetLogger(typeof(ToastNotificationService));
|
||||||
|
private static readonly CoreConfiguration CoreConfiguration = IniConfig.GetIniSection<CoreConfiguration>();
|
||||||
private readonly string _imageFilePath;
|
|
||||||
public ToastNotificationService()
|
private readonly string _imageFilePath;
|
||||||
{
|
public ToastNotificationService()
|
||||||
// Register AUMID and COM server (for Desktop Bridge apps, this no-ops)
|
{
|
||||||
DesktopNotificationManagerCompat.RegisterAumidAndComServer<GreenshotNotificationActivator>("Greenshot");
|
// Register AUMID and COM server (for Desktop Bridge apps, this no-ops)
|
||||||
// Register COM server and activator type
|
DesktopNotificationManagerCompat.RegisterAumidAndComServer<GreenshotNotificationActivator>("Greenshot");
|
||||||
DesktopNotificationManagerCompat.RegisterActivator<GreenshotNotificationActivator>();
|
// Register COM server and activator type
|
||||||
|
DesktopNotificationManagerCompat.RegisterActivator<GreenshotNotificationActivator>();
|
||||||
var localAppData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Greenshot");
|
|
||||||
if (!Directory.Exists(localAppData))
|
var localAppData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Greenshot");
|
||||||
{
|
if (!Directory.Exists(localAppData))
|
||||||
Directory.CreateDirectory(localAppData);
|
{
|
||||||
}
|
Directory.CreateDirectory(localAppData);
|
||||||
_imageFilePath = Path.Combine(localAppData, "greenshot.png");
|
}
|
||||||
|
_imageFilePath = Path.Combine(localAppData, "greenshot.png");
|
||||||
if (File.Exists(_imageFilePath))
|
|
||||||
{
|
if (File.Exists(_imageFilePath))
|
||||||
return;
|
{
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
using var greenshotImage = GreenshotResources.GetGreenshotIcon().ToBitmap();
|
|
||||||
greenshotImage.Save(_imageFilePath, ImageFormat.Png);
|
using var greenshotImage = GreenshotResources.GetGreenshotIcon().ToBitmap();
|
||||||
}
|
greenshotImage.Save(_imageFilePath, ImageFormat.Png);
|
||||||
|
}
|
||||||
/// <summary>
|
|
||||||
/// This creates the actual toast
|
/// <summary>
|
||||||
/// </summary>
|
/// This creates the actual toast
|
||||||
/// <param name="message">string</param>
|
/// </summary>
|
||||||
/// <param name="timeout">milliseconds until the toast timeouts</param>
|
/// <param name="message">string</param>
|
||||||
/// <param name="onClickAction">Action called when clicked</param>
|
/// <param name="timeout">milliseconds until the toast timeouts</param>
|
||||||
/// <param name="onClosedAction">Action called when the toast is closed</param>
|
/// <param name="onClickAction">Action called when clicked</param>
|
||||||
private void ShowMessage(string message, int timeout, Action onClickAction, Action onClosedAction)
|
/// <param name="onClosedAction">Action called when the toast is closed</param>
|
||||||
{
|
private void ShowMessage(string message, int timeout, Action onClickAction, Action onClosedAction)
|
||||||
// Do not inform the user if this is disabled
|
{
|
||||||
if (!CoreConfiguration.ShowTrayNotification)
|
// Do not inform the user if this is disabled
|
||||||
{
|
if (!CoreConfiguration.ShowTrayNotification)
|
||||||
return;
|
{
|
||||||
}
|
return;
|
||||||
// Prepare the toast notifier. Be sure to specify the AppUserModelId on your application's shortcut!
|
}
|
||||||
var toastNotifier = DesktopNotificationManagerCompat.CreateToastNotifier();
|
// Prepare the toast notifier. Be sure to specify the AppUserModelId on your application's shortcut!
|
||||||
if (toastNotifier.Setting != NotificationSetting.Enabled)
|
var toastNotifier = DesktopNotificationManagerCompat.CreateToastNotifier();
|
||||||
{
|
if (toastNotifier.Setting != NotificationSetting.Enabled)
|
||||||
Log.DebugFormat("Ignored toast due to {0}", toastNotifier.Setting);
|
{
|
||||||
return;
|
Log.DebugFormat("Ignored toast due to {0}", toastNotifier.Setting);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
// Get a toast XML template
|
|
||||||
var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01);
|
// Get a toast XML template
|
||||||
|
var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01);
|
||||||
// Fill in the text elements
|
|
||||||
var stringElement = toastXml.GetElementsByTagName("text").First();
|
// Fill in the text elements
|
||||||
stringElement.AppendChild(toastXml.CreateTextNode(message));
|
var stringElement = toastXml.GetElementsByTagName("text").First();
|
||||||
|
stringElement.AppendChild(toastXml.CreateTextNode(message));
|
||||||
if (_imageFilePath != null && File.Exists(_imageFilePath))
|
|
||||||
{
|
if (_imageFilePath != null && File.Exists(_imageFilePath))
|
||||||
// Specify the absolute path to an image
|
{
|
||||||
var imageElement = toastXml.GetElementsByTagName("image").First();
|
// Specify the absolute path to an image
|
||||||
var imageSrcNode = imageElement.Attributes.GetNamedItem("src");
|
var imageElement = toastXml.GetElementsByTagName("image").First();
|
||||||
if (imageSrcNode != null)
|
var imageSrcNode = imageElement.Attributes.GetNamedItem("src");
|
||||||
{
|
if (imageSrcNode != null)
|
||||||
imageSrcNode.NodeValue = _imageFilePath;
|
{
|
||||||
}
|
imageSrcNode.NodeValue = _imageFilePath;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Create the toast and attach event listeners
|
|
||||||
var toast = new ToastNotification(toastXml)
|
// Create the toast and attach event listeners
|
||||||
{
|
var toast = new ToastNotification(toastXml)
|
||||||
// Windows 10 first with 1903: ExpiresOnReboot = true,
|
{
|
||||||
ExpirationTime = timeout > 0 ? DateTimeOffset.Now.AddMilliseconds(timeout) : (DateTimeOffset?)null
|
// Windows 10 first with 1903: ExpiresOnReboot = true,
|
||||||
};
|
ExpirationTime = timeout > 0 ? DateTimeOffset.Now.AddMilliseconds(timeout) : (DateTimeOffset?)null
|
||||||
|
};
|
||||||
void ToastActivatedHandler(ToastNotification toastNotification, object sender)
|
|
||||||
{
|
void ToastActivatedHandler(ToastNotification toastNotification, object sender)
|
||||||
try
|
{
|
||||||
{
|
try
|
||||||
onClickAction?.Invoke();
|
{
|
||||||
}
|
onClickAction?.Invoke();
|
||||||
catch (Exception ex)
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
Log.Warn("Exception while handling the onclick action: ", ex);
|
{
|
||||||
}
|
Log.Warn("Exception while handling the onclick action: ", ex);
|
||||||
|
}
|
||||||
toast.Activated -= ToastActivatedHandler;
|
|
||||||
}
|
toast.Activated -= ToastActivatedHandler;
|
||||||
|
}
|
||||||
if (onClickAction != null)
|
|
||||||
{
|
if (onClickAction != null)
|
||||||
toast.Activated += ToastActivatedHandler;
|
{
|
||||||
}
|
toast.Activated += ToastActivatedHandler;
|
||||||
|
}
|
||||||
void ToastDismissedHandler(ToastNotification toastNotification, ToastDismissedEventArgs eventArgs)
|
|
||||||
{
|
void ToastDismissedHandler(ToastNotification toastNotification, ToastDismissedEventArgs eventArgs)
|
||||||
Log.Debug("Toast closed");
|
{
|
||||||
try
|
Log.Debug("Toast closed");
|
||||||
{
|
try
|
||||||
onClosedAction?.Invoke();
|
{
|
||||||
}
|
onClosedAction?.Invoke();
|
||||||
catch (Exception ex)
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
Log.Warn("Exception while handling the onClosed action: ", ex);
|
{
|
||||||
}
|
Log.Warn("Exception while handling the onClosed action: ", ex);
|
||||||
|
}
|
||||||
toast.Dismissed -= ToastDismissedHandler;
|
|
||||||
// Remove the other handler too
|
toast.Dismissed -= ToastDismissedHandler;
|
||||||
toast.Activated -= ToastActivatedHandler;
|
// Remove the other handler too
|
||||||
toast.Failed -= ToastOnFailed;
|
toast.Activated -= ToastActivatedHandler;
|
||||||
}
|
toast.Failed -= ToastOnFailed;
|
||||||
toast.Dismissed += ToastDismissedHandler;
|
}
|
||||||
toast.Failed += ToastOnFailed;
|
toast.Dismissed += ToastDismissedHandler;
|
||||||
toastNotifier.Show(toast);
|
toast.Failed += ToastOnFailed;
|
||||||
}
|
try
|
||||||
|
{
|
||||||
private void ToastOnFailed(ToastNotification sender, ToastFailedEventArgs args)
|
toastNotifier.Show(toast);
|
||||||
{
|
}
|
||||||
Log.WarnFormat("Failed to display a toast due to {0}", args.ErrorCode);
|
catch (Exception ex)
|
||||||
Log.Debug(sender.Content.GetXml());
|
{
|
||||||
}
|
Log.Error("Couldn't show notification.", ex);
|
||||||
|
}
|
||||||
public void ShowWarningMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null)
|
}
|
||||||
{
|
|
||||||
ShowMessage(message, timeout, onClickAction, onClosedAction);
|
private void ToastOnFailed(ToastNotification sender, ToastFailedEventArgs args)
|
||||||
}
|
{
|
||||||
|
Log.WarnFormat("Failed to display a toast due to {0}", args.ErrorCode);
|
||||||
public void ShowErrorMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null)
|
Log.Debug(sender.Content.GetXml());
|
||||||
{
|
}
|
||||||
ShowMessage(message, timeout, onClickAction, onClosedAction);
|
|
||||||
}
|
public void ShowWarningMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null)
|
||||||
|
{
|
||||||
public void ShowInfoMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null)
|
ShowMessage(message, timeout, onClickAction, onClosedAction);
|
||||||
{
|
}
|
||||||
ShowMessage(message, timeout, onClickAction, onClosedAction);
|
|
||||||
}
|
public void ShowErrorMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null)
|
||||||
}
|
{
|
||||||
}
|
ShowMessage(message, timeout, onClickAction, onClosedAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ShowInfoMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null)
|
||||||
|
{
|
||||||
|
ShowMessage(message, timeout, onClickAction, onClosedAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Factory method, helping with checking if the notification service is even available
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ToastNotificationService</returns>
|
||||||
|
public static ToastNotificationService Create()
|
||||||
|
{
|
||||||
|
if (ApiInformation.IsTypePresent("Windows.ApplicationModel.Background.ToastNotificationActionTrigger"))
|
||||||
|
{
|
||||||
|
return new ToastNotificationService();
|
||||||
|
}
|
||||||
|
Log.Warn("ToastNotificationActionTrigger not available.");
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ namespace GreenshotWin10Plugin
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
SimpleServiceProvider.Current.AddService<INotificationService>(new ToastNotificationService());
|
SimpleServiceProvider.Current.AddService<INotificationService>(ToastNotificationService.Create());
|
||||||
// Set this as IOcrProvider
|
// Set this as IOcrProvider
|
||||||
SimpleServiceProvider.Current.AddService<IOcrProvider>(new Win10OcrProvider());
|
SimpleServiceProvider.Current.AddService<IOcrProvider>(new Win10OcrProvider());
|
||||||
// Add the processor
|
// Add the processor
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue