First step in moving towards replaceable notification messages, extracted the logic to do so into it's own module. [skip ci]

This commit is contained in:
Robin Krom 2020-03-09 21:50:37 +01:00
parent 87b7905457
commit d0846b0f09
4 changed files with 168 additions and 65 deletions

View file

@ -46,7 +46,6 @@ using System.Threading.Tasks;
using GreenshotPlugin.IniFile;
using GreenshotPlugin.Interfaces;
using GreenshotPlugin.Interfaces.Plugin;
using GreenshotPlugin.UnmanagedHelpers.Enums;
namespace Greenshot {
/// <summary>
@ -350,6 +349,12 @@ 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;
@ -428,19 +433,6 @@ namespace Greenshot {
HandleDataTransport(dataTransport);
}
private void BalloonTipClicked(object sender, EventArgs e) {
try {
ShowSetting();
} finally {
BalloonTipClosed(sender, e);
}
}
private void BalloonTipClosed(object sender, EventArgs e) {
notifyIcon.BalloonTipClicked -= BalloonTipClicked;
notifyIcon.BalloonTipClosed -= BalloonTipClosed;
}
private void HandleDataTransport(CopyDataTransport dataTransport) {
foreach(KeyValuePair<CommandEnum, string> command in dataTransport.Commands) {
LOG.Debug("Data received, Command = " + command.Key + ", Data: " + command.Value);
@ -451,13 +443,8 @@ namespace Greenshot {
break;
case CommandEnum.FirstLaunch:
LOG.Info("FirstLaunch: Created new configuration, showing balloon.");
try {
notifyIcon.BalloonTipClicked += BalloonTipClicked;
notifyIcon.BalloonTipClosed += BalloonTipClosed;
notifyIcon.ShowBalloonTip(2000, "Greenshot", Language.GetFormattedString(LangKey.tooltip_firststart, HotkeyControl.GetLocalizedHotkeyStringFromString(_conf.RegionHotkey)), ToolTipIcon.Info);
} catch (Exception ex) {
LOG.Warn("Exception while showing first launch: ", ex);
}
var notifyIconClassicMessageHandler = SimpleServiceProvider.Current.GetInstance<NotifyIconNotificationService>();
notifyIconClassicMessageHandler.ShowInfoMessage(Language.GetFormattedString(LangKey.tooltip_firststart, HotkeyControl.GetLocalizedHotkeyStringFromString(_conf.RegionHotkey)), 2000, ShowSetting);
break;
case CommandEnum.ReloadConfig:
LOG.Info("Reload requested");

View file

@ -514,13 +514,11 @@ namespace Greenshot.Helpers {
/// <summary>
/// If a balloon tip is show for a taken capture, this handles the click on it
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OpenCaptureOnClick(object sender, EventArgs e) {
/// <param name="e">SurfaceMessageEventArgs</param>
private void OpenCaptureOnClick(SurfaceMessageEventArgs e) {
var notifyIcon = SimpleServiceProvider.Current.GetInstance<NotifyIcon>();
if (!(notifyIcon.Tag is SurfaceMessageEventArgs eventArgs)) {
Log.Warn("OpenCaptureOnClick called without SurfaceMessageEventArgs");
RemoveEventHandler(sender, e);
return;
}
ISurface surface = eventArgs.Surface;
@ -537,14 +535,6 @@ namespace Greenshot.Helpers {
}
}
Log.DebugFormat("Deregistering the BalloonTipClicked");
RemoveEventHandler(sender, e);
}
private void RemoveEventHandler(object sender, EventArgs e) {
var notifyIcon = SimpleServiceProvider.Current.GetInstance<NotifyIcon>();
notifyIcon.BalloonTipClicked -= OpenCaptureOnClick;
notifyIcon.BalloonTipClosed -= RemoveEventHandler;
notifyIcon.Tag = null;
}
/// <summary>
@ -556,27 +546,22 @@ namespace Greenshot.Helpers {
if (string.IsNullOrEmpty(eventArgs?.Message)) {
return;
}
var notifyIcon = SimpleServiceProvider.Current.GetInstance<NotifyIcon>();
var notifyIconClassicMessageHandler = SimpleServiceProvider.Current.GetInstance<NotifyIconNotificationService>();
switch (eventArgs.MessageType) {
case SurfaceMessageTyp.Error:
notifyIcon.ShowBalloonTip(10000, "Greenshot", eventArgs.Message, ToolTipIcon.Error);
notifyIconClassicMessageHandler.ShowErrorMessage(eventArgs.Message, 10000);
break;
case SurfaceMessageTyp.Info:
notifyIcon.ShowBalloonTip(10000, "Greenshot", eventArgs.Message, ToolTipIcon.Info);
notifyIconClassicMessageHandler.ShowInfoMessage(eventArgs.Message, 10000);
break;
case SurfaceMessageTyp.FileSaved:
case SurfaceMessageTyp.UploadedUri:
// Show a balloon and register an event handler to open the "capture" for if someone clicks the balloon.
notifyIcon.BalloonTipClicked += OpenCaptureOnClick;
notifyIcon.BalloonTipClosed += RemoveEventHandler;
// Store for later usage
notifyIcon.Tag = eventArgs;
notifyIcon.ShowBalloonTip(10000, "Greenshot", eventArgs.Message, ToolTipIcon.Info);
notifyIconClassicMessageHandler.ShowInfoMessage(eventArgs.Message, 10000, () => OpenCaptureOnClick(eventArgs));
break;
}
}
private void HandleCapture() {
// Flag to see if the image was "exported" so the FileEditor doesn't
// ask to save the file as long as nothing is done.

View file

@ -0,0 +1,125 @@
/*
* 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.Windows.Forms;
using GreenshotPlugin.Core;
using log4net;
namespace Greenshot.Helpers
{
/// <summary>
/// Notify the user with messages via the NotifyIcon
/// </summary>
public class NotifyIconNotificationService
{
private static readonly ILog Log = LogManager.GetLogger(typeof(NotifyIconNotificationService));
private readonly NotifyIcon _notifyIcon;
public NotifyIconNotificationService()
{
_notifyIcon = SimpleServiceProvider.Current.GetInstance<NotifyIcon>();
}
/// <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>
public void ShowWarningMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null)
{
ShowMessage(message, timeout, ToolTipIcon.Warning, onClickAction, onClosedAction);
}
/// <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>
public void ShowErrorMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null)
{
ShowMessage(message, timeout, ToolTipIcon.Error, onClickAction, onClosedAction);
}
/// <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>
public void ShowInfoMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null)
{
ShowMessage(message, timeout, ToolTipIcon.Info, onClickAction, onClosedAction);
}
/// <summary>
/// This will show a message to the user
/// </summary>
/// <param name="message">string</param>
/// <param name="timeout">int</param>
/// <param name="level">ToolTipIcon</param>
/// <param name="onClickAction">Action</param>
/// <param name="onClosedAction">Action</param>
public void ShowMessage(string message, int timeout, ToolTipIcon level, Action onClickAction = null, Action onClosedAction = null) {
void BalloonClickedHandler(object s, EventArgs e)
{
try
{
onClickAction?.Invoke();
}
catch (Exception ex)
{
Log.Warn("Exception while handling the onclick action: ", ex);
}
_notifyIcon.BalloonTipClicked -= BalloonClickedHandler;
}
if (onClickAction != null)
{
_notifyIcon.BalloonTipClicked += BalloonClickedHandler;
}
void BalloonClosedHandler(object s, EventArgs e)
{
try
{
onClosedAction?.Invoke();
}
catch (Exception ex)
{
Log.Warn("Exception while handling the onClosed action: ", ex);
}
_notifyIcon.BalloonTipClosed -= BalloonClosedHandler;
// Remove the other handler too
_notifyIcon.BalloonTipClicked -= BalloonClickedHandler;
}
_notifyIcon.BalloonTipClosed += BalloonClosedHandler;
_notifyIcon.ShowBalloonTip(timeout, @"Greenshot", message, level);
}
}
}

View file

@ -21,6 +21,12 @@ namespace GreenshotPlugin.Core
/// <returns>true if we are running on Windows 10</returns>
public static bool IsWindows10 { get; } = WinVersion.Major == 10;
/// <summary>
/// Test if the current OS is before Windows 10
/// </summary>
/// <returns>true if we are running on Windows before 10</returns>
public static bool IsBeforeWindows10 { get; } = WinVersion.Major < 10;
/// <summary>
/// Test if the current OS is Windows 10 or later
/// </summary>