diff --git a/Greenshot/Forms/MainForm.cs b/Greenshot/Forms/MainForm.cs index 917553305..c880aa209 100644 --- a/Greenshot/Forms/MainForm.cs +++ b/Greenshot/Forms/MainForm.cs @@ -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() == null) + { + SimpleServiceProvider.Current.AddService(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(); + var notifyIconClassicMessageHandler = SimpleServiceProvider.Current.GetInstance(); 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); } - /// + /// /// Helper method to cleanly register a hotkey /// - /// - /// - /// - /// - /// + /// StringBuilder + /// string + /// string + /// HotKeyHandler + /// bool 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 { /// /// Fix icon reference /// - /// - /// + /// object + /// PropertyChangedEventArgs private void OnIconSizeChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "IconSize") { diff --git a/Greenshot/Helpers/CaptureHelper.cs b/Greenshot/Helpers/CaptureHelper.cs index 761b2ec11..0007727ef 100644 --- a/Greenshot/Helpers/CaptureHelper.cs +++ b/Greenshot/Helpers/CaptureHelper.cs @@ -546,7 +546,7 @@ namespace Greenshot.Helpers { if (string.IsNullOrEmpty(eventArgs?.Message)) { return; } - var notifyIconClassicMessageHandler = SimpleServiceProvider.Current.GetInstance(); + var notifyIconClassicMessageHandler = SimpleServiceProvider.Current.GetInstance(); switch (eventArgs.MessageType) { case SurfaceMessageTyp.Error: notifyIconClassicMessageHandler.ShowErrorMessage(eventArgs.Message, 10000); diff --git a/Greenshot/Helpers/NotifyIconNotificationService.cs b/Greenshot/Helpers/NotifyIconNotificationService.cs index bba6b1517..e69464f84 100644 --- a/Greenshot/Helpers/NotifyIconNotificationService.cs +++ b/Greenshot/Helpers/NotifyIconNotificationService.cs @@ -29,7 +29,7 @@ namespace Greenshot.Helpers /// /// Notify the user with messages via the NotifyIcon /// - public class NotifyIconNotificationService + public class NotifyIconNotificationService : INotificationService { private static readonly ILog Log = LogManager.GetLogger(typeof(NotifyIconNotificationService)); private readonly NotifyIcon _notifyIcon; diff --git a/GreenshotPlugin/Interfaces/INotificationService.cs b/GreenshotPlugin/Interfaces/INotificationService.cs new file mode 100644 index 000000000..a6605e588 --- /dev/null +++ b/GreenshotPlugin/Interfaces/INotificationService.cs @@ -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 . + */ + +using System; + +namespace Greenshot.Helpers +{ + public interface INotificationService + { + /// + /// This will show a warning message to the user + /// + /// string + /// + /// Action called if the user clicks the notification + /// Action + void ShowWarningMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null); + + /// + /// This will show an error message to the user + /// + /// string + /// + /// Action called if the user clicks the notification + /// Action + void ShowErrorMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null); + + /// + /// This will show an info message to the user + /// + /// string + /// int + /// Action called if the user clicks the notification + /// Action + void ShowInfoMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null); + } +} \ No newline at end of file diff --git a/GreenshotWin10Plugin/ToastNotificationService.cs b/GreenshotWin10Plugin/ToastNotificationService.cs new file mode 100644 index 000000000..e21bf0389 --- /dev/null +++ b/GreenshotWin10Plugin/ToastNotificationService.cs @@ -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 . + */ + +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); + } + } +} diff --git a/GreenshotWin10Plugin/Win10Plugin.cs b/GreenshotWin10Plugin/Win10Plugin.cs index f760c9a52..d21fa269e 100644 --- a/GreenshotWin10Plugin/Win10Plugin.cs +++ b/GreenshotWin10Plugin/Win10Plugin.cs @@ -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 /// [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(new Win10OcrProvider()); + + SimpleServiceProvider.Current.AddService(new ToastNotificationService()); + // Set this as IOcrProvider + SimpleServiceProvider.Current.AddService(new Win10OcrProvider()); // Add the processor SimpleServiceProvider.Current.AddService(new Win10OcrProcessor());