/* * 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.Windows.Forms; using GreenshotPlugin.Core; using log4net; namespace Greenshot.Helpers { /// /// Notify the user with messages via the NotifyIcon /// public class NotifyIconNotificationService { private static readonly ILog Log = LogManager.GetLogger(typeof(NotifyIconNotificationService)); private readonly NotifyIcon _notifyIcon; public NotifyIconNotificationService() { _notifyIcon = SimpleServiceProvider.Current.GetInstance(); } /// /// This will show a warning message to the user /// /// string /// /// Action called if the user clicks the notification /// Action public void ShowWarningMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null) { ShowMessage(message, timeout, ToolTipIcon.Warning, onClickAction, onClosedAction); } /// /// This will show an error message to the user /// /// string /// /// Action called if the user clicks the notification /// Action public void ShowErrorMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null) { ShowMessage(message, timeout, ToolTipIcon.Error, onClickAction, onClosedAction); } /// /// This will show an info message to the user /// /// string /// int /// Action called if the user clicks the notification /// Action public void ShowInfoMessage(string message, int timeout, Action onClickAction = null, Action onClosedAction = null) { ShowMessage(message, timeout, ToolTipIcon.Info, onClickAction, onClosedAction); } /// /// This will show a message to the user /// /// string /// int /// ToolTipIcon /// Action /// Action 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); } } }