diff --git a/SlackUI/Core/ChromiumNotification.cs b/SlackUI/Core/ChromiumNotification.cs index 83c6138..147e082 100644 --- a/SlackUI/Core/ChromiumNotification.cs +++ b/SlackUI/Core/ChromiumNotification.cs @@ -1,4 +1,5 @@ using System; +using System.Windows.Forms; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,13 +7,30 @@ namespace SlackUI.Core { class ChromiumNotification { + private const string GroupMessageTitle = "New message in #"; + private const string MemberMessageTitle = "New message from "; public void ShowNotification(String title, String body, String icon, String tag) { Program.WrapperForm.InvokeOnUiThreadIfRequired(() => { - Toaster.Toast.ShowNotification(title, body, icon, tag); - }); + var notification = Toaster.Toast.ShowNotification(title, body, icon, tag); + notification.FormClosed += delegate { + if (!notification.Dismissed) + { + var target = ""; + if (notification.Title.Contains(GroupMessageTitle)) + { + target = Program.ActiveTeamAddress + "/messages/" + notification.Title.Replace(GroupMessageTitle, ""); + } + if (notification.Title.Contains(MemberMessageTitle)) + { + target = Program.ActiveTeamAddress + "/messages/@" + notification.Title.Replace(MemberMessageTitle, ""); + } - System.Diagnostics.Debug.WriteLine("[Notification] title: '{0}', body: '{1}', icon: '{2}', tag: '{3}'.", title, body, icon, tag); + if (target != "" && !Program.WrapperForm.address().Contains(target)) { Program.WrapperForm.redirect(target); } + NotificationContext.DisplayWrapperForm(); + } + }; + }); } } } diff --git a/SlackUI/Core/GeneralNotification.cs b/SlackUI/Core/GeneralNotification.cs new file mode 100644 index 0000000..436ce4e --- /dev/null +++ b/SlackUI/Core/GeneralNotification.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SlackUI.Core { + class GeneralNotification + { + public string permission; + + public void onclick() + { + System.Diagnostics.Debug.WriteLine("[GeneralNotification/onclick]"); + } + + public void onshow() + { + System.Diagnostics.Debug.WriteLine("[GeneralNotification/onshow]"); + } + + public void onerror() + { + System.Diagnostics.Debug.WriteLine("[GeneralNotification/onerror]"); + } + + public void onclose() + { + System.Diagnostics.Debug.WriteLine("[GeneralNotification/onclose]"); + } + + public void requestPermission(String callback) + { + this.permission = "granted"; + System.Diagnostics.Debug.WriteLine("[GeneralNotification/requestPermission]"); + } + public void close() + { + System.Diagnostics.Debug.WriteLine("[GeneralNotification/close]"); + } + } +} diff --git a/SlackUI/Core/NotificationContext.cs b/SlackUI/Core/NotificationContext.cs index 9dcbec7..ec1e44d 100644 --- a/SlackUI/Core/NotificationContext.cs +++ b/SlackUI/Core/NotificationContext.cs @@ -76,14 +76,10 @@ internal NotificationContext() { } } - #endregion - - #region Private Methods - /* * Displays the wrapper form to the user. */ - private static void DisplayWrapperForm() { + internal static void DisplayWrapperForm() { // Focus the wrapper form or display it to the user if(Program.WrapperForm.Visible) { // Restore normal window state if form is minimized @@ -99,6 +95,7 @@ private static void DisplayWrapperForm() { using(TeamPickerForm teamPickerForm = new TeamPickerForm()) { if(teamPickerForm.ShowDialog() == DialogResult.OK) { Program.Settings.Data.InitialTeamToLoad = teamPickerForm.SlackTeamDomain; + Program.WrapperForm.redirect(Program.ActiveTeamAddress); Program.WrapperForm.Show(); } } @@ -110,6 +107,10 @@ private static void DisplayWrapperForm() { } } + #endregion + + #region Private Methods + /* * Context menu open about form item click event handler. */ diff --git a/SlackUI/Forms/WrapperForm.cs b/SlackUI/Forms/WrapperForm.cs index bdc46a7..c616e8b 100644 --- a/SlackUI/Forms/WrapperForm.cs +++ b/SlackUI/Forms/WrapperForm.cs @@ -45,6 +45,7 @@ public WrapperForm() { RequestHandler = new BrowserRequestHandler() }; chromium.RegisterJsObject("chromiumNotification", new Core.ChromiumNotification()); + chromium.RegisterJsObject("Notification", new Core.GeneralNotification()); // Subscribe to multiple chromium web browser events chromium.FrameLoadEnd += chromium_FrameLoadEnd; @@ -59,7 +60,6 @@ public WrapperForm() { // Save the current window state as the previous one previousWindowState = WindowState; } - #endregion #region Private Methods @@ -72,14 +72,14 @@ private void chromium_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs if(!e.Url.Contains(AboutBlankPage)) { // Remove the browser load overlay from the form this.InvokeOnUiThreadIfRequired(() => { - if(browserPanel.Controls["browserLoadOverlay"].Visible) { - browserPanel.Controls["browserLoadOverlay"].Visible = false; - chromium.ExecuteScriptAsync(@"(function () {window.Notification = function (title, options) { window.chromiumNotification.showNotification(title, options.body, options.icon, options.tag); }; window.Notification.prototype = window.Notification; window.Notification.permission = 'granted'; window.Notification.requestPermission = function (callback) { window.Notification.permission = 'granted'; if(callback) { callback('granted'); } }})();"); - } + browserPanel.Controls["browserLoadOverlay"].Visible = false; + chromium.ExecuteScriptAsync(@"(function () { + window.Notification = function (title, options) { + window.chromiumNotification.showNotification(title, options.body, options.icon, options.tag); + }; + window.Notification.permission = 'granted'; + })();"); }); - - // Unsubscribe the frame load end event - chromium.FrameLoadEnd -= chromium_FrameLoadEnd; } } @@ -210,6 +210,22 @@ protected override void WndProc(ref Message m) { #endregion + + #region Internal Methods + + public void redirect(String url) + { + browserPanel.Controls["browserLoadOverlay"].Visible = true; + chromium.NavStateChanged += chromium_NavStateChanged; + chromium.Load(url); + } + + public string address() + { + return chromium.Address; + } + + #endregion } } diff --git a/SlackUI/SlackUI.csproj b/SlackUI/SlackUI.csproj index edcf770..ecf9d89 100644 --- a/SlackUI/SlackUI.csproj +++ b/SlackUI/SlackUI.csproj @@ -128,6 +128,7 @@ +