Working on making multiple notification systems possible, removing the old "notifyicon" balloon for Windows 10.

This commit is contained in:
Robin Krom 2020-03-10 13:17:13 +01:00
parent d0846b0f09
commit 1ba0bf9b10
6 changed files with 189 additions and 24 deletions

View file

@ -349,12 +349,6 @@ namespace Greenshot {
// Make the notify icon available
SimpleServiceProvider.Current.AddService(notifyIcon);
// TODO: Enable check if the Windows 10 notification service is available
//if (WindowsVersion.IsBeforeWindows10)
//{
SimpleServiceProvider.Current.AddService(new NotifyIconNotificationService());
//}
// Disable access to the settings, for feature #3521446
contextmenu_settings.Visible = !_conf.DisableSettings;
@ -374,8 +368,14 @@ namespace Greenshot {
// Load all the plugins
PluginHelper.Instance.LoadPlugins();
// Check destinations, remove all that don't exist
foreach(string destination in _conf.OutputDestinations.ToArray()) {
// Check to see if there is already another INotificationService
if (SimpleServiceProvider.Current.GetInstance<INotificationService>() == null)
{
SimpleServiceProvider.Current.AddService<INotificationService>(new NotifyIconNotificationService());
}
// Check destinations, remove all that don't exist
foreach (string destination in _conf.OutputDestinations.ToArray()) {
if (DestinationHelper.GetDestination(destination) == null) {
_conf.OutputDestinations.Remove(destination);
}
@ -416,6 +416,7 @@ namespace Greenshot {
if (dataTransport != null) {
HandleDataTransport(dataTransport);
}
// Make Greenshot use less memory after startup
if (_conf.MinimizeWorkingSetSize) {
PsAPI.EmptyWorkingSet();
@ -443,7 +444,7 @@ namespace Greenshot {
break;
case CommandEnum.FirstLaunch:
LOG.Info("FirstLaunch: Created new configuration, showing balloon.");
var notifyIconClassicMessageHandler = SimpleServiceProvider.Current.GetInstance<NotifyIconNotificationService>();
var notifyIconClassicMessageHandler = SimpleServiceProvider.Current.GetInstance<INotificationService>();
notifyIconClassicMessageHandler.ShowInfoMessage(Language.GetFormattedString(LangKey.tooltip_firststart, HotkeyControl.GetLocalizedHotkeyStringFromString(_conf.RegionHotkey)), 2000, ShowSetting);
break;
case CommandEnum.ReloadConfig:
@ -497,14 +498,14 @@ namespace Greenshot {
base.WndProc(ref m);
}
/// <summary>
/// <summary>
/// Helper method to cleanly register a hotkey
/// </summary>
/// <param name="failedKeys"></param>
/// <param name="functionName"></param>
/// <param name="hotkeyString"></param>
/// <param name="handler"></param>
/// <returns></returns>
/// <param name="failedKeys">StringBuilder</param>
/// <param name="functionName">string</param>
/// <param name="hotkeyString">string</param>
/// <param name="handler">HotKeyHandler</param>
/// <returns>bool</returns>
private static bool RegisterHotkey(StringBuilder failedKeys, string functionName, string hotkeyString, HotKeyHandler handler) {
Keys modifierKeyCode = HotkeyControl.HotkeyModifiersFromString(hotkeyString);
Keys virtualKeyCode = HotkeyControl.HotkeyFromString(hotkeyString);
@ -547,8 +548,8 @@ namespace Greenshot {
/// <summary>
/// Fix icon reference
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <param name="sender">object</param>
/// <param name="e">PropertyChangedEventArgs</param>
private void OnIconSizeChanged(object sender, PropertyChangedEventArgs e) {
if (e.PropertyName == "IconSize")
{

View file

@ -546,7 +546,7 @@ namespace Greenshot.Helpers {
if (string.IsNullOrEmpty(eventArgs?.Message)) {
return;
}
var notifyIconClassicMessageHandler = SimpleServiceProvider.Current.GetInstance<NotifyIconNotificationService>();
var notifyIconClassicMessageHandler = SimpleServiceProvider.Current.GetInstance<INotificationService>();
switch (eventArgs.MessageType) {
case SurfaceMessageTyp.Error:
notifyIconClassicMessageHandler.ShowErrorMessage(eventArgs.Message, 10000);

View file

@ -29,7 +29,7 @@ namespace Greenshot.Helpers
/// <summary>
/// Notify the user with messages via the NotifyIcon
/// </summary>
public class NotifyIconNotificationService
public class NotifyIconNotificationService : INotificationService
{
private static readonly ILog Log = LogManager.GetLogger(typeof(NotifyIconNotificationService));
private readonly NotifyIcon _notifyIcon;

View file

@ -0,0 +1,55 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 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;
namespace Greenshot.Helpers
{
public interface INotificationService
{
/// <summary>
/// This will show a warning message to the user
/// </summary>
/// <param name="message">string</param>
/// <param name="timeout"></param>
/// <param name="onClickAction">Action called if the user clicks the notification</param>
/// <param name="onClosedAction">Action</param>
void ShowWarningMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null);
/// <summary>
/// This will show an error message to the user
/// </summary>
/// <param name="message">string</param>
/// <param name="timeout"></param>
/// <param name="onClickAction">Action called if the user clicks the notification</param>
/// <param name="onClosedAction">Action</param>
void ShowErrorMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null);
/// <summary>
/// This will show an info message to the user
/// </summary>
/// <param name="message">string</param>
/// <param name="timeout">int</param>
/// <param name="onClickAction">Action called if the user clicks the notification</param>
/// <param name="onClosedAction">Action</param>
void ShowInfoMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null);
}
}

View file

@ -0,0 +1,107 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 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;
using System.IO;
using System.Linq;
using Windows.UI.Notifications;
using Greenshot.Helpers;
using log4net;
namespace GreenshotWin10Plugin
{
public class ToastNotificationService : INotificationService
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ToastNotificationService));
private void ShowMessage(string message, Action onClickAction, Action onClosedAction)
{
// Get a toast XML template
var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01);
// Fill in the text elements
var stringElement = toastXml.GetElementsByTagName("text").First();
stringElement.AppendChild(toastXml.CreateTextNode(message));
// Specify the absolute path to an image
//string imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
//var imageElement = toastXml.GetElementsByTagName("image").First();
//imageElement.Attributes.GetNamedItem("src").NodeValue = imagePath;
// Create the toast and attach event listeners
var toast = new ToastNotification(toastXml);
void ToastActivatedHandler(ToastNotification toastNotification, object sender)
{
try
{
onClickAction?.Invoke();
}
catch (Exception ex)
{
Log.Warn("Exception while handling the onclick action: ", ex);
}
toast.Activated -= ToastActivatedHandler;
}
if (onClickAction != null)
{
toast.Activated += ToastActivatedHandler;
}
void ToastDismissedHandler(ToastNotification toastNotification, ToastDismissedEventArgs eventArgs)
{
Log.Debug("Toast closed");
try
{
onClosedAction?.Invoke();
}
catch (Exception ex)
{
Log.Warn("Exception while handling the onClosed action: ", ex);
}
toast.Dismissed -= ToastDismissedHandler;
// Remove the other handler too
toast.Activated -= ToastActivatedHandler;
}
toast.Dismissed += ToastDismissedHandler;
// Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(@"Greenshot").Show(toast);
}
public void ShowWarningMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null)
{
ShowMessage(message, onClickAction, onClosedAction);
}
public void ShowErrorMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null)
{
ShowMessage(message, onClickAction, onClosedAction);
}
public void ShowInfoMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null)
{
ShowMessage(message, onClickAction, onClosedAction);
}
}
}

View file

@ -20,7 +20,7 @@
*/
using System;
using System.Collections.Generic;
using Greenshot.Helpers;
using GreenshotPlugin.Core;
using GreenshotPlugin.Interfaces;
using GreenshotPlugin.Interfaces.Ocr;
@ -34,14 +34,14 @@ namespace GreenshotWin10Plugin
/// This is the Win10Plugin
/// </summary>
[Plugin("Win10", false)]
public class Win10Plugin : IGreenshotPlugin
public sealed class Win10Plugin : IGreenshotPlugin
{
public void Dispose()
{
Dispose(true);
}
protected void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (disposing)
{
@ -63,8 +63,10 @@ namespace GreenshotWin10Plugin
{
return false;
}
// Set this as IOcrProvider
SimpleServiceProvider.Current.AddService<IOcrProvider>(new Win10OcrProvider());
SimpleServiceProvider.Current.AddService<INotificationService>(new ToastNotificationService());
// Set this as IOcrProvider
SimpleServiceProvider.Current.AddService<IOcrProvider>(new Win10OcrProvider());
// Add the processor
SimpleServiceProvider.Current.AddService<IProcessor>(new Win10OcrProcessor());