From a3f768c84b0591099e4eda83034ad6fec878551d Mon Sep 17 00:00:00 2001 From: Junian Triajianto Date: Sat, 26 May 2018 12:21:31 +0700 Subject: [PATCH] Fix notification handler for winforms --- .../Controls/NotificationHandler.cs | 86 +++++++++++++++++++ src/Termission.WinForms/Program.cs | 2 + 2 files changed, 88 insertions(+) create mode 100644 src/Termission.WinForms/Controls/NotificationHandler.cs diff --git a/src/Termission.WinForms/Controls/NotificationHandler.cs b/src/Termission.WinForms/Controls/NotificationHandler.cs new file mode 100644 index 0000000..2059891 --- /dev/null +++ b/src/Termission.WinForms/Controls/NotificationHandler.cs @@ -0,0 +1,86 @@ +using Eto.Drawing; +using Eto.Forms; +using System; +using swf = System.Windows.Forms; +using sd = System.Drawing; +using Eto; +using Eto.WinForms; +using Eto.WinForms.Forms; + +namespace Juniansoft.Termission.WinForms.Controls +{ + public class NotificationHandler : WidgetHandler, Notification.IHandler + { + public string Message { get; set; } + + public bool RequiresTrayIndicator + { + get { return true; } + } + + public string Title { get; set; } + + public string UserData { get; set; } + + public Image ContentImage { get; set; } + + static TrayIndicator s_sharedIndicator; + + TrayIndicator GetSharedIndicator() + { + if (s_sharedIndicator == null) + { + s_sharedIndicator = new TrayIndicator + { + Image = Application.Instance?.MainForm?.Icon ?? sd.SystemIcons.Application.ToEto() + }; + } + s_sharedIndicator.Show(); + return s_sharedIndicator; + } + + static readonly object NotificationHandler_Key = new object(); + + public void Show(TrayIndicator indicator = null) + { + indicator = indicator ?? GetSharedIndicator(); + var notifyIcon = TrayIndicatorHandler.GetControl(indicator); + + var currentNotification = indicator.Properties.Get(NotificationHandler_Key); + if (currentNotification != null) + { + currentNotification.Unhook(notifyIcon); + indicator.Properties.Remove(NotificationHandler_Key); + } + + notifyIcon.ShowBalloonTip(3000, Title, Message, swf.ToolTipIcon.None); + notifyIcon.BalloonTipClicked += Tray_BalloonTipClicked; + notifyIcon.BalloonTipClosed += Tray_BalloonTipClosed; + indicator.Properties.Set(NotificationHandler_Key, this); + } + + void Unhook(swf.NotifyIcon notifyIcon) + { + if (notifyIcon == null) + return; + notifyIcon.BalloonTipClicked -= Tray_BalloonTipClicked; + notifyIcon.BalloonTipClosed -= Tray_BalloonTipClosed; + notifyIcon = null; + } + + void Tray_BalloonTipClosed(object sender, EventArgs e) + { + Unhook(sender as swf.NotifyIcon); + s_sharedIndicator?.Hide(); + } + + void Tray_BalloonTipClicked(object sender, EventArgs e) + { + Unhook(sender as swf.NotifyIcon); + s_sharedIndicator?.Hide(); + + var app = ApplicationHandler.Instance; + app?.Callback.OnNotificationActivated(app.Widget, new NotificationEventArgs(ID, UserData)); + } + } +} diff --git a/src/Termission.WinForms/Program.cs b/src/Termission.WinForms/Program.cs index f5bf146..4128061 100644 --- a/src/Termission.WinForms/Program.cs +++ b/src/Termission.WinForms/Program.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using System.Windows.Forms; using Eto; +using Eto.Forms; using Eto.WinForms.Forms; using Eto.WinForms.Forms.Controls; using Juniansoft.MvvmReady; @@ -43,6 +44,7 @@ private static void RegisterServices() private static void RegisterUIHandlers(Platform platform) { + platform.Add(() => new Controls.NotificationHandler()); platform.Add(() => new SyntaxHightlightTextAreaHandler()); }