Updated Microsoft.Toolkit.Uwp.Notifications which brings a lot more to Greenshot [skip ci]

This commit is contained in:
Robin Krom 2021-03-19 22:53:19 +01:00
commit c303c07320
No known key found for this signature in database
GPG key ID: BCC01364F1371490
3 changed files with 67 additions and 121 deletions

View file

@ -12,7 +12,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" version="6.1.1" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GreenshotPlugin\GreenshotPlugin.csproj" />

View file

@ -1,44 +0,0 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
*
* 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
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Runtime.InteropServices;
using log4net;
using Microsoft.Toolkit.Uwp.Notifications;
namespace GreenshotWin10Plugin.Native
{
/// <summary>
/// This implements the NotificationActivator
/// </summary>
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(NotificationActivator.INotificationActivationCallback))]
[Guid("F48E86D3-E34C-4DB7-8F8F-9A0EA55F0D08"), ComVisible(true)]
public class GreenshotNotificationActivator : NotificationActivator
{
private static readonly ILog Log = LogManager.GetLogger(typeof(GreenshotNotificationActivator));
public override void OnActivated(string invokedArgs, NotificationUserInput userInput, string appUserModelId)
{
// TODO: Handle activation
Log.Info("Activated");
}
}
}

View file

@ -22,13 +22,12 @@
using System;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using Windows.Foundation.Collections;
using Windows.Foundation.Metadata;
using Windows.UI.Notifications;
using GreenshotPlugin.Core;
using GreenshotPlugin.IniFile;
using GreenshotPlugin.Interfaces;
using GreenshotWin10Plugin.Native;
using log4net;
using Microsoft.Toolkit.Uwp.Notifications;
@ -45,10 +44,21 @@ namespace GreenshotWin10Plugin
private readonly string _imageFilePath;
public ToastNotificationService()
{
// Register AUMID and COM server (for Desktop Bridge apps, this no-ops)
DesktopNotificationManagerCompat.RegisterAumidAndComServer<GreenshotNotificationActivator>("Greenshot");
// Register COM server and activator type
DesktopNotificationManagerCompat.RegisterActivator<GreenshotNotificationActivator>();
if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
{
Log.Info("Greenshot was activated due to a toast.");
}
// Listen to notification activation
ToastNotificationManagerCompat.OnActivated += toastArgs =>
{
// Obtain the arguments from the notification
ToastArguments args = ToastArguments.Parse(toastArgs.Argument);
// Obtain any user input (text boxes, menu selections) from the notification
ValueSet userInput = toastArgs.UserInput;
Log.Info("Toast activated. Args: " + toastArgs.Argument);
};
var localAppData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Greenshot");
if (!Directory.Exists(localAppData))
@ -81,7 +91,7 @@ namespace GreenshotWin10Plugin
return;
}
// Prepare the toast notifier. Be sure to specify the AppUserModelId on your application's shortcut!
var toastNotifier = DesktopNotificationManagerCompat.CreateToastNotifier();
var toastNotifier = ToastNotificationManagerCompat.CreateToastNotifier();
// Here is an interesting article on reading the settings: https://www.rudyhuyn.com/blog/2018/02/10/toastnotifier-and-settings-careful-with-non-uwp-applications/
try
@ -97,31 +107,18 @@ namespace GreenshotWin10Plugin
Log.Info("Ignoring exception as this means that there was no stored settings.");
}
// Get a toast XML template
var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01);
// Generate the toast and send it off
new ToastContentBuilder()
// Fill in the text elements
var stringElement = toastXml.GetElementsByTagName("text").First();
stringElement.AppendChild(toastXml.CreateTextNode(message));
if (_imageFilePath != null && File.Exists(_imageFilePath))
.AddArgument("ToastID", 100)
// Inline image
.AddText(message)
// Profile (app logo override) image
//.AddAppLogoOverride(new Uri($@"file://{_imageFilePath}"), ToastGenericAppLogoCrop.None)
.Show(toast =>
{
// Specify the absolute path to an image
var imageElement = toastXml.GetElementsByTagName("image").First();
var imageSrcNode = imageElement.Attributes.GetNamedItem("src");
if (imageSrcNode != null)
{
imageSrcNode.NodeValue = _imageFilePath;
}
}
// Create the toast and attach event listeners
var toast = new ToastNotification(toastXml)
{
// Windows 10 first with 1903: ExpiresOnReboot = true,
ExpirationTime = timeout.HasValue ? DateTimeOffset.Now.Add(timeout.Value) : (DateTimeOffset?)null
};
// Windows 10 first with 1903: ExpiresOnReboot = true
toast.ExpirationTime = timeout.HasValue ? DateTimeOffset.Now.Add(timeout.Value) : (DateTimeOffset?) null;
void ToastActivatedHandler(ToastNotification toastNotification, object sender)
{
try
@ -165,14 +162,7 @@ namespace GreenshotWin10Plugin
}
toast.Dismissed += ToastDismissedHandler;
toast.Failed += ToastOnFailed;
try
{
toastNotifier.Show(toast);
}
catch (Exception ex)
{
Log.Error("Couldn't show notification.", ex);
}
});
}
private void ToastOnFailed(ToastNotification sender, ToastFailedEventArgs args)